Mercurial > hg > toybox
comparison lib/lib.c @ 185:29e2051296fd
Add loopfiles() function, make catv use it.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 29 Nov 2007 17:49:50 -0600 |
parents | f16c8e5e9435 |
children | c983a0af6d4e |
comparison
equal
deleted
inserted
replaced
184:2d0de0d21e80 | 185:29e2051296fd |
---|---|
543 if (i == 3) error_exit("xpidfile %s", name); | 543 if (i == 3) error_exit("xpidfile %s", name); |
544 | 544 |
545 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); | 545 xwrite(fd, spid, sprintf(spid, "%ld\n", (long)getpid())); |
546 close(fd); | 546 close(fd); |
547 } | 547 } |
548 | |
549 // Iterate through an array of files, opening each one (read only) and | |
550 // calling a function on that filehandle and name. The special filename | |
551 // "-" means stdin. An empty argument list calls function() on stdin. | |
552 void loopfiles(char **argv, void (*function)(int fd, char *name)) | |
553 { | |
554 int fd; | |
555 | |
556 // If no arguments, read from stdin. | |
557 if (!*argv) function(0, *argv); | |
558 else do { | |
559 // Filename "-" means read from stdin. | |
560 // Inability to open a file prints a warning, but doesn't exit. | |
561 | |
562 if (!strcmp(*argv,"-")) fd=0; | |
563 else if (0>(fd = open(*argv, O_RDONLY))) { | |
564 perror_msg("%s",*argv); | |
565 toys.exitval = 1; | |
566 } | |
567 function(fd, *argv); | |
568 close(fd); | |
569 } while (*++argv); | |
570 } |