URI:
       tutil.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tutil.c (1958B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <draw.h>
            4 #include <thread.h>
            5 #include <bio.h>
            6 #include <cursor.h>
            7 #include "page.h"
            8 
            9 void*
           10 emalloc(int sz)
           11 {
           12         void *v;
           13         v = malloc(sz);
           14         if(v == nil) {
           15                 fprint(2, "out of memory allocating %d\n", sz);
           16                 wexits("mem");
           17         }
           18         memset(v, 0, sz);
           19         return v;
           20 }
           21 
           22 void*
           23 erealloc(void *v, int sz)
           24 {
           25         v = realloc(v, sz);
           26         if(v == nil) {
           27                 fprint(2, "out of memory allocating %d\n", sz);
           28                 wexits("mem");
           29         }
           30         return v;
           31 }
           32 
           33 char*
           34 estrdup(char *s)
           35 {
           36         char *t;
           37         if((t = strdup(s)) == nil) {
           38                 fprint(2, "out of memory in strdup(%.10s)\n", s);
           39                 wexits("mem");
           40         }
           41         return t;
           42 }
           43 
           44 /*
           45  * spool standard input to /tmp.
           46  * we've already read the initial in bytes into ibuf.
           47  */
           48 int
           49 spooltodisk(uchar *ibuf, int in, char **name)
           50 {
           51         uchar buf[8192];
           52         int fd, n;
           53 
           54         strcpy(tempfile, "/tmp/pagespoolXXXXXXXXX");
           55         fd = opentemp(tempfile, ORDWR);
           56         if(name)
           57                 *name = estrdup(tempfile);
           58 
           59         if(write(fd, ibuf, in) != in){
           60                 fprint(2, "error writing temporary file\n");
           61                 wexits("write temp");
           62         }
           63 
           64         while((n = read(stdinfd, buf, sizeof buf)) > 0){
           65                 if(write(fd, buf, n) != n){
           66                         fprint(2, "error writing temporary file\n");
           67                         wexits("write temp0");
           68                 }
           69         }
           70         seek(fd, 0, 0);
           71         return fd;
           72 }
           73 
           74 typedef struct StdinArg StdinArg;
           75 
           76 struct StdinArg {
           77         Channel *cp;
           78         uchar        *ibuf;
           79         int        in;
           80 };
           81 
           82 /*
           83  * spool standard input into a pipe.
           84  * we've already ready the first in bytes into ibuf
           85  */
           86 static void
           87 _stdinpipe(void *a)
           88 {
           89         uchar buf[8192];
           90         StdinArg *arg;
           91         int p[2];
           92         int n;
           93 
           94         arg = a;
           95 
           96         if(pipe(p) < 0){
           97                 fprint(2, "pipe fails: %r\n");
           98                 wexits("pipe");
           99         }
          100 
          101         send(arg->cp, &p[0]);
          102 
          103         write(p[1], arg->ibuf, arg->in);
          104         while((n = read(stdinfd, buf, sizeof buf)) > 0)
          105                 write(p[1], buf, n);
          106 
          107         close(p[1]);
          108         threadexits(0);
          109 }
          110 
          111 int
          112 stdinpipe(uchar *ibuf, int in) {
          113         StdinArg arg;
          114         int fd;
          115 
          116         arg.ibuf = ibuf;
          117         arg.in = in;
          118         arg.cp = chancreate(sizeof(int), 0);
          119         proccreate(_stdinpipe, &arg, mainstacksize);
          120         recv(arg.cp, &fd);
          121         chanfree(arg.cp);
          122 
          123         return fd;
          124 }