sbase/rm.c
Roberto E. Vargas Caballero 0df8cdc12d rm: Add -i and cleanup rm()
POSIX mandates that if the input of rm is a tty and it does not have
write rights over a file/dir then it should ask for confirmation, in
the same way that is done with the -i flag. To accomodate both things
the code has been rearrenged a bit to have only one case instead of
having two. Also, this rework adds the error message when a directory
is removed without a -r flag.
2025-04-24 09:27:59 +02:00

45 lines
667 B
C

/* See LICENSE file for copyright and license details. */
#include <fcntl.h>
#include "fs.h"
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-f] [-iRr] file ...\n", argv0);
}
int
main(int argc, char *argv[])
{
struct recursor r = { .fn = rm, .maxdepth = 1, .follow = 'P' };
ARGBEGIN {
case 'f':
r.flags |= SILENT | IGNORE;
break;
case 'i':
r.flags |= CONFIRM;
break;
case 'R':
case 'r':
r.maxdepth = 0;
break;
default:
usage();
} ARGEND
if (!argc) {
if (!(r.flags & IGNORE))
usage();
else
return 0;
}
for (; *argv; argc--, argv++)
recurse(AT_FDCWD, *argv, NULL, &r);
return rm_status || recurse_status;
}