Object module now uses ANSI C I/O

This commit is contained in:
carl 2019-03-25 00:07:12 +08:00
parent 5ec9de401d
commit 8457ec6791
16 changed files with 219 additions and 241 deletions

View file

@ -3,21 +3,14 @@
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <local.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <out.h>
#include <ranlib.h>
#include <arch.h>
#include "object.h"
#if ! defined(CHAR_UNSIGNED)
#define CHAR_UNSIGNED 0
#endif
#define Xchar(ch) (uint32_t)(uint8_t)(ch)
#define Xchar(ch) (uint32_t)(uint8_t)(ch)
#define uget2(c) (Xchar((c)[0]) | (Xchar((c)[1])<<8))
@ -28,33 +21,33 @@
#define get4(c) ((int32_t) uget4(c))
#define put4(i, c) (put2(i, c), put2((i)>>16, (c)+2))
#define SECTCNT 3 /* number of sections with own output buffer */
#define SECTCNT 3 /* number of sections with own output buffer */
#if BIGMACHINE
#define WBUFSIZ (8*BUFSIZ)
#define WBUFSIZ (8*BUFSIZ)
#else
#define WBUFSIZ BUFSIZ
#define WBUFSIZ BUFSIZ
#endif
struct fil {
int cnt;
char *pnow;
char *pbegin;
long currpos;
int fd;
char pbuf[WBUFSIZ];
int cnt;
char *pnow;
char *pbegin;
long currpos;
FILE* fd;
char pbuf[WBUFSIZ];
};
extern struct fil __parts[];
#define PARTEMIT 0
#define PARTRELO (PARTEMIT+SECTCNT)
#define PARTNAME (PARTRELO+1)
#define PARTCHAR (PARTNAME+1)
#define PARTEMIT 0
#define PARTRELO (PARTEMIT+SECTCNT)
#define PARTNAME (PARTRELO+1)
#define PARTCHAR (PARTNAME+1)
#ifdef SYMDBUG
#define PARTDBUG (PARTCHAR+1)
#define PARTDBUG (PARTCHAR+1)
#else
#define PARTDBUG (PARTCHAR+0)
#define PARTDBUG (PARTCHAR+0)
#endif
#define NPARTS (PARTDBUG + 1)
#define NPARTS (PARTDBUG + 1)
#define getsect(s) (PARTEMIT+((s)>=(SECTCNT-1)?(SECTCNT-1):(s)))

View file

@ -16,147 +16,71 @@ and write ACK-object files and libraries
.br
.B #include <object.h>
.PP
.B int wr_open(filename)
.br
.B char *filename;
.B FILE* wr_open(const char *filename)
.PP
.B void wr_close()
.PP
.B void wr_ohead(head)
.br
.B struct outhead *head;
.B void wr_ohead(struct outhead *head)
.PP
.B void wr_sect(sect, cnt)
.br
.B struct outsect *sect;
.br
.B unsigned int cnt;
.B void wr_sect(struct outsect *sect, unsigned int cnt)
.PP
.B void wr_outsect(sectionnr)
.br
.B int sectionnr;
.B void wr_outsect(int sectionnr)
.PP
.B void wr_emit(emit, cnt)
.br
.B char *emit;
.br
.B long cnt;
.B void wr_emit(const char *emit, long cnt)
.PP
.B void wr_putc(ch)
.B void wr_putc(int ch)
.PP
.B void wr_relo(relo, cnt)
.br
.B struct outrelo *relo;
.br
.B unsigned int cnt;
.B void wr_relo(const struct outrelo *relo, unsigned int cnt)
.PP
.B void wr_name(name, cnt)
.br
.B struct outname *name;
.br
.B unsigned int cnt;
.B void wr_name(struct outname *name, unsigned int cnt)
.PP
.B void wr_string(stringaddr, cnt)
.br
.B char *stringaddr;
.br
.B long cnt;
.B void wr_string(const char *stringaddr, long cnt)
.PP
.B void wr_arhdr(fd, arhdr)
.br
.B struct ar_hdr *arhdr;
.B void wr_arhdr(FILE* fd, struct ar_hdr *arhdr)
.PP
.B void wr_ranlib(fd, ran, cnt)
.br
.B struct ranlib *ran;
.br
.B long cnt;
.B void wr_ranlib(FILE* fd, struct ranlib *ran, long cnt)
.PP
.B void wr_int2(fd, i)
.B void wr_int2(FILE* fd, int i)
.PP
.B void wr_long(fd, l)
.br
.B long l;
.B void wr_long(FILE* fd, long l)
.PP
.B void wr_bytes(fd, buf, l)
.br
.B char *buf;
.br
.B long l;
.B void wr_bytes(FILE* fd, const char* buf, long l)
.PP
.B int rd_open(filename)
.br
.B char *filename;
.B int rd_open(const char *filename)
.PP
.B int rd_fdopen(fd)
.B int rd_fdopen(FILE* fd)
.PP
.B void rd_close()
.PP
.B void rd_ohead(head)
.br
.B struct outhead *head;
.B void rd_ohead(struct outhead *head)
.PP
.B void rd_sect(sect, cnt)
.br
.B struct outsect *sect;
.br
.B unsigned int cnt;
.B void rd_sect(struct outsect *sect, unsigned int cnt)
.PP
.B void rd_outsect(sectionnr)
.br
.B int sectionnr;
.B void rd_outsect(int sectionnr)
.PP
.B void rd_emit(emit, cnt)
.br
.B char *emit;
.br
.B long cnt;
.B void rd_emit(char *emit, long cnt)
.PP
.B void rd_relo(relo, cnt)
.br
.B struct outrelo *relo;
.br
.B unsigned int cnt;
.B void rd_relo(struct outrelo *relo, unsigned int cnt)
.PP
.B void rd_rew_relo(head)
.br
.B struct outhead *head;
.B void rd_rew_relo(struct outhead *head)
.PP
.B void rd_name(name, cnt)
.br
.B struct outname *name;
.br
.B unsigned int cnt;
.B void rd_name(struct outname *name, unsigned int cnt)
.PP
.B void rd_string(stringaddr, cnt)
.br
.B char *stringaddr;
.br
.B long cnt;
.B void rd_string(char *stringaddr, long cnt)
.PP
.B int rd_arhdr(fd, arhdr)
.br
.B struct ar_hdr *arhdr;
.B int rd_arhdr(FILE* fd, struct ar_hdr *arhdr)
.PP
.B void rd_ranlib(fd, ran, cnt)
.br
.B struct ranlib *ran;
.br
.B long cnt;
.B void rd_ranlib(FILE* fd, struct ranlib *ran, long cnt)
.PP
.B int rd_int2(fd)
.B int rd_int2(FILE* fd)
.PP
.B unsigned int rd_unsigned2(fd)
.B unsigned int rd_unsigned2(FILE* fd)
.PP
.B long rd_long(fd)
.B long rd_long(FILE* fd)
.PP
.B void rd_bytes(fd, buf, l)
.br
.B char *buf;
.br
.B long l;
.B void rd_bytes(FILE* fd, char *buf, long l)
.PP
.B int rd_fd()
.B FILE* rd_fd()
.SH DESCRIPTION
These routines come in handy when reading or writing ACK-object files
or libraries. No checking is performed.

View file

@ -1,9 +1,13 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
/*
@ -29,7 +33,7 @@
static long offset[MAXSECT];
static int outfile;
static FILE* outfile;
static long outseek[NPARTS];
static long currpos;
static long rd_base;
@ -46,7 +50,7 @@ OUTREAD(int p, char* b, long n)
register long l = outseek[p];
if (currpos != l) {
lseek(outfile, l, 0);
fseek(outfile, l, SEEK_SET);
}
rd_bytes(outfile, b, n);
l += n;
@ -57,25 +61,23 @@ OUTREAD(int p, char* b, long n)
/*
* Open the output file according to the chosen strategy.
*/
int
rd_open(const char* f)
int rd_open(const char* f)
{
if ((outfile = open(f, 0)) < 0)
if ((outfile = fopen(f, "rb")) == NULL)
return 0;
return rd_fdopen(outfile);
}
static int offcnt;
int
rd_fdopen(int fd)
int rd_fdopen(FILE* fd)
{
register int i;
for (i = 0; i < NPARTS; i++) outseek[i] = 0;
offcnt = 0;
rd_base = lseek(fd, 0L, 1);
rd_base = fseek(fd, 0L, SEEK_CUR);
if (rd_base < 0) {
return 0;
}
@ -86,16 +88,14 @@ rd_fdopen(int fd)
return 1;
}
void
rd_close(void)
void rd_close(void)
{
close(outfile);
outfile = -1;
fclose(outfile);
outfile = NULL;
}
int
rd_fd(void)
FILE* rd_fd(void)
{
return outfile;
}
@ -221,6 +221,10 @@ rd_string(char* addr, long len)
OUTREAD(PARTCHAR, addr, len);
}
#ifdef SYMDBUG
void
rd_dbug(char* buf, long size)

View file

