Mercurial > hg > toybox
diff lib/args.c @ 49:bb4c38853a7d
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.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 07 Jan 2007 22:51:12 -0500 |
parents | 993eab821bd5 |
children | cd1f36a96185 |
line wrap: on
line diff
--- a/lib/args.c Sun Jan 07 03:48:26 2007 -0500 +++ b/lib/args.c Sun Jan 07 22:51:12 2007 -0500 @@ -289,3 +289,35 @@ 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); +}