From: Mike Frysinger Date: Fri, 7 May 2021 04:08:43 +0000 (-0400) Subject: sim: m32c: switch from custom fgets to getline X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a5884035977f95b09c28d4bb2276ac4869930e96;p=binutils-gdb.git sim: m32c: switch from custom fgets to getline No need to implement this ourselves when POSIX has a nice API. --- diff --git a/sim/m32c/ChangeLog b/sim/m32c/ChangeLog index b60faf5e707..ad2bece00e2 100644 --- a/sim/m32c/ChangeLog +++ b/sim/m32c/ChangeLog @@ -1,3 +1,10 @@ +2021-05-07 Mike Frysinger + + * Makefile.in: Delete safe-fgets. + * opc2c.c: Delete safe-fgets.h include. + (main): Replace safe_fgets with getline. + * safe-fgets.c, safe-fgets.h: Removed. + 2021-05-05 Mike Frysinger * gdb-if.c: Include libiberty.h. diff --git a/sim/m32c/Makefile.in b/sim/m32c/Makefile.in index 05aa75309fa..0c101a835cf 100644 --- a/sim/m32c/Makefile.in +++ b/sim/m32c/Makefile.in @@ -56,14 +56,11 @@ m32c.c : m32c.opc opc2c $(OPC2C) -l m32c.out $(srcdir)/m32c.opc > m32c.c.tmp mv m32c.c.tmp m32c.c -opc2c : opc2c.o safe-fgets.o +opc2c : opc2c.o $(LINK_FOR_BUILD) $^ encodings: grep '/\* [01]' $(srcdir)/r8c.opc | sort -opc2c.o : opc2c.c safe-fgets.h +opc2c.o : opc2c.c $(COMPILE_FOR_BUILD) -c $(srcdir)/opc2c.c - -safe-fgets.o : safe-fgets.c safe-fgets.h - $(COMPILE_FOR_BUILD) -c $(srcdir)/safe-fgets.c diff --git a/sim/m32c/opc2c.c b/sim/m32c/opc2c.c index 64874279179..4c976449433 100644 --- a/sim/m32c/opc2c.c +++ b/sim/m32c/opc2c.c @@ -24,8 +24,6 @@ along with this program. If not, see . */ #include #include -#include "safe-fgets.h" - static int errors = 0; #define MAX_BYTES 10 @@ -504,10 +502,11 @@ log_indirect (Indirect * ind, int byte) int main (int argc, char **argv) { - char *line; + char *linebuf; FILE *in; int lineno = 0; int i; + size_t len; if (argc > 2 && strcmp (argv[1], "-l") == 0) { @@ -536,8 +535,12 @@ main (int argc, char **argv) opcodes = (opcode **) malloc (sizeof (opcode *)); op = &prefix_text; op->lineno = 1; - while ((line = safe_fgets (in)) != 0) + linebuf = NULL; + len = 0; + while (getline (&linebuf, &len, in) >= 0) { + char *line = linebuf; + lineno++; if (strncmp (line, " /** ", 6) == 0 && (isdigit (line[6]) || memcmp (line + 6, "VARY", 4) == 0)) @@ -629,6 +632,7 @@ main (int argc, char **argv) op->lines[op->nlines - 1] = strdup (line); } } + free (linebuf); { int i, j; diff --git a/sim/m32c/safe-fgets.c b/sim/m32c/safe-fgets.c deleted file mode 100644 index 3516fa80bd0..00000000000 --- a/sim/m32c/safe-fgets.c +++ /dev/null @@ -1,69 +0,0 @@ -/* safe-fgets.c --- like fgets, but allocates its own static buffer. - -Copyright (C) 2005-2021 Free Software Foundation, Inc. -Contributed by Red Hat, Inc. - -This file is part of the GNU simulators. - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . */ - - -#include -#include - -#include "safe-fgets.h" - -static char *line_buf = 0; -static int line_buf_size = 0; - -#define LBUFINCR 100 - -char * -safe_fgets (FILE * f) -{ - char *line_ptr; - - if (line_buf == 0) - { - line_buf = (char *) malloc (LBUFINCR); - line_buf_size = LBUFINCR; - } - - /* points to last byte */ - line_ptr = line_buf + line_buf_size - 1; - - /* so we can see if fgets put a 0 there */ - *line_ptr = 1; - if (fgets (line_buf, line_buf_size, f) == 0) - return 0; - - /* we filled the buffer? */ - while (line_ptr[0] == 0 && line_ptr[-1] != '\n') - { - /* Make the buffer bigger and read more of the line */ - line_buf_size += LBUFINCR; - line_buf = (char *) realloc (line_buf, line_buf_size); - - /* points to last byte again */ - line_ptr = line_buf + line_buf_size - 1; - /* so we can see if fgets put a 0 there */ - *line_ptr = 1; - - if (fgets (line_buf + line_buf_size - LBUFINCR - 1, LBUFINCR + 1, f) == - 0) - return 0; - } - - return line_buf; -} diff --git a/sim/m32c/safe-fgets.h b/sim/m32c/safe-fgets.h deleted file mode 100644 index 7c03f27e26a..00000000000 --- a/sim/m32c/safe-fgets.h +++ /dev/null @@ -1,27 +0,0 @@ -/* safe-fgets.h --- interface to safe version of fgets. - -Copyright (C) 2005-2021 Free Software Foundation, Inc. -Contributed by Red Hat, Inc. - -This file is part of the GNU simulators. - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . */ - - -#ifndef _safe_gets_h_ -#define _safe_gets_h_ - -char *safe_fgets (FILE * f); - -#endif