Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. int fd, fd2, digits = 0, low = 0, up = 0, n, occurences=0;
  12.  
  13. int process_file(char *path, char *ch, char *output){
  14.     //int fd, fd2;
  15.     char buf[256];
  16.     char character = *ch;
  17.     if((fd = open(path, O_RDONLY)) < 0) {
  18.         printf("Error opening input file\n");
  19.         exit(3);
  20.     }
  21.     if((fd2 = open(output, O_WRONLY | O_EXCL, S_IRWXU)) < 0) {
  22.         printf("Error at opening output file.");
  23.         exit(4);
  24.     }
  25.     while((n = read(fd, buf, 256)) > 0) {
  26.         for(int i=0; i<n; i++) {
  27.             if(isdigit(buf[i])) digits++;
  28.             else {
  29.                 if(buf[i]>='a' && buf[i]<='z') low++;
  30.                 else if(buf[i]>='A' && buf[i]<='Z') up++;
  31.             }
  32.             if(buf[i]==character) occurences++;
  33.         }
  34.     }
  35.     if(n<0) {
  36.         printf("Error reading input file\n");
  37.         exit(4);
  38.     }
  39.    
  40.     /*Write file*/
  41.     char str[256];
  42.     sprintf(str, "Number of digits: %d\nNumber of lowercase: %d\nNumber of uppercase: %d\nNumber occurences: %d\n", digits, low, up, occurences);
  43.     write(fd2,str, strlen(str));
  44.     close(fd);
  45.     close(fd2);
  46.  
  47.  
  48. }
  49.  
  50. int parse(char *ch, char *dirName, char *fileName) {
  51.     DIR *dir;
  52.  
  53.     if ((dir = opendir(dirName)) == NULL) {
  54.         printf("Error at opendir().");
  55.         exit(1);
  56.     }
  57.  
  58.     struct dirent *d;
  59.     struct stat st;
  60.     char path[50];
  61.  
  62.     while ((d = readdir(dir))) {
  63.         if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
  64.             continue;
  65.         snprintf(path, 50, "%s/%s", dirName, d->d_name);
  66.         printf("%s\n", path);
  67.         if (lstat(path, &st) == -1) {
  68.             printf("Error at lstat().");
  69.             exit(2);
  70.         }
  71.         if (S_ISDIR(st.st_mode)) {
  72.             parse(ch, path, fileName);
  73.         }
  74.         if (S_ISREG(st.st_mode)) {
  75.             process_file(path, ch, fileName);
  76.         }
  77.     }
  78.     closedir(dir);
  79.     return 0;
  80. }
  81.  
  82. int main (int argc, char *argv[]) {
  83.     if (argc != 4) {
  84.         printf("Usage: Program <%s> char directory file", argv[0]);
  85.         exit(0);
  86.     }
  87.     parse(argv[1], argv[2], argv[3]);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement