Mercurial > hg > toybox
diff lib/lib.c @ 308:52600eee8dd6
Add "tee" command.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 03 Jul 2008 19:19:00 -0500 |
parents | 5a0faa267866 |
children | 79a61cd58596 |
line wrap: on
line diff
--- a/lib/lib.c Thu Jul 03 19:16:38 2008 -0500 +++ b/lib/lib.c Thu Jul 03 19:19:00 2008 -0500 @@ -593,30 +593,40 @@ close(fd); } -// Iterate through an array of files, opening each one (read only) and -// calling a function on that filehandle and name. The special filename -// "-" means stdin. An empty argument list calls function() on stdin. -void loopfiles(char **argv, void (*function)(int fd, char *name)) +// Iterate through an array of files, opening each one and calling a function +// on that filehandle and name. The special filename "-" means stdin if +// flags is O_RDONLY, stdout otherwise. An empty argument list calls +// function() on just stdin/stdout. +// +// Note: read only filehandles are automatically closed when function() +// returns, but writeable filehandles must be close by function() +void loopfiles_rw(char **argv, int flags, void (*function)(int fd, char *name)) { int fd; // If no arguments, read from stdin. - if (!*argv) function(0, "-"); + if (!*argv) function(flags ? 1 : 0, "-"); else do { // Filename "-" means read from stdin. // Inability to open a file prints a warning, but doesn't exit. if (!strcmp(*argv,"-")) fd=0; - else if (0>(fd = open(*argv, O_RDONLY))) { + else if (0>(fd = open(*argv, flags, 0666))) { perror_msg("%s", *argv); toys.exitval = 1; continue; } function(fd, *argv); - close(fd); + if (!flags) close(fd); } while (*++argv); } +// Call loopfiles_rw with O_RDONLY (common case). +void loopfiles(char **argv, void (*function)(int fd, char *name)) +{ + loopfiles_rw(argv, O_RDONLY, function); +} + // Slow, but small. char *get_rawline(int fd, long *plen, char end)