diff --git a/coreutils/printenv.c b/coreutils/printenv.c index fcc25ec..a4de279 100644 --- a/coreutils/printenv.c +++ b/coreutils/printenv.c @@ -1,9 +1,10 @@ #include #include #include +#include unsigned int nullline; -int printvars(const int len, const char **names) { +int printvars(int len, char **names) { int ret = 0; for (int i = 0; i < len; i++) { @@ -19,27 +20,29 @@ int printvars(const int len, const char **names) { return ret; } -int main(const int argc, const char **argv, const char **envp) { - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; +int main(int argc, char **argv, const char **envp) { + int opt; + while ((opt = getopt(argc, argv, "0")) != -1) { + switch (opt) { + case '0': + nullline = 1; + break; - else if (!strcmp(argv[i], "-0")) - nullline = 1; - - else if (!strcmp(argv[i], "--help")) { - printf("printenv [-n end each output line with NUL, not newline]\n"); - return 0; + default: + printf("printenv [var1 var2...]\n\t[-n end each output line with NUL]\n"); + return 0; } } - if (i == argc) + argv += optind; + argc -= optind; + + if (argc == 0) while (*envp) printf("%s%c", *envp++, (nullline == 1) ? 0 : '\n'); else - return printvars(argc - i, argv + i); + return printvars(argc, argv); return 0; } diff --git a/coreutils/shred.c b/coreutils/shred.c index a45ebf6..82f8406 100644 --- a/coreutils/shred.c +++ b/coreutils/shred.c @@ -42,40 +42,48 @@ int shred(int rand_fd, int fd) { return 0; } -int main(const int argc, const char **argv) { +int main(int argc, char **argv) { n_loops = 3; - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; + int opt; + while ((opt = getopt(argc, argv, "fuzn:")) != -1) { + switch (opt) { + case 'f': + f_flag = 1; + break; - else if (!strcmp(argv[i], "-f")) - f_flag = 1; + case 'u': + u_flag = 1; + break; - else if (!strcmp(argv[i], "-u")) - u_flag = 1; + case 'z': + z_flag = 1; + break; - else if (!strcmp(argv[i], "-z")) - z_flag = 1; + case 'n': + n_loops = atoi(optarg); + break; - else if (!strncmp(argv[i], "-n=", 3)) - n_loops = atoi(argv[i] + 3); - - else if (!strcmp(argv[i], "--help")) { - printf("shred [-n=N Overwrite N times (default 3)] [-z Final overwrite with zeros] [-u Remove file] [-f Chmod to ensure writability] [file file2...]\n"); - return 1; + default: + printf("shred [file1 file2...]\n\t[-n N Overwrite N times, default 3]\n\t[-z Final overwrite with zeros]\n\t[-u Remove file] [-f Chmod to ensure writability]\n"); + return 0; } } - - int rand_fd = open(RAND_SOURCE, O_RDONLY); - if (rand_fd < 0) { - fprintf(stderr, "shred: %s is %s\n", RAND_SOURCE, strerror(errno)); + argv += optind; + argc -= optind; + if (argc == 0) { + fprintf(stderr, "shred: missing operand\n"); return 1; } - for (; i < argc; i++) { + int rand_fd = open(RAND_SOURCE, O_RDONLY); + if (rand_fd < 0) { + fprintf(stderr, "shred: %s: %s\n", RAND_SOURCE, strerror(errno)); + return 1; + } + + for (int i = 0; i < argc; i++) { int fd = open(argv[i], O_RDWR); if (fd < 0) { fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno)); @@ -86,13 +94,15 @@ int main(const int argc, const char **argv) { fsync(fd); close(fd); - if (u_flag) - if (unlink(argv[i]) < 0) - fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno)); - - if (f_flag) + if (f_flag) { if (chmod(argv[i], 0) < 0) fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno)); + } + + if (u_flag) { + if (unlink(argv[i]) < 0) + fprintf(stderr, "shred: %s is %s\n", argv[i], strerror(errno)); + } } close(rand_fd); diff --git a/coreutils/truncate.c b/coreutils/truncate.c index f17172f..a460545 100644 --- a/coreutils/truncate.c +++ b/coreutils/truncate.c @@ -5,28 +5,31 @@ #include #include -int main(const int argc, const char **argv) { +int main(int argc, char **argv) { unsigned int c_flag = 0; unsigned int s_size = 0; - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; + int opt; + while ((opt = getopt(argc, argv, "s:c")) != -1) { + switch (opt) { + case 's': + s_size = atoi(optarg); + break; - else if (!strncmp(argv[i], "-s=", 3)) - s_size = atoi(argv[i] + 3); + case 'c': + c_flag = 1; + break; - else if (!strcmp(argv[i], "-c")) - c_flag = 1; - - else if (!strcmp(argv[i], "--help")) { - printf("truncate [-c Do not create files] [-s=SIZE] file1 file2...\n"); - return 0; + default: + printf("truncate [file]\n\t[-c Do not create files] [-s=SIZE]\n"); + return 0; } } - if (argc - i == 0) { + argv += optind; + argc -= optind; + + if (argc == 0) { fprintf(stderr, "truncate: missing operand\n"); return 1; } @@ -35,15 +38,15 @@ int main(const int argc, const char **argv) { if (!c_flag) flags |= O_CREAT; - int fd = open(argv[i], flags, 0666); + int fd = open(argv[0], flags, 0666); if (fd < 0) { - fprintf(stderr, "truncate %s %s\n", argv[i], strerror(errno)); + fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno)); return 1; } if (ftruncate(fd, s_size) == -1) { close(fd); - fprintf(stderr, "truncate %s %s\n", argv[i], strerror(errno)); + fprintf(stderr, "truncate %s %s\n", argv[0], strerror(errno)); return 1; } diff --git a/coreutils/uname.c b/coreutils/uname.c index b66ae32..8c14d52 100644 --- a/coreutils/uname.c +++ b/coreutils/uname.c @@ -1,6 +1,7 @@ #include #include #include +#include #include /* sys node release version machine */ @@ -10,34 +11,42 @@ enum { O_NODE, O_RELEASE, O_VERSION, - O_MACHINE + O_MACHINE, + O_ALL }; -int main(const int argc, const char **argv) { - for (int i = 0; i < argc; i++) { - if (!strcmp("-a", argv[i])) { - for (size_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) - flags[i] = 1; - } +int main(int argc, char **argv) { + int opt; + while ((opt = getopt(argc, argv, "ashrvm")) != -1) { + switch (opt) { + case 'a': + for (int i = 0; i < O_ALL; i++) + flags[i] = 1; + break; - else if (!strcmp("-s", argv[i])) - flags[O_SYS] = 1; + case 's': + flags[O_SYS] = 1; + break; - else if (!strcmp("-n", argv[i])) - flags[O_NODE] = 1; + case 'n': + flags[O_NODE] = 1; + break; - else if (!strcmp("-r", argv[i])) - flags[O_RELEASE] = 1; + case 'r': + flags[O_RELEASE] = 1; + break; - else if (!strcmp("-v", argv[i])) - flags[O_VERSION] = 1; + case 'v': + flags[O_VERSION] = 1; + break; - else if (!strcmp("-m", argv[i])) - flags[O_MACHINE] = 1; + case 'm': + flags[O_MACHINE] = 1; + break; - else if (!strcmp("--help", argv[i])) { - printf("uname [-a All] [-s Sys] [-n Nodename] [-r Release] [-v Version] [-m Machine]\n"); - return 0; + default: + printf("uname [-a All] [-s Sys] [-n Nodename] [-r Release] [-v Version] [-m Machine]\n"); + return 0; } } diff --git a/coreutils/wc.c b/coreutils/wc.c index 391e71d..d720ff2 100644 --- a/coreutils/wc.c +++ b/coreutils/wc.c @@ -60,27 +60,30 @@ void print_count(const char *path, unsigned int plines, unsigned int pwords, uns printf(" %s\n", path); } -int main(const int argc, const char **argv) { - int i; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') - break; +int main(int argc, char **argv) { + int opt; + while ((opt = getopt(argc, argv, "lcw")) != -1) { + switch (opt) { + case 'l': + l_flag = 1; + break; - else if (!strcmp(argv[i], "-l")) - l_flag = 1; + case 'c': + c_flag = 1; + break; - else if (!strcmp(argv[i], "-c")) - c_flag = 1; + case 'w': + w_flag = 1; + break; - else if (!strcmp(argv[i], "-w")) - w_flag = 1; - - else if (!strcmp(argv[i], "--help")) { - printf("wc [-l lines] [-c bytes] [-w words] [file1 file2...]\n"); - return 0; + default: + printf("wc [file1 file2...]\n\t[-l Lines] [-c Bytes] [-w Words]\n"); + return 0; } } + argv += optind; + argc -= optind; if (!w_flag && !l_flag && !c_flag) { w_flag = 1; @@ -88,12 +91,13 @@ int main(const int argc, const char **argv) { c_flag = 1; } - if (i == argc) { + if (argc == 0 || argv[0][0] == '-') { count(STDIN_FILENO); print_count("", lines, words, bytes); + return 0; } - for (; i < argc; i++) { + for (int i = 0; i < argc; i++) { int fd = open(argv[i], O_RDONLY); if (fd < 0) { fprintf(stderr, "wc: %s\n", strerror(errno));