commit 66390331e836daad55adb33b41b8eecee0a4fd6b from: Alexander Arkhipov date: Fri Sep 30 21:01:32 2022 UTC remove BSD dependencies commit - b79428b4aec5e99d9c0f1dcc07fe677c2679cf4a commit + 66390331e836daad55adb33b41b8eecee0a4fd6b blob - 2ab6b9e65216392e7300fb04a6f2f76a3c4d1ba2 blob + ca6ddb5d3b7b497dbbb5dc1ca18cb8fa9fc19d81 --- rene.c +++ rene.c @@ -1,36 +1,31 @@ +#include #include #include #include #include #include -int interactive = 0; -int noact = 0; -int nooverride = 0; -int replaceall = 0; -int replacelast = 0; -int verbose = 0; +#define O_INTERACTIVE 1 +#define O_NOACT (1<<1) +#define O_NOOVERRIDE (1<<2) +#define O_REPLACEALL (1<<3) +#define O_REPLACELAST (1<<4) +#define O_VERBOSE (1<<5) -void -usage(void) -{ - fprintf(stderr, "usage: rene [-ailnov] from to file ...\n"); - exit(EXIT_FAILURE); -} +uint8_t opts; -char *rene; - void -err(const char *fmt, ...) +warn(const char *fmt, ...) { - fprintf(stderr, "%s: ", rene); + char *w = strerror(errno); + fputs("rene: ", stderr); if (fmt != NULL) { va_list argp; va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); } - fprintf(stderr, "\n"); + fprintf(stderr, ": %s\n", w); } int @@ -47,17 +42,19 @@ strrep(char *from, char *to, char *s, char **new) return 0; int fromlen = strlen(from); int count = 1; - if (replacelast || replaceall) { + if (opts & O_REPLACELAST || opts & O_REPLACEALL) { char *temp = p; while (temp) { temp = strstr(temp+fromlen, from); - count = (replaceall && temp) ? count + 1 : count; - p = (replacelast && temp) ? temp : p; + count = (opts & O_REPLACEALL && temp) ? count + 1 : + count; + p = (opts & O_REPLACELAST && temp) ? temp : p; } } - if (!(*new = malloc(strlen(s) + strlen(to)*count - fromlen*count + 1))) { - err("%s: couldn't allocate memory", s); + if (!(*new = + malloc(strlen(s) + strlen(to)*count - fromlen*count + 1))) { + warn("malloc"); return 0; } @@ -69,7 +66,7 @@ strrep(char *from, char *to, char *s, char **new) for (; *top != '\0'; *newp++ = *top++) ; s += fromlen; - p = replaceall ? strstr(s, from) : strchr(s, '\0'); + p = opts & O_REPLACEALL ? strstr(s, from) : strchr(s, '\0'); p = p ? p : strchr(s, '\0'); for (; s != p; *newp++ = *s++) ; @@ -94,41 +91,47 @@ ren(char *from, char *to, char *f) char *new = NULL; if (!strrep(from, to, f, &new)) return; - if ((nooverride || interactive) && access(new, F_OK) == 0) - yes = nooverride ? 0 : ask(f, new); - if (yes && !noact) - yes -= rename(f, new); - if (verbose && yes) + if ((opts & O_NOOVERRIDE || opts & O_INTERACTIVE) && + access(new, F_OK) == 0) + yes = opts & O_NOOVERRIDE ? 0 : ask(f, new); + if (yes && !(opts & O_NOACT) && !(yes += rename(f, new))) + warn("rename"); + if (opts & O_VERBOSE && yes) printf("%s -> %s\n", f, new); - free(new); } +void +usage(void) +{ + fputs("usage: rene [-ailnov] from to file ...\n", stderr); + exit(EXIT_FAILURE); +} + int main(int argc, char *argv[]) { char *from, *to; - rene = argv[0]; int c; while ((c = getopt(argc, argv, "ailnov")) != -1) { switch (c) { case 'a': - replaceall = 1; + opts |= O_REPLACEALL; break; case 'i': - interactive = 1; + opts |= O_INTERACTIVE; break; case 'l': - replacelast = 1; + opts |= O_REPLACELAST; break; case 'n': - noact = 1; + opts |= O_NOACT; break; case 'o': - nooverride = 1; + opts |= O_NOOVERRIDE; break; case 'v': - verbose = 1; + opts |= O_VERBOSE; break; default: usage();