From 99103794f28fafef37a96e283514756b4f754205 Mon Sep 17 00:00:00 2001 From: superturbocat2001 Date: Sat, 12 Dec 2020 20:09:30 +0000 Subject: [PATCH] - Added simple function "getopt" in stdlib (tcc) git-svn-id: svn://kolibrios.org@8380 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/ktcc/trunk/bin/lib/libck.a | Bin 133256 -> 135006 bytes .../develop/ktcc/trunk/libc/include/getopt.h | 9 +++ .../develop/ktcc/trunk/libc/stdlib/getopt.c | 63 ++++++++++++++++++ programs/develop/ktcc/trunk/samples/Makefile | 1 + .../develop/ktcc/trunk/samples/build_all.sh | 1 + .../develop/ktcc/trunk/samples/getopt_ex.c | 38 +++++++++++ 6 files changed, 112 insertions(+) create mode 100644 programs/develop/ktcc/trunk/libc/include/getopt.h create mode 100644 programs/develop/ktcc/trunk/libc/stdlib/getopt.c create mode 100644 programs/develop/ktcc/trunk/samples/getopt_ex.c diff --git a/programs/develop/ktcc/trunk/bin/lib/libck.a b/programs/develop/ktcc/trunk/bin/lib/libck.a index 15bd4554f857cc866a7c9b81efcfc7f2f0ab0061..390dc8cb257dbb2775150fd9b7bc889975798727 100644 GIT binary patch delta 2683 zcmcJR4@{JG7{|ZQi+3CXJA?%n5*9GBLj*~L&?#&MoF}Ai_c`#d~?hr(w{?jRE!z z76E5?a(DC1QD7_uICs*In_~hQ=c4DM3??Rl1*k!cGx@+QPv|y)xe5?n2Q4WAnLhzp zxE)>siW~<;MS!B4K@U}d5}csKlc2|JpyVT<)D+OlMv(miD4la;a9q|XXdU}=GY!^z zK07F+ABbO*3`kp135McI(`t;-5dsveh#zZlTqfwd`|8~vi!qT2$R z{y~j#>^HjBAxpzeSR2LQsU74s>#3~1Q7t3+Mmi(NwcYa6n6+)A^z)4!nN(ccBWT1$ zdct&TQ<5*(@5`T8Z3B;3Fg=+%Hb}Hw&>TNg@A_uS*;T!Czrrzvdcr~%6<)r@;~GXI z^pzC&()m{Mpp_pEtZb}VrNGVcRWSByv95K9njLf2A;(;y%Ms{wN!Gy^EGFI^j|1$R5WZT zA|WMHeArbO9lW6MYyTQ?uXZr5~_F=#g05U28=UE^#A6PpI--5u;0+0dFl6~N^ zLNK`qOlbgZ6X1#}aHR&Om4o&H&>`PQmprEj%#{6g3k^9>Ks{Fid9~oCQgE{*6!u1J zmv~sQG@$H}CgPPX2g`SX6>Z@DDzH+Tig(};I1PQe;Eb`FYOqcU*2|5LHiM1QN<@(ZCH`74H-x=%nFgP21wgYPaZolDP1sLtY2T*y~5$mxFR7N(=N}tOi zKI_GlU@@pXUtoA~1AMjq-;7Rz%4?gl8yxe4Z*~|$GB|H*z;|-1_a)%C6!^FWoRAs$ zoMD)hvtMPfzRM8)=zxY)Xmml-CZU;1pqZ~ho1f}yHMg358xn0vT6%iRUaO<7$=cUs N<@l<;HCt)-{{s4}aN_^~ diff --git a/programs/develop/ktcc/trunk/libc/include/getopt.h b/programs/develop/ktcc/trunk/libc/include/getopt.h new file mode 100644 index 0000000000..d41eaa0568 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/include/getopt.h @@ -0,0 +1,9 @@ +#ifndef GETOPT_H +#define GETOPT_H + +extern int optind, opterr; +extern char *optarg; + +int getopt(int argc, char *argv[], char *optstring); + +#endif diff --git a/programs/develop/ktcc/trunk/libc/stdlib/getopt.c b/programs/develop/ktcc/trunk/libc/stdlib/getopt.c new file mode 100644 index 0000000000..6286d05032 --- /dev/null +++ b/programs/develop/ktcc/trunk/libc/stdlib/getopt.c @@ -0,0 +1,63 @@ +#include +#include +#include + +char *optarg = NULL; // global argument pointer +int optind = 0; // global argv index + +int getopt(int argc, char *argv[], char *optstring) { + static char *next = NULL; + char c; + char *cp = NULL; + + if (optind == 0) + next = NULL; + + optarg = NULL; + + if (next == NULL || *next == '\0') { + if (optind == 0) + optind++; + + if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') { + optarg = NULL; + if (optind < argc) + optarg = argv[optind]; + return EOF; + } + + if (strncmp(argv[optind], "--", 2) == 0) { + optind++; + optarg = NULL; + if (optind < argc) + optarg = argv[optind]; + return EOF; + } + + next = argv[optind]; + next++; // skip past - + optind++; + } + + c = *next++; + cp = strchr(optstring, c); + + if (cp == NULL || c == ':') + return '?'; + + cp++; + if (*cp == ':') { + if (*next != '\0') { + optarg = next; + next = NULL; + } + else if (optind < argc) { + optarg = argv[optind]; + optind++; + } + else { + return '?'; + } + } + return c; +} diff --git a/programs/develop/ktcc/trunk/samples/Makefile b/programs/develop/ktcc/trunk/samples/Makefile index 1fea672d73..3dce03ceb1 100755 --- a/programs/develop/ktcc/trunk/samples/Makefile +++ b/programs/develop/ktcc/trunk/samples/Makefile @@ -14,6 +14,7 @@ all: ../bin/kos32-tcc dir_example.c -lck -o dir_example.kex -I ../libc/include ../bin/kos32-tcc net/tcpsrv_demo.c -lck -o net/tcpsrv_demo.kex -I ../libc/include ../bin/kos32-tcc net/nslookup.c -lck -lnetwork -o net/nslookup.kex -I ../libc/include + ../bin/kos32-tcc getopt_ex.c -lck -o getopt_ex -I ../libc/include clean: rm *.kex diff --git a/programs/develop/ktcc/trunk/samples/build_all.sh b/programs/develop/ktcc/trunk/samples/build_all.sh index 3a2ab0cd26..02ac8a07d5 100644 --- a/programs/develop/ktcc/trunk/samples/build_all.sh +++ b/programs/develop/ktcc/trunk/samples/build_all.sh @@ -14,4 +14,5 @@ ../tcc dir_example.c -lck -o /tmp0/1/dir_example ../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo ../tcc net/nslookup.c -lck -lnetwork -o /tmp0/1/nslookup +../tcc getopt_ex.c -lck -o /tmp0/1/getopt_ex exit diff --git a/programs/develop/ktcc/trunk/samples/getopt_ex.c b/programs/develop/ktcc/trunk/samples/getopt_ex.c new file mode 100644 index 0000000000..98b9cc1db8 --- /dev/null +++ b/programs/develop/ktcc/trunk/samples/getopt_ex.c @@ -0,0 +1,38 @@ +#include +#include +#include + +void main(int argc, char *argv[]) { + int c; + if(argc<2) + { + puts("Usage: getopt_ex [options]\n"); + puts("-a Show 'Option a'"); + puts("-B Show 'Option B'"); + puts("-n [num] Show 'num'"); + } + while ((c = getopt(argc, argv, "aBn:")) != EOF) { + switch (c) { + case 'a': + puts("Option 'a'"); + break; + + case 'B': + puts("Option 'B'"); + break; + + case 'n': + printf("Option n: value=%d\n", atoi(optarg)); + break; + + case '?': + printf("ERROR: illegal option %s\n", argv[optind-1]); + exit(0); + + default: + printf("WARNING: no handler for option %c\n", c); + exit(0); + } + } + exit(0); + }