Blob


1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/wait.h>
4 #include <pwd.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <errno.h>
9 #include "sam.h"
11 Rune samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };
13 static Rune l1[] = { '{', '[', '(', '<', 0253, 0};
14 static Rune l2[] = { '\n', 0};
15 static Rune l3[] = { '\'', '"', '`', 0};
16 Rune *left[]= { l1, l2, l3, 0};
18 static Rune r1[] = {'}', ']', ')', '>', 0273, 0};
19 static Rune r2[] = {'\n', 0};
20 static Rune r3[] = {'\'', '"', '`', 0};
21 Rune *right[]= { r1, r2, r3, 0};
23 #ifndef SAMTERMNAME
24 #define SAMTERMNAME "/usr/local/bin/samterm"
25 #endif
26 #ifndef TMPDIRNAME
27 #define TMPDIRNAME "/tmp"
28 #endif
29 #ifndef SHNAME
30 #define SHNAME "rc"
31 #endif
32 #ifndef SHPATHNAME
33 #define SHPATHNAME "/bin/rc"
34 #endif
35 #ifndef RXNAME
36 #define RXNAME "ssh"
37 #endif
38 #ifndef RXPATHNAME
39 #define RXPATHNAME "/usr/local/bin/ssh"
40 #endif
41 #ifndef SAMSAVECMDNAME
42 #define SAMSAVECMDNAME "/bin/rc\n/usr/local/bin/samsave"
43 #endif
45 char RSAM[] = "sam";
46 char SAMTERM[] = SAMTERMNAME;
47 char HOME[] = "HOME";
48 char TMPDIR[] = TMPDIRNAME;
49 char SH[] = SHNAME;
50 char SHPATH[] = SHPATHNAME;
51 char RX[] = RXNAME;
52 char RXPATH[] = RXPATHNAME;
53 char SAMSAVECMD[] = SAMSAVECMDNAME;
56 void
57 dprint(char *z, ...)
58 {
59 char buf[BLOCKSIZE];
60 va_list arg;
62 va_start(arg, z);
63 vseprint(buf, &buf[BLOCKSIZE], z, arg);
64 va_end(arg);
65 termwrite(buf);
66 }
68 void
69 print_ss(char *s, String *a, String *b)
70 {
71 dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
72 }
74 void
75 print_s(char *s, String *a)
76 {
77 dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
78 }
80 char*
81 getuser(void)
82 {
83 static char user[64];
84 if(user[0] == 0){
85 struct passwd *pw = getpwuid(getuid());
86 strcpy(user, pw ? pw->pw_name : "nobody");
87 }
88 return user;
89 }
91 int
92 statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
93 {
94 struct stat dirb;
96 if (stat(name, &dirb) == -1)
97 return -1;
98 if (dev)
99 *dev = dirb.st_dev;
100 if (id)
101 *id = dirb.st_ino;
102 if (time)
103 *time = dirb.st_mtime;
104 if (length)
105 *length = dirb.st_size;
106 if(appendonly)
107 *appendonly = 0;
108 return 1;
111 int
112 statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
114 struct stat dirb;
116 if (fstat(fd, &dirb) == -1)
117 return -1;
118 if (dev)
119 *dev = dirb.st_dev;
120 if (id)
121 *id = dirb.st_ino;
122 if (time)
123 *time = dirb.st_mtime;
124 if (length)
125 *length = dirb.st_size;
126 if(appendonly)
127 *appendonly = 0;
128 return 1;
131 void
132 hup(int sig)
134 panicking = 1; // ???
135 rescue();
136 exit(1);
139 int
140 notify (void(*f)(void *, char *))
142 signal(SIGINT, SIG_IGN);
143 signal(SIGPIPE, SIG_IGN); // XXX - bpipeok?
144 signal(SIGHUP, hup);
145 return 1;
148 void
149 notifyf(void *a, char *b) /* never called; hup is instead */
153 static int
154 temp_file(char *buf, int bufsize)
156 char *tmp;
157 int n, fd;
159 tmp = getenv("TMPDIR");
160 if (!tmp)
161 tmp = TMPDIR;
163 n = snprint(buf, bufsize, "%s/sam.%d.XXXXXXX", tmp, getuid());
164 if (bufsize <= n)
165 return -1;
166 if ((fd = mkstemp(buf)) < 0) /* SES - linux sometimes uses mode 0666 */
167 return -1;
168 if (fcntl(fd, F_SETFD, fcntl(fd,F_GETFD,0) | FD_CLOEXEC) < 0)
169 return -1;
170 return fd;
173 int
174 tempdisk(void)
176 char buf[4096];
177 int fd = temp_file(buf, sizeof buf);
178 if (fd >= 0)
179 remove(buf);
180 return fd;
183 #undef wait
184 int
185 waitfor(int pid)
187 int wm;
188 int rpid;
190 do; while((rpid = wait(&wm)) != pid && rpid != -1);
191 return (WEXITSTATUS(wm));
194 void
195 samerr(char *buf)
197 sprint(buf, "%s/sam.%s.err", TMPDIR, getuser());
200 void*
201 emalloc(ulong n)
203 void *p;
205 p = malloc(n);
206 if(p == 0)
207 panic("malloc fails");
208 memset(p, 0, n);
209 return p;
212 void*
213 erealloc(void *p, ulong n)
215 p = realloc(p, n);
216 if(p == 0)
217 panic("realloc fails");
218 return p;
221 #if 0
222 char *
223 strdup(const char *s)
225 return strcpy(emalloc(strlen(s)), s);
227 #endif
229 /*
230 void exits(const char *s)
232 if (s) fprint(2, "exit: %s\n", s);
233 exit(s != 0);
236 void
237 _exits(const char *s)
239 if (s) fprint(2, "exit: %s\n", s);
240 _exit(s != 0);
243 int errstr(char *buf, int size)
245 extern int errno;
247 snprint(buf, size, "%s", strerror(errno));
248 return 1;
250 */
252 int create(char *name, int omode, int perm)
254 int mode;
255 int fd;
257 if (omode & OWRITE) mode = O_WRONLY;
258 else if (omode & OREAD) mode = O_RDONLY;
259 else mode = O_RDWR;
261 if ((fd = open(name, mode|O_CREAT|O_TRUNC, perm)) < 0)
262 return fd;
264 if (omode & OCEXEC)
265 fcntl(fd, F_SETFD, fcntl(fd,F_GETFD,0) | FD_CLOEXEC);
267 /* SES - not exactly right, but hopefully good enough. */
268 if (omode & ORCLOSE)
269 remove(name);
271 return fd;