Big update: 3/N fix

This commit is contained in:
Your Name 2023-11-07 16:21:46 +03:00
parent aff6eb449a
commit b3740af244
12 changed files with 118 additions and 96 deletions

View File

@ -83,7 +83,7 @@ int main(int argc, char **argv) {
break;
default:
printf("chgrp [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R recursive] [-f silent]\n");
printf("chgrp [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R Recursive] [-f Silent]\n");
return 0;
}
}

View File

@ -99,7 +99,7 @@ int main(int argc, char **argv) {
break;
default:
printf("chmod [ugoa]{+|-}[rwxXst] / [0 - 777] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-R recursive] [-s silent]\n");
printf("chmod [ugoa]{+|-}[rwxXst] / [0 - 777] [file1 file2...]\n\t[-H If a command line argument is a symbolic link]\n\t[-R Recursive] [-s Silent]\n");
return 0;
}
}

View File

@ -125,7 +125,7 @@ int main(int argc, char **argv) {
break;
default:
printf("chown [group] [file1 file2...]\n\t[-H if a command line argument is a symbolic link]\n\t[-r recursive]\n");
printf("chown [group] [file1 file2...]\n\t[-H If a command line argument is a symbolic link]\n\t[-r Recursive]\n\t[-f Silent]\n");
return 0;
}
}

View File

@ -59,11 +59,11 @@ int main(const int argc, const char **argv) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-s"))
else if (!strcmp(argv[i], "-n"))
s_flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("cmp [-s silent] FILE1 FILE2 [SKIP1] [SKIP2]\n");
printf("cmp FILE1 FILE2 [SKIP1] [SKIP2]\n\t[-f Silent]\n");
return 0;
}
}

View File

@ -169,7 +169,7 @@ int main(int argc, char **argv) {
break;
default:
printf("chmod [src1 src2...] [dst]\n\t[-f silent]\n");
printf("chmod [src1 src2...] [dst]\n\t[-f Silent]\n");
return 0;
}
}

View File

@ -116,7 +116,7 @@ int main(int argc, char **argv) {
default:
printf("du [file1 file2...]\n\t[-h Sizes in human readable format (e.g., 1K 243M 2G)]\n\t[-s Display only a total for each argument]\n\t[-b Apparent size] [-m Size in megabyte]\n");
printf("du [file1 file2...]\n\t[-h Sizes in human readable format]\n\t[-s Display only a total for each argument]\n\t[-b Apparent size] [-m Size in megabyte]\n");
return 0;
}
}

View File

