8 #define O_INTERACTIVE 1
10 #define O_NOOVERRIDE (1<<2)
11 #define O_REPLACEALL (1<<3)
12 #define O_REPLACELAST (1<<4)
13 #define O_VERBOSE (1<<5)
18 warn(const char *fmt, ...)
20 char *w = strerror(errno);
21 fputs("rene: ", stderr);
25 vfprintf(stderr, fmt, argp);
28 fprintf(stderr, ": %s\n", w);
32 err(int eval, const char *fmt, ...)
34 char *w = strerror(errno);
35 fputs("rene: ", stderr);
39 vfprintf(stderr, fmt, argp);
42 fprintf(stderr, ": %s\n", w);
47 strrep(char *from, char *to, char *s, char **new)
49 char *p = strrchr(s, '/');
58 int fromlen = strlen(from);
60 if (opts & O_REPLACELAST || opts & O_REPLACEALL) {
63 temp = strstr(temp+fromlen, from);
64 count = (opts & O_REPLACEALL && temp) ? count + 1 :
66 p = (opts & O_REPLACELAST && temp) ? temp : p;
71 malloc(strlen(s) + strlen(to)*count - fromlen*count + 1))) {
79 for (; s != p; *newp++ = *s++)
81 for (; *top != '\0'; *newp++ = *top++)
84 p = opts & O_REPLACEALL ? strstr(s, from) : strchr(s, '\0');
85 p = p ? p : strchr(s, '\0');
86 for (; s != p; *newp++ = *s++)
88 p = (*p == '\0') ? NULL : p;
96 ask(char *from, char *to)
98 fprintf(stderr, "replace %s with %s? ", from, to);
99 return getchar() == 'y';
103 ren(char *from, char *to, char *f)
107 if (!strrep(from, to, f, &new))
109 if ((opts & O_NOOVERRIDE || opts & O_INTERACTIVE) &&
110 access(new, F_OK) == 0)
111 yes = opts & O_NOOVERRIDE ? 0 : ask(f, new);
112 if (yes && !(opts & O_NOACT) && !(yes += rename(f, new)))
114 if (opts & O_VERBOSE && yes)
115 printf("%s -> %s\n", f, new);
121 fputs("usage: rene [-ailnov] from to file ...\n", stderr);
126 main(int argc, char *argv[])
131 if (pledge("stdio cpath rpath", NULL) == -1)
136 while ((c = getopt(argc, argv, "ailnov")) != -1) {
139 opts |= O_REPLACEALL;
142 opts |= O_INTERACTIVE;
145 opts |= O_REPLACELAST;
151 opts |= O_NOOVERRIDE;
167 for (int i = 2; i < argc; i++)
168 ren(from, to, argv[i]);