From aa6f850b8d2308b99bc13776e6e7937aacb5d711 Mon Sep 17 00:00:00 2001 From: Colin Leroy-Mira Date: Thu, 11 Jan 2024 19:51:17 +0100 Subject: [PATCH] Rewrite gets in assembler +19 bytes if used alone, because it pulls in fgets, but as code is factorized, -128 bytes in programs using both fgets and gets. --- libsrc/common/gets.c | 63 -------------------------------------------- libsrc/common/gets.s | 47 +++++++++++++++++++++++++++++++++ test/ref/test_gets.c | 46 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 63 deletions(-) delete mode 100644 libsrc/common/gets.c create mode 100644 libsrc/common/gets.s create mode 100644 test/ref/test_gets.c diff --git a/libsrc/common/gets.c b/libsrc/common/gets.c deleted file mode 100644 index 2936c70de..000000000 --- a/libsrc/common/gets.c +++ /dev/null @@ -1,63 +0,0 @@ -/* -** gets.c -** -** Ullrich von Bassewitz, 11.08.1998 -*/ - - - -#include -#include "_file.h" - - - -/*****************************************************************************/ -/* Code */ -/*****************************************************************************/ - - - -char* __fastcall__ gets (char* s) -{ - register char* p = s; - int c; - unsigned i = 0; - - while (1) { - - /* Get next character */ - if ((c = fgetc (stdin)) == EOF) { - /* Error or EOF */ - *p = '\0'; - if (stdin->f_flags & _FERROR) { - /* ERROR */ - return 0; - } else { - /* EOF */ - if (i) { - return s; - } else { - return 0; - } - } - } - - /* One char more. Newline ends the input */ - if ((char) c == '\n') { - *p = '\0'; - break; - } else { - *p = c; - ++p; - ++i; - } - - } - - /* Done */ - return s; -} - - - - diff --git a/libsrc/common/gets.s b/libsrc/common/gets.s new file mode 100644 index 000000000..dfaf2def3 --- /dev/null +++ b/libsrc/common/gets.s @@ -0,0 +1,47 @@ +; +; Colin Leroy-Mira, 2024 +; +; char* __fastcall__ gets (char* s) +; + + .export _gets + .import _fgets, _stdin, popax, pushax + .importzp ptr4 + +_gets: + ; Push buffer + sta ptr4 + stx ptr4+1 + jsr pushax + + ; Push size (there's no limit!) + lda #$FF + tax + jsr pushax + + lda _stdin + ldx _stdin+1 + + jsr _fgets + + ; Check return value + bne :+ + cpx #$00 + bne :+ + rts + +: ; At least one byte written. + jsr pushax ; Store returned pointer + + ; Remove \n if there is one. + lda ptr4 ; _fgets returns with ptr4 at + bne :+ ; end of buffer + dec ptr4+1 +: dec ptr4 + lda (ptr4),y ; _fgets returns with Y=0 + cmp #$0A + bne :+ + tya + sta (ptr4),y ; Set terminator over \n + +: jmp popax diff --git a/test/ref/test_gets.c b/test/ref/test_gets.c new file mode 100644 index 000000000..ee5b6fd58 --- /dev/null +++ b/test/ref/test_gets.c @@ -0,0 +1,46 @@ +/* + !!DESCRIPTION!! gets test + !!LICENCE!! Public domain +*/ + +#include "common.h" + +#include +#include +#include +#include +#include + +char buf[512]; + +#define INFILE "cf.in" + +#ifndef __CC65__ +/* Force declaration on host compiler, as gets() is deprecated for + * being dangerous as hell */ +char *gets (char *__s); +#endif + +#ifdef NO_OLD_FUNC_DECL +int main(int argc,char **argv) +#else +main(argc, argv) +int argc; +char *argv[]; +#endif +{ + /* Fake stdin with the reference file */ + fclose(stdin); + stdin = fopen(INFILE, "r"); + if (stdin == NULL) { + return EXIT_FAILURE; + } + + while (gets(buf) != NULL) + { + printf("%s",buf); + } + + fclose(stdin); + return 0; +}