Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 06-1
- int main (int argc, char *argv[]) {
- unsigned long long sum = 0;
- for (int i = 1; i < argc; i++) {
- struct stat sd;
- if (lstat(argv[i], &sd) == -1 || S_ISLNK(sd.st_mode) || !S_ISREG(sd.st_mode) || sd.st_nlink != 1) {
- continue;
- }
- sum += sd.st_size;
- }
- printf("%llu\n", sum);
- }
- //06-2
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <time.h>
- enum
- {
- DAY_SEC = 86400,
- MON_DAY = 30,
- YEAR_MON = 12,
- BIRTH_YEAR = 1925,
- BIRTH_MON = 10,
- BIRTH_DAY = 7,
- OFF_YEAR = 1900,
- OFF_TIME = 12
- };
- int main (int argc, char *argv[]) {
- struct tm born;
- born.tm_year = BIRTH_YEAR - OFF_YEAR;
- born.tm_mon = BIRTH_MON - 1;
- born.tm_mday = BIRTH_DAY;
- born.tm_isdst = -1;
- born.tm_hour = 0;
- born.tm_min = 0;
- born.tm_sec = 0;
- long long born_time = mktime(&born);
- struct tm curr;
- while (scanf("%d%d%d", &curr.tm_year, &curr.tm_mon, &curr.tm_mday) == 3) {
- curr.tm_isdst = -1;
- curr.tm_year -= OFF_YEAR;
- curr.tm_mon -= 1;
- curr.tm_hour = OFF_TIME;
- curr.tm_min = 0;
- curr.tm_sec = 0;
- long long diff = mktime(&curr) - born_time;
- int year = (diff / (DAY_SEC * MON_DAY * YEAR_MON)) + 1;
- int mon = ((diff % (DAY_SEC * MON_DAY * YEAR_MON)) / (DAY_SEC * MON_DAY)) + 1;
- int day = ((diff % (DAY_SEC * MON_DAY)) / (DAY_SEC)) + 1;
- printf("%d %d %d\n", year, mon, day);
- }
- }
- //06-3
- int
- parse_rwx_permissions(const char *str) {
- const char template[] = "rwxrwxrwx";
- int len = sizeof(template)/sizeof(char) - 1;
- if (str == NULL || strlen(str) != len) {
- return -1;
- }
- int res = 0;
- for (int i = 0; i < len; i++) {
- res <<= 1;
- if (str[i] == template[i]) {
- res++;
- } else if (str[i] != '-') {
- return -1;
- }
- }
- return res;
- }
- //06-4
- int main (int argc, char *argv[]) {
- char *path;
- path = argv[1];
- DIR *d = opendir(path);
- if (!d) {
- return 1;
- }
- struct dirent *curr;
- unsigned long long sum = 0;
- while ((curr = readdir(d))) {
- if (strcmp(curr->d_name, ".") && strcmp(curr->d_name, "..")) {
- char path[PATH_MAX];
- snprintf(path, sizeof(path), "%s/%s", argv[1], curr->d_name);
- struct stat sb;
- if ((stat(path, &sb) >= 0)&&(S_ISREG(sb.st_mode))&&(sb.st_uid == getuid())
- &&(curr->d_name[0]>='A')&&(curr->d_name[0]<='Z')) {
- sum += sb.st_size;
- }
- }
- }
- closedir(d);
- printf("%llu\n", sum);
- }
- //06-5
- struct Task
- {
- unsigned uid;
- int gid_count;
- unsigned *gids;
- };
- enum
- {
- MASK = 0007,
- U_OFFSET = 6,
- G_OFFSET = 3
- };
- static int isuser(const struct stat *stb, const struct Task *task) {
- return (stb->st_uid == task->uid);
- }
- static int isgrp (const struct stat *stb, const struct Task *task) {
- for (int i = 0; i < task->gid_count; i++) {
- if (task->gids[i] == stb->st_gid) {
- return 1;
- }
- }
- return 0;
- }
- int myaccess(const struct stat *stb, const struct Task *task, int access) {
- if (task->uid == 0) {
- return 1;
- }
- if (isuser(stb, task)) {
- return ((stb->st_mode >> U_OFFSET & MASK & access) == access);
- }
- if (isgrp(stb, task)) {
- return ((stb->st_mode >> G_OFFSET & MASK & access) == access);
- }
- return ((stb->st_mode & MASK & access) == access);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement