Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- int deleteDirectoryContent(char* path) {
- struct dirent *entry;
- DIR* dir = opendir(path);
- int count=0;
- if(dir) {
- while(entry = readdir(dir)) {
- if(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")) {
- struct stat st;
- char* entryPath = (char *) malloc((sizeof(entry->d_name)+sizeof(path))*sizeof(char));
- strcpy(entryPath, path);
- strcat(entryPath, "/");
- strcat(entryPath, entry->d_name);
- lstat(entryPath, &st);
- if(S_ISLNK(st.st_mode)) {
- int bufsize = st.st_size + 1;
- char* buf = malloc(bufsize);
- struct stat lst;
- readlink(entryPath, buf, bufsize);
- lstat(buf, &lst);
- if((lst.st_mode & S_IROTH) == 0) {
- count++;
- }
- } else if(S_ISDIR(st.st_mode)) {
- count += deleteDirectoryContent(entryPath);
- } else if(S_ISREG(st.st_mode)) {
- if((st.st_mode & S_IROTH) == 0) {
- remove(entryPath);
- printf("Deleted entry %s in %s\n", entry->d_name, path);
- }
- }
- }
- }
- }
- closedir(dir);
- return count;
- }
- int main(int argc, char *args[]) {
- if(argc != 2) {
- fprintf(stderr, "Usage: %s <directory>\n", args[0]);
- exit(1);
- }
- struct stat st;
- stat(args[1], &st);
- if(!S_ISDIR(st.st_mode)) {
- fprintf(stderr, "%s is not a directory\n", args[1]);
- exit(1);
- }
- int symbolicLinksCount = deleteDirectoryContent(args[1]);
- printf("Number of files pointed to by symbolic links, without READ permission while deleteing: %d\n", symbolicLinksCount);
- printf("Finished\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement