summaryrefslogtreecommitdiffstats
path: root/bin/sh
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2015-12-27 17:53:39 +0000
committerjilles <jilles@FreeBSD.org>2015-12-27 17:53:39 +0000
commit8eba6000110bc8f94779df2e6697239d645d3986 (patch)
treeb28fe74675c84d3dcc1ed58a7e0d91bdb7c5394c /bin/sh
parent14a25019d63edd9c451e5dfd43607d28b042d919 (diff)
downloadFreeBSD-src-8eba6000110bc8f94779df2e6697239d645d3986.zip
FreeBSD-src-8eba6000110bc8f94779df2e6697239d645d3986.tar.gz
MFC r292517: sh/mknodes: Close files and check for errors when writing.
This is a build tool only and does not affect run time. PR: 204951
Diffstat (limited to 'bin/sh')
-rw-r--r--bin/sh/mknodes.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/bin/sh/mknodes.c b/bin/sh/mknodes.c
index 1a177f8..1fe8bba 100644
--- a/bin/sh/mknodes.c
+++ b/bin/sh/mknodes.c
@@ -89,7 +89,6 @@ static struct str *nodestr[MAXTYPES]; /* type of structure used by the node */
static int nstr; /* number of structures */
static struct str str[MAXTYPES]; /* the structures */
static struct str *curstr; /* current structure */
-static FILE *infp;
static char line[1024];
static int linno;
static char *linep;
@@ -102,7 +101,7 @@ static void outfunc(FILE *, int);
static void indent(int, FILE *);
static int nextfield(char *);
static void skipbl(void);
-static int readline(void);
+static int readline(FILE *);
static void error(const char *, ...) __printf0like(1, 2) __dead2;
static char *savestr(const char *);
@@ -110,17 +109,19 @@ static char *savestr(const char *);
int
main(int argc, char *argv[])
{
+ FILE *infp;
+
if (argc != 3)
error("usage: mknodes file");
- infp = stdin;
if ((infp = fopen(argv[1], "r")) == NULL)
error("Can't open %s: %s", argv[1], strerror(errno));
- while (readline()) {
+ while (readline(infp)) {
if (line[0] == ' ' || line[0] == '\t')
parsefield();
else if (line[0] != '\0')
parsenode();
}
+ fclose(infp);
output(argv[2]);
exit(0);
}
@@ -253,6 +254,10 @@ output(char *file)
fputs("union node *getfuncnode(struct funcdef *);\n", hfile);
fputs("void reffunc(struct funcdef *);\n", hfile);
fputs("void unreffunc(struct funcdef *);\n", hfile);
+ if (ferror(hfile))
+ error("Can't write to nodes.h");
+ if (fclose(hfile))
+ error("Can't close nodes.h");
fputs(writer, cfile);
while (fgets(line, sizeof line, patfile) != NULL) {
@@ -266,6 +271,11 @@ output(char *file)
else
fputs(line, cfile);
}
+ fclose(patfile);
+ if (ferror(cfile))
+ error("Can't write to nodes.c");
+ if (fclose(cfile))
+ error("Can't close nodes.c");
}
@@ -401,7 +411,7 @@ skipbl(void)
static int
-readline(void)
+readline(FILE *infp)
{
char *p;
OpenPOWER on IntegriCloud