Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <ftw.h>
- #include <stdlib.h>
- void usage()
- {
- printf("usage: rm [-r] file");
- }
- short is_directory(char *path)
- {
- struct stat path_stat;
- stat(path, &path_stat);
- return S_ISREG(path_stat.st_mode);
- }
- short remove_file(char *argv)
- {
- struct stat file = {0};
- if (stat(argv, &file) == -1) /* check if file exists */
- printf("%s doesn't exist\n", argv);
- else if (is_directory(argv) == 0) /* check if file is a directory */
- printf("%s is a directory\n", argv);
- else {
- remove(argv);
- }
- return 0;
- }
- static int rmFiles(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb)
- {
- if (remove(pathname) < 0) {
- perror("ERROR: remove");
- return -1;
- }
- return 0;
- }
- short removedir(char *rdir)
- {
- if (nftw(&rdir[0], rmFiles, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS) < 0) {
- perror("ERROR: ntfw");
- exit(1);
- }
- return 0;
- }
- int main(int argc, char *argv[])
- {
- short i;
- if (argc < 2)
- usage();
- for (i = 1; i < argc; i++) {
- if (strncmp(argv[1], "-r", 2) == 0) {
- removedir(argv[i]);
- }
- else
- remove_file(argv[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement