ack/util/led/write.c
George Koehler f09f14cd4d Switch from long to size_t when allocating memory.
Also move the declarations of `incore` and `core_alloc` to "memory.h".
Also correct SYMDEBUG to SYMDBUG.  (I don't know if SYMDBUG works
because our build system never defines it.)

ind_t becomes an alias of size_t.  ind_t becomes unsigned, so I edit
some code that was using negative ind_t.  Some casts disappear, like
(long)sizeof(...) because the size is already a size_t.  There are
changes to overflow checks.  Callers with a size too big for size_t
must check it before calling the memory allocator.  An overflow check
of BASE + incr in memory.c sbreak() now happens on all platforms, not
only when a pointer is smaller than a long.

My build shows no changes in share/ack/examples (except hilo_bas.*
changing with every build).
2018-11-14 12:33:25 -05:00

119 lines
2.3 KiB
C

/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#ifndef lint
static char rcsid[] = "$Id$";
#endif
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "out.h"
#include "const.h"
#include "memory.h"
#include "sym.h"
extern struct outhead outhead;
extern struct outsect outsect[];
extern int flagword;
wr_fatal()
{
fatal("write error");
}
static long off_char;
/*
* Open the output file according to the chosen strategy.
* Write away the header and section table: they will not change anymore.
*/
begin_write()
{
register struct outhead *hd = &outhead;
assert(! incore);
wr_ohead(hd);
wr_sect(outsect, hd->oh_nsect);
off_char = OFF_CHAR(*hd);
}
static struct outname *
sectname(sectindex)
int sectindex;
{
static struct outname namebuf;
namebuf.on_foff = (long)0; /* No string name. */
namebuf.on_type = (S_MIN + sectindex) | S_SCT;
namebuf.on_desc = 0;
namebuf.on_valu = outsect[sectindex].os_base;
return &namebuf;
}
/*
* Write out the symbol table and the section names.
*/
end_write()
{
register struct outname *name;
register int sectindex;
extern long NGChars;
assert(!incore);
assert(!(flagword & SFLAG));
name = (struct outname *)address(ALLOGLOB, (ind_t)0);
namecpy(name, NGlobals, off_char);
wr_name(name, NGlobals);
wr_string(mems[ALLOGCHR].mem_base+1, NGChars);
off_char += NGChars;
for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++)
wrt_name(sectname(sectindex), 1);
}
wrt_emit(emit, sectindex, cnt)
char *emit;
int sectindex;
long cnt;
{
wr_outsect(sectindex);
wr_emit(emit, cnt);
}
wrt_nulls(sectindex, cnt)
register long cnt;
{
static char nullbuf[BUFSIZ];
wr_outsect(sectindex);
while (cnt) {
register int n = cnt >= BUFSIZ ? BUFSIZ : cnt;
wr_emit(nullbuf, (long)n);
cnt -= n;
}
}
wrt_name(name, writename)
register struct outname *name;
{
assert(!incore);
assert(!(flagword & SFLAG));
if (name->on_mptr != (char *)0) {
register long len = strlen(name->on_mptr) + 1;
wr_string(name->on_mptr, len);
name->on_foff = off_char;
off_char += len;
} else {
name->on_foff = (long)0;
}
if (writename) wr_name(name, 1);
}