commit f52fd1bae7ca0f235e825f96dffbcc1ff188ef22
parent 05833ddf290a1c1158a0418e7e450f7dc42db417
Author: torter <koubbamohamedrayan@gmail.com>
Date: Wed, 25 Nov 2026 16:22:11 +0000
mkdir -p: a directory made meanwhile is fine; sed: the delimiter means nothing inside [...]; find -files0-from
Diffstat:
| M | rifle.c | | | 43 | ++++++++++++++++++++++++++++++++++++------- |
1 file changed, 36 insertions(+), 7 deletions(-)
diff --git a/rifle.c b/rifle.c
@@ -1190,8 +1190,9 @@ static int t_mkdir(int argc, char **argv)
if (*p != '/') continue;
*p = 0;
if (stat(path, &sb) < 0 && errno == ENOENT) {
- if (mkdir(path, (0777 & ~um) | S_IWUSR | S_IXUSR) < 0) { perr("cannot create directory '%s'", path); st = 1; goto next; }
- if (verbose) printf("%s: created directory '%s'\n", prog, path);
+ if (mkdir(path, (0777 & ~um) | S_IWUSR | S_IXUSR) < 0) {
+ if (errno != EEXIST || stat(path, &sb) < 0 || !S_ISDIR(sb.st_mode)) { perr("cannot create directory '%s'", path); st = 1; goto next; }
+ } else if (verbose) printf("%s: created directory '%s'\n", prog, path);
}
*p = '/';
while (p[1] == '/') p++;
@@ -1201,6 +1202,7 @@ static int t_mkdir(int argc, char **argv)
if (change) umask(0);
if (mkdir(path, change ? (mode & 0777) : 0777) < 0) {
if (change) umask(um);
+ if (parents && errno == EEXIST && stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) goto next; /* someone else made it meanwhile */
perr("cannot create directory '%s'", path); st = 1; goto next;
}
if (change) {
@@ -8232,6 +8234,20 @@ static char *s_slash(int delim, size_t *len, int regex)
}
if (c == '\n' && regex) break;
b[n++] = c;
+ if (c == '[' && regex) { /* the delimiter means nothing inside [...], as in s:[[:space:]]:: */
+ const char *q = sp;
+ if (q < se && *q == '^') q++;
+ if (q < se && *q == ']') q++;
+ while (q < se && *q != ']' && *q != '\n') {
+ if (*q == '[' && q + 1 < se && strchr(":.=", q[1])) {
+ const char *e = q + 2;
+ while (e + 1 < se && !(e[0] == q[1] && e[1] == ']')) e++;
+ if (e + 1 >= se) break;
+ q = e + 2;
+ } else q++;
+ }
+ if (q < se && *q == ']') { while (sp <= q) b[n++] = *sp++; }
+ }
}
sdie("unterminated `s' command");
}
@@ -9345,6 +9361,19 @@ static int t_find(int argc, char **argv)
first = fi;
while (fi < fn_ && (fa[fi][0] != '-' || !fa[fi][1]) && strcmp(fa[fi], "(") && strcmp(fa[fi], "!") && strcmp(fa[fi], ")") && strcmp(fa[fi], ",")) fi++;
pend = fi;
+ char **from = NULL; size_t nfrom = 0;
+ if (fi < fn_ && !strcmp(fa[fi], "-files0-from")) { /* the starting points come from a file, each ended by a NUL */
+ char *d; size_t dl, k;
+ int fd;
+ if (fi + 1 >= fn_) fdie("missing argument to `-files0-from'");
+ if (pend != first) fdie("extra operand `%s'", fa[first]);
+ if ((fd = open_input(fa[fi + 1])) < 0 || read_all(fd, &d, &dl) < 0) fdie("cannot read %s: %s", fa[fi + 1], strerror(errno));
+ if (fd) close(fd);
+ from = xmalloc((dl + 1) * sizeof *from);
+ for (k = 0; k < dl; k += strlen(d + k) + 1)
+ if (d[k]) from[nfrom++] = d + k;
+ fi += 2;
+ }
if (fi < fn_ && !strcmp(fa[fi], "--help")) { print_usage(); return 0; }
if (fi < fn_) {
f_root = f_or();
@@ -9353,12 +9382,12 @@ static int t_find(int argc, char **argv)
if (!f_has(f_root, -1)) f_root = fnew(F_AND, f_root, fnew(F_PRINT, NULL, NULL));
} else f_root = fnew(F_PRINT, NULL, NULL);
f_now = time(NULL);
- if (pend == first) { f_start = "."; f_walk(".", 0, 0); }
- for (i = first; i < pend && !f_quit; i++) {
+ if (!from) { from = fa + first; nfrom = pend - first; if (!nfrom) from = (char *[]){ "." }, nfrom = 1; }
+ for (i = 0; i < (int)nfrom && !f_quit; i++) {
struct stat st;
- f_start = fa[i];
- if (stat(fa[i], &st) < 0 && lstat(fa[i], &st) < 0) st.st_dev = 0;
- f_walk(fa[i], 0, st.st_dev);
+ f_start = from[i];
+ if (stat(from[i], &st) < 0 && lstat(from[i], &st) < 0) st.st_dev = 0;
+ f_walk(from[i], 0, st.st_dev);
}
for (i = 0; i < f_nplus; i++) f_flush(f_plus[i]);
return finish_stdout() ? 1 : f_status;