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 /lib/args.c | |
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.
Diffstat (limited to 'lib/args.c')
-rw-r--r-- | lib/args.c | 32 |
1 files changed, 32 insertions, 0 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); +} |