]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/page/util.c
merge
[plan9front.git] / sys / src / cmd / page / util.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5 #include <bio.h>
6 #include "page.h"
7
8 void*
9 emalloc(int sz)
10 {
11         void *v;
12         v = malloc(sz);
13         if(v == nil) {
14                 fprint(2, "out of memory allocating %d\n", sz);
15                 wexits("mem");
16         }
17         memset(v, 0, sz);
18         return v;
19 }
20
21 void*
22 erealloc(void *v, int sz)
23 {
24         v = realloc(v, sz);
25         if(v == nil) {
26                 fprint(2, "out of memory allocating %d\n", sz);
27                 wexits("mem");
28         }
29         return v;
30 }
31
32 char*
33 estrdup(char *s)
34 {
35         char *t;
36         if((t = strdup(s)) == nil) {
37                 fprint(2, "out of memory in strdup(%.10s)\n", s);
38                 wexits("mem");
39         }
40         return t;
41 }
42
43 int
44 opentemp(char *template)
45 {
46         int fd, i;
47         char *p;
48
49         p = estrdup(template);
50         fd = -1;
51         for(i=0; i<10; i++){
52                 mktemp(p);
53                 if(access(p, 0) < 0 && (fd=create(p, ORDWR|ORCLOSE, 0400)) >= 0)
54                         break;
55                 strcpy(p, template);
56         }
57         if(fd < 0){
58                 fprint(2, "couldn't make temporary file\n");
59                 wexits("Ecreat");
60         }
61         strcpy(template, p);
62         free(p);
63
64         return fd;
65 }
66
67 /*
68  * spool standard input to /tmp.
69  * we've already read the initial in bytes into ibuf.
70  */
71 int
72 spooltodisk(uchar *ibuf, int in, char **name)
73 {
74         uchar buf[8192];
75         int fd, n;
76         char temp[40];
77
78         strcpy(temp, "/tmp/pagespoolXXXXXXXXX");
79         fd = opentemp(temp);
80         if(name)
81                 *name = estrdup(temp);
82
83         if(write(fd, ibuf, in) != in){
84                 fprint(2, "error writing temporary file\n");
85                 wexits("write temp");
86         }
87
88         while((n = read(stdinfd, buf, sizeof buf)) > 0){
89                 if(write(fd, buf, n) != n){
90                         fprint(2, "error writing temporary file\n");
91                         wexits("write temp0");
92                 }
93         }
94         seek(fd, 0, 0);
95         return fd;
96 }
97
98 /*
99  * spool standard input into a pipe.
100  * we've already ready the first in bytes into ibuf
101  */
102 int
103 stdinpipe(uchar *ibuf, int in)
104 {
105         uchar buf[8192];
106         int n;
107         int p[2];
108         if(pipe(p) < 0){
109                 fprint(2, "pipe fails: %r\n");  
110                 wexits("pipe");
111         }
112
113         switch(rfork(RFMEM|RFPROC|RFFDG)){
114         case -1:
115                 fprint(2, "fork fails: %r\n");
116                 wexits("fork");
117         default:
118                 close(p[1]);
119                 return p[0];
120         case 0:
121                 break;
122         }
123
124         close(p[0]);
125         write(p[1], ibuf, in);
126         while((n = read(stdinfd, buf, sizeof buf)) > 0)
127                 write(p[1], buf, n);
128
129         _exits(0);
130         return -1;      /* not reached */
131 }