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