libb. convenience tools form the plan b library. mostly as an experiment, although a local cmdfs program to create and control processes through a fs is using it. it's likely that zerocopy will replace some of these. Reference: /n/patches.lsub.org/patch/libb Date: Tue Apr 24 16:31:23 CES 2012 Signed-off-by: nemo@lsub.org --- /sys/man/2/readf Thu Jan 1 00:00:00 1970 +++ /sys/man/2/readf Tue Apr 24 16:25:29 2012 @@ -0,0 +1,136 @@ +.TH READF 2 +.SH NAME +readf, readfstr, createf, writef, writefstr, cmdoutput, tcmdoutput \- Plan B convenience tools +.SH SYNOPSIS +.B #include +.br +.B #include +.br +.B #include +.PP +.nf +.PP +.B +void* readf(char* file, void* buf, long len, long* nout) +.PP +.B +char* readfstr(char* file) +.PP +.B +long writef(char* file, void* buf, long len) +.PP +.B +long createf(char* file, void* buf, long len, ulong perm); +.PP +.B +long writefstr(char* file, char* str) +.PP +.B +long cmdoutput(char* cmd, char* out, long sz); +.PP +.B +long tcmdoutput(char* cmd, char* out, long sz); +.SH DESCRIPTION +The first few functions provide a convenience interface to perform entire file I/O and avoid +keeping file descriptors open for too long, which is important when using volumes +provided through +.IR bns (8). +The last functions are convenience routines used by many Plan B tools. +.PP +.I Readf +reads all the contents of +.I file +into a memory buffer and returns a pointer to it. For streams, only a single +.I read +is made. The memory can be supplied by the caller, +by giving a non-nil +.I buf +and setting +.I len +to the size of the buffer provided. When +.I buf +is nil, +memory for the buffer is allocated with +.IR malloc (2) +and the caller is responsible for calling +.IR free (2) +to release it. +If +.I nout +is not nil, +the number of bytes read is placed in +.BR * nout. +.PP +.I Writef +performs the opposite operation, and stores +.I len +bytes starting at +.I buf +into +.IR file . +The file must exist and have write permission. +It is legal to store zero bytes, to truncate a file. +.I Writef +returns the number of bytes stored or a negative value on errors. +.PP +.I Createf +is like +.IR writef , +but is creates the file when it does not exist. The file +mode is set to +.IR mode . +When creating a directory, the user supplied data is ignored. +.PP +The functions +.I readfstr +and +.I writefstr +are convenience wrappers that read and write files that +do not contain null characters. They provide a simpler interface +for the common case when the file contains text. Note that +.I readfstr +allocates the memory used, and the caller is responsible for +releasing it. Also, +.I readfstr +returns a valid string for C or nil on errors. +.PP +For files too big to have its size contained in a long +integer, the functions described in +.IR read (2) +must be used instead. +.PP +The last two routines, +.I cmdoutput +and +.IR tcmdoutput , +run +.I cmd +as an +.IR rc (1) +command line +and place in +.I out +at most +.I sz +bytes resulting from of standard output of the command. The number of bytes read +is the result of the function. The former uses processes and the later is for use with +.IR thread (2). +.SH SOURCE +.B /sys/src/libb +.SH SEE ALSO +.IR intro (2), +.IR read (2) +.SH DIAGNOSTICS +These functions set +.IR errstr . +.I Readf +and +.I readfstr +return +.B nil +upon errors. +.SH BUGS +The sizes should be 64 bit integers. +For streams, +.I readf +reads at most 16Kbytes each time it is called. --- /sys/include/b.h Thu Jan 1 00:00:00 1970 +++ /sys/include/b.h Tue Apr 24 16:27:05 2012 @@ -0,0 +1,10 @@ +#pragma lib "libb.a" +#pragma src "/sys/src/libb" + +extern void* readf(char*f, void* buf, long l, long *ol); +extern char* readfstr(char*f); +extern long writef(char* f, void* buf, long len); +extern long writefstr(char* f, char* s); +extern long createf(char* f, void* buf, long len, ulong mode); +extern long cmdoutput(char* cmd, char* out, long sz); +extern long tcmdoutput(char* cmd, char* out, long sz); --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Tue May 23 16:44:50 2006 @@ -0,0 +1,57 @@ +#include +#include +#include + +enum { + Nargs = 64 +}; + +long +cmdoutput(char* cmd, char* out, long sz) +{ + char* argv[Nargs]; + char* s; + int argc; + int p[2]; + long tot, n; + + s = strdup(cmd); + if (s == nil) + return -1; + argc = tokenize(s, argv, nelem(argv)-1); + if (argc < 1){ + free(s); + return -1; + } + argv[argc] = nil; + if (pipe(p) < 0){ + free(s); + return -1; + } + switch(rfork(RFPROC|RFFDG|RFENVG|RFNOWAIT)){ + case -1: + free(s); + close(p[0]); + close(p[1]); + return -1; + case 0: + close(p[0]); + dup(p[1], 1); + close(p[1]); + exec(argv[0], argv); + _exits("exec env/terms failed"); + return -1; + break; + default: + free(s); + close(p[1]); + for(tot = 0; sz - tot > 1 ; tot +=n){ + n = read(p[0], out+tot, sz - tot); + if (n <= 0) + break; + } + close(p[0]); + out[tot] = 0; + return tot; + } +} --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Fri Oct 28 11:29:04 2005 @@ -0,0 +1,24 @@ +#include +#include +#include + +long +createf(char* f, void* buf, long len, ulong mode) +{ + int fd; + long r; + + // BUG: retry it all if we get io errors while writing. + + fd = create(f, OWRITE|OTRUNC, mode); + if (fd < 0) + return -1; + if (len > 0) + r = write(fd, buf, len); + else + r = 0; + close(fd); + return r; + +} + --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Tue Apr 24 16:24:29 2012 @@ -0,0 +1,33 @@ + +#include +#include + +void* +readf(char*f, void* buf, long n, long* nout) +{ + char err[ERRMAX]; + Dir* d; + char* mbuf; + int fd; + + fd = open(f, OREAD); + if (fd < 0) + return nil; + mbuf = nil; + if (nout == nil) + nout = &n; + d = dirfstat(fd); + if (d == nil) + goto fail; + + if (buf == nil){ + n = d->length; + if (n == 0) + n = 16 * 1024; // Next read from a stream. + mbuf = buf = malloc(n + 1); + if (mbuf == nil) + goto fail; + } + + if (d->length == 0) + *nout = read(fd, buf, n); + else + *nout = readn(fd, buf, n); + if (*nout < 0) + goto fail; + if (mbuf != nil) + mbuf[*nout] = 0; + free(d); + close(fd); + return buf; +fail: + rerrstr(err, sizeof(err)); + free(mbuf); + free(d); + close(fd); + werrstr(err); + *nout = -1; + return nil; +} --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Fri Oct 28 12:32:21 2005 @@ -0,0 +1,19 @@ +#include +#include +#include + +char* +readfstr(char*f) +{ + long l; + char* s; + + s = readf(f, nil, l, &l); + if (s && strlen(s) != l){ + werrstr("binary file"); + free(s); + return nil; + } + return s; +} + --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Thu Mar 23 14:19:55 2006 @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +typedef struct Arg Arg; + +struct Arg { + int p[2]; + char**argv; +}; + +enum { + Nargs = 64 +}; + +void +cmdproc(void* x) +{ + Arg* a = x; + char** argv; + + argv = a->argv; + close(a->p[0]); + dup(a->p[1], 1); + close(a->p[1]); + procexec(nil, argv[0], argv); + threadexits(nil); +} + +long +tcmdoutput(char* cmd, char* out, long sz) +{ + long tot, n; + Arg a; + char* argv[Nargs]; + int argc; + char* s; + + s = strdup(cmd); + if (s == nil) + return -1; + argc = tokenize(s, argv, nelem(argv)-1); + if (argc < 1){ + free(s); + return -1; + } + argv[argc] = nil; + if (pipe(a.p) < 0){ + free(s); + return -1; + } + a.argv = argv; + procrfork(cmdproc, &a, 8*1024, RFFDG|RFENVG); + close(a.p[1]); + for(tot = 0; sz - tot > 1 ; tot +=n){ + n = read(a.p[0], out+tot, sz - tot); + if (n <= 0) + break; + } + free(s); + close(a.p[0]); + out[tot] = 0; + return tot; +} --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Fri Nov 4 15:36:16 2005 @@ -0,0 +1,23 @@ +#include +#include +#include + +long +writef(char* f, void* buf, long len) +{ + int fd; + long r; + + // BUG: retry it all if we get io errors while writing. + + fd = open(f, OWRITE|OTRUNC); + if (fd < 0) + return -1; + if (len > 0) + r = write(fd, buf, len); + else + r = 0; + close(fd); + return r; + +} --- /sys/src/libb Thu Jan 1 00:00:00 1970 +++ /sys/src/libb Fri Oct 28 11:29:12 2005 @@ -0,0 +1,10 @@ +#include +#include +#include + +long +writefstr(char* f, char* s) +{ + return writef(f, s, strlen(s)); +} + --- /sys/src/mkfile Thu Apr 12 12:26:26 2012 +++ /sys/src/mkfile Tue Apr 24 16:23:45 2012 @@ -6,6 +6,7 @@ libauth\ libauthsrv\ libavl\ + libb\ libbin\ libbio\ libc\