chgrp
This commit is contained in:
parent
f1bba77e6e
commit
0c3ec12ca5
2
TODO
2
TODO
@ -11,7 +11,6 @@ renice
|
|||||||
nohup
|
nohup
|
||||||
split
|
split
|
||||||
date
|
date
|
||||||
tee
|
|
||||||
tr
|
tr
|
||||||
cut
|
cut
|
||||||
shuf
|
shuf
|
||||||
@ -23,7 +22,6 @@ stat
|
|||||||
sort
|
sort
|
||||||
test
|
test
|
||||||
tar
|
tar
|
||||||
chgrp
|
|
||||||
|
|
||||||
Other:
|
Other:
|
||||||
kill
|
kill
|
||||||
|
113
coreutils/chgrp.c
Normal file
113
coreutils/chgrp.c
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
#include <grp.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include "make_path.h"
|
||||||
|
#include "get_stat.h"
|
||||||
|
|
||||||
|
int (*get_stat)(const char *prog_name, const char *path, struct stat *stat_path);
|
||||||
|
unsigned int r_flag;
|
||||||
|
unsigned int s_flag;
|
||||||
|
struct group *grp;
|
||||||
|
|
||||||
|
int change(const char *file) {
|
||||||
|
struct stat stat_path;
|
||||||
|
if (mu_get_stat("chgrp", file, &stat_path))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (lchown(file, stat_path.st_uid, grp->gr_gid)) {
|
||||||
|
if (!s_flag)
|
||||||
|
fprintf(stderr, "chgrp: %s: %s\n", file, strerror(errno));
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int chtree(const char *dst) {
|
||||||
|
int ret = change(dst);
|
||||||
|
|
||||||
|
struct stat stat_path;
|
||||||
|
if (get_stat("chgrp", dst, &stat_path))
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (!S_ISDIR(stat_path.st_mode) || !r_flag)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
DIR *dir = opendir(dst);
|
||||||
|
if (dir == NULL) {
|
||||||
|
if (!s_flag)
|
||||||
|
fprintf(stderr, "chgrp: %s: Can`t open directory\n", dst);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct dirent *ep;
|
||||||
|
while ((ep = readdir(dir)) != NULL) {
|
||||||
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
char *full_path = mu_make_path("chgrp", dst, ep->d_name);
|
||||||
|
if (full_path == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (chtree(full_path))
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
|
free(full_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
get_stat = mu_get_lstat;
|
||||||
|
for (i = 1; i < argc; i++) {
|
||||||
|
if (argv[i][0] != '-')
|
||||||
|
break;
|
||||||
|
|
||||||
|
else if (!strcmp(argv[i], "-r"))
|
||||||
|
r_flag = 1;
|
||||||
|
|
||||||
|
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "-f"))
|
||||||
|
s_flag = 1;
|
||||||
|
|
||||||
|
else if (!strcmp(argv[i], "-H"))
|
||||||
|
get_stat = mu_get_stat;
|
||||||
|
|
||||||
|
else if (!strcmp(argv[i], "--help")) {
|
||||||
|
printf("chgrp [-H if a command line argument is a symbolic link] [-r recursive] [-s silent] [group] [file1 file2...]\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc - i < 2) {
|
||||||
|
fprintf(stderr, "chgrp: missing operand\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
grp = getgrnam(argv[i]);
|
||||||
|
if (!grp) {
|
||||||
|
fprintf(stderr, "chgrp: unknow group\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
argv++;
|
||||||
|
argc--;
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
for (; i < argc; i++)
|
||||||
|
if (chtree(argv[i]))
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -19,7 +19,9 @@ int change(const char *file, const char *mode_arg) {
|
|||||||
|
|
||||||
mode_t mode = mu_parse_mode(mode_arg, old_file.st_mode);
|
mode_t mode = mu_parse_mode(mode_arg, old_file.st_mode);
|
||||||
if (chmod(file, mode) != 0) {
|
if (chmod(file, mode) != 0) {
|
||||||
fprintf(stderr, "chmod: unable to chown %s: %s\n", file, strerror(errno));
|
if (!s_flag)
|
||||||
|
fprintf(stderr, "chmod: unable to chown %s: %s\n", file, strerror(errno));
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,6 +42,7 @@ int chtree(const char *dst, const char *mode_arg) {
|
|||||||
if (dir == NULL) {
|
if (dir == NULL) {
|
||||||
if (!s_flag)
|
if (!s_flag)
|
||||||
fprintf(stderr, "chown: %s: Can`t open directory\n", dst);
|
fprintf(stderr, "chown: %s: Can`t open directory\n", dst);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
6
coreutils/tee.c
Normal file
6
coreutils/tee.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(const int argc, const char **argv) {
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
@ -26,6 +26,11 @@ int main(const int argc, const char **argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argc - i == 0) {
|
||||||
|
fprintf(stderr, "truncate: missing operand\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int flags = O_WRONLY | O_NONBLOCK;
|
int flags = O_WRONLY | O_NONBLOCK;
|
||||||
if (!c_flag)
|
if (!c_flag)
|
||||||
flags |= O_CREAT;
|
flags |= O_CREAT;
|
||||||
|
@ -1,13 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
double avg[3] = {0, 0, 0};
|
|
||||||
|
|
||||||
if (getloadavg(avg, sizeof(avg) / sizeof(avg[0])) < 0) {
|
|
||||||
fprintf(stderr, "getloadavg: getloadavg() failed\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("%.2f %.2f %.2f\n", avg[0], avg[1], avg[2]);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user