Make alt code for gets() / fgets() changes

As both Landon and I have been wondering if changing entries to use
fgets() is bordering on tampering with entries I decided to make the
original (sometimes bug fixed) versions (using gets()) alt code. I had
originally started to make the fgets() version of each entry the alt but
since the fgets() versions can handle input better and since some also
fixed bugs (see below) I decided to make the original version the alt
code (this also is how it is for other entries).

There were some exceptions here. If the change also fixed some crashes
or other bugs outside of the gets() to fgets() conversion I did not
bother to make an alt version. An example is 1992/dds. In some cases the
alt code does have bug fixes in which case they were added.

Updated the Makefiles and .gitignore files as well for the alt target
and the new files (compiled and in the case of 1992/adrian additional
source files in the Makefile alt rule).

This should solve the problem observed by Landon and me. It wouldn't be
a problem to change it so the alt version is the one that uses fgets()
but I think that might be the wrong choice.

In some entries I altogether avoided changing to fgets() say if the
author notes it uses gets() (in one case even providing a version that
uses fgets()). In some cases using fgets() introduces a bug so those are
also not changed.

As for 1990/tbr there's a strong reason for not having the fgets()
version being the alt. In macOS in particular (in linux - or maybe gcc,
not sure - it shows a warning at linker time) it will mess up the first
line of output in a potentially confusing way. For instance:

    $ ./tbr.alt
    $ warning: this program uses gets(), which is unsafe.
    # nothing here, what to do?

And that touches on another issue of making the alt version the one that
uses gets(): the warnings that can be alarming during compilation,
linking and (in the case of macOS) runtime.

Not all entries should be or can be changed to fgets() but some can and
it's warranted for these reasons. More will possibly happen in the
future.
This commit is contained in:
Cody Boone Ferguson committed 2023-04-05 05:22:35 -07:00
1 parent 843ce60b47
commit 06a5665f5d
31 files changed
+672 -86

No files matched your search

