This commit is contained in:
Your Name 2023-11-01 15:33:16 +03:00
parent 8db46ac91d
commit 4d8b780c00
4 changed files with 33 additions and 43 deletions

View File

@ -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;
}

View File

@ -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' : '-');

View File

@ -7,6 +7,7 @@
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#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;
}

View File

@ -3,42 +3,20 @@
#include <sys/stat.h>
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