More cleanups: (#53)
- sprinkle const
- add a macro (setptr) that cheats const to temporarily NUL terminate strings
remove casts from allocations
- use strdup instead of strlen+strcpy
- use x = malloc(sizeof(*x)) instead of x = malloc(sizeof(type of *x)))
- add -Wcast-qual (and casts through unitptr_t in the two macros we
cheat (xfree, setptr)).
diff --git a/lib.c b/lib.c
index 8a6b684..0ab56be 100644
--- a/lib.c
+++ b/lib.c
@@ -57,10 +57,10 @@
void recinit(unsigned int n)
{
- if ( (record = (char *) malloc(n)) == NULL
- || (fields = (char *) malloc(n+1)) == NULL
- || (fldtab = (Cell **) malloc((nfields+2) * sizeof(Cell *))) == NULL
- || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL )
+ if ( (record = malloc(n)) == NULL
+ || (fields = malloc(n+1)) == NULL
+ || (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
+ || (fldtab[0] = malloc(sizeof(**fldtab))) == NULL)
FATAL("out of space for $0 and fields");
*record = '\0';
*fldtab[0] = dollar0;
@@ -75,7 +75,7 @@
int i;
for (i = n1; i <= n2; i++) {
- fldtab[i] = (Cell *) malloc(sizeof (struct Cell));
+ fldtab[i] = malloc(sizeof(**fldtab));
if (fldtab[i] == NULL)
FATAL("out of space in makefields %d", i);
*fldtab[i] = dollar1;
@@ -215,7 +215,7 @@
fa *pfa = makedfa(rs, 1);
found = fnematch(pfa, inf, &buf, &bufsize, recsize);
if (found)
- *patbeg = 0;
+ setptr(patbeg, '\0');
} else {
if ((sep = *rs) == 0) {
sep = '\n';
@@ -304,7 +304,7 @@
n = strlen(r);
if (n > fieldssize) {
xfree(fields);
- if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
+ if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
FATAL("out of space for fields in fldbld %d", n);
fieldssize = n;
}
@@ -447,7 +447,7 @@
nf = n;
s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */
if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */
- fldtab = (Cell **) realloc(fldtab, s);
+ fldtab = realloc(fldtab, s);
else /* overflow sizeof int */
xfree(fldtab); /* make it null */
if (fldtab == NULL)
@@ -467,7 +467,7 @@
n = strlen(rec);
if (n > fieldssize) {
xfree(fields);
- if ((fields = (char *) malloc(n+1)) == NULL)
+ if ((fields = malloc(n+1)) == NULL)
FATAL("out of space for fields in refldbld %d", n);
fieldssize = n;
}