summaryrefslogtreecommitdiff
path: root/rm.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-02 19:30:22 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-02 19:30:22 -0400
commit45005cdc20fb445aa8d76ac774efd2dc72a8191d (patch)
tree2edd0a4ac36a3e023c99a55ee832c672804a04ef /rm.c
parent17f02f621319594024aedec36c937f2238524bc1 (diff)
style
Diffstat (limited to 'rm.c')
-rw-r--r--rm.c172
1 files changed, 85 insertions, 87 deletions
diff --git a/rm.c b/rm.c
index b68147a..e1412d7 100644
--- a/rm.c
+++ b/rm.c
@@ -17,6 +17,7 @@
*
*/
+#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -28,107 +29,104 @@
#include <ftw.h>
const char *rm_desc = "remove directory entries";
-const char *rm_inv = "rm [-fiRr] file...";
+const char *rm_inv = "rm [-fiRr] file...";
static int force = 0;
static int interactive = 0;
static int recursive = 0;
-static int rm (const char*);
+static int rm(const char *);
-static int rm_prompt (const char *p)
+static int rm_prompt(const char *p)
{
- char *buf = NULL;
- size_t len = 0;
-
- if (errno == ENOENT) // FIXME: this is probably wrong
- return 1;
-
- fprintf (stderr, "remove %s? ", p);
- getline (&buf, &len, stdin);
- if (!strcasecmp("yes\n", buf) || !(strcasecmp("y\n", buf))) {
- free (buf);
- return 1;
- }
- return 0;
+ char *buf = NULL;
+ size_t len = 0;
+
+ if (errno == ENOENT) // FIXME: this is probably wrong
+ return 1;
+
+ fprintf(stderr, "remove %s? ", p);
+ getline(&buf, &len, stdin);
+ if (!strcasecmp("yes\n", buf) || !(strcasecmp("y\n", buf))) {
+ free(buf);
+ return 1;
+ }
+ return 0;
}
-int nftw_rm (const char *p, const struct stat *sb, int typeflag, struct FTW *f)
+int nftw_rm(const char *p, const struct stat *sb, int typeflag, struct FTW *f)
{
- rm (p);
+ return rm(p);
}
-static int rm (const char *p)
+static int rm(const char *p)
{
- struct stat st;
- if (lstat (p, &st) != 0) {
- if (!force)
- perror (p);
- return 1;
- }
-
- if (S_ISDIR (st.st_mode)) {
- if (!recursive) {
- fprintf (stderr, "%s: %s\n", p, strerror(EISDIR));
- return 1;
- }
- if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
- || interactive))
- {
- if (rm_prompt (p) == 0)
- return 0;
- }
- if (rmdir (p) != 0)
- perror (p);
- return 0;
- }
-
- if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
- || interactive))
- {
- if (rm_prompt (p) == 0)
- return 0;
- }
-
- if (unlink (p) != 0)
- perror (p);
- return 0;
+ struct stat st;
+ if (lstat(p, &st) != 0) {
+ if (!force)
+ perror(p);
+ return 1;
+ }
+
+ if (S_ISDIR(st.st_mode)) {
+ if (!recursive) {
+ fprintf(stderr, "%s: %s\n", p, strerror(EISDIR));
+ return 1;
+ }
+ if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
+ || interactive)) {
+ if (rm_prompt(p) == 0)
+ return 0;
+ }
+ if (rmdir(p) != 0)
+ perror(p);
+ return 0;
+ }
+
+ if (!force && ((access(p, W_OK) != 0 && isatty(fileno(stdin)))
+ || interactive)) {
+ if (rm_prompt(p) == 0)
+ return 0;
+ }
+
+ if (unlink(p) != 0)
+ perror(p);
+ return 0;
}
-int
-main(int argc, char **argv)
+int main(int argc, char **argv)
{
- int c;
-
- while ((c = getopt (argc, argv, ":fiRr")) != -1) {
- switch (c) {
- case 'f':
- force = 1;
- interactive = 0;
- break;
- case 'i':
- force = 0;
- interactive = 1;
- break;
- case 'r':
- case 'R':
- recursive = 1;
- break;
- default:
- return 1;
- }
- }
-
- if (optind >= argc)
- return 1;
-
- while (optind < argc) {
- if (recursive)
- nftw (argv[optind], nftw_rm, 1024, FTW_DEPTH);
- else
- rm (argv[optind]);
- optind++;
- }
-
- return 0;
+ int c;
+
+ while ((c = getopt(argc, argv, ":fiRr")) != -1) {
+ switch (c) {
+ case 'f':
+ force = 1;
+ interactive = 0;
+ break;
+ case 'i':
+ force = 0;
+ interactive = 1;
+ break;
+ case 'r':
+ case 'R':
+ recursive = 1;
+ break;
+ default:
+ return 1;
+ }
+ }
+
+ if (optind >= argc)
+ return 1;
+
+ while (optind < argc) {
+ if (recursive)
+ nftw(argv[optind], nftw_rm, 1024, FTW_DEPTH);
+ else
+ rm(argv[optind]);
+ optind++;
+ }
+
+ return 0;
}