9base

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

plan9.c (2803B)


      1 #include "sam.h"
      2 
      3 Rune	samname[] = L"~~sam~~";
      4 
      5 Rune *left[]= {
      6 	L"{[(<«",
      7 	L"\n",
      8 	L"'\"`",
      9 	0
     10 };
     11 Rune *right[]= {
     12 	L"}])>»",
     13 	L"\n",
     14 	L"'\"`",
     15 	0
     16 };
     17 
     18 char	RSAM[] = "sam";
     19 char	SAMTERM[] = "/bin/aux/samterm";
     20 char	HOME[] = "HOME";
     21 char	TMPDIR[] = "/tmp";
     22 char	SH[] = "rc";
     23 char	SHPATH[] = "/bin/rc";
     24 char	RX[] = "rx";
     25 char	RXPATH[] = "/bin/rx";
     26 char	SAMSAVECMD[] = "/bin/rc\n/sys/lib/samsave";
     27 
     28 void
     29 dprint(char *z, ...)
     30 {
     31 	char buf[BLOCKSIZE];
     32 	va_list arg;
     33 
     34 	va_start(arg, z);
     35 	vseprint(buf, &buf[BLOCKSIZE], z, arg);
     36 	va_end(arg);
     37 	termwrite(buf);
     38 }
     39 
     40 void
     41 print_ss(char *s, String *a, String *b)
     42 {
     43 	dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
     44 }
     45 
     46 void
     47 print_s(char *s, String *a)
     48 {
     49 	dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
     50 }
     51 
     52 char*
     53 getuser(void)
     54 {
     55 	static char user[64];
     56 	int fd;
     57 
     58 	if(user[0] == 0){
     59 		fd = open("/dev/user", 0);
     60 		if(fd<0 || read(fd, user, sizeof user-1)<=0)
     61 			strcpy(user, "none");
     62 		close(fd);
     63 	}
     64 	return user;
     65 }
     66 
     67 int
     68 statfile(char *name, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
     69 {
     70 	Dir *dirb;
     71 
     72 	dirb = dirstat(name);
     73 	if(dirb == nil)
     74 		return -1;
     75 	if(dev)
     76 		*dev = dirb->type|(dirb->dev<<16);
     77 	if(id)
     78 		*id = dirb->qid.path;
     79 	if(time)
     80 		*time = dirb->mtime;
     81 	if(length)
     82 		*length = dirb->length;
     83 	if(appendonly)
     84 		*appendonly = dirb->mode & DMAPPEND;
     85 	free(dirb);
     86 	return 1;
     87 }
     88 
     89 int
     90 statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
     91 {
     92 	Dir *dirb;
     93 
     94 	dirb = dirfstat(fd);
     95 	if(dirb == nil)
     96 		return -1;
     97 	if(dev)
     98 		*dev = dirb->type|(dirb->dev<<16);
     99 	if(id)
    100 		*id = dirb->qid.path;
    101 	if(time)
    102 		*time = dirb->mtime;
    103 	if(length)
    104 		*length = dirb->length;
    105 	if(appendonly)
    106 		*appendonly = dirb->mode & DMAPPEND;
    107 	free(dirb);
    108 	return 1;
    109 }
    110 
    111 void
    112 notifyf(void *a, char *s)
    113 {
    114 	USED(a);
    115 	if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0)
    116 		noted(NCONT);
    117 	if(strcmp(s, "interrupt") == 0)
    118 		noted(NCONT);
    119 	panicking = 1;
    120 	rescue();
    121 	noted(NDFLT);
    122 }
    123 
    124 int
    125 newtmp(int num)
    126 {
    127 	int i, fd;
    128 	static char	tempnam[30];
    129 
    130 	i = getpid();
    131 	do
    132 		snprint(tempnam, sizeof tempnam, "%s/%d%.4s%dsam", TMPDIR, num, getuser(), i++);
    133 	while(access(tempnam, 0) == 0);
    134 	fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
    135 	if(fd < 0){
    136 		remove(tempnam);
    137 		fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
    138 	}
    139 	return fd;
    140 }
    141 
    142 int
    143 waitfor(int pid)
    144 {
    145 	int msg;
    146 	Waitmsg *w;
    147 
    148 	while((w = wait()) != nil){
    149 		if(w->pid != pid){
    150 			free(w);
    151 			continue;
    152 		}
    153 		msg = (w->msg[0] != '\0');
    154 		free(w);
    155 		return msg;
    156 	}
    157 	return -1;
    158 }
    159 
    160 void
    161 samerr(char *buf)
    162 {
    163 	sprint(buf, "%s/sam.err", TMPDIR);
    164 }
    165 
    166 void*
    167 emalloc(ulong n)
    168 {
    169 	void *p;
    170 
    171 	p = malloc(n);
    172 	if(p == 0)
    173 		panic("malloc fails");
    174 	memset(p, 0, n);
    175 	return p;
    176 }
    177 
    178 void*
    179 erealloc(void *p, ulong n)
    180 {
    181 	p = realloc(p, n);
    182 	if(p == 0)
    183 		panic("realloc fails");
    184 	return p;
    185 }