Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. // 06-1
  2.  
  3. int main (int argc, char *argv[]) {
  4. unsigned long long sum = 0;
  5. for (int i = 1; i < argc; i++) {
  6. struct stat sd;
  7. if (lstat(argv[i], &sd) == -1 || S_ISLNK(sd.st_mode) || !S_ISREG(sd.st_mode) || sd.st_nlink != 1) {
  8. continue;
  9. }
  10. sum += sd.st_size;
  11. }
  12. printf("%llu\n", sum);
  13. }
  14.  
  15.  
  16.  
  17. //06-2
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <time.h>
  26.  
  27. enum
  28. {
  29. DAY_SEC = 86400,
  30. MON_DAY = 30,
  31. YEAR_MON = 12,
  32. BIRTH_YEAR = 1925,
  33. BIRTH_MON = 10,
  34. BIRTH_DAY = 7,
  35. OFF_YEAR = 1900,
  36. OFF_TIME = 12
  37. };
  38.  
  39. int main (int argc, char *argv[]) {
  40. struct tm born;
  41. born.tm_year = BIRTH_YEAR - OFF_YEAR;
  42. born.tm_mon = BIRTH_MON - 1;
  43. born.tm_mday = BIRTH_DAY;
  44. born.tm_isdst = -1;
  45. born.tm_hour = 0;
  46. born.tm_min = 0;
  47. born.tm_sec = 0;
  48. long long born_time = mktime(&born);
  49. struct tm curr;
  50. while (scanf("%d%d%d", &curr.tm_year, &curr.tm_mon, &curr.tm_mday) == 3) {
  51. curr.tm_isdst = -1;
  52. curr.tm_year -= OFF_YEAR;
  53. curr.tm_mon -= 1;
  54. curr.tm_hour = OFF_TIME;
  55. curr.tm_min = 0;
  56. curr.tm_sec = 0;
  57. long long diff = mktime(&curr) - born_time;
  58. int year = (diff / (DAY_SEC * MON_DAY * YEAR_MON)) + 1;
  59. int mon = ((diff % (DAY_SEC * MON_DAY * YEAR_MON)) / (DAY_SEC * MON_DAY)) + 1;
  60. int day = ((diff % (DAY_SEC * MON_DAY)) / (DAY_SEC)) + 1;
  61. printf("%d %d %d\n", year, mon, day);
  62. }
  63. }
  64.  
  65.  
  66.  
  67. //06-3
  68.  
  69.  
  70.  
  71. int
  72. parse_rwx_permissions(const char *str) {
  73. const char template[] = "rwxrwxrwx";
  74. int len = sizeof(template)/sizeof(char) - 1;
  75. if (str == NULL || strlen(str) != len) {
  76. return -1;
  77. }
  78. int res = 0;
  79. for (int i = 0; i < len; i++) {
  80. res <<= 1;
  81. if (str[i] == template[i]) {
  82. res++;
  83. } else if (str[i] != '-') {
  84. return -1;
  85. }
  86. }
  87. return res;
  88. }
  89.  
  90.  
  91. //06-4
  92.  
  93.  
  94.  
  95. int main (int argc, char *argv[]) {
  96. char *path;
  97. path = argv[1];
  98. DIR *d = opendir(path);
  99. if (!d) {
  100. return 1;
  101. }
  102. struct dirent *curr;
  103. unsigned long long sum = 0;
  104. while ((curr = readdir(d))) {
  105. if (strcmp(curr->d_name, ".") && strcmp(curr->d_name, "..")) {
  106. char path[PATH_MAX];
  107. snprintf(path, sizeof(path), "%s/%s", argv[1], curr->d_name);
  108. struct stat sb;
  109. if ((stat(path, &sb) >= 0)&&(S_ISREG(sb.st_mode))&&(sb.st_uid == getuid())
  110. &&(curr->d_name[0]>='A')&&(curr->d_name[0]<='Z')) {
  111. sum += sb.st_size;
  112. }
  113. }
  114. }
  115. closedir(d);
  116. printf("%llu\n", sum);
  117. }
  118.  
  119.  
  120.  
  121. //06-5
  122.  
  123.  
  124.  
  125. struct Task
  126. {
  127. unsigned uid;
  128. int gid_count;
  129. unsigned *gids;
  130. };
  131.  
  132. enum
  133. {
  134. MASK = 0007,
  135. U_OFFSET = 6,
  136. G_OFFSET = 3
  137. };
  138.  
  139. static int isuser(const struct stat *stb, const struct Task *task) {
  140. return (stb->st_uid == task->uid);
  141. }
  142.  
  143. static int isgrp (const struct stat *stb, const struct Task *task) {
  144. for (int i = 0; i < task->gid_count; i++) {
  145. if (task->gids[i] == stb->st_gid) {
  146. return 1;
  147. }
  148. }
  149. return 0;
  150. }
  151.  
  152. int myaccess(const struct stat *stb, const struct Task *task, int access) {
  153. if (task->uid == 0) {
  154. return 1;
  155. }
  156. if (isuser(stb, task)) {
  157. return ((stb->st_mode >> U_OFFSET & MASK & access) == access);
  158. }
  159. if (isgrp(stb, task)) {
  160. return ((stb->st_mode >> G_OFFSET & MASK & access) == access);
  161. }
  162. return ((stb->st_mode & MASK & access) == access);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement