IT'S ALIVE
This commit is contained in:
parent
149f255428
commit
a086e27e75
19
walk/walk.c
19
walk/walk.c
@ -35,7 +35,7 @@
|
|||||||
|
|
||||||
#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> /* free(3), realloc(3) */
|
||||||
#include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
|
#include <string.h> /* stpcpy(3), strcmp(3), strerror(3), strlen(3) */
|
||||||
#if !defined EX_OK || !defined EX_INVALID || !defined EX_OSERR \
|
#if !defined EX_OK || !defined EX_INVALID || !defined EX_OSERR \
|
||||||
|| !defined EX_USAGE
|
|| !defined EX_USAGE
|
||||||
@ -45,6 +45,7 @@
|
|||||||
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
|
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR,
|
||||||
* struct dirent */
|
* struct dirent */
|
||||||
|
|
||||||
|
static char *program_name = "<no argv[0]>";
|
||||||
static char *dot[] = {".", NULL}; /* default (argc<2) */
|
static char *dot[] = {".", NULL}; /* default (argc<2) */
|
||||||
|
|
||||||
static char *nul_terminator = "\0";
|
static char *nul_terminator = "\0";
|
||||||
@ -67,7 +68,7 @@ static int
|
|||||||
walk(char *dirname, char *argv0){
|
walk(char *dirname, char *argv0){
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
struct dirent *f;
|
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;
|
size_t l;
|
||||||
char *np;
|
char *np;
|
||||||
int retval;
|
int retval;
|
||||||
@ -83,8 +84,10 @@ walk(char *dirname, char *argv0){
|
|||||||
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) > 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);
|
||||||
return EX_OSERR;
|
return EX_OSERR;
|
||||||
|
}
|
||||||
filename.a = l;
|
filename.a = l;
|
||||||
filename.s = np;
|
filename.s = np;
|
||||||
}
|
}
|
||||||
@ -94,13 +97,14 @@ walk(char *dirname, char *argv0){
|
|||||||
* directory. */
|
* directory. */
|
||||||
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
|
if(f->d_type == DT_DIR || f->d_type == DT_UNKNOWN){
|
||||||
if((retval = walk(filename.s, argv0)) != EX_OK){
|
if((retval = walk(filename.s, argv0)) != EX_OK){
|
||||||
if(retval == EX_OSERR)
|
if(retval == EX_OSERR){
|
||||||
|
free(filename.s);
|
||||||
return retval;
|
return retval;
|
||||||
|
}
|
||||||
else if(retval != EX_NOINPUT)
|
else if(retval != EX_NOINPUT)
|
||||||
fprintf(stderr, "%s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
argv0, filename.s, strerror(errno));
|
argv0, filename.s, strerror(errno));
|
||||||
}else
|
}
|
||||||
fnprint(filename.s);
|
|
||||||
}else
|
}else
|
||||||
fnprint(filename.s);
|
fnprint(filename.s);
|
||||||
}
|
}
|
||||||
@ -108,6 +112,7 @@ walk(char *dirname, char *argv0){
|
|||||||
if(errno != 0 || closedir(dir) != 0)
|
if(errno != 0 || closedir(dir) != 0)
|
||||||
fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno));
|
fprintf(stderr, "%s: %s: %s\n", argv0, dirname, strerror(errno));
|
||||||
|
|
||||||
|
free(filename.s);
|
||||||
return EX_OK;
|
return EX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,7 +121,7 @@ int main(int argc, char *argv[]){
|
|||||||
int c;
|
int c;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
argv0 = argv[0];
|
argv0 = argv[0] == NULL ? program_name : argv[0];
|
||||||
terminator = asv_terminator;
|
terminator = asv_terminator;
|
||||||
|
|
||||||
if(argc > 0){
|
if(argc > 0){
|
||||||
|
Loading…
Reference in New Issue
Block a user