Advertisement
STANAANDREY

solab4pb1

Feb 25th, 2024 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <ctype.h>
  6. #include <sys/stat.h>
  7. #include <string.h>
  8.  
  9. int getFD(const char *const fname, int flags, int perm) {
  10.   int fd = open(fname, flags, perm);
  11.   if (fd < 0) {
  12.     perror("");
  13.     exit(1);
  14.   }
  15.   return fd;
  16. }
  17.  
  18. void closeFD(int fd) {
  19.   if (close(fd) < 0) {
  20.     perror("");
  21.     exit(1);
  22.   }
  23. }
  24.  
  25. int main(int argc, char *argv[]) {
  26.  
  27.   if (argc != 4) {
  28.     fprintf(stderr, "Wrong usage!\n");
  29.     exit(1);
  30.   }
  31.  
  32.   int fd1 = getFD(argv[1], O_RDONLY, 0);
  33.  
  34.   int lower = 0, upper = 0, digits = 0, cnt = 0;
  35.   char ch;
  36.   while (read(fd1, &ch, 1) > 0) {
  37.     if (islower(ch)) {
  38.       lower++;
  39.     }
  40.     if (isupper(ch)) {
  41.       upper++;
  42.     }
  43.     if (isdigit(ch)) {
  44.       digits++;
  45.     }
  46.     if (ch == argv[3][0]) {
  47.       cnt++;
  48.     }
  49.   }
  50.  
  51.   int dim;
  52.   struct stat buff;
  53.   if (fstat(fd1, &buff) == -1) {
  54.     perror("fstat");
  55.     exit(1);
  56.   }
  57.   dim = buff.st_size;
  58.  
  59.   int fd2 = getFD(argv[2], O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
  60.  
  61.   char s[30];
  62.   sprintf(s, "nr lower: %d\n", lower);
  63.   write(fd2, s, strlen(s));
  64.   sprintf(s, "nr upper: %d\n", upper);
  65.   write(fd2, s, strlen(s));
  66.   sprintf(s, "nr digits: %d\n", digits);
  67.   write(fd2, s, strlen(s));
  68.   sprintf(s, "nr %c: %d\n", argv[3][0], cnt);
  69.   write(fd2, s, strlen(s));
  70.   sprintf(s, "size: %d\n", dim);
  71.   write(fd2, s, strlen(s));
  72.  
  73.   closeFD(fd1);
  74.   closeFD(fd2);
  75.   return 0;
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement