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().
This commit is contained in:
Cody Boone Ferguson committed 2023-03-21 07:33:00 -07:00
1 parent 6a6807e587
commit e317048e21
3 files changed
+15 -13

No files matched your search

+13 -10
View File
@@ -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:
+1 -1
View File
@@ -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:
+1 -2
View File
@@ -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: