2021-11-26 06:41:56 +03:00
|
|
|
/**
|
|
|
|
* @brief rm - Unlink files
|
|
|
|
*
|
|
|
|
* TODO: Support recursive, directory removal, etc.
|
|
|
|
*
|
|
|
|
* @copyright
|
2018-08-15 04:07:33 +03:00
|
|
|
* This file is part of ToaruOS and is released under the terms
|
2018-07-18 09:35:52 +03:00
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
|
|
|
* Copyright (C) 2013-2018 K. Lange
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2018-11-21 11:49:18 +03:00
|
|
|
#include <dirent.h>
|
2018-07-18 09:35:52 +03:00
|
|
|
|
2018-11-21 11:49:18 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
static int recursive = 0;
|
|
|
|
static int rm_thing(char * tmp);
|
|
|
|
|
|
|
|
static int rm_directory(char * source) {
|
|
|
|
DIR * dirp = opendir(source);
|
|
|
|
if (dirp == NULL) {
|
|
|
|
fprintf(stderr, "could not open %s\n", source);
|
2018-07-18 09:35:52 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:49:18 +03:00
|
|
|
struct dirent * ent = readdir(dirp);
|
|
|
|
while (ent != NULL) {
|
|
|
|
if (!strcmp(ent->d_name,".") || !strcmp(ent->d_name,"..")) {
|
|
|
|
ent = readdir(dirp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
char tmp[strlen(source)+strlen(ent->d_name)+2];
|
|
|
|
sprintf(tmp, "%s/%s", source, ent->d_name);
|
|
|
|
int status = rm_thing(tmp);
|
|
|
|
if (status) return status;
|
|
|
|
ent = readdir(dirp);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
|
2023-05-05 16:30:43 +03:00
|
|
|
int res = unlink(source);
|
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "rm: %s: %s\n", source, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-11-21 11:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rm_thing(char * tmp) {
|
|
|
|
struct stat statbuf;
|
|
|
|
lstat(tmp,&statbuf);
|
|
|
|
if (S_ISDIR(statbuf.st_mode)) {
|
|
|
|
if (!recursive) {
|
|
|
|
fprintf(stderr, "rm: %s: is a directory\n", tmp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return rm_directory(tmp);
|
|
|
|
} else {
|
2023-05-05 16:30:43 +03:00
|
|
|
int res = unlink(tmp);
|
|
|
|
if (res < 0) {
|
|
|
|
fprintf(stderr, "rm: %s: %s\n", tmp, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2018-11-21 11:49:18 +03:00
|
|
|
}
|
|
|
|
}
|
2018-07-18 09:35:52 +03:00
|
|
|
|
2018-11-21 11:49:18 +03:00
|
|
|
|
|
|
|
int main(int argc, char * argv[]) {
|
|
|
|
int opt;
|
2023-04-26 17:16:03 +03:00
|
|
|
while ((opt = getopt(argc, argv, "fr")) != -1) {
|
2018-11-21 11:49:18 +03:00
|
|
|
switch (opt) {
|
|
|
|
case 'r':
|
|
|
|
recursive = 1;
|
|
|
|
break;
|
2023-04-26 17:16:03 +03:00
|
|
|
case 'f':
|
|
|
|
/* ignore */
|
|
|
|
break;
|
2018-11-21 11:49:18 +03:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "rm: unrecognized option '%c'\n", opt);
|
|
|
|
break;
|
2018-07-18 09:35:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 11:49:18 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
for (int i = optind; i < argc; ++i) {
|
|
|
|
ret |= rm_thing(argv[i]);
|
|
|
|
}
|
|
|
|
|
2018-07-18 09:35:52 +03:00
|
|
|
return ret;
|
|
|
|
}
|