diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 3c9a894..4e571b7 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c @@ -100,7 +100,7 @@ int main(int argc, char **argv) { break; } - if (argc - i < 1) { + if (argc - i < 2) { fprintf(stderr, "chmod: missing operand\n"); return 1; } diff --git a/coreutils/ls.c b/coreutils/ls.c index 2dd254c..4c6111c 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -16,7 +16,24 @@ unsigned int a_flag; unsigned int l_flag; void PrintPerm(struct stat sb) { - printf("%c", (S_ISDIR(sb.st_mode)) ? 'd' : '-'); + if (S_ISDIR(sb.st_mode)) + printf("d"); + + else if (S_ISLNK(sb.st_mode)) + printf("l"); + + else if (S_ISCHR(sb.st_mode)) + printf("c"); + + else if (S_ISFIFO(sb.st_mode)) + printf("p"); + + else if (S_ISSOCK(sb.st_mode)) + printf("s"); + + else + printf("-"); + printf("%c%c%c", (sb.st_mode & S_IRUSR) ? 'r' : '-', (sb.st_mode & S_IWUSR) ? 'w' : '-', (sb.st_mode & S_IXUSR) ? 'x' : '-'); printf("%c%c%c", (sb.st_mode & S_IRGRP) ? 'r' : '-', (sb.st_mode & S_IWGRP) ? 'w' : '-', (sb.st_mode & S_IXGRP) ? 'x' : '-'); printf("%c%c%c", (sb.st_mode & S_IROTH) ? 'r' : '-', (sb.st_mode & S_IWOTH) ? 'w' : '-', (sb.st_mode & S_IXOTH) ? 'x' : '-'); diff --git a/coreutils/mknod.c b/coreutils/mknod.c index 58be784..73b56f4 100644 --- a/coreutils/mknod.c +++ b/coreutils/mknod.c @@ -7,6 +7,7 @@ #include #include #include +#include "parse_mode.h" long parse_int(const char *str) { char *ptr = NULL; @@ -33,14 +34,8 @@ int main(const int argc, const char **argv) { if (argv[i][0] != '-') break; - else if (!strncmp("-m=", argv[i], 3)) { - char *ptr = NULL; - mode = (mode_t)strtol(argv[i] + 3, &ptr, 8); - if (*ptr) { - fprintf(stderr, "mknod: invalid mode %s\n", argv[i] + 3); - return 1; - } - } + else if (!strncmp("-m=", argv[i], 3)) + mode = mu_parse_mode(argv[i] + 3); } long major = 0; @@ -76,7 +71,7 @@ int main(const int argc, const char **argv) { else { printf("mknod [-m=mode] [NAME] [TYPE] [MAJOR MINOR]\n"); - printf("Types:\n b - block deviece\n c or u - character device\n p - named pipe (MAJOR MINOR must be omitted)\n s - socket\n"); + printf("Types:\n b - block deviece\n c or u - character device\n p - fifo (MAJOR MINOR must be omitted)\n s - socket\n"); return 0; } diff --git a/libmu/parse_mode.h b/libmu/parse_mode.h index ccdc43e..0e89ed6 100644 --- a/libmu/parse_mode.h +++ b/libmu/parse_mode.h @@ -3,42 +3,20 @@ #include -int mu_parse_mode(const char *s) { +#define U(x) (x << 6) +#define G(x) (x << 3) +#define O(x) (x) +#define A(x) (U(x) | G(x) | O(x)) + +#define FULL_PERM ((S_IRUSR | S_IRGRP | S_IROTH) | (S_IWUSR | S_IWGRP | S_IWOTH) | (S_IXUSR | S_IXGRP | S_IXOTH)) +mode_t mu_parse_mode(const char *s) { char *p = NULL; - long mode = strtol(s, &p, 8); + mode_t mode = (mode_t)strtol(s, &p, 8); if (!*p) return mode; - int group = 0; - int other = 0; - int user = 0; - - int i; - for (i = 0; s[i]; i++) { - switch (s[i]) { - case 'u': - user = 1; - break; - - case 'g': - group = 1; - break; - - case 'o': - other = 1; - - case 'a': - group = 1; - other = 1; - user = 1; - break; - - default: - break; - } - } - - return mode; + //TODO + return A(7); } #endif