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 <unistd.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #include <string.h>
- #include <ctype.h>
- int counter = 0, fd, digits = 0, low = 0, up = 0, n;
- int parse(char *ch, char *dirName) {
- char character = *ch;
- DIR *dir;
- if ((dir = opendir(dirName)) == NULL) {
- printf("Error at opendir().");
- exit(1);
- }
- struct dirent *d;
- struct stat st;
- char path[50], c;
- while ((d = readdir(dir))) {
- if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
- continue;
- snprintf(path, 50, "%s/%s", dirName, d->d_name);
- printf("%s\n", path);
- if (lstat(path, &st) == -1) {
- printf("Error at lstat().");
- exit(2);
- }
- if (S_ISDIR(st.st_mode)) {
- parse(ch, path);
- }
- if (S_ISREG(st.st_mode)) {
- if((fd = open(path, O_RDONLY)) < 0) {
- printf("Error opening input file\n");
- exit(3);
- }
- while((n = read(fd, &c, sizeof(char))) > 0) {
- if(c == character)
- counter ++;
- if(isdigit(c))
- digits ++;
- if(islower(c))
- low ++;
- if(isupper(c))
- up ++;
- }
- close(fd);
- }
- }
- printf("Digits: %d\nLower: %d\nUpper: %d\nCharacter: %d\n", digits, low, up, counter);
- closedir(dir);
- return 0;
- }
- int main (int argc, char *argv[]) {
- if (argc != 3) {
- printf("Usage: Program <%s> char directory", argv[0]);
- exit(0);
- }
- parse(argv[1], argv[2]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement