URI:
       tprint.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
       ---
       tprint.c (1027B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <fcall.h>
            4 #include <9pclient.h>
            5 
            6 /* C99 nonsense */
            7 #ifdef va_copy
            8 #        define VA_COPY(a,b) va_copy(a,b)
            9 #        define VA_END(a) va_end(a)
           10 #else
           11 #        define VA_COPY(a,b) (a) = (b)
           12 #        define VA_END(a)
           13 #endif
           14 
           15 static int
           16 fidflush(Fmt *f)
           17 {
           18         int n;
           19 
           20         n = (char*)f->to - (char*)f->start;
           21         if(n && fswrite(f->farg, f->start, n) != n)
           22                 return 0;
           23         f->to = f->start;
           24         return 1;
           25 }
           26 
           27 static int
           28 fsfmtfidinit(Fmt *f, CFid *fid, char *buf, int size)
           29 {
           30         f->runes = 0;
           31         f->start = buf;
           32         f->to = buf;
           33         f->stop = buf + size;
           34         f->flush = fidflush;
           35         f->farg = fid;
           36         f->nfmt = 0;
           37         fmtlocaleinit(f, nil, nil, nil);
           38         return 0;
           39 }
           40 
           41 int
           42 fsprint(CFid *fd, char *fmt, ...)
           43 {
           44         int n;
           45         va_list args;
           46 
           47         va_start(args, fmt);
           48         n = fsvprint(fd, fmt, args);
           49         va_end(args);
           50         return n;
           51 }
           52 
           53 int
           54 fsvprint(CFid *fd, char *fmt, va_list args)
           55 {
           56         Fmt f;
           57         char buf[256];
           58         int n;
           59 
           60         fsfmtfidinit(&f, fd, buf, sizeof(buf));
           61         VA_COPY(f.args,args);
           62         n = dofmt(&f, fmt);
           63         VA_END(f.args);
           64         if(n > 0 && fidflush(&f) == 0)
           65                 return -1;
           66         return n;
           67 }