From ee02eaae4a4b5647db551e0a17cccfffb646c206 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Mon, 30 Oct 2023 12:52:34 -0700 Subject: [PATCH] Make 1991/dds C generated code use fgets() This took less time than the previous fix (to let it work with clang) because after stupidly doing the decryption/encryption manually I had decided to write a simple program that does it instead. Now systems won't whine about gets() being unsafe. The thanks file further documents how this was done and the bugs.md file no longer has this entry. --- 1991/dds/README.md | 11 --------- 1991/dds/dds.c | 19 ++++++++------- bugs.md | 12 ---------- thanks-for-fixes.md | 57 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/1991/dds/README.md b/1991/dds/README.md index 311e8ba5f..78c7623aa 100644 --- a/1991/dds/README.md +++ b/1991/dds/README.md @@ -13,17 +13,6 @@ make all ``` -### Bugs and (Mis)features - -The current status of this entry is: - -``` -STATUS: INABIAF - please **DO NOT** fix -STATUS: uses gets() - change to fgets() if possible -``` - -For more detailed information see [1991 dds in bugs.md](/bugs.md#1991-dds). - ## To use: diff --git a/1991/dds/dds.c b/1991/dds/dds.c index f05407a6e..b313abf71 100644 --- a/1991/dds/dds.c +++ b/1991/dds/dds.c @@ -7,12 +7,13 @@ o)*|gps)<<*txjudi)m*|aQdbtf!::::;sfuvsom,2aP4HC%SD12N1\nJNQm>s\\..q^aHC%NHb%GN1!D32P3%RN1UP1D12JPQUaP1H\ R%PN4\nQaP2Q,2aP4Hb%OD12D12N2!N3\nJVP3Q,,n\ \\(aP3Q(^*m>g\\(aP3Q(^::::aHC%VP3Q>bupj)hfut)c**aHb%JD12JON1!Qjg)a%LN1UP1D12JIQUa\ -P1HL%IQ*m>aN2!N2\nP2QP2Q>aN2\nP2Hbdd!.Xop.fssps!.Xop.sfu\ -vso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo!b/d";int k;char R[4][99] -;main(c,v)char**v;{char*p,*r,*q;for(q=s;*q;q++)*q>' '&&(*q)--;{FILE*i=fopen(v -[1],"r"),*o=fopen(q-3,"w");if(!i||!o)return 1;for(p=s;;p++)switch(*p++){B'M': -Q(k=fgetc(i))!=EOF&&k!=*p)*r++=k;if(k==EOF){fputs("}}\n",o);fclose(o);return -system(q-69);}*r=0 B'P':while(*p!='`')fputc(*p++,o)B'O':Q*r)fputc(*r++,o);p-- -B'C':k=0;Q k<*p-'0')(*r++=fgetc(i),k++);*r=0 B'I':k= *p;if(**R==k)goto G B'G' -:k= *p;G:p=s;while(*p!='$'||p[1]!= k)p++;p++B'N':R[*p-'0'][0]++;}}} +aP1Q*aHb%FN1\nQm>::::aHC%VP3Q>bupj)ghfut)c-::-tuejo**aHb%JD12JON1!Qjg)a%LN1U\ +P1D12JIQUaP1HL%IQ*m>aN2!N2\nP2QP2Q>aN2\nP2Hbdd!.Xop.fssp\ +s!.Xop.sfuvso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo!.jodmvef!tuejp/i!b/d"; +int k;char R[4][99];main(c,v)char**v;{char*p,*r,*q;for(q=s;*q;q++)*q>' '&&(* +q)--;{FILE*i=fopen(v[1],"r"),*o=fopen(q-3,"w");if(!i||!o)return 1;for(p=s;;p +++)switch(*p++){B'M':Q(k=fgetc(i))!=EOF&&k!=*p)*r++=k;if(k==EOF){fputs("}}\n" +,o);fclose(o);return system(q-86);}*r=0 B'P':while(*p!='`')fputc(*p++,o)B'O': +Q*r)fputc(*r++,o);p--B'C':k=0;Q k<*p-'0')(*r++=fgetc(i),k++);*r=0 B'I':k= *p; +if(**R==k)goto G B'G':k= *p;G:p=s;while(*p!='$'||p[1]!= k)p++;p++B'N':R[*p-'0' +][0]++;}}} diff --git a/bugs.md b/bugs.md index 39ac43dfe..d83c01ed7 100644 --- a/bugs.md +++ b/bugs.md @@ -604,18 +604,6 @@ where this occurred was fixed but this one should not be fixed. Thank you. ## 1991 dds -### STATUS: uses gets() - change to fgets() if possible -### Source code: [1991/dds/dds.c](1991/dds/dds.c) -### Information: [1991/dds/README.md](1991/dds/README.md) - -The compiled code uses `gets()` not `fgets()`. Can you fix this? It's quite -complicated to do: the `s` array is certainly relevant and you can see a bit of -the magic in the [thanks](/thanks-for-fixes.md) and the README.md file for how -it works. It's easy enough to get the code to refer to `fgets()` and call it -correctly but it might take more work to get the generated code sorted. This -will be looked at later. - - ## 1991 westley ### STATUS: INABIAF - please **DO NOT** fix diff --git a/thanks-for-fixes.md b/thanks-for-fixes.md index 6a3fbed22..814fbedbb 100644 --- a/thanks-for-fixes.md +++ b/thanks-for-fixes.md @@ -1044,12 +1044,17 @@ slightly more like the original, even though it's unused. Cody fixed a segfault that prevented this entry from working in any condition and he also made it work for clang. He also added checks for NULL `FILE *`s. +Furthermore he changed it so that the C from the BASIC uses `fgets()`, not +`gets()`. For the magic of clang and `fgets()` see below. Clang (at least in some systems?) defaults to having `-Werror` and the code that the entry generates had some warnings that were causing compilation to fail as it just ran `cc a.c`. It ran it by what was once `system(q-6);` but with clang this was not enough. +(Cody stupidly did the below manually until he thought to write a simple program +to do the conversions which he used for `gets()` to `fgets()`.) + The following had to be added to the string `s` (which to fix the segfault was changed from `char*s` to `char s[]`): @@ -1076,10 +1081,58 @@ string. Thus the end of the string actually looks like: !.Xop.fssps!.Xop.sfuvso.uzqf!.Xop.jnqmjdju.gvodujpo.efdmbsbujpo!b/d ``` -With these changes in place it will compile and work with both gcc and clang. +But then there is the matter of getting the C to use `fgets()`. As can be seen +above it's not as simple as changing `gets()` to `fgets()`. This was more magic +characters that had to be updated and some added. The C code: + +```c +atoi(gets(b)) +``` + +is in the string: + +``` +bupj)hfut)c** +``` + +which was changed to be (in C): + +```c +atoi(fgets(b,99,stdin)) +``` + +which in the program is: + +``` +bupj)ghfut)c-::-tuejo** +``` + +That's fine and well but since the code does not include `stdio.h` there +obviously would be a compilation error. Thus the compiler line had to updated to +have `-include stdio.h` which in this rotated string is: + +``` +.jodmvef!tuejp/i +``` + +which had to be added after: + +``` +efdmbsbujpo! +``` + +which is the end of the warning disabled for clang as described above. + +But now the `system(q-69);` had to be changed to `system(q-86);`. + +With these changes in place it will compile and work with both gcc and clang and +the C code generated will use `fgets()`, not `gets()`, therefore removing the +annoying warnings. Note that the array passed to `fgets()` is an int but that +was the same for `gets()` and is not necessary to update to a `char[]`. + The key to the string is that it rotates the character by `+1`. This was not immediately clear until reading the author's remarks so there was an alt version -that was something of a kludge, running `make a` instead. +that was something of a kludge, running `make a` instead but that was removed. Cody also fixed a typo in LANDER.BAS.