diff options
author | Rob Landley <rob@landley.net> | 2007-01-07 22:51:12 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2007-01-07 22:51:12 -0500 |
commit | 1322beb384ea43a15c17f8229e7db070949dd331 (patch) | |
tree | 8cd23c638a6b6aeffa0a761025848e7bce60d4f1 | |
parent | 016bf8289e12346f45f543d5b048d2496f4f0256 (diff) | |
download | toybox-1322beb384ea43a15c17f8229e7db070949dd331.tar.gz |
xopen() wants 2 arguments unless you're creating a file, in which case you
need 3. Doing varargs for this doesn't really appeal to me (bugs in waiting)
so I made an xcreate() that takes 3 args, and had xopen() call it with 0 for
the third argument. That way, if we feed O_CREAT to xopen() the permission
000 result should be easy to spot.
-rw-r--r-- | lib/args.c | 32 | ||||
-rw-r--r-- | lib/functions.c | 8 | ||||
-rw-r--r-- | lib/lib.h | 4 | ||||
-rw-r--r-- | toys/catv.c | 2 |
4 files changed, 43 insertions, 3 deletions
@@ -289,3 +289,35 @@ notflag: if (optarg<minargs) error_exit("Need %d arguments", minargs); if (optarg>maxargs) error_exit("Max %d arguments", maxargs); } + +// Loop through files listed on the command line + +static int dofileargs(char ***files, int fd, int iswrite) +{ + char *filename = *((*files)++); + static int flags[] = {O_RDONLY, O_CREAT|O_TRUNC, O_RDWR}; + + if (fd != -1) close(fd); + + for (;;) { + + // Are there no more files? + if (!*filename) + return (fd == -1) ? iswrite : -1; + + // A filename of "-" means stdin. + if (*filename == '-' && !filename[1]) return 0; + + fd = xcreate(filename, flags[iswrite], 0777); + } +} + +int readfileargs(char ***files, int fd) +{ + return dofileargs(files, fd, 0); +} + +int writefileargs(char ***files, int fd) +{ + return dofileargs(files, fd, 1); +} diff --git a/lib/functions.c b/lib/functions.c index b42f62d3..990c34fa 100644 --- a/lib/functions.c +++ b/lib/functions.c @@ -141,13 +141,19 @@ void xexec(char **argv) } // Die unless we can open/create a file, returning file descriptor. -int xopen(char *path, int flags, int mode) +int xcreate(char *path, int flags, int mode) { int fd = open(path, flags, mode); if (fd == -1) error_exit("No file %s\n", path); return fd; } +// Die unless we can open a file, returning file descriptor. +int xopen(char *path, int flags) +{ + return xcreate(path, flags, 0); +} + // Die unless we can open/create a file, returning FILE *. FILE *xfopen(char *path, char *mode) { @@ -38,7 +38,8 @@ void *xstrndup(char *s, size_t n); void *xstrdup(char *s); char *xmsprintf(char *format, ...); void xexec(char **argv); -int xopen(char *path, int flags, int mode); +int xcreate(char *path, int flags, int mode); +int xopen(char *path, int flags); FILE *xfopen(char *path, char *mode); ssize_t reread(int fd, void *buf, size_t count); ssize_t readall(int fd, void *buf, size_t count); @@ -63,3 +64,4 @@ struct mtab_list { struct mtab_list *getmountlist(int die); +char *bunzipStream(int src_fd, int dst_fd); diff --git a/toys/catv.c b/toys/catv.c index 3a36de6b..e37f307b 100644 --- a/toys/catv.c +++ b/toys/catv.c @@ -23,7 +23,7 @@ int catv_main(void) // Read from stdin if there's nothing else to do. fd = 0; - if (*argv && 0>(fd = xopen(*argv, O_RDONLY, 0))) retval = EXIT_FAILURE; + if (*argv && 0>(fd = xopen(*argv, O_RDONLY))) retval = EXIT_FAILURE; else for(;;) { int i, res; |