stylistic changes
This commit is contained in:
parent
b91ebf46c1
commit
ef590f67a9
114
walk/walk.c
114
walk/walk.c
@ -12,57 +12,42 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <dirent.h> /* closedir(3), opendir(3), readdir(3), DIR */
|
||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
#include <stdbool.h>
|
#include <stdio.h> /* fprintf(3), perror(3), stderr, stdout */
|
||||||
#include <stdio.h> /* fprintf(3), stderr, stdout */
|
#include <stdlib.h> /* realloc(3) */
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h> /* strerror(3) */
|
#include <string.h> /* strerror(3) */
|
||||||
#include <sysexits.h> /* EX_USAGE */
|
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
|
||||||
|
|
||||||
#include <dirent.h>
|
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
static char *dot = "."; /* current directory */
|
static char *dot[] = {".", NULL}; /* default (argc<2) */
|
||||||
|
|
||||||
static char *nul_terminator = "\0";
|
static char *nul_terminator = "\0";
|
||||||
static char *newl_terminator = "\n";
|
static char *newl_terminator = "\n";
|
||||||
static char *asv_terminator = "\037"; /* ASCII UNIT_SEPARATOR */
|
static char *asv_terminator = "\037"; /* ASCII UNIT_SEPARATOR */
|
||||||
|
static char *terminator;
|
||||||
|
|
||||||
|
static int
|
||||||
|
fnprint(char *fn){
|
||||||
|
if(terminator == nul_terminator)
|
||||||
|
return fprintf(stdout, "%s%c", fn, nul_terminator[0]);
|
||||||
|
else
|
||||||
|
return fprintf(stdout, "%s%s", fn, terminator);
|
||||||
|
}
|
||||||
|
|
||||||
static char *argv0;
|
static char *argv0;
|
||||||
|
|
||||||
// Like realloc(3), but exits the binary in an out-of-memory situation.
|
|
||||||
static void *xrealloc(void *ptr, const size_t size)
|
|
||||||
{
|
|
||||||
if (!(ptr = realloc(ptr, size))) {
|
|
||||||
perror("realloc");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void strcpy3(char *dest, const char *s1, const char *s2, const char *s3)
|
|
||||||
{
|
|
||||||
stpcpy(stpcpy(stpcpy(dest, s1), s2), s3);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void put_filename(const char *filename, bool null_terminate)
|
|
||||||
{
|
|
||||||
if (null_terminate) {
|
|
||||||
fputs(filename, stdout);
|
|
||||||
fputc(0, stdout);
|
|
||||||
} else {
|
|
||||||
puts(filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Walks the directory named dirname, printing the names of all files it
|
// Walks the directory named dirname, printing the names of all files it
|
||||||
// contains (but not the name of the directory itself). Returns 2 if dirname is
|
// contains (but not the name of the directory itself). Returns 2 if dirname is
|
||||||
// not a directory and 1 if another error occurs.
|
// not a directory and 1 if another error occurs.
|
||||||
static int
|
static int
|
||||||
walk(char *dirname, bool null_terminate){
|
walk(char *dirname){
|
||||||
DIR dir;
|
DIR dir;
|
||||||
struct dirent *f;
|
struct dirent *f;
|
||||||
char *filename;
|
static struct { size_t a; size_t s; char *p; } filename = {0, 0, NULL};
|
||||||
|
size_t l;
|
||||||
|
char *np;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if((dir = opendir(dirname)) == NULL){
|
if((dir = opendir(dirname)) == NULL){
|
||||||
@ -76,41 +61,40 @@ walk(char *dirname, bool null_terminate){
|
|||||||
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;
|
||||||
filename = xrealloc(
|
if((l = strlen(dirname) + 1 + strlen(f->d_name) + 1)
|
||||||
filename, strlen(dirname) + 1 + strlen(f->d_name) + 1);
|
> filename.a){
|
||||||
strcpy3(filename, dirname, "/", f->d_name);
|
if((np = realloc(filename.p, l)) == NULL){
|
||||||
// TODO(bbaren@google.com): Emulate Plan 9's cleanname(3).
|
perror(argv0);
|
||||||
put_filename(filename, null_terminate);
|
return EX_OSERR;
|
||||||
// Walk the file if we can successfully open it as a directory.
|
}else{
|
||||||
// Don't worry about it if it's not one (walk(filename) == 2).
|
filename.a = l;
|
||||||
|
filename.p = np; /* would you look at that */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stpcpy(stpcpy(stpcpy(filename.p, dirname), "/"), f->d_name);
|
||||||
|
/* TODO(bbaren@google.com): Emulate Plan 9's cleanname(3). */
|
||||||
|
fnprint(filename.p);
|
||||||
|
/* Walk the file if we can successfully open it as a directory.
|
||||||
|
* Don't worry about it if it's not one (walk(filename) == 2). */
|
||||||
if ((f->d_type == DT_DIR || f->d_type == DT_UNKNOWN)
|
if ((f->d_type == DT_DIR || f->d_type == DT_UNKNOWN)
|
||||||
&& walk(filename, null_terminate) == 1)
|
&& walk(filename.p) == 1)
|
||||||
retval = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
if (errno) { // from readdir
|
if(errno != 0 || closedir(dir) != NULL){
|
||||||
perror(dirname);
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
|
argv0, dirname, strerror(errno));
|
||||||
retval = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
free(filename);
|
|
||||||
if (closedir(dir)) {
|
|
||||||
perror(dirname);
|
|
||||||
retval = 1;
|
|
||||||
}
|
|
||||||
return retval;
|
|
||||||
}
|
|
||||||
|
|
||||||
int usage(char *argv0){
|
return retval;
|
||||||
fprintf(stderr, "Usage: %s (-0n) (-d [delimiter]) (-l [max levels])"
|
|
||||||
" (directories...)\n", argv0);
|
|
||||||
return EX_USAGE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int c;
|
int c;
|
||||||
char *terminator;
|
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
argv0 = argv[0];
|
argv0 = argv[0];
|
||||||
|
|
||||||
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){
|
||||||
@ -118,20 +102,24 @@ int main(int argc, char *argv[]){
|
|||||||
case 'd': terminator = optarg; break;
|
case 'd': terminator = optarg; break;
|
||||||
case 'l': /* TODO */ break;
|
case 'l': /* TODO */ break;
|
||||||
case 'n': terminator = newl_terminator; break;
|
case 'n': terminator = newl_terminator; break;
|
||||||
default: return usage(argv[0]);
|
default:
|
||||||
|
fprintf(stderr,
|
||||||
|
"Usage: %s (-0n)"
|
||||||
|
" (-d [delimiter]) (-l [max levels])"
|
||||||
|
" (directories...)\n", argv[0]);
|
||||||
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
argv += optind;
|
argv += optind;
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = 0;
|
retval = 0;
|
||||||
|
|
||||||
if(*argv == NULL){
|
if(*argv == NULL)
|
||||||
put_filename(dot, terminator == nul_terminator);
|
argv = dot;
|
||||||
retval |= walk(dot, terminator == nul_terminator);
|
|
||||||
}else
|
while(*argv != NULL){
|
||||||
for(argv += optind; *argv != NULL; ++argv){
|
fnprint(*argv);
|
||||||
put_filename(*argv, terminator == nul_terminator);
|
retval |= walk(*(argv++));
|
||||||
retval |= walk(*argv, terminator == nul_terminator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
|
Loading…
Reference in New Issue
Block a user