From faeec56e17baa256d742f018cf6441a72c6c5664 Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Fri, 19 Apr 2024 20:55:54 -0400 Subject: [PATCH 01/42] working on getopt --- src/getopt.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/getopt.h | 2 +- src/rm/rm.c | 17 ++++++++-- src/shared.mk | 2 ++ 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index ae24e4e..4c3780a 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -1,6 +1,7 @@ #include "./getopt.h" #include #include +#include char *optarg = NULL; int optind = 1, opterr = 0, optopt = '\0'; @@ -94,3 +95,89 @@ exit: { optind++; return c; } + +int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex) { + int c; + static char *nextchar = NULL; + static int coropt = 1; + if (!coropt) { + coropt = 1; + } + + if (coropt >= argc || argv[coropt] == NULL) { + return -1; + } + + while (argv[coropt] && argv[coropt][0] != '-') { + coropt++; + } + + if (nextchar == NULL || *nextchar == '\0') { + if (coropt >= argc) { + return -1; + } + nextchar = &argv[coropt][1]; + } + + if(*nextchar == '-') { + nextchar++; + while(longopts->name != NULL) { + printf("name: %s %s\n", longopts->name, nextchar); + if(strcmp(longopts->name, nextchar) == 0) { + coropt++; + nextchar = NULL; + c = longopts->val; + goto exit; + } + longopts++; + } + coropt++; + optind++; + nextchar = NULL; + getopt_printerr("invalid option\n"); + return '?'; + } + + int idx; + if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { + getopt_printerr("invalid option\n"); + optopt = *nextchar; + return '?'; + } + + c = *nextchar++; + if (*nextchar == '\0') { + coropt++; + nextchar = NULL; + } + + if (optstring[idx + 1] != ':') { + coropt--; + optind++; + goto exit; + } + + if (nextchar != NULL && *nextchar != '\0') { + coropt++; + } + + if (coropt >= argc || argv[coropt][0] == '-') { + getopt_printerr("option requires an argument\n"); + optopt = *nextchar; + return '?'; + } + + optarg = argv[coropt]; + +exit: { + int i = coropt; + int j = coropt - 1; + while (j >= 0 && argv[j][0] != '-') { + getopt_exchange(argv, i, j); + i--; + j--; + } +} + optind++; + return c; +} diff --git a/src/getopt.h b/src/getopt.h index d697f17..1cede4b 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -20,4 +20,4 @@ int getopt_long(int argc, char *const argv[], const char *optstring, int getopt_long_only(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex); -#endif // _GETOPT_H_ +#endif // _GETOPT_H_ \ No newline at end of file diff --git a/src/rm/rm.c b/src/rm/rm.c index 6e6d777..1f7ef81 100644 --- a/src/rm/rm.c +++ b/src/rm/rm.c @@ -1,6 +1,19 @@ #include +#include "./getopt.h" -int main(void) { - printf("Hello, World!\n"); +int main(int argc, char **argv) { + char c; + struct option opts[] = { + { + .name = "log-file", + .val = 1, + }, + {NULL}, + }; + while((c = getopt_long(argc, argv, "abc", opts, NULL)) != -1) { + printf("%d\n", c); + } + argv += optind; + printf("%s\n", *argv); return 0; } \ No newline at end of file diff --git a/src/shared.mk b/src/shared.mk index 101b180..e6853b4 100644 --- a/src/shared.mk +++ b/src/shared.mk @@ -1,4 +1,6 @@ / ?= ../../ +SRC += $/src/getopt.c + include $/base.mk include $/commons.mk From 4a69bdd35889b0f08e9065e2b9a096405f826f71 Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Sat, 20 Apr 2024 20:06:31 -0400 Subject: [PATCH 02/42] work on getopt_long --- src/getopt.c | 62 ++++++++++++++++++++++++++++++++--------------- src/getopt.h | 2 +- src/mkdir/mkdir.c | 2 +- src/rm/rm.c | 19 +++------------ 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 4c3780a..1125dfc 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -6,6 +6,16 @@ char *optarg = NULL; int optind = 1, opterr = 0, optopt = '\0'; +#define EXCHANGE(coropt) do { \ + int i = (coropt); \ + int j = (coropt) - 1; \ + while (j >= 0 && argv[j][0] != '-') { \ + getopt_exchange(argv, i, j); \ + i--; \ + j--; \ + } \ +} while(0) + static void getopt_printerr(const char *msg) { if (opterr) { fprintf(stderr, "%s", msg); @@ -20,7 +30,7 @@ static int getopt_in(char d, const char *str) { } i++; } - return 0; + return -1; } static void getopt_exchange(char *argv[], int i, int j) { @@ -96,7 +106,8 @@ exit: { return c; } -int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex) { +int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex) { + (void)longindex; int c; static char *nextchar = NULL; static int coropt = 1; @@ -122,40 +133,58 @@ int getopt_long(int argc, char *const argv[], const char *optstring, const struc if(*nextchar == '-') { nextchar++; while(longopts->name != NULL) { - printf("name: %s %s\n", longopts->name, nextchar); if(strcmp(longopts->name, nextchar) == 0) { - coropt++; - nextchar = NULL; + optind++; + nextchar = NULL; c = longopts->val; + if(longopts->has_arg) { + EXCHANGE(coropt); + coropt++; + if (coropt >= argc || argv[coropt][0] == '-') { + getopt_printerr("option requires an argument\n"); + optopt = longopts->val; + c = '?'; + goto exit; + } + optarg = argv[coropt]; + optind++; + } goto exit; } longopts++; } - coropt++; optind++; + optopt = longopts->val; nextchar = NULL; getopt_printerr("invalid option\n"); - return '?'; + c = '?'; + goto exit; } int idx; if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { getopt_printerr("invalid option\n"); optopt = *nextchar; - return '?'; + if(*(nextchar+1) == '\0') { + nextchar = NULL; + optind++; + } + c = '?'; + goto exit; } c = *nextchar++; if (*nextchar == '\0') { coropt++; nextchar = NULL; + optind++; } if (optstring[idx + 1] != ':') { coropt--; - optind++; goto exit; } + EXCHANGE(coropt-1); if (nextchar != NULL && *nextchar != '\0') { coropt++; @@ -164,20 +193,15 @@ int getopt_long(int argc, char *const argv[], const char *optstring, const struc if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); optopt = *nextchar; - return '?'; + c = '?'; + goto exit; } - + optarg = argv[coropt]; + optind++; exit: { - int i = coropt; - int j = coropt - 1; - while (j >= 0 && argv[j][0] != '-') { - getopt_exchange(argv, i, j); - i--; - j--; - } + EXCHANGE(coropt); } - optind++; return c; } diff --git a/src/getopt.h b/src/getopt.h index 1cede4b..1023d32 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -14,7 +14,7 @@ extern int optind, opterr, optopt; int getopt(int argc, char **argv, const char *optstring); -int getopt_long(int argc, char *const argv[], const char *optstring, +int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex); int getopt_long_only(int argc, char *const argv[], const char *optstring, diff --git a/src/mkdir/mkdir.c b/src/mkdir/mkdir.c index 6e6d777..91ec522 100644 --- a/src/mkdir/mkdir.c +++ b/src/mkdir/mkdir.c @@ -3,4 +3,4 @@ int main(void) { printf("Hello, World!\n"); return 0; -} \ No newline at end of file +} diff --git a/src/rm/rm.c b/src/rm/rm.c index 1f7ef81..f65fc66 100644 --- a/src/rm/rm.c +++ b/src/rm/rm.c @@ -1,19 +1,6 @@ #include -#include "./getopt.h" -int main(int argc, char **argv) { - char c; - struct option opts[] = { - { - .name = "log-file", - .val = 1, - }, - {NULL}, - }; - while((c = getopt_long(argc, argv, "abc", opts, NULL)) != -1) { - printf("%d\n", c); - } - argv += optind; - printf("%s\n", *argv); +int main(void) { + printf("Hello, world!\n"); return 0; -} \ No newline at end of file +} From 0498ab4ac93fd032113921dd027def27124def1f Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Sat, 20 Apr 2024 20:07:40 -0400 Subject: [PATCH 03/42] clang format --- src/getopt.c | 96 ++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 1125dfc..3886f8f 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -6,15 +6,16 @@ char *optarg = NULL; int optind = 1, opterr = 0, optopt = '\0'; -#define EXCHANGE(coropt) do { \ - int i = (coropt); \ - int j = (coropt) - 1; \ - while (j >= 0 && argv[j][0] != '-') { \ - getopt_exchange(argv, i, j); \ - i--; \ - j--; \ - } \ -} while(0) +#define EXCHANGE(coropt) \ + do { \ + int i = (coropt); \ + int j = (coropt)-1; \ + while (j >= 0 && argv[j][0] != '-') { \ + getopt_exchange(argv, i, j); \ + i--; \ + j--; \ + } \ + } while (0) static void getopt_printerr(const char *msg) { if (opterr) { @@ -106,7 +107,8 @@ exit: { return c; } -int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex) { +int getopt_long(int argc, char *argv[], const char *optstring, + const struct option *longopts, int *longindex) { (void)longindex; int c; static char *nextchar = NULL; @@ -129,34 +131,34 @@ int getopt_long(int argc, char *argv[], const char *optstring, const struct opti } nextchar = &argv[coropt][1]; } - - if(*nextchar == '-') { - nextchar++; - while(longopts->name != NULL) { - if(strcmp(longopts->name, nextchar) == 0) { - optind++; - nextchar = NULL; - c = longopts->val; - if(longopts->has_arg) { - EXCHANGE(coropt); - coropt++; - if (coropt >= argc || argv[coropt][0] == '-') { - getopt_printerr("option requires an argument\n"); - optopt = longopts->val; - c = '?'; - goto exit; - } - optarg = argv[coropt]; - optind++; - } - goto exit; - } - longopts++; - } - optind++; - optopt = longopts->val; - nextchar = NULL; - getopt_printerr("invalid option\n"); + + if (*nextchar == '-') { + nextchar++; + while (longopts->name != NULL) { + if (strcmp(longopts->name, nextchar) == 0) { + optind++; + nextchar = NULL; + c = longopts->val; + if (longopts->has_arg) { + EXCHANGE(coropt); + coropt++; + if (coropt >= argc || argv[coropt][0] == '-') { + getopt_printerr("option requires an argument\n"); + optopt = longopts->val; + c = '?'; + goto exit; + } + optarg = argv[coropt]; + optind++; + } + goto exit; + } + longopts++; + } + optind++; + optopt = longopts->val; + nextchar = NULL; + getopt_printerr("invalid option\n"); c = '?'; goto exit; } @@ -165,10 +167,10 @@ int getopt_long(int argc, char *argv[], const char *optstring, const struct opti if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { getopt_printerr("invalid option\n"); optopt = *nextchar; - if(*(nextchar+1) == '\0') { - nextchar = NULL; - optind++; - } + if (*(nextchar + 1) == '\0') { + nextchar = NULL; + optind++; + } c = '?'; goto exit; } @@ -177,14 +179,14 @@ int getopt_long(int argc, char *argv[], const char *optstring, const struct opti if (*nextchar == '\0') { coropt++; nextchar = NULL; - optind++; + optind++; } if (optstring[idx + 1] != ':') { coropt--; goto exit; } - EXCHANGE(coropt-1); + EXCHANGE(coropt - 1); if (nextchar != NULL && *nextchar != '\0') { coropt++; @@ -196,12 +198,10 @@ int getopt_long(int argc, char *argv[], const char *optstring, const struct opti c = '?'; goto exit; } - + optarg = argv[coropt]; optind++; -exit: { - EXCHANGE(coropt); -} +exit: { EXCHANGE(coropt); } return c; } From 5a06e8597563cb7d3536822a6ed42efacfb7ffaf Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 13:03:20 +1200 Subject: [PATCH 04/42] Fixed normal getopt's push-back --- src/getopt.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 3886f8f..1b68e1c 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -40,7 +40,7 @@ static void getopt_exchange(char *argv[], int i, int j) { argv[j] = tmp; } -int getopt(int argc, char **argv, const char *optstring) { +int getopt(int argc, char *argv[], const char *optstring) { int c; static char *nextchar = NULL; static int coropt = 1; @@ -67,20 +67,26 @@ int getopt(int argc, char **argv, const char *optstring) { if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { getopt_printerr("invalid option\n"); optopt = *nextchar; - return '?'; + if (*(nextchar + 1) == '\0') { + nextchar = NULL; + optind++; + } + c = '?'; + goto exit; } c = *nextchar++; if (*nextchar == '\0') { coropt++; nextchar = NULL; + optind++; } if (optstring[idx + 1] != ':') { coropt--; - optind++; goto exit; } + EXCHANGE(coropt - 1); if (nextchar != NULL && *nextchar != '\0') { coropt++; @@ -89,21 +95,14 @@ int getopt(int argc, char **argv, const char *optstring) { if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); optopt = *nextchar; - return '?'; + c = '?'; + goto exit; } optarg = argv[coropt]; - -exit: { - int i = coropt; - int j = coropt - 1; - while (j >= 0 && argv[j][0] != '-') { - getopt_exchange(argv, i, j); - i--; - j--; - } -} optind++; + +exit : { EXCHANGE(coropt); } return c; } @@ -202,6 +201,6 @@ int getopt_long(int argc, char *argv[], const char *optstring, optarg = argv[coropt]; optind++; -exit: { EXCHANGE(coropt); } +exit : { EXCHANGE(coropt); } return c; } From 94575cec71ab1bde07d15adcac9bb2974d66d399 Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 13:17:29 +1200 Subject: [PATCH 05/42] Added '--' for getopt --- src/getopt.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/getopt.c b/src/getopt.c index 1b68e1c..b8597b3 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -5,6 +5,7 @@ char *optarg = NULL; int optind = 1, opterr = 0, optopt = '\0'; +static int end_of_args = 1; #define EXCHANGE(coropt) \ do { \ @@ -56,6 +57,13 @@ int getopt(int argc, char *argv[], const char *optstring) { coropt++; } + if (strcmp(argv[coropt], "--") == 0) { + EXCHANGE(coropt); + end_of_args = 0; + coropt++; + return -1; + } + if (nextchar == NULL || *nextchar == '\0') { if (coropt >= argc) { return -1; From a13bdefa3c618f4f2d7cd6aac0b8cbcf412b9bbe Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 13:46:04 +1200 Subject: [PATCH 06/42] Fixed getopt segfualts --- src/getopt.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index b8597b3..3ba0759 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -57,6 +57,12 @@ int getopt(int argc, char *argv[], const char *optstring) { coropt++; } + if (coropt >= argc) { + return -1; + } + + nextchar = &argv[coropt][1]; + if (strcmp(argv[coropt], "--") == 0) { EXCHANGE(coropt); end_of_args = 0; @@ -64,13 +70,6 @@ int getopt(int argc, char *argv[], const char *optstring) { return -1; } - if (nextchar == NULL || *nextchar == '\0') { - if (coropt >= argc) { - return -1; - } - nextchar = &argv[coropt][1]; - } - int idx; if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { getopt_printerr("invalid option\n"); @@ -86,8 +85,8 @@ int getopt(int argc, char *argv[], const char *optstring) { c = *nextchar++; if (*nextchar == '\0') { coropt++; - nextchar = NULL; optind++; + nextchar = argv[coropt]; } if (optstring[idx + 1] != ':') { From 16fce6cc3c4051620a4ca75e133aeb88e7f48cce Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 13:47:12 +1200 Subject: [PATCH 07/42] Update comment --- src/getopt.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/getopt.h b/src/getopt.h index 1023d32..b55e48d 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -1,4 +1,4 @@ -// Reimplementation of GNU getopt +// Reimplementation of GNU getopt by proh14 and cobbcoding #ifndef _GETOPT_H_ #define _GETOPT_H_ @@ -20,4 +20,4 @@ int getopt_long(int argc, char *argv[], const char *optstring, int getopt_long_only(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex); -#endif // _GETOPT_H_ \ No newline at end of file +#endif // _GETOPT_H_ From 9937e223288b9aef36129343808c938987eefd5a Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 15:13:22 +1200 Subject: [PATCH 08/42] Fixed a small bug in getopt --- src/getopt.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 3ba0759..6e2576f 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -5,7 +5,7 @@ char *optarg = NULL; int optind = 1, opterr = 0, optopt = '\0'; -static int end_of_args = 1; +static int end_of_args = 0; #define EXCHANGE(coropt) \ do { \ @@ -65,7 +65,7 @@ int getopt(int argc, char *argv[], const char *optstring) { if (strcmp(argv[coropt], "--") == 0) { EXCHANGE(coropt); - end_of_args = 0; + end_of_args = 1; coropt++; return -1; } @@ -86,7 +86,6 @@ int getopt(int argc, char *argv[], const char *optstring) { if (*nextchar == '\0') { coropt++; optind++; - nextchar = argv[coropt]; } if (optstring[idx + 1] != ':') { From 38ed0c8a4ba855298523160b753ea3450255cd50 Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 15:22:03 +1200 Subject: [PATCH 09/42] add one to optind in argument error so filename is not in list of args --- src/getopt.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/getopt.c b/src/getopt.c index 6e2576f..00a1b93 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -66,7 +66,7 @@ int getopt(int argc, char *argv[], const char *optstring) { if (strcmp(argv[coropt], "--") == 0) { EXCHANGE(coropt); end_of_args = 1; - coropt++; + optind++; return -1; } @@ -102,6 +102,7 @@ int getopt(int argc, char *argv[], const char *optstring) { getopt_printerr("option requires an argument\n"); optopt = *nextchar; c = '?'; + optind++; goto exit; } From dd5da3c1a296301724951abdc380e27904f4ed9a Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 15:24:56 +1200 Subject: [PATCH 10/42] Add one to optind at invalid option so filename is not in argv list --- src/mkdir/mkdir.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/mkdir/mkdir.c b/src/mkdir/mkdir.c index 91ec522..a6fc420 100644 --- a/src/mkdir/mkdir.c +++ b/src/mkdir/mkdir.c @@ -1,6 +1,16 @@ +#define _GNU_SOURCE #include +#include -int main(void) { - printf("Hello, World!\n"); +int main(int argc, char **argv) { + int ch; + while ((ch = getopt(argc, argv, "a:b")) != -1) { + printf("ch = %c\n", ch); + } + argv += optind; + argc -= optind; + for (int i = 0; i < argc; i++) { + printf("argv[%d] = %s\n", i, argv[i]); + } return 0; } From 7360ac7c6e75ff132fb2679e03d00427f51cd265 Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Sun, 21 Apr 2024 15:35:33 +1200 Subject: [PATCH 11/42] wrong commit :( --- src/getopt.c | 1 + src/mkdir/mkdir.c | 14 ++------------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 00a1b93..4ffdfcd 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -78,6 +78,7 @@ int getopt(int argc, char *argv[], const char *optstring) { nextchar = NULL; optind++; } + optind++; c = '?'; goto exit; } diff --git a/src/mkdir/mkdir.c b/src/mkdir/mkdir.c index a6fc420..91ec522 100644 --- a/src/mkdir/mkdir.c +++ b/src/mkdir/mkdir.c @@ -1,16 +1,6 @@ -#define _GNU_SOURCE #include -#include -int main(int argc, char **argv) { - int ch; - while ((ch = getopt(argc, argv, "a:b")) != -1) { - printf("ch = %c\n", ch); - } - argv += optind; - argc -= optind; - for (int i = 0; i < argc; i++) { - printf("argv[%d] = %s\n", i, argv[i]); - } +int main(void) { + printf("Hello, World!\n"); return 0; } From 55c96d623dbc0043203cbdaf4dad4192efdc8e65 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 16:15:11 -0500 Subject: [PATCH 12/42] implemented cksum --- src/cksum/Makefile | 5 +++++ src/cksum/cksum | Bin 0 -> 15800 bytes src/cksum/cksum.1 | 1 + src/cksum/cksum.c | 26 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 src/cksum/Makefile create mode 100755 src/cksum/cksum create mode 100644 src/cksum/cksum.1 create mode 100644 src/cksum/cksum.c diff --git a/src/cksum/Makefile b/src/cksum/Makefile new file mode 100644 index 0000000..f84ebce --- /dev/null +++ b/src/cksum/Makefile @@ -0,0 +1,5 @@ +OUT := cksum + +SRC := cksum.c + +include ../shared.mk diff --git a/src/cksum/cksum b/src/cksum/cksum new file mode 100755 index 0000000000000000000000000000000000000000..60f4b965d759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vb0%V{k%$v0Z~HVl~T6$?-M zripK}@O|J*M48wud8mfmh4TuhNAP_rX)%bxa@bb}Oorq6Hq^z&OS>4Tee1Z6g7@OE z$}tejon?ZrKi3~8o^~sHO@{k@9%>23bwj}9`TQsfD}+1uzY^m+9&lAMKf3}@Al%LX z$~3O(0I~D@pHQZ}1p>s*^M6K}>}vzW&hsy#Oz~Lr`8V(sPp_Qk#&<^GUNd-{EOHk5 zV)^t?{KyZ-!)`0Vds)fD4dAcL)48|{{0b+Y&X)Ub5Akf2kH$B2zILHD-nb*Uy}1E< z&~YcKO^Uj;HMqS=RT^7`x~*kreXvE{xpnLHO>Jshu)d`UyVI>Z;sdSQ!o)5v;co+xM6dA0;#$Pl8B6(k6ZpqLLOYm8?N3GV7m{dLQo;J>3N6$r4D+r~ zvP<~F2V>~ML@i;~$S+zKC@Sg_k?s&V$Wm{&CVWQRJ}uIRvT;#-iMVlze334-BN6I} zs9jhvp=uT z7o;-hk zA39S362f+-Hm@`PDk^C1aR0o%&He9_1_z<}Pr`nD9p3>*ChX7uNaj>Z3UlnoI)&Kwi literal 0 HcmV?d00001 diff --git a/src/cksum/cksum.1 b/src/cksum/cksum.1 new file mode 100644 index 0000000..fca0bf6 --- /dev/null +++ b/src/cksum/cksum.1 @@ -0,0 +1 @@ +\" TODO \ No newline at end of file diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c new file mode 100644 index 0000000..79c4500 --- /dev/null +++ b/src/cksum/cksum.c @@ -0,0 +1,26 @@ +#include +#include + +#define NAME "cksum (canoutils)" +#define VERSION "0.0.1" +#define AUTHOR "cospplredman" + +#include"../version_info.h" + +#define X(a,...) for(a)for(r=(r<<8)^(e&0xff)^__VA_ARGS__,e=0,i=31;i>23;i--)if(r&(1<>(32-i)),e^=q<<(i-24); + +uint32_t q=0x04c11db7,r,e;d,i=1,t,j,k,f=2;main(c,v)char**v;{ + if(c<2)exit(1); + for(;++k=0;t++,d)X(j=t;j;,(j&0xff),j>>=8)X(j=3;j--;,0) + if(printf("%u %d %s\n",~((r<<8)|(e&0xff)),t,v[k])<0)exit(1); + r=e=t=0; + } +} From 0ce4291e0cfc38f49207bd4d7bf949a39e1700cc Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Mon, 22 Apr 2024 09:45:52 +1200 Subject: [PATCH 13/42] Fixed warnings for getopt --- src/getopt.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 4ffdfcd..6113081 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -70,10 +70,10 @@ int getopt(int argc, char *argv[], const char *optstring) { return -1; } - int idx; - if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { + int idx = idx = getopt_in(*nextchar, optstring); + if (!(idx >= 0)) { getopt_printerr("invalid option\n"); - optopt = *nextchar; + optopt = (unsigned)*nextchar; if (*(nextchar + 1) == '\0') { nextchar = NULL; optind++; @@ -83,7 +83,7 @@ int getopt(int argc, char *argv[], const char *optstring) { goto exit; } - c = *nextchar++; + c = (unsigned)*nextchar++; if (*nextchar == '\0') { coropt++; optind++; @@ -101,7 +101,7 @@ int getopt(int argc, char *argv[], const char *optstring) { if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); - optopt = *nextchar; + optopt = (unsigned)*nextchar; c = '?'; optind++; goto exit; From b58fcf1b0dcac83b1ea3660c8ae0db0e76073ae2 Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Sun, 21 Apr 2024 17:53:02 -0400 Subject: [PATCH 14/42] add changes to getopt_long --- src/getopt.c | 23 +++++++++++------------ src/getopt.h | 2 +- src/rm/rm.c | 24 ++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 4ffdfcd..6959eb2 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -132,13 +132,12 @@ int getopt_long(int argc, char *argv[], const char *optstring, coropt++; } - if (nextchar == NULL || *nextchar == '\0') { - if (coropt >= argc) { - return -1; - } - nextchar = &argv[coropt][1]; + if (coropt >= argc) { + return -1; } + nextchar = &argv[coropt][1]; + if (*nextchar == '-') { nextchar++; while (longopts->name != NULL) { @@ -170,22 +169,22 @@ int getopt_long(int argc, char *argv[], const char *optstring, goto exit; } - int idx; - if (!((idx = getopt_in(*nextchar, optstring)) >= 0)) { + int idx = getopt_in(*nextchar, optstring); + if (!(idx >= 0)) { getopt_printerr("invalid option\n"); - optopt = *nextchar; + optopt = (unsigned)*nextchar; if (*(nextchar + 1) == '\0') { nextchar = NULL; - optind++; } + optind++; c = '?'; goto exit; } - c = *nextchar++; + c = (unsigned)*nextchar++; if (*nextchar == '\0') { coropt++; - nextchar = NULL; + nextchar = argv[coropt]; optind++; } @@ -201,7 +200,7 @@ int getopt_long(int argc, char *argv[], const char *optstring, if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); - optopt = *nextchar; + optopt = (unsigned)*nextchar; c = '?'; goto exit; } diff --git a/src/getopt.h b/src/getopt.h index b55e48d..1620401 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -17,7 +17,7 @@ int getopt(int argc, char **argv, const char *optstring); int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex); -int getopt_long_only(int argc, char *const argv[], const char *optstring, +int getopt_long_only(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex); #endif // _GETOPT_H_ diff --git a/src/rm/rm.c b/src/rm/rm.c index f65fc66..133602e 100644 --- a/src/rm/rm.c +++ b/src/rm/rm.c @@ -1,6 +1,26 @@ #include +#include "getopt.h" -int main(void) { - printf("Hello, world!\n"); +int main(int argc, char **argv) { + struct option opts[] = { + { + .name = "log-file", + .val = 1, + }, + {0}, + }; + int c; + while((c = getopt_long(argc, argv, "", opts, NULL)) >= 0) { + switch(c) { + case 1: + printf("log file\n"); + break; + default: + printf("%c\n", c); + break; + } + } + argv += optind; + printf("extra value: %s\n", *argv); return 0; } From 31571b7692c32d4c18a7e2f1bc94cc5ff3863922 Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Sun, 21 Apr 2024 18:43:56 -0400 Subject: [PATCH 15/42] implement flag and longindex for getopt_long --- src/getopt.c | 40 ++++++++++++++++++++++++++-------------- src/getopt.h | 4 ++++ src/rm/rm.c | 24 ++---------------------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/getopt.c b/src/getopt.c index 7a674d6..8d597d5 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -110,13 +110,12 @@ int getopt(int argc, char *argv[], const char *optstring) { optarg = argv[coropt]; optind++; -exit : { EXCHANGE(coropt); } +exit: { EXCHANGE(coropt); } return c; } int getopt_long(int argc, char *argv[], const char *optstring, const struct option *longopts, int *longindex) { - (void)longindex; int c; static char *nextchar = NULL; static int coropt = 1; @@ -132,25 +131,37 @@ int getopt_long(int argc, char *argv[], const char *optstring, coropt++; } - if (coropt >= argc) { - return -1; + if (nextchar == NULL || *nextchar == '\0') { + if (coropt >= argc) { + return -1; + } + nextchar = &argv[coropt][1]; } - nextchar = &argv[coropt][1]; - if (*nextchar == '-') { nextchar++; - while (longopts->name != NULL) { - if (strcmp(longopts->name, nextchar) == 0) { + const struct option *curlong = longopts; + while (curlong->name != NULL) { + if (strcmp(curlong->name, nextchar) == 0) { + if (longindex != NULL) + *longindex = curlong - longopts; optind++; nextchar = NULL; - c = longopts->val; - if (longopts->has_arg) { + if (longopts->flag) { + *curlong->flag = curlong->val; + c = 0; + } else { + c = curlong->val; + } + if (curlong->has_arg == optional_argument && + ((coropt + 1) >= argc || argv[(coropt + 1)][0] == '-')) + goto exit; + if (curlong->has_arg) { EXCHANGE(coropt); coropt++; if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); - optopt = longopts->val; + optopt = curlong->val; c = '?'; goto exit; } @@ -159,7 +170,7 @@ int getopt_long(int argc, char *argv[], const char *optstring, } goto exit; } - longopts++; + curlong++; } optind++; optopt = longopts->val; @@ -175,6 +186,7 @@ int getopt_long(int argc, char *argv[], const char *optstring, optopt = (unsigned)*nextchar; if (*(nextchar + 1) == '\0') { nextchar = NULL; + optind++; } optind++; c = '?'; @@ -184,7 +196,6 @@ int getopt_long(int argc, char *argv[], const char *optstring, c = (unsigned)*nextchar++; if (*nextchar == '\0') { coropt++; - nextchar = argv[coropt]; optind++; } @@ -201,6 +212,7 @@ int getopt_long(int argc, char *argv[], const char *optstring, if (coropt >= argc || argv[coropt][0] == '-') { getopt_printerr("option requires an argument\n"); optopt = (unsigned)*nextchar; + optind++; c = '?'; goto exit; } @@ -208,6 +220,6 @@ int getopt_long(int argc, char *argv[], const char *optstring, optarg = argv[coropt]; optind++; -exit : { EXCHANGE(coropt); } +exit: { EXCHANGE(coropt); } return c; } diff --git a/src/getopt.h b/src/getopt.h index 1620401..e5732a5 100644 --- a/src/getopt.h +++ b/src/getopt.h @@ -2,6 +2,10 @@ #ifndef _GETOPT_H_ #define _GETOPT_H_ +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 + struct option { const char *name; int has_arg; diff --git a/src/rm/rm.c b/src/rm/rm.c index 133602e..f65fc66 100644 --- a/src/rm/rm.c +++ b/src/rm/rm.c @@ -1,26 +1,6 @@ #include -#include "getopt.h" -int main(int argc, char **argv) { - struct option opts[] = { - { - .name = "log-file", - .val = 1, - }, - {0}, - }; - int c; - while((c = getopt_long(argc, argv, "", opts, NULL)) >= 0) { - switch(c) { - case 1: - printf("log file\n"); - break; - default: - printf("%c\n", c); - break; - } - } - argv += optind; - printf("extra value: %s\n", *argv); +int main(void) { + printf("Hello, world!\n"); return 0; } From 80d78b108177747d92bdab765aa9aaf87a91e050 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 17:50:36 -0500 Subject: [PATCH 16/42] re-implemented cksum --- src/cksum/cksum.c | 103 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 79c4500..7a135d5 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,26 +1,101 @@ #include #include +#include #define NAME "cksum (canoutils)" #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include"../version_info.h" +#include "../version_info.h" +#include "../getopt.h" -#define X(a,...) for(a)for(r=(r<<8)^(e&0xff)^__VA_ARGS__,e=0,i=31;i>23;i--)if(r&(1<>(32-i)),e^=q<<(i-24); +static const char usage[] = { + " -v version information\n" +}; -uint32_t q=0x04c11db7,r,e;d,i=1,t,j,k,f=2;main(c,v)char**v;{ - if(c<2)exit(1); - for(;++k 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + length++; + } + + *octets = length; + + /* calculate crc of file contents + length of files in bytes */ + while(length){ + cur_char = (length & 0xff); + length >>= 8; + + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for(int i = 31; i > 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + + /* work through the remaining 3 bytes */ + for(int j = 0; j < 3; j++){ + remainder = (remainder << 8) ^ (difference & 0xff); + difference = 0; + for(int i = 31; i > 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } } - FILE*f=fopen(v[k],"r"); - if(f==NULL)exit(1); - X(;(d=getc(f))>=0;t++,d)X(j=t;j;,(j&0xff),j>>=8)X(j=3;j--;,0) - if(printf("%u %d %s\n",~((r<<8)|(e&0xff)),t,v[k])<0)exit(1); - r=e=t=0; + } + + return ~((remainder<<8)|(difference&0xff)); + +} + +int main(int argc, char ** argv) { + if(argc<2){ + printf("%s\n", usage); + exit(1); + } + + int opt; + while((opt = getopt(argc, argv, "v")) != -1){ + switch(opt){ + case 'v': + print_version(); + return EXIT_SUCCESS; + break; + } + } + + int index = optind; + for(;index < argc; index++){ + FILE*file=fopen(argv[index],"r"); + + if(file==NULL) + return EXIT_FAILURE; + + size_t octets; + uint32_t crc = crc32(file, &octets); + + printf("%u %lu %s\n", crc, octets, argv[index]); } } From 6bfb6b9faa005659944cfbb580f6fb34871b5493 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 22 Apr 2024 00:59:39 +0200 Subject: [PATCH 17/42] Move shared sources to global folder --- commons.mk | 2 +- src/{ => global}/getopt.c | 0 src/{ => global}/getopt.h | 0 src/{ => global}/version_info.h | 0 src/shared.mk | 2 +- 5 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => global}/getopt.c (100%) rename src/{ => global}/getopt.h (100%) rename src/{ => global}/version_info.h (100%) diff --git a/commons.mk b/commons.mk index d9a1baf..f014aeb 100644 --- a/commons.mk +++ b/commons.mk @@ -4,7 +4,7 @@ CFLAGS := @$/compile_flags.txt CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -Wp,-U_FORTIFY_SOURCE -CFLAGS += -iquote $/src +CFLAGS += -iquote $/src/global LDFLAGS := -fwhole-program -flto LDFLAGS += -Wl,--gc-sections diff --git a/src/getopt.c b/src/global/getopt.c similarity index 100% rename from src/getopt.c rename to src/global/getopt.c diff --git a/src/getopt.h b/src/global/getopt.h similarity index 100% rename from src/getopt.h rename to src/global/getopt.h diff --git a/src/version_info.h b/src/global/version_info.h similarity index 100% rename from src/version_info.h rename to src/global/version_info.h diff --git a/src/shared.mk b/src/shared.mk index e6853b4..987ff08 100644 --- a/src/shared.mk +++ b/src/shared.mk @@ -1,6 +1,6 @@ / ?= ../../ -SRC += $/src/getopt.c +SRC += $/src/global/getopt.c include $/base.mk include $/commons.mk From dc561efbacef8a1f313c95f575b5d9686949c053 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 22 Apr 2024 01:00:29 +0200 Subject: [PATCH 18/42] Ignore lastest commit in git blame --- .git-blame-ignore-revs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..d6670c2 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,28 @@ +# This file contains a list of commits that are not likely what you +# are looking for in a blame, such as mass reformatting or renaming. +# You can set this file as a default ignore file for blame by running +# the following command. +# +# $ git config blame.ignoreRevsFile .git-blame-ignore-revs +# +# To temporarily not use this file add +# --ignore-revs-file="" +# to your blame command. +# +# The ignoreRevsFile can't be set globally due to blame failing if the file isn't present. +# To not have to set the option in every repository it is needed in, +# save the following script in your path with the name "git-bblame" +# now you can run +# $ git bblame $FILE +# to use the .git-blame-ignore-revs file if it is present. +# +# #!/usr/bin/env bash +# repo_root=$(git rev-parse --show-toplevel) +# if [[ -e $repo_root/.git-blame-ignore-revs ]]; then +# git blame --ignore-revs-file="$repo_root/.git-blame-ignore-revs" $@ +# else +# git blame $@ +# fi + +# Move to global directory +6bfb6b9faa005659944cfbb580f6fb34871b5493 From 9c39b39a0e7771099a769d2ff2d41f4de8dff9f2 Mon Sep 17 00:00:00 2001 From: cospplredman <62368514+cospplredman@users.noreply.github.com> Date: Sun, 21 Apr 2024 18:21:47 -0500 Subject: [PATCH 19/42] Delete src/cksum/cksum bro i swear i did git rm --- src/cksum/cksum | Bin 15800 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 src/cksum/cksum diff --git a/src/cksum/cksum b/src/cksum/cksum deleted file mode 100755 index 60f4b965d759eeaf9d16a4a0aa81533137b4beb3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15800 zcmeHOYj7LY6~0m|VnZxB0T)AnXdq^iwpOgzaUBvjk{`($oJWn5@Cu7!$+n6u8EKU| z8K!vZn4&SJ=?^F^bfD88X_@IvTRJI)c90!UY==S}(xK__qaLPExx7rFm}j=%*}X@W z7YCSj+UbuyGrQkC-#zEvbI;yg-IcEHYHHc)bT|ad67f|*TK|P66Oa|PHL?H*h+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)^t?{KyZ-!)`0Vds)fD4dAcL)48|{{0b+Y&X)Ub5Akf2kH$B2zILHD-nb*Uy}1E< z&~YcKO^Uj;HMqS=RT^7`x~*kreXvE{xpnLHO>Jshu)d`UyVI>Z;sdSQ!o)5v;co+xM6dA0;#$Pl8B6(k6ZpqLLOYm8?N3GV7m{dLQo;J>3N6$r4D+r~ zvP<~F2V>~ML@i;~$S+zKC@Sg_k?s&V$Wm{&CVWQRJ}uIRvT;#-iMVlze334-BN6I} zs9jhvp=uT z7o;-hk zA39S362f+-Hm@`PDk^C1aR0o%&He9_1_z<}Pr`nD9p3>*ChX7uNaj>Z3UlnoI)&Kwi From 4c1b59d8ce170604917c125217755928d035492b Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Mon, 22 Apr 2024 01:24:08 +0200 Subject: [PATCH 20/42] Ignore global directly in derivation generator --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index d739ca4..56d9a8c 100644 --- a/flake.nix +++ b/flake.nix @@ -100,7 +100,7 @@ value = build-single-bin name; }) (attrNames (pkgs.lib.filterAttrs - (n: v: v == "directory") + (n: v: v == "directory" && n != "global") (readDir ./src))))); }); } From 815a831571c5601fd7523888e93f262f72eb0d8d Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 18:35:01 -0500 Subject: [PATCH 21/42] formatting fix --- src/cksum/cksum.c | 172 ++++++++++++++++++++++------------------------ 1 file changed, 84 insertions(+), 88 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 7a135d5..3014c8e 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,101 +1,97 @@ -#include -#include -#include +#include +#include +#include #define NAME "cksum (canoutils)" #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include "../version_info.h" #include "../getopt.h" +#include "../version_info.h" -static const char usage[] = { - " -v version information\n" -}; +static const char usage[] = {" -v version information\n"}; static const uint32_t crc32_ = 0x04c11db7; -uint32_t crc32(FILE *file, size_t *octets){ - uint32_t remainder = 0; - uint8_t difference = 0; - - size_t length = 0; - - - /* calculate crc of file contents */ - int cur_char; - while((cur_char = getc(file)) != EOF){ - remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - length++; - } - - *octets = length; - - /* calculate crc of file contents + length of files in bytes */ - while(length){ - cur_char = (length & 0xff); - length >>= 8; - - remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - } - - - /* work through the remaining 3 bytes */ - for(int j = 0; j < 3; j++){ - remainder = (remainder << 8) ^ (difference & 0xff); - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - } - - return ~((remainder<<8)|(difference&0xff)); - +uint32_t crc32(FILE *file, size_t *octets) { + uint32_t remainder = 0; + uint8_t difference = 0; + + size_t length = 0; + + /* calculate crc of file contents */ + int cur_char; + while ((cur_char = getc(file)) != EOF) { + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + length++; + } + + *octets = length; + + /* calculate crc of file contents + length of files in bytes */ + while (length) { + cur_char = (length & 0xff); + length >>= 8; + + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + /* work through the remaining 3 bytes */ + for (int j = 0; j < 3; j++) { + remainder = (remainder << 8) ^ (difference & 0xff); + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + return ~((remainder << 8) | (difference & 0xff)); } -int main(int argc, char ** argv) { - if(argc<2){ - printf("%s\n", usage); - exit(1); - } - - int opt; - while((opt = getopt(argc, argv, "v")) != -1){ - switch(opt){ - case 'v': - print_version(); - return EXIT_SUCCESS; - break; - } - } - - int index = optind; - for(;index < argc; index++){ - FILE*file=fopen(argv[index],"r"); - - if(file==NULL) - return EXIT_FAILURE; - - size_t octets; - uint32_t crc = crc32(file, &octets); - - printf("%u %lu %s\n", crc, octets, argv[index]); - } +int main(int argc, char **argv) { + if (argc < 2) { + printf("%s\n", usage); + exit(1); + } + + int opt; + while ((opt = getopt(argc, argv, "v")) != -1) { + switch (opt) { + case 'v': + print_version(); + return EXIT_SUCCESS; + break; + } + } + + int index = optind; + for (; index < argc; index++) { + FILE *file = fopen(argv[index], "r"); + + if (file == NULL) + return EXIT_FAILURE; + + size_t octets; + uint32_t crc = crc32(file, &octets); + fclose(file); + + printf("%u %lu %s\n", crc, octets, argv[index]); + } } From e1547e898d90c7178aabac7352b59d32848c321b Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Sun, 21 Apr 2024 19:44:21 -0400 Subject: [PATCH 22/42] rename getopt.h to cano_getopt.h --- src/global/{getopt.h => cano_getopt.h} | 0 src/global/getopt.c | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/global/{getopt.h => cano_getopt.h} (100%) diff --git a/src/global/getopt.h b/src/global/cano_getopt.h similarity index 100% rename from src/global/getopt.h rename to src/global/cano_getopt.h diff --git a/src/global/getopt.c b/src/global/getopt.c index 8d597d5..529dfdc 100644 --- a/src/global/getopt.c +++ b/src/global/getopt.c @@ -1,4 +1,4 @@ -#include "./getopt.h" +#include "./cano_getopt.h" #include #include #include From 01ec9b945b8f7b4a7dd5d6a87d349da214e46208 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 18:50:58 -0500 Subject: [PATCH 23/42] updated makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 983cbfe..98f764e 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ BINS += mv BINS += chmod BINS += sh BINS += sleep +BINS += cksum BINARIES := $(foreach b, $(BINS), src/$b/$b) BINS-COPY := $(foreach b, $(BINS), bin/$b) From 1f1b0b36a24e8838c3539e6c09f8da4c52a0b2ac Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 19:57:19 -0500 Subject: [PATCH 24/42] updated include paths --- src/cksum/cksum.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 3014c8e..f3f1333 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -6,8 +6,8 @@ #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include "../getopt.h" -#include "../version_info.h" +#include "getopt.h" +#include "version_info.h" static const char usage[] = {" -v version information\n"}; From f694cae764611af0b34d7c56bc8e025fd44e475e Mon Sep 17 00:00:00 2001 From: Hoorad Farrokh Date: Mon, 22 Apr 2024 12:06:33 +1200 Subject: [PATCH 25/42] Renamed getopt --- src/global/{getopt.c => cgetopt.c} | 4 +++- src/global/{cano_getopt.h => cgetopt.h} | 4 ++-- src/shared.mk | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) rename src/global/{getopt.c => cgetopt.c} (98%) rename src/global/{cano_getopt.h => cgetopt.h} (93%) diff --git a/src/global/getopt.c b/src/global/cgetopt.c similarity index 98% rename from src/global/getopt.c rename to src/global/cgetopt.c index 529dfdc..f67a890 100644 --- a/src/global/getopt.c +++ b/src/global/cgetopt.c @@ -1,4 +1,6 @@ -#include "./cano_getopt.h" +// Reimplementation of GNU getopt by proh14 and cobbcoding + +#include "./cgetopt.h" #include #include #include diff --git a/src/global/cano_getopt.h b/src/global/cgetopt.h similarity index 93% rename from src/global/cano_getopt.h rename to src/global/cgetopt.h index e5732a5..70dd2fd 100644 --- a/src/global/cano_getopt.h +++ b/src/global/cgetopt.h @@ -1,6 +1,6 @@ // Reimplementation of GNU getopt by proh14 and cobbcoding -#ifndef _GETOPT_H_ -#define _GETOPT_H_ +#ifndef _CGETOPT_H_ +#define _CGETOPT_H_ #define no_argument 0 #define required_argument 1 diff --git a/src/shared.mk b/src/shared.mk index 987ff08..fd7f5ee 100644 --- a/src/shared.mk +++ b/src/shared.mk @@ -1,6 +1,6 @@ / ?= ../../ -SRC += $/src/global/getopt.c +SRC += $/src/global/cgetopt.c include $/base.mk include $/commons.mk From cb008b31e70772e2bda229f426f3a1cf26af20ce Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 16:15:11 -0500 Subject: [PATCH 26/42] implemented cksum --- src/cksum/Makefile | 5 +++++ src/cksum/cksum | Bin 0 -> 15800 bytes src/cksum/cksum.1 | 1 + src/cksum/cksum.c | 26 ++++++++++++++++++++++++++ 4 files changed, 32 insertions(+) create mode 100644 src/cksum/Makefile create mode 100755 src/cksum/cksum create mode 100644 src/cksum/cksum.1 create mode 100644 src/cksum/cksum.c diff --git a/src/cksum/Makefile b/src/cksum/Makefile new file mode 100644 index 0000000..f84ebce --- /dev/null +++ b/src/cksum/Makefile @@ -0,0 +1,5 @@ +OUT := cksum + +SRC := cksum.c + +include ../shared.mk diff --git a/src/cksum/cksum b/src/cksum/cksum new file mode 100755 index 0000000000000000000000000000000000000000..60f4b965d759eeaf9d16a4a0aa81533137b4beb3 GIT binary patch literal 15800 zcmeHOYj7LY6~0m|VnZxB0T)AnXdq^iwpOgzaUBvjk{`($oJWn5@Cu7!$+n6u8EKU| z8K!vZn4&SJ=?^F^bfD88X_@IvTRJI)c90!UY==S}(xK__qaLPExx7rFm}j=%*}X@W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pHQZ}1p>s*^M6K}>}vzW&hsy#Oz~Lr`8V(sPp_Qk#&<^GUNd-{EOHk5 zV)^t?{KyZ-!)`0Vds)fD4dAcL)48|{{0b+Y&X)Ub5Akf2kH$B2zILHD-nb*Uy}1E< z&~YcKO^Uj;HMqS=RT^7`x~*kreXvE{xpnLHO>Jshu)d`UyVI>Z;sdSQ!o)5v;co+xM6dA0;#$Pl8B6(k6ZpqLLOYm8?N3GV7m{dLQo;J>3N6$r4D+r~ zvP<~F2V>~ML@i;~$S+zKC@Sg_k?s&V$Wm{&CVWQRJ}uIRvT;#-iMVlze334-BN6I} zs9jhvp=uT z7o;-hk zA39S362f+-Hm@`PDk^C1aR0o%&He9_1_z<}Pr`nD9p3>*ChX7uNaj>Z3UlnoI)&Kwi literal 0 HcmV?d00001 diff --git a/src/cksum/cksum.1 b/src/cksum/cksum.1 new file mode 100644 index 0000000..fca0bf6 --- /dev/null +++ b/src/cksum/cksum.1 @@ -0,0 +1 @@ +\" TODO \ No newline at end of file diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c new file mode 100644 index 0000000..79c4500 --- /dev/null +++ b/src/cksum/cksum.c @@ -0,0 +1,26 @@ +#include +#include + +#define NAME "cksum (canoutils)" +#define VERSION "0.0.1" +#define AUTHOR "cospplredman" + +#include"../version_info.h" + +#define X(a,...) for(a)for(r=(r<<8)^(e&0xff)^__VA_ARGS__,e=0,i=31;i>23;i--)if(r&(1<>(32-i)),e^=q<<(i-24); + +uint32_t q=0x04c11db7,r,e;d,i=1,t,j,k,f=2;main(c,v)char**v;{ + if(c<2)exit(1); + for(;++k=0;t++,d)X(j=t;j;,(j&0xff),j>>=8)X(j=3;j--;,0) + if(printf("%u %d %s\n",~((r<<8)|(e&0xff)),t,v[k])<0)exit(1); + r=e=t=0; + } +} From e008a9311a2f781578e66ec79dc4bd6e2b1f814c Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 17:50:36 -0500 Subject: [PATCH 27/42] re-implemented cksum --- src/cksum/cksum.c | 103 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 79c4500..7a135d5 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,26 +1,101 @@ #include #include +#include #define NAME "cksum (canoutils)" #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include"../version_info.h" +#include "../version_info.h" +#include "../getopt.h" -#define X(a,...) for(a)for(r=(r<<8)^(e&0xff)^__VA_ARGS__,e=0,i=31;i>23;i--)if(r&(1<>(32-i)),e^=q<<(i-24); +static const char usage[] = { + " -v version information\n" +}; -uint32_t q=0x04c11db7,r,e;d,i=1,t,j,k,f=2;main(c,v)char**v;{ - if(c<2)exit(1); - for(;++k 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + length++; + } + + *octets = length; + + /* calculate crc of file contents + length of files in bytes */ + while(length){ + cur_char = (length & 0xff); + length >>= 8; + + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for(int i = 31; i > 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + + /* work through the remaining 3 bytes */ + for(int j = 0; j < 3; j++){ + remainder = (remainder << 8) ^ (difference & 0xff); + difference = 0; + for(int i = 31; i > 23; i--){ + if(remainder & (1 << i)){ + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } } - FILE*f=fopen(v[k],"r"); - if(f==NULL)exit(1); - X(;(d=getc(f))>=0;t++,d)X(j=t;j;,(j&0xff),j>>=8)X(j=3;j--;,0) - if(printf("%u %d %s\n",~((r<<8)|(e&0xff)),t,v[k])<0)exit(1); - r=e=t=0; + } + + return ~((remainder<<8)|(difference&0xff)); + +} + +int main(int argc, char ** argv) { + if(argc<2){ + printf("%s\n", usage); + exit(1); + } + + int opt; + while((opt = getopt(argc, argv, "v")) != -1){ + switch(opt){ + case 'v': + print_version(); + return EXIT_SUCCESS; + break; + } + } + + int index = optind; + for(;index < argc; index++){ + FILE*file=fopen(argv[index],"r"); + + if(file==NULL) + return EXIT_FAILURE; + + size_t octets; + uint32_t crc = crc32(file, &octets); + + printf("%u %lu %s\n", crc, octets, argv[index]); } } From 7217e109c807bdaae8137e224fb4c922c6744a09 Mon Sep 17 00:00:00 2001 From: cospplredman <62368514+cospplredman@users.noreply.github.com> Date: Sun, 21 Apr 2024 18:21:47 -0500 Subject: [PATCH 28/42] Delete src/cksum/cksum bro i swear i did git rm --- src/cksum/cksum | Bin 15800 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 src/cksum/cksum diff --git a/src/cksum/cksum b/src/cksum/cksum deleted file mode 100755 index 60f4b965d759eeaf9d16a4a0aa81533137b4beb3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15800 zcmeHOYj7LY6~0m|VnZxB0T)AnXdq^iwpOgzaUBvjk{`($oJWn5@Cu7!$+n6u8EKU| z8K!vZn4&SJ=?^F^bfD88X_@IvTRJI)c90!UY==S}(xK__qaLPExx7rFm}j=%*}X@W z7YCSj+UbuyGrQkC-#zEvbI;yg-IcEHYHHc)bT|ad67f|*TK|P66Oa|PHL?H*h+0vM z^A+N9aUS#ngEQ*`CZN`oGtPxrDETEo@++glB6yie3r3ERko?XiXDA>DqbMQ+q4Mj5$2*PoZ1AztI0!!j~}Fy{MFC{qYvF&Vzg^O=#cMPpthm~w1ta(O zHTcO(D=j`9_%gT3{?2tBDrNo|Wm?6$?&$uS>UG`WitcDE)mPD1TT@X}?Mude>y3^n zu1XvgW4n;65_ZG(y5LQ(m$^(hvAAvjBf(xU4*AmFJ^z>9QGHS!=7SYtewGgv~v~s zD{)wb=E7p_h)7>l6P*#QU3A2IBQc?>xRUK^QVS(CwI>uMroAtuc0^;L?&zV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9p3>*ChX7uNaj>Z3UlnoI)&Kwi From bc172e1c311b14c923f6cef5def1d187fa732c68 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 18:35:01 -0500 Subject: [PATCH 29/42] formatting fix --- src/cksum/cksum.c | 172 ++++++++++++++++++++++------------------------ 1 file changed, 84 insertions(+), 88 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 7a135d5..3014c8e 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,101 +1,97 @@ -#include -#include -#include +#include +#include +#include #define NAME "cksum (canoutils)" #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include "../version_info.h" #include "../getopt.h" +#include "../version_info.h" -static const char usage[] = { - " -v version information\n" -}; +static const char usage[] = {" -v version information\n"}; static const uint32_t crc32_ = 0x04c11db7; -uint32_t crc32(FILE *file, size_t *octets){ - uint32_t remainder = 0; - uint8_t difference = 0; - - size_t length = 0; - - - /* calculate crc of file contents */ - int cur_char; - while((cur_char = getc(file)) != EOF){ - remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - length++; - } - - *octets = length; - - /* calculate crc of file contents + length of files in bytes */ - while(length){ - cur_char = (length & 0xff); - length >>= 8; - - remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - } - - - /* work through the remaining 3 bytes */ - for(int j = 0; j < 3; j++){ - remainder = (remainder << 8) ^ (difference & 0xff); - difference = 0; - for(int i = 31; i > 23; i--){ - if(remainder & (1 << i)){ - remainder ^= (1 << i) | (crc32_ >> (32 - i)); - difference ^= crc32_ << (i - 24); - } - } - } - - return ~((remainder<<8)|(difference&0xff)); - +uint32_t crc32(FILE *file, size_t *octets) { + uint32_t remainder = 0; + uint8_t difference = 0; + + size_t length = 0; + + /* calculate crc of file contents */ + int cur_char; + while ((cur_char = getc(file)) != EOF) { + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + length++; + } + + *octets = length; + + /* calculate crc of file contents + length of files in bytes */ + while (length) { + cur_char = (length & 0xff); + length >>= 8; + + remainder = (remainder << 8) ^ (difference & 0xff) ^ cur_char; + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + /* work through the remaining 3 bytes */ + for (int j = 0; j < 3; j++) { + remainder = (remainder << 8) ^ (difference & 0xff); + difference = 0; + for (int i = 31; i > 23; i--) { + if (remainder & (1 << i)) { + remainder ^= (1 << i) | (crc32_ >> (32 - i)); + difference ^= crc32_ << (i - 24); + } + } + } + + return ~((remainder << 8) | (difference & 0xff)); } -int main(int argc, char ** argv) { - if(argc<2){ - printf("%s\n", usage); - exit(1); - } - - int opt; - while((opt = getopt(argc, argv, "v")) != -1){ - switch(opt){ - case 'v': - print_version(); - return EXIT_SUCCESS; - break; - } - } - - int index = optind; - for(;index < argc; index++){ - FILE*file=fopen(argv[index],"r"); - - if(file==NULL) - return EXIT_FAILURE; - - size_t octets; - uint32_t crc = crc32(file, &octets); - - printf("%u %lu %s\n", crc, octets, argv[index]); - } +int main(int argc, char **argv) { + if (argc < 2) { + printf("%s\n", usage); + exit(1); + } + + int opt; + while ((opt = getopt(argc, argv, "v")) != -1) { + switch (opt) { + case 'v': + print_version(); + return EXIT_SUCCESS; + break; + } + } + + int index = optind; + for (; index < argc; index++) { + FILE *file = fopen(argv[index], "r"); + + if (file == NULL) + return EXIT_FAILURE; + + size_t octets; + uint32_t crc = crc32(file, &octets); + fclose(file); + + printf("%u %lu %s\n", crc, octets, argv[index]); + } } From b078275193aebb105425464a895843c98f87b200 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 18:50:58 -0500 Subject: [PATCH 30/42] updated makefile --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 983cbfe..98f764e 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ BINS += mv BINS += chmod BINS += sh BINS += sleep +BINS += cksum BINARIES := $(foreach b, $(BINS), src/$b/$b) BINS-COPY := $(foreach b, $(BINS), bin/$b) From 0c1afeb9fa406e0cda7def8f54143ab015f3b395 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Sun, 21 Apr 2024 19:57:19 -0500 Subject: [PATCH 31/42] updated include paths --- src/cksum/cksum.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 3014c8e..f3f1333 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -6,8 +6,8 @@ #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include "../getopt.h" -#include "../version_info.h" +#include "getopt.h" +#include "version_info.h" static const char usage[] = {" -v version information\n"}; From 07fffc8a0451be9950e3528c997b53123f29f1ca Mon Sep 17 00:00:00 2001 From: CobbCoding Date: Mon, 22 Apr 2024 11:31:24 -0400 Subject: [PATCH 32/42] add = to getopt_long --- src/global/cgetopt.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/global/cgetopt.c b/src/global/cgetopt.c index f67a890..01509f4 100644 --- a/src/global/cgetopt.c +++ b/src/global/cgetopt.c @@ -144,11 +144,11 @@ int getopt_long(int argc, char *argv[], const char *optstring, nextchar++; const struct option *curlong = longopts; while (curlong->name != NULL) { - if (strcmp(curlong->name, nextchar) == 0) { + size_t name_len = strlen(curlong->name); + if (strncmp(curlong->name, nextchar, name_len) == 0) { if (longindex != NULL) *longindex = curlong - longopts; optind++; - nextchar = NULL; if (longopts->flag) { *curlong->flag = curlong->val; c = 0; @@ -159,17 +159,23 @@ int getopt_long(int argc, char *argv[], const char *optstring, ((coropt + 1) >= argc || argv[(coropt + 1)][0] == '-')) goto exit; if (curlong->has_arg) { - EXCHANGE(coropt); - coropt++; - if (coropt >= argc || argv[coropt][0] == '-') { - getopt_printerr("option requires an argument\n"); - optopt = curlong->val; - c = '?'; - goto exit; + if (*(nextchar + name_len) != '=') { + nextchar = NULL; + EXCHANGE(coropt); + coropt++; + if (coropt >= argc || argv[coropt][0] == '-') { + getopt_printerr("option requires an argument\n"); + optopt = curlong->val; + c = '?'; + goto exit; + } + optarg = argv[coropt]; + optind++; + } else { + optarg = nextchar + (name_len + 1); } - optarg = argv[coropt]; - optind++; } + nextchar = NULL; goto exit; } curlong++; From 62048505b38bf9e2d8ea6dc87d818d1238f3e14d Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 08:22:51 -0500 Subject: [PATCH 33/42] added flags to cksum --- .gitignore | 1 + src/cksum/cksum.c | 81 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index ca890b5..1e0675a 100644 --- a/.gitignore +++ b/.gitignore @@ -76,6 +76,7 @@ src/rm/rm src/rmdir/rmdir src/sh/sh src/sleep/sleep +src/sleep/cksum bin/ diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index f3f1333..4c6f8bb 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,15 +1,34 @@ #include #include #include +#include +#include #define NAME "cksum (canoutils)" #define VERSION "0.0.1" #define AUTHOR "cospplredman" -#include "getopt.h" +#include "cgetopt.h" #include "version_info.h" -static const char usage[] = {" -v version information\n"}; +static const char usage[] = { + "Usage: cksum [Option]... [File]...\n" + " --version version information\n" + " --help display this help and exit\n" + " --raw output raw crc\n" + " --tag default output style\n" +}; + +//flags +static int output_format = 't'; + +static struct option long_options[] = { + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {"raw", no_argument, NULL, 'r'}, + {"tag", no_argument, NULL, 't'}, +}; + static const uint32_t crc32_ = 0x04c11db7; @@ -65,33 +84,65 @@ uint32_t crc32(FILE *file, size_t *octets) { return ~((remainder << 8) | (difference & 0xff)); } -int main(int argc, char **argv) { - if (argc < 2) { - printf("%s\n", usage); - exit(1); +void crc_print(char *filename, uint32_t crc, size_t octets){ + printf("%c\n", output_format); + switch(output_format){ + case 't': + if(filename == NULL) + printf("%u %lu\n", crc, octets); + else + printf("%u %lu %s\n", crc, octets, filename); + break; + case 'r': + printf("%c%c%c%c", (crc >> 24) & 255, (crc >> 16) & 255, (crc >> 8) & 255, crc & 255); + break; } +} - int opt; - while ((opt = getopt(argc, argv, "v")) != -1) { +int main(int argc, char **argv) { + + int opt, option_index; + while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) != -1) { switch (opt) { + case 0: + break; case 'v': print_version(); return EXIT_SUCCESS; break; + case 'h': + printf("%s\n", usage); + return EXIT_SUCCESS; + case 'r': + output_format = 'r'; + break; + case 't': + output_format = 't'; + break; } } int index = optind; + size_t octets; + uint32_t crc; + + if (argc < 2) { + crc = crc32(stdin, &octets); + crc_print(NULL, crc, octets); + return EXIT_SUCCESS; + } + for (; index < argc; index++) { - FILE *file = fopen(argv[index], "r"); + FILE *file = fopen(argv[index], "r+"); - if (file == NULL) - return EXIT_FAILURE; + if (file == NULL){ + fprintf(stderr, "cksum: %s: %s\n", argv[index], strerror(errno)); + continue; + } - size_t octets; - uint32_t crc = crc32(file, &octets); - fclose(file); + crc = crc32(file, &octets); + crc_print(argv[index], crc, octets); - printf("%u %lu %s\n", crc, octets, argv[index]); + fclose(file); } } From e5951dbf2f24968e4cd6e262a9b7d965b8ebd088 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 08:23:28 -0500 Subject: [PATCH 34/42] fixed typo --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1e0675a..de81bf8 100644 --- a/.gitignore +++ b/.gitignore @@ -76,7 +76,7 @@ src/rm/rm src/rmdir/rmdir src/sh/sh src/sleep/sleep -src/sleep/cksum +src/cksum/cksum bin/ From 01ce6211af445bdcb3a9468931d14f9bf4128864 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 14:01:21 -0500 Subject: [PATCH 35/42] formatting fix --- src/cksum/cksum.c | 53 +++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 4c6f8bb..13ce0de 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -1,7 +1,7 @@ +#include #include #include #include -#include #include #define NAME "cksum (canoutils)" @@ -11,25 +11,22 @@ #include "cgetopt.h" #include "version_info.h" -static const char usage[] = { - "Usage: cksum [Option]... [File]...\n" - " --version version information\n" - " --help display this help and exit\n" - " --raw output raw crc\n" - " --tag default output style\n" -}; +static const char usage[] = {"Usage: cksum [Option]... [File]...\n" + " --version version information\n" + " --help display this help and exit\n" + " --raw output raw crc\n" + " --tag default output style\n"}; -//flags +// flags static int output_format = 't'; static struct option long_options[] = { - {"version", no_argument, NULL, 'v'}, - {"help", no_argument, NULL, 'h'}, - {"raw", no_argument, NULL, 'r'}, - {"tag", no_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {"help", no_argument, NULL, 'h'}, + {"raw", no_argument, NULL, 'r'}, + {"tag", no_argument, NULL, 't'}, }; - static const uint32_t crc32_ = 0x04c11db7; uint32_t crc32(FILE *file, size_t *octets) { @@ -84,25 +81,27 @@ uint32_t crc32(FILE *file, size_t *octets) { return ~((remainder << 8) | (difference & 0xff)); } -void crc_print(char *filename, uint32_t crc, size_t octets){ +void crc_print(char *filename, uint32_t crc, size_t octets) { printf("%c\n", output_format); - switch(output_format){ - case 't': - if(filename == NULL) - printf("%u %lu\n", crc, octets); - else - printf("%u %lu %s\n", crc, octets, filename); - break; - case 'r': - printf("%c%c%c%c", (crc >> 24) & 255, (crc >> 16) & 255, (crc >> 8) & 255, crc & 255); - break; + switch (output_format) { + case 't': + if (filename == NULL) + printf("%u %lu\n", crc, octets); + else + printf("%u %lu %s\n", crc, octets, filename); + break; + case 'r': + printf("%c%c%c%c", (crc >> 24) & 255, (crc >> 16) & 255, (crc >> 8) & 255, + crc & 255); + break; } } int main(int argc, char **argv) { int opt, option_index; - while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) != -1) { + while ((opt = getopt_long(argc, argv, "", long_options, &option_index)) != + -1) { switch (opt) { case 0: break; @@ -135,7 +134,7 @@ int main(int argc, char **argv) { for (; index < argc; index++) { FILE *file = fopen(argv[index], "r+"); - if (file == NULL){ + if (file == NULL) { fprintf(stderr, "cksum: %s: %s\n", argv[index], strerror(errno)); continue; } From 3bb50ffb5b72a0ca00846972e0ed90324a23b349 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Wed, 24 Apr 2024 01:40:11 +0200 Subject: [PATCH 36/42] Migrate ls command to getopt --- src/ls/ls_main.c | 71 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/src/ls/ls_main.c b/src/ls/ls_main.c index d50813a..e28cdff 100644 --- a/src/ls/ls_main.c +++ b/src/ls/ls_main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,21 +9,65 @@ #define VERSION "1.0.0" #define AUTHOR "Yohann Boniface (Sigmanificient)" +#include "cgetopt.h" #include "version_info.h" -static const char FLAGLIST[] = "alRdrt"; static char DEFAULT_LOCATION[] = "."; +enum { + GETOPT_VERSION_CHAR = (CHAR_MIN - 1), + GETOPT_SORT_CHAR = (CHAR_MIN - 2), +}; + +static const struct option LONG_OPTIONS[] = { + {"version", no_argument, NULL, GETOPT_VERSION_CHAR}, + {"all", no_argument, NULL, 'a'}, + {"recursive", no_argument, NULL, 'R'}, + {"directory", no_argument, NULL, 'd'}, + {"reverse", no_argument, NULL, 'r'}, + {"sort", required_argument, NULL, GETOPT_SORT_CHAR}, + {0}}; + static char compose_flaglist(int argc, char **argv) { - int flags = 0; + char flags = 0; + int oi = -1; - for (int i = 1; i < argc; i++) { - if (argv[i][0] != '-' || argv[i][1] == '\0') - continue; - for (int j = 1; argv[i][j] != '\0'; j++) - flags |= 1 << (stridx(FLAGLIST, argv[i][j]) + 1); + for (;;) { + int c = getopt_long(argc, argv, "alRdrt", LONG_OPTIONS, &oi); + + if (c == -1) + return flags; + switch (c) { + case 'a': + flags |= F_ALL_FILES; + break; + case 'l': + flags |= F_LONG_FORM; + break; + case 'R': + flags |= F_RECURSIVE; + break; + case 'd': + flags |= F_DIRECTORY; + break; + case 'r': + flags |= F_REV_ORDER; + break; + case 't': + flags |= F_SORT_TIME; + break; + case (CHAR_MAX + 1): + if (!strcmp(optarg, "time")) + flags |= F_SORT_TIME; + break; + case GETOPT_VERSION_CHAR: + print_version(); + return -1; + default: + fprintf(stderr, "ls: invalid option -- '%c'\n", c); + return -2; + } } - return (char)(flags >> 1); } static size_t count_targets(int argc, char **argv) { @@ -55,15 +100,11 @@ static bool list_dirs(dirbuff_t *db, int argc, char **argv, char flags) { int main(int argc, char **argv) { dirbuff_t db = {.size = MIN_ALLOCATED_ENTRY}; - char flags; + char flags = compose_flaglist(argc, argv); int err = 0; - for (int i = 0; argv[i] != NULL; i++) - if (!strcmp(argv[i], "--version")) { - print_version(); - return EXIT_SUCCESS; - } - flags = compose_flaglist(argc, argv); + if (flags < 0) + return (flags == -1) ? EXIT_SUCCESS : EXIT_FAILURE; db.entries = malloc(db.size * sizeof(*db.entries)); if (db.entries == NULL) return EXIT_FAILURE; From 6d604950a82ca45fbed34b733b9ea847c89e6a78 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 18:48:23 -0500 Subject: [PATCH 37/42] conformed to proh14 coding style --- src/cksum/cksum.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index 13ce0de..a21f96e 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -82,7 +82,6 @@ uint32_t crc32(FILE *file, size_t *octets) { } void crc_print(char *filename, uint32_t crc, size_t octets) { - printf("%c\n", output_format); switch (output_format) { case 't': if (filename == NULL) @@ -121,11 +120,14 @@ int main(int argc, char **argv) { } } - int index = optind; + argc -= optind; + argv += optind; + + int index = 0; size_t octets; uint32_t crc; - if (argc < 2) { + if (argc < 1) { crc = crc32(stdin, &octets); crc_print(NULL, crc, octets); return EXIT_SUCCESS; From 9a8541aa267adcaae6a5871453c9833642c02197 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 19:00:09 -0500 Subject: [PATCH 38/42] added cksum to utils.txt --- utils.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/utils.txt b/utils.txt index 2961c53..95719e4 100644 --- a/utils.txt +++ b/utils.txt @@ -22,3 +22,4 @@ pwd rm rmdir sleep +cksum From c18649c07cdee74c7a9e45c9753548a41e6cf6c4 Mon Sep 17 00:00:00 2001 From: cospplredman Date: Tue, 23 Apr 2024 19:00:24 -0500 Subject: [PATCH 39/42] static correctness --- src/cksum/cksum.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cksum/cksum.c b/src/cksum/cksum.c index a21f96e..a683940 100644 --- a/src/cksum/cksum.c +++ b/src/cksum/cksum.c @@ -29,7 +29,7 @@ static struct option long_options[] = { static const uint32_t crc32_ = 0x04c11db7; -uint32_t crc32(FILE *file, size_t *octets) { +static uint32_t crc32(FILE *file, size_t *octets) { uint32_t remainder = 0; uint8_t difference = 0; @@ -81,7 +81,7 @@ uint32_t crc32(FILE *file, size_t *octets) { return ~((remainder << 8) | (difference & 0xff)); } -void crc_print(char *filename, uint32_t crc, size_t octets) { +static void crc_print(char *filename, uint32_t crc, size_t octets) { switch (output_format) { case 't': if (filename == NULL) From f50ebba671e923513e01d739e8c23ae9832235ab Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Wed, 24 Apr 2024 02:13:01 +0200 Subject: [PATCH 40/42] Fix directory listing loop by offsetting argv properly --- src/ls/ls_main.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/ls/ls_main.c b/src/ls/ls_main.c index e28cdff..1229116 100644 --- a/src/ls/ls_main.c +++ b/src/ls/ls_main.c @@ -70,24 +70,17 @@ static char compose_flaglist(int argc, char **argv) { } } -static size_t count_targets(int argc, char **argv) { - int count = 0; - - for (int i = 1; i < argc; i++) - if (argv[i][0] != '-' || argv[i][1] == '\0') - count++; - return count; -} - static bool list_dirs(dirbuff_t *db, int argc, char **argv, char flags) { int err = 0; - size_t count = count_targets(argc, argv); + size_t count = argc - optind; if (count == 0) { db->name = DEFAULT_LOCATION; err |= list_dir(db, flags); } - for (int i = 1; i < argc; i++) { + argv += optind; + argc -= optind; + for (int i = 0; i < argc; i++) { if (argv[i][0] == '-' && argv[i][1] != '\0') continue; db->name = argv[i]; From 752e6b7de6f07d7cb85cc48f59745a4a2938b2af Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Wed, 24 Apr 2024 02:14:36 +0200 Subject: [PATCH 41/42] Improve getopt_long call --- src/ls/ls_main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ls/ls_main.c b/src/ls/ls_main.c index 1229116..aacf2b9 100644 --- a/src/ls/ls_main.c +++ b/src/ls/ls_main.c @@ -30,10 +30,9 @@ static const struct option LONG_OPTIONS[] = { static char compose_flaglist(int argc, char **argv) { char flags = 0; - int oi = -1; for (;;) { - int c = getopt_long(argc, argv, "alRdrt", LONG_OPTIONS, &oi); + int c = getopt_long(argc, argv, "alRdrt", LONG_OPTIONS, NULL); if (c == -1) return flags; @@ -49,6 +48,7 @@ static char compose_flaglist(int argc, char **argv) { break; case 'd': flags |= F_DIRECTORY; + flags &= ~F_RECURSIVE; break; case 'r': flags |= F_REV_ORDER; @@ -101,8 +101,6 @@ int main(int argc, char **argv) { db.entries = malloc(db.size * sizeof(*db.entries)); if (db.entries == NULL) return EXIT_FAILURE; - if (flags & F_DIRECTORY) - flags &= ~F_RECURSIVE; err |= !list_dirs(&db, argc, argv, flags); free(db.entries); return err ? EXIT_SUCCESS : EXIT_FAILURE; From 0f3f56effe2f010dc4c2fb048e03c3db25c3e148 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Wed, 24 Apr 2024 02:41:02 +0200 Subject: [PATCH 42/42] Use while c != -1 loop pattern --- src/ls/ls_main.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ls/ls_main.c b/src/ls/ls_main.c index aacf2b9..5b061ab 100644 --- a/src/ls/ls_main.c +++ b/src/ls/ls_main.c @@ -30,12 +30,9 @@ static const struct option LONG_OPTIONS[] = { static char compose_flaglist(int argc, char **argv) { char flags = 0; + int c; - for (;;) { - int c = getopt_long(argc, argv, "alRdrt", LONG_OPTIONS, NULL); - - if (c == -1) - return flags; + while ((c = getopt_long(argc, argv, "alRdrt", LONG_OPTIONS, NULL)) != -1) { switch (c) { case 'a': flags |= F_ALL_FILES; @@ -68,6 +65,7 @@ static char compose_flaglist(int argc, char **argv) { return -2; } } + return flags; } static bool list_dirs(dirbuff_t *db, int argc, char **argv, char flags) {