+11
View File
@@ -10,3 +10,14 @@ adsleep.c
adwc
adwc.c
adrian
adrian.alt
adbasename.alt
adbasename.alt.c
adecho.alt
adecho.alt.c
adhead.alt
adhead.alt.c
adsleep.alt
adsleep.alt.c
adwc.alt
adwc.alt.c
+21 -2
View File
@@ -118,8 +118,8 @@ OBJ= ${PROG}.o
DATA=
TARGET= ${PROG}
#
ALT_OBJ=
ALT_TARGET=
ALT_OBJ= ${PROG}.alt.o
ALT_TARGET= ${PROG}.alt
#################
@@ -160,6 +160,25 @@ adgrep: ${PROG}
# alternative executable
#
alt: data ${ALT_TARGET}
${RM} -f ad.alt.temp adwc.alt.c adbasename.alt.c adecho.alt.c adhead.alt.c adsleep.alt.c
${SED} '1s/\..*\./. wc ./' adrian.alt.c > ad.alt.temp
-./adrian.alt ad.alt.temp <ad.alt.temp >adwc.alt.c
${CC} ${CFLAGS} -o adwc.alt adwc.alt.c
${SED} '1s/\..*\./. basename ./' adrian.alt.c > ad.alt.temp
-./adrian.alt ad.alt.temp <ad.alt.temp >adbasename.alt.c
${CC} ${CFLAGS} -o adbasename.alt adbasename.alt.c
${SED} '1s/\..*\./. echo| ./' adrian.alt.c > ad.alt.temp
-./adrian.alt ad.alt.temp <ad.alt.temp >adecho.alt.c
${CC} ${CFLAGS} -o adecho.alt adecho.alt.c
${SED} '1s/\..*\./. head; ./' adrian.alt.c > ad.alt.temp
-./adrian.alt ad.alt.temp <ad.alt.temp >adhead.alt.c
${CC} ${CFLAGS} -o adhead.alt adhead.alt.c
${SED} '1s/\..*\./. sleep ./' adrian.alt.c > ad.alt.temp
-./adrian.alt ad.alt.temp <ad.alt.temp >adsleep.alt.c
${CC} ${CFLAGS} -o adsleep.alt adsleep.alt.c
${RM} -f ad.alt.temp
@${TRUE}
# data files
+25 -4
View File
@@ -8,7 +8,9 @@
## To build:
make all
```sh
make all
```
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) changed the location
@@ -23,15 +25,34 @@ macro definition! Thus the code now first trims off the last character of the
buffer read to get the same correct functionality but in a safe way. Thank you
Cody for your assistance!
For the original version [adrian.alt.c](adrian.alt.c) see the `Alternate code` section below.
## Try:
./adrian adrian.grep.try < README.md
```sh
./adrian adrian.grep.try < README.md
```
For the slow minded, try:
./adsleep 32767
```sh
./adsleep 32767
```
NOTE: if you do not specify an arg to the program it will segfault.
### Alternate code:
For the original version that uses `gets()` try:
```sh
make alt
```
Use `adrian.alt` as you would `adrian`.
## Judges' comments:
+113
View File
@@ -0,0 +1,113 @@
/* . wc . */
#include<stdio.h>/* Wht majr flwchrt? */
#include<string.h>/* That mjr flwchrt! */
#define P 257
#define G 17
#define z 8
#define v(jr) jr
int W ,head;
#define S(W,b,f) strncpy(W,b,f),W[f]=0\
char *wcs=" \t\n";
struct{ char X[z+1];
char f ;
int e ;
struct{ char g[z+1];
int b ;
} w[ G];
} o[ P];
int L=0,j= -28;
void E(int i, int m,char*c)
{
for(; i<43; i+=3)
putc("}|uutsrq`_^bji`[Zkediml[PO]a_M__]ISOYIRGTNR"[i]+i-9,stderr);
fprintf(stderr,"(%d): %s\n" ,m,c);
exit(1);
}
int N(int m, char *t)
{
int i ;
if (strlen (
t)>z) t[z ]=0;
for(i= 0; i< L ; i++) if(
!strcmp (o[ i] . X
, t))return i;
if( L== P)
E(0, m, t);
S (o[ L] . X , t
,z);
head; W = .1* head;
o[L ].f = !( strchr( t,']' )== 0 );
o[L ++ ] . e = 0;
return L -1 ; }
#define v(x )
int A(char *R)
{
int c=0, i;
while(* R) {
i = -1;
while(j){
if( ++ i==o[ c].e ) return 0;
if(o[
c] .w[i ] .g[0 ]
=='.' ||strchr (o[ c].w[i] .g ,* R)){
c=
o[ c ] .w[i].b; break;/*
main(int sl,char *j[]){
sleep (~~~~~~~~atoi(j[1])) ;/* . sl
sl s l . sl
l l ]
sl */ }
}
R++;
}
return o[ c].f;
}
main(int wc,char *V[]){char Y[999],*s;FILE*W;int m=0,echo,jr,q,wcl=0,wcw=0,wcc=0;
v(s = V[1]; if (*V=strrchr(s,'/'))s=*V+1; if(( !strncmp( s + (jr=strlen(s)) -
(q=strlen(V[2]) ),V[2],q))&&jr!=q) s[jr-q] = 0; puts(s); )
int e,p,C=0,Q ,basename;
W= fopen(wc>= 2 ? V[1] : "adgrep.c","rt");
echo| m^ e| 5| (int) .8| echo|
wc |C ==o[o[C] .e] .
e| e==+p,p; s[o[C] .e ]
;
while( fgets(Y,998,W)) { wcc += strlen(Y); m++;
if( s = strtok(Y,wcs)) wcw++;
else continue ;
C=
basename= j +j+*"* . basename"
+j+*"* r ] " + N(m,s) ;
while( s = strtok(0,wcs)) {
if( o[ C ] . e ==
G) E(1 ,m,o[C] .X) ;
S(o[C
] .w[o[C ] .e].g,s, z);
Q= C ;
if(! ( s =strtok ( 0 ,wcs )))wcw --
,E( 2 , m, o[C]
. w[ o[ Q ] .e] . g );
e
= o[C ] .w[o[C ] .e++ ] .b= N(m,s)
; wcw += 2; }
0&&
W && wcl++
< 10 && printf((W,Y)); }
if(j+28) { {
; } printf("%7u%7u%7u\n", wcl , wcw , wcc); }
while( gets(Y) ) if(A(Y)) puts(Y);
W, jr; }
O(int wc,char**V) {
--wc && (printf("%s ",*++V), main(wc,V), 1) || printf("\n"); }
+1 -1
View File
@@ -7,7 +7,7 @@
#define v(jr) jr
int W ,head;
#define S(W,b,f) strncpy(W,b,f),W[f]=0\
char *wcs=" \t\n";
struct{ char X[z+1];
char f ;
+6 -6
View File
@@ -17,12 +17,12 @@
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) changed the buffer size
in such a way that gets() should be safe (theoretically) as it comes from the
command line. Ideally `fgets()` would be used but this is a more problematic.
Previously it had a buffer size of 256 which could easily overflow. Now in this
entry `gets()` is used in a more complicated way. First `m` is set to `*++p` in
a for loop where `p` is argv and then later `m` is set to point to `h` which is
of size 256. `gets()` is called as `m = gets(m)`) but trying to change it to use
fgets() proved more a problem. Since the input must come from the command line
Cody changed the buffer size to `ARG_MAX+1` which should be enough (again
Previously it had a buffer size of 256 which could easily overflow. In this
entry `gets()` is used in a more complicated way: first `m` is set to `*++p` in
a for loop where `p` is argv. Later `m` is set to point to `h` which was of size
\256. `gets()` is called as `m = gets(m)`) but trying to change it to use fgets()
proved more a problem. Since the input must come from the command line Cody
changed the buffer size to `ARG_MAX+1` which should be enough (again
theoretically) especially since the command expects redirecting a dictionary
file as part of the command line. This also makes it possible for longer strings
to be read (in case the `gets()` was not used in a loop). Thank you Cody for
+2 -2
View File
@@ -112,8 +112,8 @@ OBJ= ${PROG}.o
DATA=
TARGET= ${PROG}
#
ALT_OBJ=
ALT_TARGET=
ALT_OBJ= ${PROG}.alt.o
ALT_TARGET= ${PROG}.alt
#################
+21 -5
View File
@@ -15,11 +15,27 @@
[Yusuke Endoh](/winners.html#Yusuke_Endoh) supplied a patch which makes this
work with gcc. Thank you Yusuke! [Cody Boone
Ferguson](/winners.html#Cody_Boone_Ferguson) made it use `fgets()` instead of
`gets()` and also added the `runme.sh` script to demonstrate it (using make was
problematic). Cody notes that unfortunately this does not work with clang due to
different compiler messages. Thank you Cody!
work with gcc. Due to how it works (see Judges' comments below) this will not
work with clang. Thank you Yusuke!
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) made it use `fgets()`
instead of `gets()` and also added the `runme.sh` script to demonstrate it
(using make was problematic). Cody notes that unfortunately this does not work
with clang due to different compiler messages. Thank you Cody!
For the original entry see [lush.alt.c](lush.alt.c) described in the `Alternate
code` section below.
### Alternate code:
The original source (fixed to work with gcc) which uses gets() try:
```sh
make alt
```
Use `lush.alt` as you would `lush`.
## Judges' comments:
+167
View File
@@ -0,0 +1,167 @@
#define f 000:
char
*s,*
t,v;
char
_ [
999]
,x [
999]
,*p=
x ;
char
*av[
]= {
"W",
"H",
"o",
"l",
"N",
"e",
" ",
"r",
"d",
".",
"m",
"s",
"g",
"!",
"*",
"\n"
} ;
int
n= 0
;int
__ =
0,i=
000;
main
() {
#define f 001:
#define F 042:
#define f 100:
_[i--
#define f 126:
#define f 132:
#define f 103:
#define f 202:
#define f 666:
#define f 77::
#define f 101:
]=0
#define f 232:
#define f 151:
;if(
#define f 256:
!i){
puts
("I"
"BM"
" S"
"UX"
);;;
;;;}
;for
( ;
gets
(_);
) {
for(
#define f 301:
t=_,
#define f 700:
#define f 999:
s=t,
#define f 77 :
#define f 300:
s=t;
#define f s=t;
#define f 123:
#define f 127:
*t&&
!(!(
*t <
'0'
||*t
>'9'
)&&!
((t[
1] ^
':')
&&(t
[1]^
'.')
)) ;
t++)
#define f 137:
#define f 133:
;if(
#define f 111:
#define f 220:
v^*t
&&*t
){v=
*t;;
n=n*
2+(1
&*t)
;__=
(__+
1)&3
; __
||((
n<15
&&(*
p= *
av[n
] ),
#define f 2+2:
p++)
,n=0
);}}
#define f ==4:
;*p=
'\0'
;;;;
#define f puts
puts
(x);
s=t;
#define f s=5;
s=t;
#define f s=5:
s=t;
#define f 445:
#define f 113:
#define f 900:
s=t;
#define f n22:
#define f n40:
s=t;
#define f nn6:
s=t;
#define f nn1:
#define f 0:--
s=t;
#define f 19:-
#define f x2:x
s=t;
#define f x1:x
s=t;
#define f y2:x
#define f y7:y
s=t;
#define f 1:::
s=t;
#define f @3:@
#define f 10:0
#define f x+1:
s=t;
#define f 2:::
#define f 3:::
#define x 7:::
#define y 3:::
#define z 990:
#define Z 47::
;;;}