1
0
Fork 0

more magic

This commit is contained in:
dtb 2024-02-16 21:53:42 -07:00
parent ef590f67a9
commit b26f7dbf93
1 changed files with 44 additions and 33 deletions

View File

@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR */ #include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
* struct dirent */
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */ #include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
#include <stdlib.h> /* realloc(3) */ #include <stdlib.h> /* realloc(3) */
#include <string.h> /* strerror(3) */ #include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
#include <sysexits.h> /* EX_OSERR, EX_USAGE */ #include <sysexits.h> /* EX_OK, EX_INVALID, EX_OSERR, EX_USAGE */
#include <unistd.h> /* getopt(3) */
#include <getopt.h>
static char *dot[] = {".", NULL}; /* default (argc<2) */ static char *dot[] = {".", NULL}; /* default (argc<2) */
@ -38,9 +38,9 @@ fnprint(char *fn){
static char *argv0; static char *argv0;
// Walks the directory named dirname, printing the names of all files it /* Walks the directory named dirname, printing the names of all files it
// contains (but not the name of the directory itself). Returns 2 if dirname is * contains (but not the name of the directory itself).
// not a directory and 1 if another error occurs. * Returns something other than EX_OK with errno set if an error occurs. */
static int static int
walk(char *dirname){ walk(char *dirname){
DIR dir; DIR dir;
@ -50,23 +50,20 @@ walk(char *dirname){
char *np; char *np;
int retval; int retval;
if((dir = opendir(dirname)) == NULL){ fnprint(dirname);
fprintf(stdout, "%s: %s: %s\n",
argv0, dirname, strerror(errno)); if((dir = opendir(dirname)) == NULL)
return 1 + (errno == ENOTDIR); return EX_NOINPUT;
}
errno = 0; errno = 0;
retval = 0;
while((f = readdir(dir)) != NULL){ while((f = readdir(dir)) != NULL){
if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0) if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0)
continue; continue;
if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1) if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1)
> filename.a){ > filename.a){
if((np = realloc(filename.p, l)) == NULL){ if((np = realloc(filename.p, l)) == NULL)
perror(argv0);
return EX_OSERR; return EX_OSERR;
}else{ else{
filename.a = l; filename.a = l;
filename.p = np; /* would you look at that */ filename.p = np; /* would you look at that */
} }
@ -74,19 +71,28 @@ walk(char *dirname){
stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name); stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name);
/* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */ /* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */
fnprint(filename.p); fnprint(filename.p);
/* Walk the file if we can successfully open it as a directory. /* Walk the file if we can successfully open it as a
* Don't worry about it if it's not one (walk(filename) == 2). */ * directory. */
if ((f->d_type == DT_DIR || f->d_type == DT_UNKNOWN) if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
&& walk(filename.p) == 1) if((retval = walk(filename.p)) != EX_OK){
retval = 1; if(retval == EX_OSERR)
} return retval;
if(errno != 0 || closedir(dir) != NULL){ else{
fprintf(stderr, "%s: %s: %s\n", fnprint(filename.p);
argv0, dirname, strerror(errno)); fprintf(stderr, "%s: %s: %s\n",
retval = 1; argv0, filename.p,
strerror(errno));
}
}
}else
fnprint(filename.p);
} }
return retval; if(errno != 0 || closedir(dir) != NULL)
fprintf(stderr, "%s: %s: %s\n",
argv0, dirname, strerror(errno));
return EX_OK;
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
@ -117,10 +123,15 @@ int main(int argc, char *argv[]){
if(*argv == NULL) if(*argv == NULL)
argv = dot; argv = dot;
while(*argv != NULL){ while(*argv != NULL)
fnprint(*argv); if((retval = walk(*(argv++))) != EX_OK)
retval |= walk(*(argv++)); switch(retval){
} case EX_OSERR: perror(argv0); return retval;
case EX_NOINPUT:
fprintf(stderr, "%s: %s: %s\n",
argv0, *(argv - 1), strerror(errno));
break;
}
return retval; return EX_OK;
} }