1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pwd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include "sam.h"
Rune samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };
static Rune l1[] = { '{', '[', '(', '<', 0253, 0};
static Rune l2[] = { '\n', 0};
static Rune l3[] = { '\'', '"', '`', 0};
Rune *left[]= { l1, l2, l3, 0};
static Rune r1[] = {'}', ']', ')', '>', 0273, 0};
static Rune r2[] = {'\n', 0};
static Rune r3[] = {'\'', '"', '`', 0};
Rune *right[]= { r1, r2, r3, 0};
#ifndef SAMTERMNAME
#define SAMTERMNAME "samterm"
#endif
#ifndef TMPDIRNAME
#define TMPDIRNAME "/tmp"
#endif
#ifndef SHNAME
#define SHNAME "sh"
#endif
#ifndef SHPATHNAME
#define SHPATHNAME "/bin/sh"
#endif
#ifndef RXNAME
#define RXNAME "ssh"
#endif
#ifndef RXPATHNAME
#define RXPATHNAME "ssh"
#endif
#ifndef SAMSAVECMDNAME
#define SAMSAVECMDNAME "/bin/sh\n/usr/local/plan9/bin/samsave"
#endif
char RSAM[] = "sam";
char SAMTERM[] = SAMTERMNAME;
char HOME[] = "HOME";
char TMPDIR[] = TMPDIRNAME;
char SH[] = SHNAME;
char SHPATH[] = SHPATHNAME;
char RX[] = RXNAME;
char RXPATH[] = RXPATHNAME;
char SAMSAVECMD[] = SAMSAVECMDNAME;
void
dprint(char *z, ...)
{
char buf[BLOCKSIZE];
va_list arg;
va_start(arg, z);
vseprint(buf, &buf[BLOCKSIZE], z, arg);
va_end(arg);
termwrite(buf);
}
void
print_ss(char *s, String *a, String *b)
{
dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
}
void
print_s(char *s, String *a)
{
dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
}
char*
getuser(void)
{
static char user[64];
if(user[0] == 0){
struct passwd *pw = getpwuid(getuid());
strcpy(user, pw ? pw->pw_name : "nobody");
}
return user;
}
int
statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
{
struct stat dirb;
if (stat(name, &dirb) == -1)
return -1;
if (dev)
*dev = dirb.st_dev;
if (id)
*id = dirb.st_ino;
if (time)
*time = dirb.st_mtime;
if (length)
*length = dirb.st_size;
if(appendonly)
*appendonly = 0;
return 1;
}
int
statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
{
struct stat dirb;
if (fstat(fd, &dirb) == -1)
return -1;
if (dev)
*dev = dirb.st_dev;
if (id)
*id = dirb.st_ino;
if (time)
*time = dirb.st_mtime;
if (length)
*length = dirb.st_size;
if(appendonly)
*appendonly = 0;
return 1;
}
void
hup(int sig)
{
panicking = 1; // ???
rescue();
exit(1);
}
int
notify(void(*f)(void *, char *))
{
signal(SIGINT, SIG_IGN);
signal(SIGPIPE, SIG_IGN); // XXX - bpipeok?
signal(SIGHUP, hup);
return 1;
}
void
notifyf(void *a, char *b) /* never called; hup is instead */
{
}
static int
temp_file(char *buf, int bufsize)
{
char *tmp;
int n, fd;
tmp = getenv("TMPDIR");
if (!tmp)
tmp = TMPDIR;
n = snprint(buf, bufsize, "%s/sam.%d.XXXXXXX", tmp, getuid());
if (bufsize <= n)
return -1;
if ((fd = mkstemp(buf)) < 0) /* SES - linux sometimes uses mode 0666 */
return -1;
if (fcntl(fd, F_SETFD, fcntl(fd,F_GETFD,0) | FD_CLOEXEC) < 0)
return -1;
return fd;
}
int
tempdisk(void)
{
char buf[4096];
int fd = temp_file(buf, sizeof buf);
if (fd >= 0)
remove(buf);
return fd;
}
#undef wait
int
waitfor(int pid)
{
int wm;
int rpid;
do; while((rpid = wait(&wm)) != pid && rpid != -1);
return (WEXITSTATUS(wm));
}
void
samerr(char *buf)
{
sprint(buf, "%s/sam.%s.err", TMPDIR, getuser());
}
void*
emalloc(ulong n)
{
void *p;
p = malloc(n);
if(p == 0)
panic("malloc fails");
memset(p, 0, n);
return p;
}
void*
erealloc(void *p, ulong n)
{
p = realloc(p, n);
if(p == 0)
panic("realloc fails");
return p;
}
#if 0
char *
strdup(const char *s)
{
return strcpy(emalloc(strlen(s)), s);
}
#endif
/*
void exits(const char *s)
{
if (s) fprint(2, "exit: %s\n", s);
exit(s != 0);
}
void
_exits(const char *s)
{
if (s) fprint(2, "exit: %s\n", s);
_exit(s != 0);
}
int errstr(char *buf, int size)
{
extern int errno;
snprint(buf, size, "%s", strerror(errno));
return 1;
}
*/
int
create(char *name, int omode, ulong perm)
{
int mode;
int fd;
if (omode & OWRITE) mode = O_WRONLY;
else if (omode & OREAD) mode = O_RDONLY;
else mode = O_RDWR;
if ((fd = open(name, mode|O_CREAT|O_TRUNC, perm)) < 0)
return fd;
if (omode & OCEXEC)
fcntl(fd, F_SETFD, fcntl(fd,F_GETFD,0) | FD_CLOEXEC);
/* SES - not exactly right, but hopefully good enough. */
if (omode & ORCLOSE)
remove(name);
return fd;
}
/* SHOULD BE ELSEWHERE */
#if 0 /* needed on old __APPLE__ */
#include <lib9.h>
Lock plk;
ulong
pread(int fd, void *buf, ulong n, ulong off)
{
ulong rv;
lock(&plk);
if (lseek(fd, off, 0) != off)
return -1;
rv = read(fd, buf, n);
unlock(&plk);
return rv;
}
ulong
pwrite(int fd, void *buf, ulong n, ulong off)
{
ulong rv;
lock(&plk);
if (lseek(fd, off, 0) != off)
return -1;
rv = write(fd, buf, n);
unlock(&plk);
return rv;
}
#endif
|