Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- int count(char *filepath, char option)
- {
- int fd;
- if ((fd = open(filepath, O_RDONLY)) == -1)
- {
- fprintf(stderr, "Error while opening '%s' file.\n", filepath);
- exit(EXIT_FAILURE + 1);
- }
- char block[30];
- int i, r, count = 0;
- while ((r = read(fd, block, sizeof(block))))
- {
- switch (option)
- {
- case 'c':
- {
- for (i = 0; i < r; i++)
- if ((block[i] >= 'a' && block[i] <= 'z') || (block[i] >= 'A' && block[i] <= 'Z'))
- count ++;
- break;
- }
- case 'l':
- {
- for (i = 0; i < r; i++)
- if (block[i] == '\n')
- count ++;
- break;
- }
- default:
- {
- fprintf(stderr, "Invalid option.\n");
- exit(EXIT_FAILURE + 2);
- }
- }
- }
- if (close(fd) == -1)
- {
- fprintf(stderr, "Error while closing '%s' file.\n", filepath);
- exit(EXIT_FAILURE + 3);
- }
- return count;
- }
- void traverse(char *dirpath, char option, int fd)
- {
- DIR *dir;
- if ((dir = opendir(dirpath)) == NULL)
- {
- fprintf(stderr, "Error while opening the '%s' directory.\n", dirpath);
- exit(EXIT_FAILURE + 4);
- }
- struct dirent *d;
- struct stat st;
- char path[256];
- while ((d = readdir(dir)) != NULL)
- {
- if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
- continue;
- sprintf(path, "%s/%s", dirpath, d->d_name);
- if (lstat(path, &st) < 0)
- {
- fprintf(stderr, "Error while getting file '%s' stats.\n", path);
- exit(EXIT_FAILURE + 7);
- }
- if (S_ISREG(st.st_mode))
- {
- char buff[280];
- sprintf(buff, "F <%s> <%d>\n", path, count(path, option));
- fprintf(stdout, "%s", buff);
- write(fd, buff, strlen(buff));
- }
- if (S_ISLNK(st.st_mode))
- {
- char buff[512], link_path[256], new_path[256];
- if (readlink(path, link_path, sizeof(link_path)) == -1)
- {
- fprintf(stderr, "Error while getting links' path.\n");
- exit(EXIT_FAILURE + 8);
- }
- sprintf(new_path, "%s/%s", dirpath, link_path);
- sprintf(buff, "L <%s> <%d>\n", path, count(new_path, option));
- fprintf(stdout, "%s", buff);
- write(fd, buff, strlen(buff));
- }
- if (S_ISDIR(st.st_mode))
- traverse(path, option, fd);
- }
- if (closedir(dir) == -1)
- {
- fprintf(stderr, "Error while closing the '%s' directory.\n", dirpath);
- exit(EXIT_FAILURE + 9);
- }
- }
- int main(int argc, char **argv)
- {
- if (argc != 3)
- {
- fprintf(stderr, "Sintaxa: %s <cale_dir> <optiune>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- if (open("log.txt", O_RDONLY) != -1)
- {
- fprintf(stderr, "File 'log.txt' does already exist.\n");
- exit(EXIT_FAILURE + 5);
- }
- int fd;
- if ((fd = open("log.txt", O_WRONLY | O_CREAT, 0666)) == -1)
- {
- fprintf(stderr, "Error while creating 'log.txt' file.\n");
- exit(EXIT_FAILURE + 6);
- }
- traverse(argv[1], argv[2][1], fd);
- if (close(fd) == -1)
- {
- fprintf(stderr, "Error while closing the 'log.txt' file.\n");
- exit(EXIT_FAILURE + 3);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement