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 (1300B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <libsec.h>
            5 #include <ctype.h>
            6 
            7 #include "iso9660.h"
            8 
            9 typedef struct Stringtab        Stringtab;
           10 struct Stringtab {
           11         Stringtab *link;
           12         char *str;
           13 };
           14 
           15 static Stringtab *stab[1024];
           16 
           17 static uint
           18 hash(char *s)
           19 {
           20         uint h;
           21         uchar *p;
           22 
           23         h = 0;
           24         for(p=(uchar*)s; *p; p++)
           25                 h = h*37 + *p;
           26         return h;
           27 }
           28 
           29 static char*
           30 estrdup(char *s)
           31 {
           32         if((s = strdup(s)) == nil)
           33                 sysfatal("strdup(%.10s): out of memory", s);
           34         return s;
           35 }
           36 
           37 char*
           38 atom(char *str)
           39 {
           40         uint h;
           41         Stringtab *tab;
           42 
           43         h = hash(str) % nelem(stab);
           44         for(tab=stab[h]; tab; tab=tab->link)
           45                 if(strcmp(str, tab->str) == 0)
           46                         return tab->str;
           47 
           48         tab = emalloc(sizeof *tab);
           49         tab->str = estrdup(str);
           50         tab->link = stab[h];
           51         stab[h] = tab;
           52         return tab->str;
           53 }
           54 
           55 void*
           56 emalloc(ulong n)
           57 {
           58         void *p;
           59 
           60         if((p = malloc(n)) == nil)
           61                 sysfatal("malloc(%lud): out of memory", n);
           62         memset(p, 0, n);
           63         return p;
           64 }
           65 
           66 void*
           67 erealloc(void *v, ulong n)
           68 {
           69         if((v = realloc(v, n)) == nil)
           70                 sysfatal("realloc(%p, %lud): out of memory", v, n);
           71         return v;
           72 }
           73 
           74 char*
           75 struprcpy(char *p, char *s)
           76 {
           77         char *op;
           78 
           79         op = p;
           80         for(; *s; s++)
           81                 *p++ = toupper((uchar)*s);
           82         *p = '\0';
           83 
           84         return op;
           85 }
           86 
           87 int
           88 chat(char *fmt, ...)
           89 {
           90         va_list arg;
           91 
           92         if(!chatty)
           93                 return 0;
           94         va_start(arg, fmt);
           95         vfprint(2, fmt, arg);
           96         va_end(arg);
           97         return 1;
           98 }