aboutsummaryrefslogtreecommitdiff
path: root/lib/args.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2007-01-07 22:51:12 -0500
committerRob Landley <rob@landley.net>2007-01-07 22:51:12 -0500
commit1322beb384ea43a15c17f8229e7db070949dd331 (patch)
tree8cd23c638a6b6aeffa0a761025848e7bce60d4f1 /lib/args.c
parent016bf8289e12346f45f543d5b048d2496f4f0256 (diff)
downloadtoybox-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.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/args.c b/lib/args.c
index a6a3b229..be62c7f7 100644
--- a/lib/args.c
+++ b/lib/args.c
@@ -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);
+}