light hacking
This commit is contained in:
parent
25609ed548
commit
b91ebf46c1
127
walk/walk.c
127
walk/walk.c
@ -12,34 +12,23 @@
|
|||||||
// 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 <errno.h>
|
#include <errno.h> /* errno */
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h> /* fprintf(3), stderr, stdout */
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h> /* strerror(3) */
|
||||||
|
#include <sysexits.h> /* EX_USAGE */
|
||||||
|
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
|
|
||||||
static const char SHORT_USAGE[] = "Usage: walk [OPTION...] [DIRECTORY...]\n";
|
static char *dot = "."; /* current directory */
|
||||||
|
|
||||||
static const char HELP[] =
|
static char *nul_terminator = "\0";
|
||||||
"Recursively walk the specified directories (or current directory, if none is\n"
|
static char *newl_terminator = "\n";
|
||||||
"specified.\n\n"
|
static char *asv_terminator = "\037"; /* ASCII UNIT_SEPARATOR */
|
||||||
" -0, --null separate filenames by a null character\n"
|
|
||||||
" --help display this help and exit\n";
|
|
||||||
|
|
||||||
static const char ASK_FOR_HELP[] = "Try 'walk --help' for more information.\n";
|
static char *argv0;
|
||||||
|
|
||||||
static const char *const JUST_CURRENT_DIRECTORY[] = {".", NULL};
|
|
||||||
|
|
||||||
// Like readdir(3), but resets errno(3) before the call to make it easy to
|
|
||||||
// check.
|
|
||||||
static struct dirent *readdir2(DIR *const dirp)
|
|
||||||
{
|
|
||||||
errno = 0;
|
|
||||||
return readdir(dirp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Like realloc(3), but exits the binary in an out-of-memory situation.
|
// Like realloc(3), but exits the binary in an out-of-memory situation.
|
||||||
static void *xrealloc(void *ptr, const size_t size)
|
static void *xrealloc(void *ptr, const size_t size)
|
||||||
@ -69,19 +58,22 @@ static void put_filename(const char *filename, bool null_terminate)
|
|||||||
// 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 walk(const char dirname[], bool null_terminate)
|
static int
|
||||||
{
|
walk(char *dirname, bool null_terminate){
|
||||||
DIR *const dir = opendir(dirname);
|
DIR dir;
|
||||||
if (!dir) {
|
struct dirent *f;
|
||||||
if (errno != ENOTDIR) {
|
char *filename;
|
||||||
perror(dirname);
|
int retval;
|
||||||
return 1;
|
|
||||||
|
if((dir = opendir(dirname)) == NULL){
|
||||||
|
fprintf(stdout, "%s: %s: %s\n",
|
||||||
|
argv0, dirname, strerror(errno));
|
||||||
|
return 1 + (errno == ENOTDIR);
|
||||||
}
|
}
|
||||||
return 2;
|
|
||||||
}
|
errno = 0;
|
||||||
int r = 0;
|
retval = 0;
|
||||||
char *filename = NULL;
|
while((f = readdir(dir)) != NULL){
|
||||||
for (const struct dirent *f = readdir2(dir); f; f = readdir2(dir)) {
|
|
||||||
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(
|
filename = xrealloc(
|
||||||
@ -93,55 +85,54 @@ static int walk(const char dirname[], bool null_terminate)
|
|||||||
// Don't worry about it if it's not one (walk(filename) == 2).
|
// 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, null_terminate) == 1)
|
||||||
r = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
if (errno) { // from readdir
|
if (errno) { // from readdir
|
||||||
perror(dirname);
|
perror(dirname);
|
||||||
r = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
free(filename);
|
free(filename);
|
||||||
if (closedir(dir)) {
|
if (closedir(dir)) {
|
||||||
perror(dirname);
|
perror(dirname);
|
||||||
r = 1;
|
retval = 1;
|
||||||
}
|
}
|
||||||
return r;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(const int argc, char *const argv[])
|
int usage(char *argv0){
|
||||||
{
|
fprintf(stderr, "Usage: %s (-0n) (-d [delimiter]) (-l [max levels])"
|
||||||
static const struct option long_options[] = {
|
" (directories...)\n", argv0);
|
||||||
{"help", no_argument, NULL, 'h'},
|
return EX_USAGE;
|
||||||
{"null", no_argument, NULL, '0'},
|
}
|
||||||
{NULL, 0, NULL, 0},
|
|
||||||
};
|
int main(int argc, char *argv[]){
|
||||||
bool null_terminate = false;
|
int c;
|
||||||
while (true) {
|
char *terminator;
|
||||||
const int c = getopt_long(argc, argv, "0", long_options, NULL);
|
int retval;
|
||||||
if (c == -1) break;
|
|
||||||
|
argv0 = argv[0];
|
||||||
|
if(argc > 0){
|
||||||
|
while((c = getopt(argc, argv, "0d:l:n")) != -1)
|
||||||
switch(c){
|
switch(c){
|
||||||
case 'h':
|
case '0': terminator = nul_terminator; break;
|
||||||
fputs(SHORT_USAGE, stdout);
|
case 'd': terminator = optarg; break;
|
||||||
fputs(HELP, stdout);
|
case 'l': /* TODO */ break;
|
||||||
return 0;
|
case 'n': terminator = newl_terminator; break;
|
||||||
case '0':
|
default: return usage(argv[0]);
|
||||||
null_terminate = true;
|
|
||||||
break;
|
|
||||||
case '?':
|
|
||||||
fputs(ASK_FOR_HELP, stderr);
|
|
||||||
return 1;
|
|
||||||
default:
|
|
||||||
fputs("Internal error; please report.\n", stderr);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
argv += optind;
|
||||||
}
|
}
|
||||||
|
|
||||||
int r = 0;
|
retval = 0;
|
||||||
const char *const *const dirs = argc == optind
|
|
||||||
? JUST_CURRENT_DIRECTORY
|
if(*argv == NULL){
|
||||||
: (const char *const *)argv + optind;
|
put_filename(dot, terminator == nul_terminator);
|
||||||
for (int i = 0; dirs[i]; ++i) {
|
retval |= walk(dot, terminator == nul_terminator);
|
||||||
put_filename(dirs[i], null_terminate);
|
}else
|
||||||
r |= walk(dirs[i], null_terminate);
|
for(argv += optind; *argv != NULL; ++argv){
|
||||||
|
put_filename(*argv, terminator == nul_terminator);
|
||||||
|
retval |= walk(*argv, terminator == nul_terminator);
|
||||||
}
|
}
|
||||||
return r;
|
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user