1
0

light hacking

This commit is contained in:
dtb 2024-02-16 09:53:26 -07:00
parent 25609ed548
commit b91ebf46c1

View File

@ -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){
return 2; fprintf(stdout, "%s: %s: %s\n",
argv0, dirname, strerror(errno));
return 1 + (errno == ENOTDIR);
} }
int r = 0;
char *filename = NULL; errno = 0;
for (const struct dirent *f = readdir2(dir); f; f = readdir2(dir)) { retval = 0;
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( 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;
switch (c) { argv0 = argv[0];
case 'h': if(argc > 0){
fputs(SHORT_USAGE, stdout); while((c = getopt(argc, argv, "0d:l:n")) != -1)
fputs(HELP, stdout); switch(c){
return 0; case '0': terminator = nul_terminator; break;
case '0': case 'd': terminator = optarg; break;
null_terminate = true; case 'l': /* TODO */ break;
break; case 'n': terminator = newl_terminator; break;
case '?': default: return usage(argv[0]);
fputs(ASK_FOR_HELP, stderr); }
return 1; argv += optind;
default: }
fputs("Internal error; please report.\n", stderr);
return 1; retval = 0;
if(*argv == NULL){
put_filename(dot, terminator == nul_terminator);
retval |= walk(dot, terminator == nul_terminator);
}else
for(argv += optind; *argv != NULL; ++argv){
put_filename(*argv, terminator == nul_terminator);
retval |= walk(*argv, terminator == nul_terminator);
} }
}
int r = 0; return retval;
const char *const *const dirs = argc == optind
? JUST_CURRENT_DIRECTORY
: (const char *const *)argv + optind;
for (int i = 0; dirs[i]; ++i) {
put_filename(dirs[i], null_terminate);
r |= walk(dirs[i], null_terminate);
}
return r;
} }