1 66390331 2022-09-30 src #include <errno.h>
2 b79428b4 2022-07-13 src #include <stdarg.h>
3 c1ec7947 2022-10-20 src #include <stdint.h>
4 b79428b4 2022-07-13 src #include <stdio.h>
5 b79428b4 2022-07-13 src #include <stdlib.h>
6 b79428b4 2022-07-13 src #include <string.h>
7 b79428b4 2022-07-13 src #include <unistd.h>
9 66390331 2022-09-30 src #define O_INTERACTIVE 1
10 66390331 2022-09-30 src #define O_NOACT (1<<1)
11 66390331 2022-09-30 src #define O_NOOVERRIDE (1<<2)
12 66390331 2022-09-30 src #define O_REPLACEALL (1<<3)
13 66390331 2022-09-30 src #define O_REPLACELAST (1<<4)
14 66390331 2022-09-30 src #define O_VERBOSE (1<<5)
16 1111d9d2 2022-10-06 src #define MAX_FILENAME 4096
18 66390331 2022-09-30 src uint8_t opts;
19 1111d9d2 2022-10-06 src char new[MAX_FILENAME], *newe = new + MAX_FILENAME-1;
22 66390331 2022-09-30 src warn(const char *fmt, ...)
24 66390331 2022-09-30 src char *w = strerror(errno);
25 66390331 2022-09-30 src fputs("rene: ", stderr);
26 b79428b4 2022-07-13 src if (fmt != NULL) {
27 b79428b4 2022-07-13 src va_list argp;
28 b79428b4 2022-07-13 src va_start(argp, fmt);
29 b79428b4 2022-07-13 src vfprintf(stderr, fmt, argp);
30 b79428b4 2022-07-13 src va_end(argp);
32 66390331 2022-09-30 src fprintf(stderr, ": %s\n", w);
36 1e12e95d 2022-09-30 src err(int eval, const char *fmt, ...)
38 1e12e95d 2022-09-30 src char *w = strerror(errno);
39 1e12e95d 2022-09-30 src fputs("rene: ", stderr);
40 1e12e95d 2022-09-30 src if (fmt != NULL) {
41 1e12e95d 2022-09-30 src va_list argp;
42 1e12e95d 2022-09-30 src va_start(argp, fmt);
43 1e12e95d 2022-09-30 src vfprintf(stderr, fmt, argp);
44 1e12e95d 2022-09-30 src va_end(argp);
46 1e12e95d 2022-09-30 src fprintf(stderr, ": %s\n", w);
51 1111d9d2 2022-10-06 src errx(int eval, const char *fmt, ...)
53 1111d9d2 2022-10-06 src fputs("rene: ", stderr);
54 1111d9d2 2022-10-06 src if (fmt != NULL) {
55 1111d9d2 2022-10-06 src va_list argp;
56 1111d9d2 2022-10-06 src va_start(argp, fmt);
57 1111d9d2 2022-10-06 src vfprintf(stderr, fmt, argp);
58 1111d9d2 2022-10-06 src va_end(argp);
60 1111d9d2 2022-10-06 src fputc('\n', stderr);
65 1111d9d2 2022-10-06 src ren(char *from, char *to, char *f)
67 1111d9d2 2022-10-06 src int y = !(opts & O_NOOVERRIDE);
69 1111d9d2 2022-10-06 src char *p = strrchr(f, '/');
71 b79428b4 2022-07-13 src if (*++p == '\0')
75 1111d9d2 2022-10-06 src if (!(p = strstr(p, from)))
77 b79428b4 2022-07-13 src int fromlen = strlen(from);
78 1111d9d2 2022-10-06 src if (opts & O_REPLACELAST)
79 1111d9d2 2022-10-06 src for(char *x; (x = strstr(p+fromlen, from)); p = x)
82 1111d9d2 2022-10-06 src char *fp = f;
83 1111d9d2 2022-10-06 src char *newp = new;
84 b79428b4 2022-07-13 src char *top = to;
86 1111d9d2 2022-10-06 src for (; fp != p; *newp++ = *fp++)
87 1111d9d2 2022-10-06 src if (newp == newe)
89 1111d9d2 2022-10-06 src errx(1, "%s: final file name is too long", f);
92 1111d9d2 2022-10-06 src for (top = to; *top != '\0'; *newp++ = *top++)
93 1111d9d2 2022-10-06 src if (newp == newe)
94 1111d9d2 2022-10-06 src goto toolong;
95 1111d9d2 2022-10-06 src fp += fromlen;
96 1111d9d2 2022-10-06 src if (!(opts & O_REPLACEALL) || !(p = strstr(fp, from)))
97 1111d9d2 2022-10-06 src p = strchr(fp, '\0');
99 b79428b4 2022-07-13 src *newp = '\0';
101 1111d9d2 2022-10-06 src if (opts & O_INTERACTIVE && access(new, F_OK) == 0) {
102 1111d9d2 2022-10-06 src fprintf(stderr, "replace %s with %s? ", from, to);
103 1111d9d2 2022-10-06 src y = getchar() == 'y';
105 1111d9d2 2022-10-06 src if (y && !(opts & O_NOACT) && !(y += rename(f, new))) {
106 66390331 2022-09-30 src warn("rename");
109 1111d9d2 2022-10-06 src if (opts & O_VERBOSE && y)
110 b79428b4 2022-07-13 src printf("%s -> %s\n", f, new);
117 66390331 2022-09-30 src fputs("usage: rene [-ailnov] from to file ...\n", stderr);
118 66390331 2022-09-30 src exit(EXIT_FAILURE);
122 b79428b4 2022-07-13 src main(int argc, char *argv[])
124 1111d9d2 2022-10-06 src int c, ret = 0;
125 1e12e95d 2022-09-30 src #ifdef __OpenBSD__
126 1e12e95d 2022-09-30 src if (pledge("stdio cpath rpath", NULL) == -1)
127 1e12e95d 2022-09-30 src err(1, "pledge");
129 b79428b4 2022-07-13 src while ((c = getopt(argc, argv, "ailnov")) != -1) {
130 b79428b4 2022-07-13 src switch (c) {
132 66390331 2022-09-30 src opts |= O_REPLACEALL;
135 66390331 2022-09-30 src opts |= O_INTERACTIVE;
138 66390331 2022-09-30 src opts |= O_REPLACELAST;
141 66390331 2022-09-30 src opts |= O_NOACT;
144 66390331 2022-09-30 src opts |= O_NOOVERRIDE;
147 66390331 2022-09-30 src opts |= O_VERBOSE;
153 b79428b4 2022-07-13 src argc -= optind;
154 b79428b4 2022-07-13 src argv += optind;
156 1111d9d2 2022-10-06 src if (argc < 3 || !argv[0] || !argv[1] || !argv[0][0])
158 b79428b4 2022-07-13 src for (int i = 2; i < argc; i++)
159 1111d9d2 2022-10-06 src if (!ren(argv[0], argv[1], argv[i]))