This commit is contained in:
Your Name 2023-11-01 14:17:25 +03:00
parent 81f54e0005
commit 8db46ac91d
4 changed files with 62 additions and 15 deletions

View File

@ -6,13 +6,13 @@
#include <sys/stat.h>
#include "make_path.h"
#include "get_stat.h"
#include "parse_mode.h"
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
unsigned int r_flag;
unsigned int s_flag;
mode_t mode;
int change(const char *file) {
struct stat old_file;
if (get_stat("chmod", file, &old_file))
@ -74,13 +74,6 @@ int chtree(const char *dst) {
return ret;
}
void parse_mode(const char *arg){
char *p;
mode = strtol(arg, &p, 8);
if (*p)
return;
}
int main(int argc, char **argv) {
int i;
@ -99,17 +92,20 @@ int main(int argc, char **argv) {
get_stat = mu_get_stat;
else if (!strcmp(argv[i], "--help")) {
printf("chmod [-H if a command line argument is a symbolic link] [-r recursive] [-s silent] [file1 file2...]\n");
printf("chmod [-H if a command line argument is a symbolic link] [-r recursive] [-s silent] [ugoa]{+|-|=}[rwxXst] / [sstrwxrwxrwx] [file1 file2...]\n");
return 0;
}
if (argc - i <= 2)
break;
}
if (argc - i - 1 <= 0) {
if (argc - i < 1) {
fprintf(stderr, "chmod: missing operand\n");
return 1;
}
parse_mode(argv[i]);
mode = mu_parse_mode(argv[i]);
argv++;
argc--;

View File

@ -122,6 +122,8 @@ int cptree(const char *src, const char *dst) {
return 1;
}
int ret = 0;
struct dirent *ep;
while ((ep = readdir(dir)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
@ -137,14 +139,15 @@ int cptree(const char *src, const char *dst) {
continue;
}
cptree(src_path, dst_path);
if (cptree(src_path, dst_path))
ret = 1;
free(src_path);
free(dst_path);
}
closedir(dir);
return 0;
return ret;
}
int main(const int argc, char **argv) {

View File

@ -30,6 +30,8 @@ int rmtree(const char *path) {
return 1;
}
int ret = 0;
struct dirent *ep;
while ((ep = readdir(dir)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
@ -39,7 +41,9 @@ int rmtree(const char *path) {
if (full_path == NULL)
continue;
rmtree(full_path);
if (rmtree(full_path))
ret = 1;
free(full_path);
}
@ -48,7 +52,7 @@ int rmtree(const char *path) {
if (!f_flag)
fprintf(stderr, "rm: %s: can`t remove a directory\n", path);
return 0;
return ret;
}
int main(const int argc, char **argv) {

44
libmu/parse_mode.h Normal file
View File

@ -0,0 +1,44 @@
#ifndef _PARSE_MODE_H
#define _PARSE_MODE_H
#include <sys/stat.h>
int mu_parse_mode(const char *s) {
char *p = NULL;
long mode = 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;
}
#endif