Blob
1 #include <u.h>2 #include <sys/types.h>3 #include <sys/stat.h>4 #include <sys/wait.h>5 #include <pwd.h>6 #include <signal.h>7 #include <fcntl.h>8 #include <errno.h>10 #include "sam.h"12 Rune samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };14 static Rune l1[] = { '{', '[', '(', '<', 0253, 0};15 static Rune l2[] = { '\n', 0};16 static Rune l3[] = { '\'', '"', '`', 0};17 Rune *left[]= { l1, l2, l3, 0};19 static Rune r1[] = {'}', ']', ')', '>', 0273, 0};20 static Rune r2[] = {'\n', 0};21 static Rune r3[] = {'\'', '"', '`', 0};22 Rune *right[]= { r1, r2, r3, 0};24 #ifndef SAMTERMNAME25 #define SAMTERMNAME "samterm"26 #endif27 #ifndef TMPDIRNAME28 #define TMPDIRNAME "/tmp"29 #endif30 #ifndef SHNAME31 #define SHNAME "sh"32 #endif33 #ifndef SHPATHNAME34 #define SHPATHNAME "/bin/sh"35 #endif36 #ifndef RXNAME37 #define RXNAME "ssh"38 #endif39 #ifndef RXPATHNAME40 #define RXPATHNAME "ssh"41 #endif43 char RSAM[] = "sam";44 char SAMTERM[] = SAMTERMNAME;45 char HOME[] = "HOME";46 char TMPDIR[] = TMPDIRNAME;47 char SH[] = SHNAME;48 char SHPATH[] = SHPATHNAME;49 char RX[] = RXNAME;50 char RXPATH[] = RXPATHNAME;53 void54 dprint(char *z, ...)55 {56 char buf[BLOCKSIZE];57 va_list arg;59 va_start(arg, z);60 vseprint(buf, &buf[BLOCKSIZE], z, arg);61 va_end(arg);62 termwrite(buf);63 }65 void66 print_ss(char *s, String *a, String *b)67 {68 dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);69 }71 void72 print_s(char *s, String *a)73 {74 dprint("?warning: %s `%.*S'\n", s, a->n, a->s);75 }77 char*78 getuser(void)79 {80 static char user[64];81 if(user[0] == 0){82 struct passwd *pw = getpwuid(getuid());83 strcpy(user, pw ? pw->pw_name : "nobody");84 }85 return user;86 }88 int89 statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)90 {91 struct stat dirb;93 if (stat(name, &dirb) == -1)94 return -1;95 if (dev)96 *dev = dirb.st_dev;97 if (id)98 *id = dirb.st_ino;99 if (time)100 *time = dirb.st_mtime;101 if (length)102 *length = dirb.st_size;103 if(appendonly)104 *appendonly = 0;105 return 1;106 }108 int109 statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)110 {111 struct stat dirb;113 if (fstat(fd, &dirb) == -1)114 return -1;115 if (dev)116 *dev = dirb.st_dev;117 if (id)118 *id = dirb.st_ino;119 if (time)120 *time = dirb.st_mtime;121 if (length)122 *length = dirb.st_size;123 if(appendonly)124 *appendonly = 0;125 return 1;126 }128 void129 hup(int sig)130 {131 panicking = 1; /* ??? */132 rescue();133 exit(1);134 }136 int137 notify(void(*f)(void *, char *))138 {139 signal(SIGINT, SIG_IGN);140 signal(SIGPIPE, SIG_IGN); /* XXX - bpipeok? */141 signal(SIGHUP, hup);142 return 1;143 }145 void146 notifyf(void *a, char *b) /* never called; hup is instead */147 {148 }150 static int151 temp_file(char *buf, int bufsize)152 {153 char *tmp;154 int n, fd;156 tmp = getenv("TMPDIR");157 if (!tmp)158 tmp = TMPDIR;160 n = snprint(buf, bufsize, "%s/sam.%d.XXXXXXX", tmp, getuid());161 if (bufsize <= n)162 return -1;163 if ((fd = mkstemp(buf)) < 0) /* SES - linux sometimes uses mode 0666 */164 return -1;165 if (fcntl(fd, F_SETFD, fcntl(fd,F_GETFD,0) | FD_CLOEXEC) < 0)166 return -1;167 return fd;168 }170 int171 tempdisk(void)172 {173 char buf[4096];174 int fd = temp_file(buf, sizeof buf);175 if (fd >= 0)176 remove(buf);177 return fd;178 }180 #undef waitfor181 int182 samwaitfor(int pid)183 {184 int r;185 Waitmsg *w;187 w = p9waitfor(pid);188 if(w == nil)189 return -1;190 r = atoi(w->msg);191 free(w);192 return r;193 }195 void196 samerr(char *buf)197 {198 sprint(buf, "%s/sam.%s.err", TMPDIR, getuser());199 }201 void*202 emalloc(ulong n)203 {204 void *p;206 p = malloc(n);207 if(p == 0)208 panic("malloc fails");209 memset(p, 0, n);210 return p;211 }213 void*214 erealloc(void *p, ulong n)215 {216 p = realloc(p, n);217 if(p == 0)218 panic("realloc fails");219 return p;220 }