Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <ctype.h>
- #define BUFF_SIZE (1 << 12)
- void procRegFile(const char path[]) {
- int fd = open(path, O_RDONLY);
- if (fd <= 0) {
- perror("");
- exit(-1);
- }
- int cntLower = 0;
- char ch;
- while (read(fd, &ch, 1)) {
- if (islower(ch)) {
- cntLower++;
- }
- }
- if (close(fd)) {
- perror("");
- }
- exit(cntLower);
- }
- void procEntry(const char path[], int *kids) {
- struct stat statbuf;
- if (stat(path, &statbuf)) {
- perror("");
- exit(-1);
- }
- if (!S_ISREG(statbuf.st_mode)) {
- return;
- }
- pid_t kidPid = fork();
- if (kidPid < 0) {
- perror("");
- exit(-1);
- }
- if (kidPid == 0) {//kid
- procRegFile(path);
- }
- (*kids)++;
- }
- void iterDir(const char path[]) {
- DIR *dir = opendir(path);
- if (dir == NULL) {
- perror("");
- exit(-1);
- }
- int kids = 0, globalCnt = 0;
- for (struct dirent *dep; (dep = readdir(dir));) {
- const char *const dname = dep->d_name;
- if (!strcmp(dname, ".") || !strcmp(dname, "..")) {
- continue;
- }
- char absPath[BUFF_SIZE];
- sprintf(absPath, "%s/%s", path, dname);
- procEntry(absPath, &kids);
- }
- if (closedir(dir)) {
- perror("");
- }
- int wstat = 0;
- pid_t wr;
- while ((wr = wait(&wstat)))
- {
- if (wr == -1) {
- perror("");
- exit(-1);
- }
- if (WIFEXITED(wstat)) {
- int cnt = WEXITSTATUS(wstat);
- globalCnt += cnt;
- }
- kids--;
- if (kids == 0) {
- break;
- }
- }
- printf("%d\n", globalCnt);
- }
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Wrong usage!\n");
- exit(1);
- }
- iterDir(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement