9 #define O_INTERACTIVE 1
10 #define O_NOACT (1<<1)
11 #define O_NOOVERRIDE (1<<2)
12 #define O_REPLACEALL (1<<3)
13 #define O_REPLACELAST (1<<4)
14 #define O_VERBOSE (1<<5)
16 #define MAX_FILENAME 4096
19 char new[MAX_FILENAME], *newe = new + MAX_FILENAME-1;
22 warn(const char *fmt, ...)
24 char *w = strerror(errno);
25 fputs("rene: ", stderr);
29 vfprintf(stderr, fmt, argp);
32 fprintf(stderr, ": %s\n", w);
36 err(int eval, const char *fmt, ...)
38 char *w = strerror(errno);
39 fputs("rene: ", stderr);
43 vfprintf(stderr, fmt, argp);
46 fprintf(stderr, ": %s\n", w);
51 errx(int eval, const char *fmt, ...)
53 fputs("rene: ", stderr);
57 vfprintf(stderr, fmt, argp);
65 ren(char *from, char *to, char *f)
67 int y = !(opts & O_NOOVERRIDE);
69 char *p = strrchr(f, '/');
75 if (!(p = strstr(p, from)))
77 int fromlen = strlen(from);
78 if (opts & O_REPLACELAST)
79 for(char *x; (x = strstr(p+fromlen, from)); p = x)
86 for (; fp != p; *newp++ = *fp++)
89 errx(1, "%s: final file name is too long", f);
92 for (top = to; *top != '\0'; *newp++ = *top++)
96 if (!(opts & O_REPLACEALL) || !(p = strstr(fp, from)))
101 if (opts & O_INTERACTIVE && access(new, F_OK) == 0) {
102 fprintf(stderr, "replace %s with %s? ", from, to);
103 y = getchar() == 'y';
105 if (y && !(opts & O_NOACT) && !(y += rename(f, new))) {
109 if (opts & O_VERBOSE && y)
110 printf("%s -> %s\n", f, new);
117 fputs("usage: rene [-ailnov] from to file ...\n", stderr);
122 main(int argc, char *argv[])
126 if (pledge("stdio cpath rpath", NULL) == -1)
129 while ((c = getopt(argc, argv, "ailnov")) != -1) {
132 opts |= O_REPLACEALL;
135 opts |= O_INTERACTIVE;
138 opts |= O_REPLACELAST;
144 opts |= O_NOOVERRIDE;
156 if (argc < 3 || !argv[0] || !argv[1] || !argv[0][0])
158 for (int i = 2; i < argc; i++)
159 if (!ren(argv[0], argv[1], argv[i]))