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 (1061B)
       ---
            1 #include "a.h"
            2 
            3 void*
            4 emalloc(int n)
            5 {
            6         void *v;
            7 
            8         v = mallocz(n, 1);
            9         if(v == nil)
           10                 sysfatal("out of memory");
           11         return v;
           12 }
           13 
           14 void*
           15 erealloc(void *v, int n)
           16 {
           17         v = realloc(v, n);
           18         if(v == nil)
           19                 sysfatal("out of memory");
           20         return v;
           21 }
           22 
           23 char*
           24 estrdup(char *s)
           25 {
           26         s = strdup(s);
           27         if(s == nil)
           28                 sysfatal("out of memory");
           29         return s;
           30 }
           31 
           32 int
           33 timefmt(Fmt *f)
           34 {
           35         Tm tm;
           36         vlong ms;
           37 
           38         ms = nsec()/1000000;
           39 
           40         tm = *localtime(ms/1000);
           41         fmtprint(f, "%02d:%02d:%02d.%03d",
           42                 tm.hour, tm.min, tm.sec,
           43                 (int)(ms%1000));
           44         return 0;
           45 }
           46 
           47 int
           48 writen(int fd, void *buf, int n)
           49 {
           50         long m, tot;
           51 
           52         for(tot=0; tot<n; tot+=m){
           53                 m = n - tot;
           54                 if(m > 8192)
           55                         m = 8192;
           56                 if(write(fd, (uchar*)buf+tot, m) != m)
           57                         break;
           58         }
           59         return tot;
           60 }
           61 
           62 int
           63 urlencodefmt(Fmt *fmt)
           64 {
           65         int x;
           66         char *s;
           67 
           68         s = va_arg(fmt->args, char*);
           69         for(; *s; s++){
           70                 x = (uchar)*s;
           71                 if(x == ' ')
           72                         fmtrune(fmt, '+');
           73                 else if(('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z') || ('0' <= x && x <= '9')
           74                         || strchr("$-_.+!*'()", x)){
           75                         fmtrune(fmt, x);
           76                 }else
           77                         fmtprint(fmt, "%%%02ux", x);
           78         }
           79         return 0;
           80 }