uuprog
diff rm.c @ 0:4f628556fa3e
uuprog initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Aug 2011 08:53:01 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rm.c Thu Aug 25 08:53:01 2011 +0300 1.3 @@ -0,0 +1,154 @@ 1.4 +/*! cc -o rm rm.c */ 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 +#include <string.h> 1.8 +#include <signal.h> 1.9 +#include <stdarg.h> 1.10 +#include <errno.h> 1.11 +#include <unistd.h> 1.12 +#include <dirent.h> 1.13 +#include <sys/stat.h> 1.14 + 1.15 +int rm(const char *path); 1.16 +int rec_rm(const char *path); 1.17 +char askuser(const char *fmt, ...); 1.18 +void sigfunc(int sig); 1.19 + 1.20 +int ask, stop_proc, force, rec, verbose; 1.21 + 1.22 +int main(int argc, char **argv) 1.23 +{ 1.24 + int i; 1.25 + 1.26 + signal(SIGINT, sigfunc); 1.27 + 1.28 + for(i=1; i<argc; i++) { 1.29 + if(!stop_proc && argv[i][0] == '-' && argv[i][2] == 0) { 1.30 + switch(argv[i][1]) { 1.31 + case 'f': 1.32 + force = 1; 1.33 + break; 1.34 + 1.35 + case '-': 1.36 + stop_proc = 1; 1.37 + break; 1.38 + 1.39 + case 'i': 1.40 + ask = 1; 1.41 + break; 1.42 + 1.43 + case 'r': 1.44 + case 'R': 1.45 + rec = 1; 1.46 + break; 1.47 + 1.48 + case 'v': 1.49 + verbose = 1; 1.50 + break; 1.51 + 1.52 + default: 1.53 + fprintf(stderr, "invalid option: %s\n", argv[i]); 1.54 + return 1; 1.55 + } 1.56 + } else { 1.57 + 1.58 + if(rm(argv[i]) == -1) { 1.59 + return 1; 1.60 + } 1.61 + } 1.62 + } 1.63 + 1.64 + return 0; 1.65 +} 1.66 + 1.67 +char askuser(const char *fmt, ...) 1.68 +{ 1.69 + int c, resp; 1.70 + va_list ap; 1.71 + 1.72 + va_start(ap, fmt); 1.73 + vprintf(fmt, ap); 1.74 + va_end(ap); 1.75 + 1.76 + resp = getchar(); 1.77 + do { 1.78 + c = getchar(); 1.79 + } while(c != '\n' && c != -1); 1.80 + 1.81 + return resp; 1.82 +} 1.83 + 1.84 +int rm(const char *path) 1.85 +{ 1.86 + if(ask) { 1.87 + char c = askuser("remove file `%s'? ", path); 1.88 + 1.89 + if(c != 'y' && c != 'Y') { 1.90 + return 0; 1.91 + } 1.92 + } 1.93 + 1.94 + if(unlink(path) == -1) { 1.95 + if((errno != EISDIR || rec_rm(path) == -1) && !force) { 1.96 + fprintf(stderr, "cannot remove \"%s\": %s\n", path, strerror(errno)); 1.97 + return -1; 1.98 + } 1.99 + } else { 1.100 + if(verbose) { 1.101 + printf("removed `%s'\n", path); 1.102 + } 1.103 + } 1.104 + return 0; 1.105 +} 1.106 + 1.107 +int rec_rm(const char *path) 1.108 +{ 1.109 + DIR *dir; 1.110 + struct dirent *dent; 1.111 + 1.112 + if(ask) { 1.113 + char c = askuser("descend into directory `%s'? ", path); 1.114 + 1.115 + if(c != 'y' && c != 'Y') { 1.116 + return 0; 1.117 + } 1.118 + } 1.119 + 1.120 + if(!(dir = opendir(path))) { 1.121 + return force ? 0 : -1; 1.122 + } 1.123 + 1.124 + while((dent = readdir(dir))) { 1.125 + struct stat st; 1.126 + 1.127 + if(stat(dent->d_name, &st) == -1) { 1.128 + if(force) continue; 1.129 + closedir(dir); 1.130 + return -1; 1.131 + } 1.132 + 1.133 + if(S_ISDIR(st.st_mode)) { 1.134 + if(rec_rm(dent->d_name) == -1) { 1.135 + closedir(dir); 1.136 + return -1; 1.137 + } 1.138 + } else { 1.139 + if(rm(dent->d_name) == -1) { 1.140 + closedir(dir); 1.141 + return -1; 1.142 + } 1.143 + } 1.144 + } 1.145 + 1.146 + if(verbose) { 1.147 + printf("removed directory: `%s'\n", path); 1.148 + } 1.149 + 1.150 + closedir(dir); 1.151 + return 0; 1.152 +} 1.153 + 1.154 +void sigfunc(int sig) 1.155 +{ 1.156 + exit(1); 1.157 +}