shithub: trueawk

Download patch

ref: 3b42cfaf73fbe63c1332f9cf2c8e4d90ab686f05
parent: 9804285af0866f90731a6e0ce767ab0e7b23b6c6
author: Arnold D. Robbins <arnold@skeeve.com>
date: Tue Oct 13 16:52:43 EDT 2020

Make it compile with g++.

--- a/FIXES
+++ b/FIXES
@@ -25,6 +25,10 @@
 This file lists all bug fixes, changes, etc., made since the AWK book
 was sent to the printers in August, 1987.
 
+October 13, 2020:
+	Add casts before all the calls to malloc/calloc/realloc in order
+	to get it to compile with g++. Thanks to Arnold Robbins.
+
 August 16, 2020:
 	Additional fixes for DJGPP. Thanks to Eli Zaretskii for
 	the testing.
--- a/README.md
+++ b/README.md
@@ -99,6 +99,9 @@
 This compiles without change on Macintosh OS X using `gcc` and
 the standard developer tools.
 
+You can also use `make CC=g++` to build with the GNU C++ compiler,
+should you choose to do so.
+
 The version of `malloc` that comes with some systems is sometimes
 astonishly slow.  If `awk` seems slow, you might try fixing that.
 More generally, turning on optimization can significantly improve
@@ -112,8 +115,9 @@
 is not at the top of our priority list.
 
 _If_ you (yes, you!) are interested in taking over active maintenance of
-`awk`, please open an issue to indicate that fact, give a little bit of
+`awk`, please open an issue to indicate that fact, and give us a little bit of
 your background and some idea of your plans and dreams.  Thanks!
 
 #### Last Updated
-Thu Jul  2 21:39:31 IDT 2020
+
+Tue Oct 13 20:00:09 IDT 2020
--- a/b.c
+++ b/b.c
@@ -83,7 +83,7 @@
 static int *
 intalloc(size_t n, const char *f)
 {
-	void *p = calloc(n, sizeof(int));
+	int *p = (int *) calloc(n, sizeof(int));
 	if (p == NULL)
 		overflo(f);
 	return p;
@@ -96,8 +96,8 @@
 		maxsetvec = MAXLIN;
 	else
 		maxsetvec *= 4;
-	setvec = realloc(setvec, maxsetvec * sizeof(*setvec));
-	tmpset = realloc(tmpset, maxsetvec * sizeof(*tmpset));
+	setvec = (int *) realloc(setvec, maxsetvec * sizeof(*setvec));
+	tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(*tmpset));
 	if (setvec == NULL || tmpset == NULL)
 		overflo(f);
 }