@ -19,35 +19,37 @@ int ln(const char *src, const char *dst) {
return link(src, dst);
}
int main(const int argc, const char **argv) {
int main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "sf")) != -1) {
switch (opt) {
case 's':
s_flag = 1;
break;
else if (!strcmp("-s", argv[i]))
s_flag = 1;
else if (!strcmp("-f", argv[i]))
case 'f':
f_flag = 1;
break;
else if (!strcmp("--help", argv[i])) {
printf("ln [-f force] [-s symbolic] [TARGET] [LINK/DIR]\n");
default:
printf("ln [src] [dst]\n\t[-f Force] [-s Symbolic]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc - i == 2) {
if (ln(argv[i], argv[i + 1])) {
char *new_path = mu_make_path("ln", argv[i + 1], argv[i]);
if (argc == 2) {
if (ln(argv[0], argv[1])) {
char *new_path = mu_make_path("ln", argv[1], argv[0]);
if (new_path == NULL)
return 1;
if (ln(argv[i], new_path)) {
if (ln(argv[0], new_path)) {
free(new_path);
fprintf(stderr, "ln: %s %s\n", argv[i], strerror(errno));
fprintf(stderr, "ln: %s %s\n", argv[1], strerror(errno));
return 1;
}
@ -55,5 +57,10 @@ int main(const int argc, const char **argv) {
}
}
else {
fprintf(stderr, "ln: missing operand\n");
return 1;
}
return 0;
}

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int do_mkdir(const char *path) {
@ -20,7 +21,7 @@ int do_parents(const char *path) {
char *path2 = strdup(path);
if (!path2) {
fprintf(stderr, "mkdir: %s %s\n", path, strerror(errno));
fprintf(stderr, "mkdir: %s: %s\n", path, strerror(errno));
return 1;
}
@ -32,25 +33,27 @@ int do_parents(const char *path) {
return 0;
}
int main(const int argc, const char **argv) {
unsigned int flag = 0;
int main(int argc, char **argv) {
unsigned int p_flag = 0;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "p")) != -1) {
switch (opt) {
case 'p':
p_flag = 1;
break;
else if (!strcmp(argv[i], "-p"))
flag = 1;
else if (!strcmp(argv[i], "--help")) {
printf("mkdir [-p make parent dir] [dir1 dir2...]\n");
default:
printf("mkdir [dst1 dst2...]\n\t[-p Make parent dir]\n");
return 0;
}
}
for (; i < argc; i++) {
if (flag) {
argv += optind;
argc -= optind;
for (int i = 0; i < argc; i++) {
if (p_flag) {
if (do_parents(argv[i]))
return 1;
}

View File

@ -5,6 +5,7 @@
#include <sys/stat.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include "parse_mode.h"
@ -26,54 +27,55 @@ long parse_int(const char *str) {
return value;
}
int main(const int argc, const char **argv) {
int main(int argc, char **argv) {
mode_t mode = 0666;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "m:")) != -1) {
switch (opt) {
case 'm':
mode = mu_parse_mode(optarg, 0);
break;
else if (!strncmp("-m=", argv[i], 3))
mode = mu_parse_mode(argv[i] + 3, 0);
default:
printf("mknod [-m MODE] [NAME] [TYPE] [MAJOR MINOR]\n");
printf("Types:\n\tb - block device\n\tc or u - character device\n\tp - named pipe/fifo (MAJOR MINOR must be omitted)\n\ts - socket\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc <= 1) {
fprintf(stderr, "mknod: missing operand\n");
return 1;
}
long major = 0;
long minor = 0;
dev_t dev = 0;
if (argc == 4)
dev = makedev(parse_int(argv[2]), parse_int(argv[3]));
if (argc - i >= 2 && argc - i != 3) {
if (argc - i == 4) {
major = parse_int(argv[i + 2]);
minor = parse_int(argv[i + 3]);
dev = makedev(major, minor);
}
if (!strncmp("b", argv[i + 1], 1))
if (!strncmp("b", argv[1], 1))
mode |= S_IFBLK;
else if (!strncmp("c", argv[i + 1], 1) || !strncmp("u", argv[i + 1], 1))
else if (!strncmp("c", argv[1], 1) || !strncmp("u", argv[1], 1))
mode |= S_IFCHR;
else if (!strncmp("p", argv[i + 1], 1))
else if (!strncmp("p", argv[1], 1))
mode |= S_IFIFO;
else if (!strncmp("s", argv[i + 1], 1))
else if (!strncmp("s", argv[1], 1))
mode |= S_IFSOCK;
if (mknod(argv[i], mode, dev) < 0) {
else {
fprintf(stderr, "mknod: unknow file type\n");
return 1;
}
if (mknod(argv[0], mode, dev) < 0) {
fprintf(stderr, "mknod: %s\n", strerror(errno));
return 1;
}
}
else {
printf("mknod [-m=mode] [NAME] [TYPE] [MAJOR MINOR]\n");
printf("Types:\n b - block device\n c or u - character device\n p - named pipe/fifo (MAJOR MINOR must be omitted)\n s - socket\n");
return 0;
}
return 0;
}

View File

@ -43,36 +43,46 @@ int make_temp_file(char *tmp) {
return 0;
}
int main(const int argc, char **argv) {
int main(int argc, char **argv) {
unsigned int d_flag = 0;
char *path = NULL;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
int opt;
while ((opt = getopt(argc, argv, "dp:")) != -1) {
switch (opt) {
case 'd':
d_flag = 1;
break;
else if (!strcmp(argv[i], "-d"))
d_flag = 1;
case 'p':
path = optarg;
break;
else if (!strcmp(argv[i], "--help")) {
printf("mktemp [-d dir] [file]\n");
default:
printf("mktemp [file]\n\t[-d Dir] [-p Base dir]\n");
return 0;
}
}
if (argc - i == 0) {
argv += optind;
argc -= optind;
if (argc == 0) {
fprintf(stderr, "mktemp: missing operand\n");
return 1;
}
if (argc - 1 > i) {
if (argc > 1) {
fprintf(stderr, "mktemp: extra operands\n");
return 1;
}
char *path = getenv("TMPDIR");
if (path == NULL) {
path = getenv("TMPDIR");
if (!path || path[0] == '\0')
path = "/tmp";
}
if (chdir(path)) {
fprintf(stderr, "mktemp: %s\n", strerror(errno));
@ -80,15 +90,15 @@ int main(const int argc, char **argv) {
}
if (d_flag) {
if (make_temp_dir(argv[i]))
if (make_temp_dir(argv[0]))
return 1;
}
else {
if (make_temp_file(argv[i]))
if (make_temp_file(argv[0]))
return 1;
}
printf("%s/%s\n", path, argv[i]);
printf("%s/%s\n", path, argv[0]);
return 0;
}

View File

@ -29,8 +29,8 @@ int move(const char *src, const char *dst) {
}
int main(const int argc, const char **argv) {
if (argc <= 2 || !strcmp(argv[argc - 1], "--help")) {
printf("mv [Src1 src2...] [Dst]\n");
if (argc <= 2 || argv[argc - 1][0] == '-') {
printf("mv [src1 src2...] [dst]\n");
return 0;
}

View File

@ -68,7 +68,7 @@ int main(int argc, char **argv) {
break;
default:
printf("rm [file1 file2...]\n\t[-f force]\n");
printf("rm [file1 file2...]\n\t[-f Force]\n");
return 0;
}
}