@ -1,34 +1,40 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "arch.h"
#include "object.h"
#include "obj.h"
int
rd_arhdr(fd, arhdr)
register struct ar_hdr *arhdr;
{
char buf[AR_TOTAL];
register char *c = buf;
register char *p = arhdr->ar_name;
register int i;
extern void rd_fatal(void);
i = read(fd, c, AR_TOTAL);
if (i == 0) return 0;
if (i != AR_TOTAL) {
rd_fatal();
}
i = 14;
while (i--) {
*p++ = *c++;
}
arhdr->ar_date = ((long) get2(c)) << 16; c += 2;
arhdr->ar_date |= ((long) get2(c)) & 0xffff; c += 2;
arhdr->ar_uid = *c++;
arhdr->ar_gid = *c++;
arhdr->ar_mode = get2(c); c += 2;
arhdr->ar_size = (long) get2(c) << 16; c += 2;
arhdr->ar_size |= (long) get2(c) & 0xffff;
return 1;
int rd_arhdr(FILE* fd, register struct ar_hdr *arhdr)
{
char buf[AR_TOTAL];
register char *c = buf;
register char *p = arhdr->ar_name;
register size_t i;
i = fread(c, 1, AR_TOTAL, fd);
if (i == 0) return 0;
if (i != AR_TOTAL) {
rd_fatal();
}
i = AR_NAME_MAX;
while (i--) {
*p++ = *c++;
}
arhdr->ar_date = ((long) get2(c)) << 16; c += 2;
arhdr->ar_date |= ((long) get2(c)) & 0xffff; c += 2;
arhdr->ar_uid = *c++;
arhdr->ar_gid = *c++;
arhdr->ar_mode = get2(c); c += 2;
arhdr->ar_size = (long) get2(c) << 16; c += 2;
arhdr->ar_size |= (long) get2(c) & 0xffff;
return 1;
}

View file

@ -4,32 +4,35 @@
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "obj.h"
#define MININT (1 << (sizeof(int) * 8 - 1))
#define MAXCHUNK (~MININT) /* Highest count we read(2). */
#define MININT (1 << (sizeof(int) * 8 - 1))
#define MAXCHUNK (~MININT) /* Highest count we read(2). */
/* Unfortunately, MAXCHUNK is too large with some compilers. Put it in
an int!
*/
static int maxchunk = MAXCHUNK;
extern void rd_fatal(void);
/*
* We don't have to worry about byte order here.
* Just read "cnt" bytes from file-descriptor "fd".
*/
void
rd_bytes(fd, string, cnt)
register char *string;
register long cnt;
void
rd_bytes(FILE* fd, register char *string, register long cnt)
{
size_t read_bytes;
while (cnt) {
register size_t n = cnt >= maxchunk ? maxchunk : cnt;
while (cnt) {
register int n = cnt >= maxchunk ? maxchunk : cnt;
if (read(fd, string, n) != n)
rd_fatal();
string += n;
cnt -= n;
}
read_bytes = fread(string, 1, n, fd);
if (read_bytes != n)
rd_fatal();
string += n;
cnt -= n;
}
}

View file

@ -1,12 +1,16 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
int
rd_int2(fd)
int rd_int2(FILE* fd)
{
char buf[2];

View file

@ -1,12 +1,17 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
long
rd_long(fd)
long rd_long(FILE* fd)
{
char buf[4];

View file

@ -1,14 +1,18 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "arch.h"
#include "ranlib.h"
#include "object.h"
#include "obj.h"
void
rd_ranlib(fd, ran, cnt)
register struct ranlib *ran;
register long cnt;
void rd_ranlib(FILE* fd, register struct ranlib *ran, register long cnt)
{
rd_bytes(fd, (char *) ran, cnt * SZ_RAN);
{

View file

@ -1,12 +1,16 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
unsigned int
rd_unsigned2(fd)
unsigned int rd_unsigned2(FILE* fd)
{
char buf[2];

View file

@ -11,8 +11,12 @@
* part. In this case #define OUTSEEK.
*/
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
#include "ranlib.h"
/*
* Parts of the output file.
@ -21,7 +25,7 @@ static long offset[MAXSECT];
struct fil __parts[NPARTS];
#ifdef OUTSEEK
static int outfile;
static FILE* outfile;
static long currpos;
#endif
int __sectionnr;
@ -34,7 +38,7 @@ __wr_flush(struct fil *ptr)
#ifdef OUTSEEK
/* seek to correct position even if we aren't going to write now */
if (currpos != ptr->currpos) {
currpos = lseek(ptr->fd, ptr->currpos, 0);
currpos = fseek(ptr->fd, ptr->currpos, SEEK_SET);
}
#endif
if (ptr->pnow > ptr->pbegin) {
@ -116,8 +120,7 @@ OUTWRITE(int p, const char *b, long n)
}
}
static void
BEGINSEEK(int p, long o)
static void BEGINSEEK(int p, long o)
/* p = part number, o = offset in file */
{
struct fil *ptr = &__parts[p];
@ -126,7 +129,7 @@ BEGINSEEK(int p, long o)
ptr->fd = outfile;
ptr->currpos = o;
#else
ptr->currpos = lseek(ptr->fd, o, 0);
ptr->currpos = fseek(ptr->fd, o, SEEK_SET);
#endif
if (p >= PARTRELO) o = 0; /* no attempt to align writes
for the time being */
@ -139,39 +142,37 @@ BEGINSEEK(int p, long o)
/*
* Open the output file according to the chosen strategy.
*/
int
wr_open(const char *f)
int wr_open(const char *f)
{
struct fil *fdp;
close(creat(f, 0666));
fclose(fopen(f,"wb"));
#ifdef OUTSEEK
if ((outfile = open(f, 1)) < 0)
if ((outfile = fopen(f, "ab+")) == NULL)
return 0;
currpos = 0;
#else /* not OUTSEEK */
for (fdp = &__parts[PARTEMIT]; fdp < &__parts[NPARTS]; fdp++)
if ((fdp->fd = open(f, 1)) < 0)
if ((fdp->fd = fopen(f, "wb+")) == NULL)
return 0;
#endif /* not OUTSEEK */
offcnt = 0;
return 1;
}
void
wr_close()
void wr_close(void)
{
struct fil *ptr;
for (ptr = &__parts[PARTEMIT]; ptr < &__parts[NPARTS]; ptr++) {
__wr_flush(ptr);
#ifndef OUTSEEK
close(ptr->fd);
fclose(ptr->fd);
#endif /* not OUTSEEK */
ptr->fd = -1;
ptr->fd = NULL;
}
#ifdef OUTSEEK
close(outfile);
fclose(outfile);
outfile = -1;
#endif /* OUTSEEK */
}
@ -259,7 +260,7 @@ wr_outsect(int s)
if (s != sectionnr && s >= (SECTCNT-1) && sectionnr >= (SECTCNT-1)) {
#ifdef OUTSEEK
if (currpos != ptr->currpos)
currpos = lseek(ptr->fd, ptr->currpos, 0);
currpos = fseek(ptr->fd, ptr->currpos, SEEK_SET);
#endif
wr_bytes(ptr->fd, ptr->pbegin, (long)(ptr->pnow - ptr->pbegin));
ptr->currpos += ptr->pnow - ptr->pbegin;
@ -268,7 +269,7 @@ wr_outsect(int s)
#endif
offset[sectionnr] = ptr->currpos;
if (offset[s] != ptr->currpos) {
ptr->currpos = lseek(ptr->fd, offset[s], 0);
ptr->currpos = fseek(ptr->fd, offset[s], SEEK_SET);
#ifdef OUTSEEK
currpos = ptr->currpos;
#endif

View file

@ -1,18 +1,24 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "arch.h"
#include "object.h"
#include "obj.h"
void
wr_arhdr(fd, arhdr)
register struct ar_hdr *arhdr;
void wr_arhdr(FILE* fd, register struct ar_hdr *arhdr)
{
char buf[AR_TOTAL];
register char *c = buf;
register char *p = arhdr->ar_name;
register int i = 14;
register int i = AR_NAME_MAX;
while (i--) {
*c++ = *p++;

View file

@ -4,29 +4,38 @@
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "obj.h"
#define MININT (1 << (sizeof(int) * 8 - 1))
#define MAXCHUNK (~MININT) /* Highest count we write(2). */
#define MININT (1 << (sizeof(int) * 8 - 1))
#define MAXCHUNK (~MININT) /* Highest count we write(2). */
/* Notice that MAXCHUNK itself might be too large with some compilers.
You have to put it in an int!
*/
static const int maxchunk = MAXCHUNK;
extern void wr_fatal(void);
/*
* Just write "cnt" bytes to file-descriptor "fd".
*/
void
wr_bytes(int fd, const char *string, long cnt)
void wr_bytes(FILE* fd, const char *string, long cnt)
{
while (cnt) {
int n = cnt >= maxchunk ? maxchunk : cnt;
size_t written_bytes;
while (cnt)
{
int n = cnt >= maxchunk ? maxchunk : cnt;
if (write(fd, string, n) != n)
wr_fatal();
string += n;
cnt -= n;
}
written_bytes = fwrite(string, 1, n, fd);
if (written_bytes != (size_t)n)
{
wr_fatal();
}
string += n;
cnt -= n;
}
}

View file

@ -1,12 +1,16 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
void
wr_int2(fd, i)
void wr_int2(FILE* fd, int i)
{
char buf[2];

View file

@ -1,13 +1,16 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
void
wr_long(fd, l)
long l;
void wr_long(FILE* fd, long l)
{
char buf[4];

View file

@ -1,15 +1,19 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
extern int __sectionnr;
void __wr_flush(struct fil *);
void
wr_putc(int ch)
void wr_putc(int ch)
{
struct fil *ptr = &__parts[PARTEMIT+getsect(__sectionnr)];

View file

@ -1,14 +1,18 @@
/* $Id$ */
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
#include "obj.h"
void
wr_ranlib(fd, ran, cnt1)
struct ranlib *ran;
long cnt1;
#include <stdlib.h>
#include <stdio.h>
#include "out.h"
#include "object.h"
#include "obj.h"
#include "ranlib.h"
void wr_ranlib(FILE* fd, struct ranlib *ran, long cnt1)
{
struct ranlib *r;
long cnt, val;