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 fd, fd2, digits = 0, low = 0, up = 0, n, occurences=0;
- int process_file(char *path, char *ch, char *output){
- //int fd, fd2;
- char buf[256];
- char character = *ch;
- if((fd = open(path, O_RDONLY)) < 0) {
- printf("Error opening input file\n");
- exit(3);
- }
- if((fd2 = open(output, O_WRONLY | O_EXCL, S_IRWXU)) < 0) {
- printf("Error at opening output file.");
- exit(4);
- }
- while((n = read(fd, buf, 256)) > 0) {
- for(int i=0; i<n; i++) {
- if(isdigit(buf[i])) digits++;
- else {
- if(buf[i]>='a' && buf[i]<='z') low++;
- else if(buf[i]>='A' && buf[i]<='Z') up++;
- }
- if(buf[i]==character) occurences++;
- }
- }
- if(n<0) {
- printf("Error reading input file\n");
- exit(4);
- }
- /*Write file*/
- char str[256];
- sprintf(str, "Number of digits: %d\nNumber of lowercase: %d\nNumber of uppercase: %d\nNumber occurences: %d\n", digits, low, up, occurences);
- write(fd2,str, strlen(str));
- close(fd);
- close(fd2);
- }
- int parse(char *ch, char *dirName, char *fileName) {
- DIR *dir;
- if ((dir = opendir(dirName)) == NULL) {
- printf("Error at opendir().");
- exit(1);
- }
- struct dirent *d;
- struct stat st;
- char path[50];
- 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, fileName);
- }
- if (S_ISREG(st.st_mode)) {
- process_file(path, ch, fileName);
- }
- }
- closedir(dir);
- return 0;
- }
- int main (int argc, char *argv[]) {
- if (argc != 4) {
- printf("Usage: Program <%s> char directory file", argv[0]);
- exit(0);
- }
- parse(argv[1], argv[2], argv[3]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement