xref: /plan9-contrib/sys/src/cmd/sam/plan9.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
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 
33 	doprint(buf, &buf[BLOCKSIZE], z, ((long*)&z)+1);
34 	termwrite(buf);
35 }
36 
37 void
38 print_ss(char *s, String *a, String *b)
39 {
40 	dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
41 }
42 
43 void
44 print_s(char *s, String *a)
45 {
46 	dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
47 }
48 
49 char*
50 getuser(void)
51 {
52 	static char user[NAMELEN];
53 	int fd;
54 
55 	if(user[0] == 0){
56 		fd = open("/dev/user", 0);
57 		if(fd<0 || read(fd, user, sizeof user)<=0)
58 			strcpy(user, "none");
59 		close(fd);
60 	}
61 	return user;
62 }
63 
64 int
65 statfile(char *name, ulong *dev, ulong *id, long *time, long *length, long *appendonly)
66 {
67 	Dir dirb;
68 
69 	if(dirstat(name, &dirb) == -1)
70 		return -1;
71 	if(dev)
72 		*dev = dirb.type|(dirb.dev<<16);
73 	if(id)
74 		*id = dirb.qid.path;
75 	if(time)
76 		*time = dirb.mtime;
77 	if(length)
78 		*length = dirb.length;
79 	if(appendonly)
80 		*appendonly = dirb.mode & CHAPPEND;
81 	return 1;
82 }
83 
84 int
85 statfd(int fd, ulong *dev, ulong *id, long *time, long *length, long *appendonly)
86 {
87 	Dir dirb;
88 
89 	if(dirfstat(fd, &dirb) == -1)
90 		return -1;
91 	if(dev)
92 		*dev = dirb.type|(dirb.dev<<16);
93 	if(id)
94 		*id = dirb.qid.path;
95 	if(time)
96 		*time = dirb.mtime;
97 	if(length)
98 		*length = dirb.length;
99 	if(appendonly)
100 		*appendonly = dirb.mode & CHAPPEND;
101 	return 1;
102 }
103 
104 void
105 notifyf(void *a, char *s)
106 {
107 	USED(a);
108 	if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0)
109 		noted(NCONT);
110 	if(strcmp(s, "interrupt") == 0)
111 		noted(NCONT);
112 	panicking = 1;
113 	rescue();
114 	noted(NDFLT);
115 }
116 
117 int
118 newtmp(int num)
119 {
120 	int i, fd;
121 	static char	tempnam[30];
122 
123 	i = getpid();
124 	do
125 		sprint(tempnam, "%s/%d%.4s%dsam", TMPDIR, num, getuser(), i++);
126 	while(access(tempnam, 0) == 0);
127 	fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
128 	if(fd < 0){
129 		remove(tempnam);
130 		fd = create(tempnam, ORDWR|OCEXEC|ORCLOSE, 0000);
131 	}
132 	return fd;
133 }
134 
135 int
136 waitfor(int pid)
137 {
138 	int rpid;
139 	Waitmsg wm;
140 
141 	do; while((rpid = wait(&wm)) != pid && rpid != -1);
142 	return wm.msg[0];
143 }
144 
145 void
146 samerr(char *buf)
147 {
148 	sprint(buf, "%s/sam.err", TMPDIR);
149 }
150 
151 void*
152 emalloc(ulong n)
153 {
154 	void *p;
155 
156 	p = malloc(n);
157 	if(p == 0)
158 		panic("malloc fails");
159 	memset(p, 0, n);
160 	return p;
161 }
162 
163 void*
164 erealloc(void *p, ulong n)
165 {
166 	p = realloc(p, n);
167 	if(p == 0)
168 		panic("realloc fails");
169 	return p;
170 }
171