From 6a6807e58750a279d1dad55ce9842bbfa6d946d0 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Tue, 21 Mar 2023 05:58:50 -0700 Subject: [PATCH 1/3] Make 1990/tbr alt version that uses fgets not gets This is so that the warning about gets() is not interspersed with the initial output of the program (see below). I do want to note that if one sends EOF it will segfault so to exit one should do ctrl-c (for instance) (exit never worked). I will look at fixing that fault later on to match the original code but this is good for now. When fixed (low priority though) I will consider making the gets() version the alt but probably not (I guess time will tell). As for the interspersed output it looks like: $ warning: this program uses gets(), which is unsafe. # cursor is here At the cursor I can either hit enter to get another prompt or I can input a command but this is not immediately obvious. Updated .gitignore to have tbr.alt. --- 1990/tbr/.gitignore | 1 + 1990/tbr/Makefile | 6 +++--- 1990/tbr/README.md | 19 ++++++++++++++++++- 1990/tbr/tbr.alt.c | 23 +++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 1990/tbr/tbr.alt.c diff --git a/1990/tbr/.gitignore b/1990/tbr/.gitignore index 1e7f77674..68e9c773f 100644 --- a/1990/tbr/.gitignore +++ b/1990/tbr/.gitignore @@ -1 +1,2 @@ tbr +tbr.alt diff --git a/1990/tbr/Makefile b/1990/tbr/Makefile index 2feb9dc3c..f6bdcc07a 100644 --- a/1990/tbr/Makefile +++ b/1990/tbr/Makefile @@ -98,7 +98,7 @@ CDEFINE= # Include files that are needed to compile # -CINCLUDE= +CINCLUDE= -include stdio.h #CINCLUDE= -include stdlib.h #CINCLUDE= -include stdio.h #CINCLUDE= -include stdlib.h -include stdio.h @@ -168,8 +168,8 @@ OBJ= ${PROG}.o DATA= TARGET= ${PROG} # -ALT_OBJ= -ALT_TARGET= +ALT_OBJ= ${PROG}.alt.o +ALT_TARGET= ${PROG}.alt ################# diff --git a/1990/tbr/README.md b/1990/tbr/README.md index 8fb645eb2..eab1c0f7b 100644 --- a/1990/tbr/README.md +++ b/1990/tbr/README.md @@ -22,11 +22,27 @@ modern computers; `exit(3)` returns void but the function was used in a binary expression so this would not even compile. Thank you Cody for your assistance! +## Alternative code: + +Cody also made a version that uses `fgets()` instead of `gets()` so one would +not get a warning that was interspersed with the output of the initial prompt +when running the program, to make it both safer and to have a better look. Thank +you Cody! + +To build try: + + make alt + +NOTE: it will segfault if you send EOF; to quit send an interrupt like ctrl-c. +Cody will look at this later. + ## To run: ./tbr # enter some shell commands +NOTE: globs do not work with this entry. Why? + ## Try: ./tbr @@ -37,6 +53,7 @@ expression so this would not even compile. Thank you Cody for your assistance! vi tbr.c # then figure out how to exit vi :-) +What happens if you try using `cd -` to return to previous directory? ## Judges' comments: @@ -64,7 +81,7 @@ of the program which we include below: -## Author's comments +## Author's comments: Rot13: diff --git a/1990/tbr/tbr.alt.c b/1990/tbr/tbr.alt.c new file mode 100644 index 000000000..f7bc55d40 --- /dev/null +++ b/1990/tbr/tbr.alt.c @@ -0,0 +1,23 @@ + +#define D ,close( + +char *c,q [512 ],m[ 256 +],*v[ 99], **u, *i[3];int f[2],p;main (){for + (m[m [60]= m[62 ]=32 ]=m[* m=124 [m]= 9]=6; + e(-8) ,(c=fgets (q,512,stdin),c[strlen(c)-1]='\0',1)||(exit (0),0);r(0,0) + )for( ;*++ c;); }r(t, o){ *i=i [2]= 0;for + (u=v +98 ;m[*--c] ^9;m [*c] &32 ?i[*c + &2]= *u,u- v^98 &&++u: + + 3 )if(!m[*c]){for(*++c=0;!m[*--c];); + * --u= ++c;}u-v^98?strcmp(*u,"cd")?*c?pipe(f),o=f[ + 1 ]: + 4 ,(p=fork())?e(p),o?r(o,0)D o)D*f): + 1 ,wait(0):(o?dup2(*f,0)D*f)D o):*i? + 5 D 0),e(open(*i,0)): + 9 ,t?dup2(t,1)D t):i[ + 2 ]? + 6 D 1),e(creat(i[2],438)): + 5 ,e(execvp(*u,u))):e(chdir(u[1])*2): + 3 ;}e(x){x<0?write(2,"?\n$ "-x/4,2),x+1||(exit(1),0): + 5 ;} From e317048e218bc183295aca119ba0a182d1d90990 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Tue, 21 Mar 2023 07:33:00 -0700 Subject: [PATCH 2/3] Fix segfault in tbr.alt.c, swap tbr.alt.c code The segfault has been fixed when EOF is sent to the program (causing fgets() to return NULL). Since fgets() is far safer than gets() and since the gets() version will intersperse the warning and the first line of output I have changed the alt version to be the original (fixed to compile with modern compilers) and the tbr is the one with fgets(). --- 1990/tbr/README.md | 23 +++++++++++++---------- 1990/tbr/tbr.alt.c | 2 +- 1990/tbr/tbr.c | 3 +-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/1990/tbr/README.md b/1990/tbr/README.md index eab1c0f7b..1c95708d3 100644 --- a/1990/tbr/README.md +++ b/1990/tbr/README.md @@ -19,22 +19,24 @@ [Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed this to work with modern computers; `exit(3)` returns void but the function was used in a binary -expression so this would not even compile. Thank you Cody for your assistance! +expression so this wouldn't even compile. Cody also changed the code to use +`fgets()` instead of `gets()` so one would not get a warning about the use of +`gets()` at execution which was interspersed with the output. Thank you Cody for +your assistance! +For the original version with `gets()` see the alternative code section below. ## Alternative code: -Cody also made a version that uses `fgets()` instead of `gets()` so one would -not get a warning that was interspersed with the output of the initial prompt -when running the program, to make it both safer and to have a better look. Thank -you Cody! - -To build try: +If you wish to see the original source (fixed so it can compile) that uses +`gets()` instead of `fgets()` you can look at [tbr.alt.c](tbr.alt.c). To compile +it: make alt -NOTE: it will segfault if you send EOF; to quit send an interrupt like ctrl-c. -Cody will look at this later. +Use `./tbr.alt` instead of `./tbr` for this version. You'll likely get a warning +about the usage of `gets()`. + ## To run: @@ -53,7 +55,8 @@ NOTE: globs do not work with this entry. Why? vi tbr.c # then figure out how to exit vi :-) -What happens if you try using `cd -` to return to previous directory? +What happens if you try using `cd -` to return to previous directory? What about +`cd` with no args? ## Judges' comments: diff --git a/1990/tbr/tbr.alt.c b/1990/tbr/tbr.alt.c index f7bc55d40..70c39ee19 100644 --- a/1990/tbr/tbr.alt.c +++ b/1990/tbr/tbr.alt.c @@ -4,7 +4,7 @@ char *c,q [512 ],m[ 256 ],*v[ 99], **u, *i[3];int f[2],p;main (){for (m[m [60]= m[62 ]=32 ]=m[* m=124 [m]= 9]=6; - e(-8) ,(c=fgets (q,512,stdin),c[strlen(c)-1]='\0',1)||(exit (0),0);r(0,0) + e(-8) ,gets (1+( c=q) )|| (exit (0),0); r(0,0) )for( ;*++ c;); }r(t, o){ *i=i [2]= 0;for (u=v +98 ;m[*--c] ^9;m [*c] &32 ?i[*c &2]= *u,u- v^98 &&++u: diff --git a/1990/tbr/tbr.c b/1990/tbr/tbr.c index 70c39ee19..54668f404 100644 --- a/1990/tbr/tbr.c +++ b/1990/tbr/tbr.c @@ -1,10 +1,9 @@ - #define D ,close( char *c,q [512 ],m[ 256 ],*v[ 99], **u, *i[3];int f[2],p;main (){for (m[m [60]= m[62 ]=32 ]=m[* m=124 [m]= 9]=6; - e(-8) ,gets (1+( c=q) )|| (exit (0),0); r(0,0) + e(-8) ,(c=fgets(q,512,stdin),(c==NULL &&(exit(0),1))||(c[strlen(c)-1]='\0'),1)||(exit(0),0);r(0,0) )for( ;*++ c;); }r(t, o){ *i=i [2]= 0;for (u=v +98 ;m[*--c] ^9;m [*c] &32 ?i[*c &2]= *u,u- v^98 &&++u: From 8a815e2f350e8850bae484d055a2a362f7c8a1da Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Tue, 21 Mar 2023 07:36:28 -0700 Subject: [PATCH 3/3] Typo fix in 1990/tbr/README.md --- 1990/tbr/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1990/tbr/README.md b/1990/tbr/README.md index 1c95708d3..ffd462153 100644 --- a/1990/tbr/README.md +++ b/1990/tbr/README.md @@ -18,7 +18,7 @@ make all [Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed this to work with -modern computers; `exit(3)` returns void but the function was used in a binary +modern compilers; `exit(3)` returns void but the function was used in a binary expression so this wouldn't even compile. Cody also changed the code to use `fgets()` instead of `gets()` so one would not get a warning about the use of `gets()` at execution which was interspersed with the output. Thank you Cody for