1
0

IT'S ALIVE

This commit is contained in:
dtb 2024-02-18 08:46:54 -07:00
parent 149f255428
commit a086e27e75

View File

@ -35,7 +35,7 @@
#include <errno.h> /* errno */
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
#include <stdlib.h> /* realloc(3) */
#include <stdlib.h> /* free(3), realloc(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
@ -45,6 +45,7 @@
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
* struct dirent */
static char *program_name = "<no argv[0]>";
static char *dot[] = {".", NULL}; /* default (argc<2) */
static char *nul_terminator = "\0";
@ -67,7 +68,7 @@ static int
walk(char *dirname, char *argv0){
DIR *dir;
struct dirent *f;
static struct { size_t a; char *s; } filename = { 0, NULL };
struct { size_t a; char *s; } filename = { 0, NULL };
size_t l;
char *np;
int retval;
@ -83,8 +84,10 @@ walk(char *dirname, char *argv0){
if(strcmp(f->d_name, ".") == 0 || strcmp(f->d_name, "..") == 0)
continue;
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);
return EX_OSERR;
}
filename.a = l;
filename.s = np;
}
@ -94,13 +97,14 @@ walk(char *dirname, char *argv0){
* directory. */
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
if((retval = walk(filename.s, argv0)) != EX_OK){
if(retval == EX_OSERR)
if(retval == EX_OSERR){
free(filename.s);
return retval;
}
else if(retval != EX_NOINPUT)
fprintf(stderr, "%s: %s: %s\n",
argv0, filename.s, strerror(errno));
}else
fnprint(filename.s);
}
}else
fnprint(filename.s);
}
@ -108,6 +112,7 @@ walk(char *dirname, char *argv0){
if(errno != 0 || closedir(dir) != 0)
fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno));
free(filename.s);
return EX_OK;
}
@ -116,7 +121,7 @@ int main(int argc, char *argv[]){
int c;
int retval;
argv0 = argv[0];
argv0 = argv[0] == NULL ? program_name : argv[0];
terminator = asv_terminator;
if(argc > 0){