@@ -105,7 +105,9 @@
 static void
 resize_state(fa *f, int state)
 {
-	void *p;
+	unsigned int **p;
+	uschar *p2;
+	int **p3;
 	int i, new_count;
 
 	if (++state < f->state_count)
@@ -113,23 +115,23 @@
 
 	new_count = state + 10; /* needs to be tuned */
 
-	p = realloc(f->gototab, new_count * sizeof(f->gototab[0]));
+	p = (unsigned int **) realloc(f->gototab, new_count * sizeof(f->gototab[0]));
 	if (p == NULL)
 		goto out;
 	f->gototab = p;
 
-	p = realloc(f->out, new_count * sizeof(f->out[0]));
-	if (p == NULL)
+	p2 = (uschar *) realloc(f->out, new_count * sizeof(f->out[0]));
+	if (p2 == NULL)
 		goto out;
-	f->out = p;
+	f->out = p2;
 
-	p = realloc(f->posns, new_count * sizeof(f->posns[0]));
-	if (p == NULL)
+	p3 = (int **) realloc(f->posns, new_count * sizeof(f->posns[0]));
+	if (p3 == NULL)
 		goto out;
-	f->posns = p;
+	f->posns = p3;
 
 	for (i = f->state_count; i < new_count; ++i) {
-		f->gototab[i] = calloc(NCHARS, sizeof(**f->gototab));
+		f->gototab[i] = (unsigned int *) calloc(NCHARS, sizeof(**f->gototab));
 		if (f->gototab[i] == NULL)
 			goto out;
 		f->out[i]  = 0;
@@ -195,7 +197,7 @@
 
 	poscnt = 0;
 	penter(p1);	/* enter parent pointers and leaf indices */
-	if ((f = calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
+	if ((f = (fa *) calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
 		overflo(__func__);
 	f->accept = poscnt-1;	/* penter has computed number of positions in re */
 	cfoll(f, p1);	/* set up follow sets */
@@ -365,7 +367,7 @@
 	static int bufsz = 100;
 
 	op = p;
-	if (buf == NULL && (buf = malloc(bufsz)) == NULL)
+	if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL)
 		FATAL("out of space for character class [%.10s...] 1", p);
 	bp = buf;
 	for (i = 0; (c = *p++) != 0; ) {
@@ -937,7 +939,7 @@
 	} else if (special_case == REPEAT_ZERO) {
 		size += 2;	/* just a null ERE: () */
 	}
-	if ((buf = malloc(size + 1)) == NULL)
+	if ((buf = (uschar *) malloc(size + 1)) == NULL)
 		FATAL("out of space in reg expr %.10s..", lastre);
 	memcpy(buf, basestr, prefix_length);	/* copy prefix	*/
 	j = prefix_length;
@@ -1065,7 +1067,7 @@
 		rlxval = c;
 		return CHAR;
 	case '[':
-		if (buf == NULL && (buf = malloc(bufsz)) == NULL)
+		if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL)
 			FATAL("out of space in reg expr %.10s..", lastre);
 		bp = buf;
 		if (*prestr == '^') {
--- a/lex.c
+++ b/lex.c
@@ -173,7 +173,7 @@
 	static char *buf = NULL;
 	static int bufsize = 5; /* BUG: setting this small causes core dump! */
 
-	if (buf == NULL && (buf = malloc(bufsize)) == NULL)
+	if (buf == NULL && (buf = (char *) malloc(bufsize)) == NULL)
 		FATAL( "out of space in yylex" );
 	if (sc) {
 		sc = false;
@@ -370,7 +370,7 @@
 	static char *buf = NULL;
 	static int bufsz = 500;
 
-	if (buf == NULL && (buf = malloc(bufsz)) == NULL)
+	if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of space for strings");
 	for (bp = buf; (c = input()) != '"'; ) {
 		if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, "string"))
@@ -519,7 +519,7 @@
 	static int bufsz = 500;
 	char *bp;
 
-	if (buf == NULL && (buf = malloc(bufsz)) == NULL)
+	if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of space for rex expr");
 	bp = buf;
 	for ( ; (c = input()) != '/' && c != 0; ) {
--- a/lib.c
+++ b/lib.c
@@ -60,10 +60,10 @@
 
 void recinit(unsigned int n)
 {
-	if ( (record = malloc(n)) == NULL
-	  || (fields = malloc(n+1)) == NULL
-	  || (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
-	  || (fldtab[0] = malloc(sizeof(**fldtab))) == NULL)
+	if ( (record = (char *) malloc(n)) == NULL
+	  || (fields = (char *) malloc(n+1)) == NULL
+	  || (fldtab = (Cell **) calloc(nfields+2, sizeof(*fldtab))) == NULL
+	  || (fldtab[0] = (Cell *) malloc(sizeof(**fldtab))) == NULL)
 		FATAL("out of space for $0 and fields");
 	*record = '\0';
 	*fldtab[0] = dollar0;
@@ -78,7 +78,7 @@
 	int i;
 
 	for (i = n1; i <= n2; i++) {
-		fldtab[i] = malloc(sizeof(**fldtab));
+		fldtab[i] = (Cell *) malloc(sizeof(**fldtab));
 		if (fldtab[i] == NULL)
 			FATAL("out of space in makefields %d", i);
 		*fldtab[i] = dollar1;
@@ -127,7 +127,7 @@
 	}
 
 	len_inputFS = len + 1;
-	inputFS = realloc(inputFS, len_inputFS);
+	inputFS = (char *) realloc(inputFS, len_inputFS);
 	if (inputFS == NULL)
 		FATAL("field separator %.10s... is too long", *FS);
 	memcpy(inputFS, *FS, len_inputFS);
@@ -325,7 +325,7 @@
 	n = strlen(r);
 	if (n > fieldssize) {
 		xfree(fields);
-		if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
+		if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
 			FATAL("out of space for fields in fldbld %d", n);
 		fieldssize = n;
 	}
@@ -474,7 +474,7 @@
 		nf = n;
 	s = (nf+1) * (sizeof (struct Cell *));  /* freebsd: how much do we need? */
 	if (s / sizeof(struct Cell *) - 1 == (size_t)nf) /* didn't overflow */
-		fldtab = realloc(fldtab, s);
+		fldtab = (Cell **) realloc(fldtab, s);
 	else					/* overflow sizeof int */
 		xfree(fldtab);	/* make it null */
 	if (fldtab == NULL)
@@ -494,7 +494,7 @@
 	n = strlen(rec);
 	if (n > fieldssize) {
 		xfree(fields);
-		if ((fields = malloc(n+1)) == NULL)
+		if ((fields = (char *) malloc(n+1)) == NULL)
 			FATAL("out of space for fields in refldbld %d", n);
 		fieldssize = n;
 	}
--- a/main.c
+++ b/main.c
@@ -22,7 +22,7 @@
 THIS SOFTWARE.
 ****************************************************************/
 
-const char	*version = "version 20200816";
+const char	*version = "version 20201013";
 
 #define DEBUG
 #include <stdio.h>
@@ -161,7 +161,7 @@
 			fn = getarg(&argc, &argv, "no program filename");
 			if (npfile >= maxpfile) {
 				maxpfile += 20;
-				pfile = realloc(pfile, maxpfile * sizeof(*pfile));
+				pfile = (char **) realloc(pfile, maxpfile * sizeof(*pfile));
 				if (pfile == NULL)
 					FATAL("error allocating space for -f options");
  			}
--- a/makefile
+++ b/makefile
@@ -104,7 +104,7 @@
 # This is a bit of a band-aid until we can invest some more time
 # in the test suite.
 testclean:
-	cd testdir; rm -fr arnold-fixes beebe echo foo* \
+	cd testdir; rm -fr arnold-fixes beebe devnull echo foo* \
 		glop glop1 glop2 lilly.diff tempbig tempsmall time
 
 # For the habits of GNU maintainers:
--- a/parse.c
+++ b/parse.c
@@ -33,7 +33,7 @@
 {
 	Node *x;
 
-	x = malloc(sizeof(*x) + (n-1) * sizeof(x));
+	x = (Node *) malloc(sizeof(*x) + (n-1) * sizeof(x));
 	if (x == NULL)
 		FATAL("out of space in nodealloc");
 	x->nnext = NULL;
--- a/run.c
+++ b/run.c
@@ -118,7 +118,7 @@
 		/* round up to next multiple of quantum */
 		if (rminlen)
 			minlen += quantum - rminlen;
-		tbuf = realloc(*pbuf, minlen);
+		tbuf = (char *) realloc(*pbuf, minlen);
 		DPRINTF("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void*)*pbuf, (void*)tbuf);
 		if (tbuf == NULL) {
 			if (whatrtn)
@@ -240,7 +240,7 @@
 	if (!isfcn(fcn))
 		FATAL("calling undefined function %s", s);
 	if (frame == NULL) {
-		frp = frame = calloc(nframe += 100, sizeof(*frame));
+		frp = frame = (struct Frame *) calloc(nframe += 100, sizeof(*frame));
 		if (frame == NULL)
 			FATAL("out of space for stack frames calling %s", s);
 	}
@@ -274,7 +274,7 @@
 	frp++;	/* now ok to up frame */
 	if (frp >= frame + nframe) {
 		int dfp = frp - frame;	/* old index */
-		frame = realloc(frame, (nframe += 100) * sizeof(*frame));
+		frame = (struct Frame *) realloc(frame, (nframe += 100) * sizeof(*frame));
 		if (frame == NULL)
 			FATAL("out of space for stack frames in %s", s);
 		frp = frame + dfp;
@@ -408,7 +408,7 @@
 	int mode;
 	bool newflag;
 
-	if ((buf = malloc(bufsize)) == NULL)
+	if ((buf = (char *) malloc(bufsize)) == NULL)
 		FATAL("out of memory in getline");
 
 	fflush(stdout);	/* in case someone is waiting for a prompt */
@@ -474,7 +474,7 @@
 	int bufsz = recsize;
 	size_t blen;
 
-	if ((buf = malloc(bufsz)) == NULL) {
+	if ((buf = (char *) malloc(bufsz)) == NULL) {
 		FATAL("%s: out of memory", func);
 	}
 
@@ -701,7 +701,7 @@
 	Cell *x;
 
 	if (!tmps) {
-		tmps = calloc(100, sizeof(*tmps));
+		tmps = (Cell *) calloc(100, sizeof(*tmps));
 		if (!tmps)
 			FATAL("out of space for temporaries");
 		for (i = 1; i < 100; i++)
@@ -839,7 +839,7 @@
 
 	os = s;
 	p = buf;
-	if ((fmt = malloc(fmtsz)) == NULL)
+	if ((fmt = (char *) malloc(fmtsz)) == NULL)
 		FATAL("out of memory in format()");
 	while (*s) {
 		adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
@@ -982,7 +982,7 @@
 	char *buf;
 	int bufsz=3*recsize;
 
-	if ((buf = malloc(bufsz)) == NULL)
+	if ((buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of memory in awksprintf");
 	y = a[0]->nnext;
 	x = execute(a[0]);
@@ -1005,7 +1005,7 @@
 	int len;
 	int bufsz=3*recsize;
 
-	if ((buf = malloc(bufsz)) == NULL)
+	if ((buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of memory in awkprintf");
 	y = a[0]->nnext;
 	x = execute(a[0]);
@@ -1772,7 +1772,7 @@
 static void stdinit(void)	/* in case stdin, etc., are not constants */
 {
 	nfiles = FOPEN_MAX;
-	files = calloc(nfiles, sizeof(*files));
+	files = (struct files *) calloc(nfiles, sizeof(*files));
 	if (files == NULL)
 		FATAL("can't allocate file memory for %zu files", nfiles);
         files[0].fp = stdin;
@@ -1812,7 +1812,7 @@
 	if (i >= nfiles) {
 		struct files *nf;
 		size_t nnf = nfiles + FOPEN_MAX;
-		nf = realloc(files, nnf * sizeof(*nf));
+		nf = (struct files *) realloc(files, nnf * sizeof(*nf));
 		if (nf == NULL)
 			FATAL("cannot grow files for %s and %zu files", s, nnf);
 		memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf));
@@ -1933,7 +1933,7 @@
 	fa *pfa;
 	int bufsz = recsize;
 
-	if ((buf = malloc(bufsz)) == NULL)
+	if ((buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of memory in sub");
 	x = execute(a[3]);	/* target string */
 	t = getsval(x);
@@ -1995,7 +1995,7 @@
 	int mflag, tempstat, num;
 	int bufsz = recsize;
 
-	if ((buf = malloc(bufsz)) == NULL)
+	if ((buf = (char *) malloc(bufsz)) == NULL)
 		FATAL("out of memory in gsub");
 	mflag = 0;	/* if mflag == 0, can replace empty string */
 	num = 0;
--- a/tran.c
+++ b/tran.c
@@ -166,8 +166,8 @@
 	Array *ap;
 	Cell **tp;
 
-	ap = malloc(sizeof(*ap));
-	tp = calloc(n, sizeof(*tp));
+	ap = (Array *) malloc(sizeof(*ap));
+	tp = (Cell **) calloc(n, sizeof(*tp));
 	if (ap == NULL || tp == NULL)
 		FATAL("out of space in makesymtab");
 	ap->nelem = 0;
@@ -237,7 +237,7 @@
 			(void*)p, NN(p->nval), NN(p->sval), p->fval, p->tval);
 		return(p);
 	}
-	p = malloc(sizeof(*p));
+	p = (Cell *) malloc(sizeof(*p));
 	if (p == NULL)
 		FATAL("out of space for symbol table at %s", n);
 	p->nval = tostring(n);
@@ -272,7 +272,7 @@
 	Cell *cp, *op, **np;
 
 	nsz = GROWTAB * tp->size;
-	np = calloc(nsz, sizeof(*np));
+	np = (Cell **) calloc(nsz, sizeof(*np));
 	if (np == NULL)		/* can't do it, but can keep running. */
 		return;		/* someone else will run out later. */
 	for (i = 0; i < tp->size; i++) {
@@ -519,7 +519,7 @@
 {
 	char *p;
 
-	p = malloc(n);
+	p = (char *) malloc(n);
 	if (p == NULL)
 		FATAL("out of space in tostring on %s", s);
 	strcpy(p, s);
@@ -533,13 +533,13 @@
 	char *sa = getsval(a);
 	char *sb = getsval(b);
 	size_t l = strlen(sa) + strlen(sb) + 1;
-	p = malloc(l);
+	p = (char *) malloc(l);
 	if (p == NULL)
 		FATAL("out of space concatenating %s and %s", sa, sb);
 	snprintf(p, l, "%s%s", sa, sb);
 
 	l++;	// add room for ' '
-	char *newbuf = malloc(l);
+	char *newbuf = (char *) malloc(l);
 	if (newbuf == NULL)
 		FATAL("out of space concatenating %s and %s", sa, sb);
 	// See string() in lex.c; a string "xx" is stored in the symbol
@@ -558,7 +558,7 @@
 	const uschar *s = (const uschar *) is;
 	uschar *buf, *bp;
 
-	if ((buf = malloc(strlen(is)+3)) == NULL)
+	if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL)
 		FATAL( "out of space in qstring(%s)", s);
 	for (bp = buf; (c = *s) != delim; s++) {
 		if (c == '\n')
--