1
0

almost done

This commit is contained in:
dtb 2024-02-18 09:38:53 -07:00
parent a086e27e75
commit 6a0e107835

View File

@ -34,8 +34,9 @@
*/
#include <errno.h> /* errno */
#include <limits.h> /* UINT_MAX */
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
#include <stdlib.h> /* free(3), realloc(3) */
#include <stdlib.h> /* free(3), realloc(3), strtol(3) */
#include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
#if !defined EX_OK || !defined EX_INVALID || !defined EX_OSERR \
|| !defined EX_USAGE
@ -65,7 +66,7 @@ fnprint(char *fn){
* contains (but not the name of the directory itself).
* Returns something other than EX_OK with errno set if an error occurs. */
static int
walk(char *dirname, char *argv0){
walk(char *dirname, unsigned int levels, char *argv0){
DIR *dir;
struct dirent *f;
struct { size_t a; char *s; } filename = { 0, NULL };
@ -92,11 +93,11 @@ walk(char *dirname, char *argv0){
filename.s = np;
}
stpcpy(stpcpy(stpcpy(filename.s, dirname), "/"), f->d_name);
/* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */
/* Walk the file if we can successfully open it as a
* directory. */
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
if((retval = walk(filename.s, argv0)) != EX_OK){
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){
if(retval == EX_OSERR){
free(filename.s);
return retval;
@ -119,18 +120,28 @@ walk(char *dirname, char *argv0){
int main(int argc, char *argv[]){
char *argv0;
int c;
int levels;
int retval;
argv0 = argv[0] == NULL ? program_name : argv[0];
levels = -1; /* no limit */
terminator = asv_terminator;
if(argc > 0){
while((c = getopt(argc, argv, "0d:l:n")) != -1)
switch(c){
case '0': terminator = nul_terminator; break;
case 'd': terminator = optarg; break;
case 'l': /* TODO */ break;
case 'n': terminator = newl_terminator; break;
case 'd': terminator = optarg; break;
case 'l': {
long l;
if((l = strtol(optarg, &optarg, 0) - 1) >= 1 && l <= INT_MAX
&& *optarg != '\0'){
levels = l;
break;
}
}
default:
fprintf(stderr,
"Usage: %s (-0n)"
@ -147,7 +158,7 @@ int main(int argc, char *argv[]){
argv = dot;
while(*argv != NULL)
if((retval = walk(*(argv++), argv0)) != EX_OK)
if((retval = walk(*(argv++), levels, argv0)) != EX_OK)
switch(retval){
case EX_OSERR: perror(argv0); return retval;
case EX_NOINPUT: