sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

rm.c (916B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <sys/stat.h>
      3 
      4 #include <errno.h>
      5 #include <fcntl.h>
      6 #include <stdio.h>
      7 #include <unistd.h>
      8 
      9 #include "../fs.h"
     10 #include "../util.h"
     11 
     12 int rm_status = 0;
     13 
     14 void
     15 rm(int dirfd, const char *name, struct stat *st, void *data, struct recursor *r)
     16 {
     17 	int quiet, ask, write, flags, ignore;
     18 
     19 	ignore = r->flags & IGNORE;
     20 	quiet = r->flags & SILENT;
     21 	ask = r->flags & CONFIRM;
     22 	write = faccessat(dirfd, name, W_OK, 0) == 0;
     23 	flags = 0;
     24 
     25 	if (S_ISDIR(st->st_mode) && r->maxdepth) {
     26 		errno = EISDIR;
     27 		goto err;
     28 	}
     29 
     30 	if (!quiet && (!write && isatty(0) || ask)) {
     31 		if (!confirm("remove file '%s'", r->path));
     32 			return;
     33 	}
     34 
     35 	if (S_ISDIR(st->st_mode)) {
     36 		flags = AT_REMOVEDIR;
     37 		recurse(dirfd, name, NULL, r);
     38 	}
     39 
     40 	if (unlinkat(dirfd, name, flags) < 0)
     41 		goto err;
     42 	return;
     43 
     44 err:
     45 	if (!ignore) {
     46 		weprintf("cannot remove '%s':", r->path);
     47 		rm_status = 1;
     48 	}
     49 }