diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 48 | ||||
-rw-r--r-- | lib/lib.h | 10 |
2 files changed, 58 insertions, 0 deletions
@@ -192,6 +192,11 @@ int xopen(char *path, int flags) return xcreate(path, flags, 0); } +void xclose(int fd) +{ + if (close(fd)) perror_exit("xclose"); +} + // Die unless we can open/create a file, returning FILE *. FILE *xfopen(char *path, char *mode) { @@ -567,3 +572,46 @@ void loopfiles(char **argv, void (*function)(int fd, char *name)) close(fd); } while (*++argv); } + +// Slow, but small. + +char *get_rawline(int fd, long *plen) +{ + char c, *buf = NULL; + long len = 0; + + for (;;) { + if (1>read(fd, &c, 1)) break; + if (!(len & 63)) buf=xrealloc(buf, len+64); + if ((buf[len++]=c) == '\n') break; + } + if (buf) buf[len]=0; + if (plen) *plen = len; + + return buf; +} + +char *get_line(int fd) +{ + long len; + char *buf = get_rawline(fd, &len); + + if (buf && buf[--len]=='\n') buf[len]=0; + + return buf; +} + +// Copy the rest of in to out and close both files. + +void xsendfile(int in, int out) +{ + long len; + + if (in<0) return; + for (;;) { + len = xread(in, toybuf, sizeof(toybuf)); + if (len<1) break; + xwrite(out, toybuf, len); + } + xclose(in); +} @@ -21,6 +21,12 @@ struct arg_list { char *arg; }; +struct double_list { + struct double_list *next; + struct double_list *prev; + char *data; +}; + // args.c void get_optflags(void); @@ -56,6 +62,7 @@ void xexec(char **argv); void xaccess(char *path, int flags); int xcreate(char *path, int flags, int mode); int xopen(char *path, int flags); +void xclose(int fd); FILE *xfopen(char *path, char *mode); ssize_t readall(int fd, void *buf, size_t len); ssize_t writeall(int fd, void *buf, size_t len); @@ -74,6 +81,9 @@ long atolx(char *c); off_t fdlength(int fd); char *xreadlink(char *name); void loopfiles(char **argv, void (*function)(int fd, char *name)); +char *get_rawline(int fd, long *plen); +char *get_line(int fd); +void xsendfile(int in, int out); // getmountlist.c struct mtab_list { |