Advertisement
STANAANDREY

so2 lab2 pb2

Oct 18th, 2024
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <stdbool.h>
  10.  
  11. #define BLOCK_SIZE (1 << 12)
  12. #define AUX_SIZE 50
  13.  
  14. int openFile(const char path[], int flags, mode_t mode) {
  15.     int fd = open(path, flags, mode);
  16.     if (fd < 0) {
  17.         perror("");
  18.         exit(-1);
  19.     }
  20.     return fd;
  21. }
  22.  
  23. void closeFile(int fd) {
  24.     if (close(fd) == -1) {
  25.         perror("");
  26.         exit(-2);
  27.     }
  28. }
  29.  
  30. int main(int argc, char *argv[]) {
  31.     if (argc != 2) {
  32.         fprintf(stderr, "Wrong usage!\n");
  33.         exit(-1);
  34.     }
  35.    
  36.     struct stat st;
  37.     lstat(argv[1], &st);
  38.     printf("%ld\n", st.st_size);
  39.     if (S_ISDIR(st.st_mode)) puts("dir");
  40.     else if (S_ISLNK(st.st_mode)) puts("link");
  41.     else if (S_ISREG(st.st_mode)) puts("reg file");
  42.  
  43.     puts("user: ");
  44.     printf("read: %d\n", (bool)(st.st_mode & S_IRUSR));
  45.     printf("write: %d\n", (bool)(st.st_mode & S_IWUSR));
  46.     printf("exec: %d\n", (bool)(st.st_mode & S_IXUSR));
  47.  
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement