9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

unix.c (4057B)


      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>
      9 
     10 #include "sam.h"
     11 
     12 Rune    samname[] = { '~', '~', 's', 'a', 'm', '~', '~', 0 };
     13  
     14 static Rune l1[] = { '{', '[', '(', '<', 0253, 0};
     15 static Rune l2[] = { '\n', 0};
     16 static Rune l3[] = { '\'', '"', '`', 0};
     17 Rune *left[]= { l1, l2, l3, 0};
     18 
     19 static Rune r1[] = {'}', ']', ')', '>', 0273, 0};
     20 static Rune r2[] = {'\n', 0};
     21 static Rune r3[] = {'\'', '"', '`', 0};
     22 Rune *right[]= { r1, r2, r3, 0};
     23 
     24 #ifndef SAMTERMNAME
     25 #define SAMTERMNAME "samterm"
     26 #endif
     27 #ifndef TMPDIRNAME
     28 #define TMPDIRNAME "/tmp"
     29 #endif
     30 #ifndef SHNAME
     31 #define SHNAME "sh"
     32 #endif
     33 #ifndef SHPATHNAME
     34 #define SHPATHNAME "/bin/sh"
     35 #endif
     36 #ifndef RXNAME
     37 #define RXNAME "ssh"
     38 #endif
     39 #ifndef RXPATHNAME
     40 #define RXPATHNAME "ssh"
     41 #endif
     42 
     43 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;
     51 
     52 
     53 void
     54 dprint(char *z, ...)
     55 {
     56 	char buf[BLOCKSIZE];
     57 	va_list arg;
     58 
     59 	va_start(arg, z);
     60 	vseprint(buf, &buf[BLOCKSIZE], z, arg);
     61 	va_end(arg);
     62 	termwrite(buf);
     63 }
     64 
     65 void
     66 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 }
     70 
     71 void
     72 print_s(char *s, String *a)
     73 {
     74 	dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
     75 }
     76 
     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 }
     87 
     88 int     
     89 statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly) 
     90 {
     91         struct stat dirb;
     92 
     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 }
    107 
    108 int
    109 statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
    110 {
    111         struct stat dirb;
    112 
    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 }
    127 
    128 void
    129 hup(int sig)
    130 {
    131         panicking = 1; /* ??? */
    132         rescue();
    133         exit(1);
    134 }
    135 
    136 int
    137 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 }
    144 
    145 void
    146 notifyf(void *a, char *b)       /* never called; hup is instead */
    147 {
    148 }
    149 
    150 static int
    151 temp_file(char *buf, int bufsize)
    152 {
    153         char *tmp;
    154         int n, fd;
    155 
    156         tmp = getenv("TMPDIR");
    157         if (!tmp)
    158                 tmp = TMPDIR;
    159 
    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 }
    169 
    170 int
    171 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 }
    179 
    180 #undef waitfor
    181 int     
    182 samwaitfor(int pid)
    183 {
    184 	int r;
    185 	Waitmsg *w;
    186 
    187 	w = p9waitfor(pid);
    188 	if(w == nil)
    189 		return -1;
    190 	r = atoi(w->msg);
    191 	free(w);
    192 	return r;
    193 }
    194 
    195 void
    196 samerr(char *buf)
    197 {
    198 	sprint(buf, "%s/sam.%s.err", TMPDIR, getuser());
    199 }
    200 
    201 void*
    202 emalloc(ulong n)
    203 {
    204 	void *p;
    205 
    206 	p = malloc(n);
    207 	if(p == 0)
    208 		panic("malloc fails");
    209 	memset(p, 0, n);
    210 	return p;
    211 }
    212 
    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 }
    221 
    222