sim: m32c: switch from custom fgets to getline

No need to implement this ourselves when POSIX has a nice API.
This commit is contained in:
Mike Frysinger 2021-05-07 00:08:43 -04:00
parent 8e78e9b995
commit a588403597
5 changed files with 17 additions and 105 deletions

View File

@ -1,3 +1,10 @@
2021-05-07 Mike Frysinger <vapier@gentoo.org>
* 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 <vapier@gentoo.org>
* gdb-if.c: Include libiberty.h.

View File

@ -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

View File

@ -24,8 +24,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <ctype.h>
#include <stdlib.h>
#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;

View File

@ -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 <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#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;
}

View File

@ -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 <http://www.gnu.org/licenses/>. */
#ifndef _safe_gets_h_
#define _safe_gets_h_
char *safe_fgets (FILE * f);
#endif