add -q, finish -l
This commit is contained in:
parent
6a0e107835
commit
d4960d974e
52
walk/walk.c
52
walk/walk.c
@ -62,9 +62,12 @@ fnprint(char *fn){
|
||||
return fprintf(stdout, "%s%s", fn, terminator);
|
||||
}
|
||||
|
||||
/* Walks the directory named dirname, printing the names of all files it
|
||||
* contains (but not the name of the directory itself).
|
||||
* Returns something other than EX_OK with errno set if an error occurs. */
|
||||
/* Walks dirname, printing first its name and then the names of its files,
|
||||
* unless a file is a directory, in which case, as long as levels is -1 or
|
||||
* greater than 0, it recurses (passing the recurring walk a decremented
|
||||
* levels). argv0 is used for diagnostic messages; if it's NULL none are
|
||||
* printed. Returns EX_OK if all is well, EX_NOINPUT if dirname wasn't a
|
||||
* directory, and EX_OSERR if a memory allocation failed. */
|
||||
static int
|
||||
walk(char *dirname, unsigned int levels, char *argv0){
|
||||
DIR *dir;
|
||||
@ -84,6 +87,7 @@ walk(char *dirname, unsigned int levels, char *argv0){
|
||||
while((f = readdir(dir)) != NULL){
|
||||
if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0)
|
||||
continue;
|
||||
/* fat pointer foolishness to avoid unnecessary reallocation */
|
||||
if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1) > filename.a){
|
||||
if((np = realloc(filename.s, l)) == NULL){
|
||||
free(filename.s);
|
||||
@ -93,8 +97,8 @@ walk(char *dirname, unsigned int levels, char *argv0){
|
||||
filename.s = np;
|
||||
}
|
||||
stpcpy(stpcpy(stpcpy(filename.s, dirname), "/"), f->d_name);
|
||||
/* Walk the file if we can successfully open it as a
|
||||
* directory. */
|
||||
|
||||
/* Walk the file if we can successfully open it as a directory. */
|
||||
if((f->d_type == DT_DIR || f->d_type == DT_UNKNOWN) && levels != 0){
|
||||
if((retval = walk(filename.s, levels == -1 ? -1 : levels - 1,
|
||||
argv0)) != EX_OK){
|
||||
@ -102,7 +106,7 @@ walk(char *dirname, unsigned int levels, char *argv0){
|
||||
free(filename.s);
|
||||
return retval;
|
||||
}
|
||||
else if(retval != EX_NOINPUT)
|
||||
else if(retval != EX_NOINPUT && argv0 != NULL)
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
argv0, filename.s, strerror(errno));
|
||||
}
|
||||
@ -110,7 +114,7 @@ walk(char *dirname, unsigned int levels, char *argv0){
|
||||
fnprint(filename.s);
|
||||
}
|
||||
|
||||
if(errno != 0 || closedir(dir) != 0)
|
||||
if((errno != 0 || closedir(dir) != 0) && argv0 != NULL)
|
||||
fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno));
|
||||
|
||||
free(filename.s);
|
||||
@ -121,30 +125,33 @@ int main(int argc, char *argv[]){
|
||||
char *argv0;
|
||||
int c;
|
||||
int levels;
|
||||
int retval;
|
||||
int verbosity;
|
||||
|
||||
argv0 = argv[0] == NULL ? program_name : argv[0];
|
||||
levels = -1; /* no limit */
|
||||
terminator = asv_terminator;
|
||||
verbosity = 2;
|
||||
|
||||
if(argc > 0){
|
||||
while((c = getopt(argc, argv, "0d:l:n")) != -1)
|
||||
switch(c){
|
||||
case '0': terminator = nul_terminator; break;
|
||||
case '0': terminator = nul_terminator; break;
|
||||
case 'n': terminator = newl_terminator; break;
|
||||
case 'd': terminator = optarg; break;
|
||||
case 'd': terminator = optarg; break;
|
||||
case 'l': {
|
||||
long l;
|
||||
|
||||
errno = 0;
|
||||
if((l = strtol(optarg, &optarg, 0) - 1) >= 1 && l <= INT_MAX
|
||||
&& *optarg != '\0'){
|
||||
&& *optarg == '\0' && errno == 0){
|
||||
levels = l;
|
||||
break;
|
||||
}
|
||||
}
|
||||
case 'q': --verbosity; break;
|
||||
default:
|
||||
fprintf(stderr,
|
||||
"Usage: %s (-0n)"
|
||||
"Usage: %s (-0nq)"
|
||||
" (-d [delimiter]) (-l [max levels])"
|
||||
" (directories...)\n", argv[0]);
|
||||
return EX_USAGE;
|
||||
@ -152,20 +159,21 @@ int main(int argc, char *argv[]){
|
||||
argv += optind;
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
|
||||
if(*argv == NULL)
|
||||
argv = dot;
|
||||
|
||||
while(*argv != NULL)
|
||||
if((retval = walk(*(argv++), levels, argv0)) != EX_OK)
|
||||
switch(retval){
|
||||
case EX_OSERR: perror(argv0); return retval;
|
||||
case EX_NOINPUT:
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
argv0, *(argv - 1), strerror(errno));
|
||||
break;
|
||||
{
|
||||
int retval;
|
||||
|
||||
for(retval = 0; *argv != NULL; ++argv)
|
||||
if((retval = walk(*argv, levels, verbosity >= 2 ? argv0 : NULL))
|
||||
!= EX_OK){
|
||||
if(verbosity >= 1)
|
||||
fprintf(stderr, "%s: %s: %s\n",
|
||||
argv0, *argv, strerror(errno));
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user