* More ANSI C portability fixes.

This commit is contained in:
carl 2019-03-24 23:53:13 +08:00
parent f7e4fac687
commit 5ec9de401d
7 changed files with 105 additions and 99 deletions

View file

@ -32,22 +32,18 @@ long s_base[S_MAX]; /* for specially encoded bases */
char *filename; char *filename;
int narg; int narg;
extern int rd_unsigned2();
static const char prefix[] = "_bmodule_"; static const char prefix[] = "_bmodule_";
static struct stringlist modules; static struct stringlist modules;
static void do_file(int fd) static void do_file(FILE *fd)
{ {
struct outhead hbuf; struct outhead hbuf;
struct outname *nbufp = NULL;
char *cbufp; char *cbufp;
long fi_to_co; long fi_to_co;
long n; long n;
unsigned readcount; unsigned readcount;
int i,j;
int compare();
read_error = 0; read_error = 0;
rd_fdopen(fd); rd_fdopen(fd);
@ -75,7 +71,6 @@ static void do_file(int fd)
while (--n >= 0) while (--n >= 0)
{ {
struct outname nbuf; struct outname nbuf;
struct stringfragment* f;
rd_name(&nbuf, 1); rd_name(&nbuf, 1);
if (read_error) if (read_error)
@ -107,12 +102,12 @@ corrupt:
fatal("%s --- corrupt", filename); fatal("%s --- corrupt", filename);
} }
static void process(int fd) static void process(FILE* fd)
{ {
uint16_t magic = rd_unsigned2(fd); uint16_t magic = rd_unsigned2(fd);
switch(magic) { switch(magic) {
case O_MAGIC: case O_MAGIC:
lseek(fd, 0L, 0); fseek(fd, 0L, SEEK_SET);
do_file(fd); do_file(fd);
break; break;
@ -124,7 +119,7 @@ static void process(int fd)
while (rd_arhdr(fd, &archive_header)) while (rd_arhdr(fd, &archive_header))
{ {
long nextpos = lseek(fd, 0L, SEEK_CUR) + archive_header.ar_size; long nextpos = ftell(fd) + archive_header.ar_size;
if (nextpos & 1) if (nextpos & 1)
nextpos++; nextpos++;
@ -132,7 +127,7 @@ static void process(int fd)
filename = buf; filename = buf;
if (strcmp(filename, SYMDEF) != 0) if (strcmp(filename, SYMDEF) != 0)
do_file(fd); do_file(fd);
lseek(fd, nextpos, 0); fseek(fd, nextpos, SEEK_SET);
} }
break; break;
} }
@ -144,7 +139,6 @@ static void process(int fd)
int main(int argc, char* const argv[]) int main(int argc, char* const argv[])
{ {
int opt;
FILE* outputfp = NULL; FILE* outputfp = NULL;
program_name = argv[0]; program_name = argv[0];
@ -169,15 +163,15 @@ int main(int argc, char* const argv[])
for (;;) for (;;)
{ {
int fd; FILE *fd;
filename = argv[optind++]; filename = argv[optind++];
if (!filename) if (!filename)
break; break;
if ((fd = open(filename, 0)) < 0) if ((fd = fopen(filename, "rb")) == NULL)
fatal("cannot open %s: %s", filename, strerror(errno)); fatal("cannot open %s: %s", filename, strerror(errno));
process(fd); process(fd);
close(fd); fclose(fd);
} }
if (outputfp) if (outputfp)

View file

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <inttypes.h> #include <inttypes.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h>
#include "out.h" #include "out.h"
#define ASSERT(x) switch (2) { case 0: case (x): ; } #define ASSERT(x) switch (2) { case 0: case (x): ; }
@ -859,7 +860,12 @@ int main(int argc, char* argv[])
if (ferror(output)) if (ferror(output))
fatal("output write error"); fatal("output write error");
if (outputfile) if (outputfile)
chmod(outputfile, 0755); {
/* mode = 0755 in standard UNIX */
chmod(outputfile, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
/* Summarise what we've done. */ /* Summarise what we've done. */

View file

@ -10,12 +10,10 @@
** anm [-gopruns] [name ...] ** anm [-gopruns] [name ...]
*/ */
#include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h>
#include "object.h" #include "object.h"
#include "out.h" #include "out.h"
@ -38,10 +36,10 @@ long s_base[S_MAX]; /* for specially encoded bases */
char *filename; char *filename;
int narg; int narg;
void do_file(); static void process(FILE *);
static void do_file(FILE *);
main(argc, argv) int main(int argc, char **argv)
char **argv;
{ {
if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) { if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
@ -88,23 +86,21 @@ char **argv;
narg = argc; narg = argc;
while(argc--) { while(argc--) {
int fd; FILE *fd;
filename = *++argv; filename = *++argv;
if ((fd = open(filename, 0)) < 0) { if ((fd = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "anm: cannot open %s\n", filename); fprintf(stderr, "anm: cannot open %s\n", filename);
continue; continue;
} }
process(fd); process(fd);
close(fd); fclose(fd);
} }
exit(0); exit(EXIT_SUCCESS);
} }
extern int rd_unsigned2();
process(fd) static void process(FILE *fd)
int fd;
{ {
unsigned int magic; unsigned int magic;
long nextpos; long nextpos;
@ -116,13 +112,13 @@ process(fd)
magic = rd_unsigned2(fd); magic = rd_unsigned2(fd);
switch(magic) { switch(magic) {
case O_MAGIC: case O_MAGIC:
lseek(fd, 0L, 0); fseek(fd, 0L, SEEK_SET);
do_file(fd); do_file(fd);
break; break;
case ARMAG: case ARMAG:
case AALMAG: case AALMAG:
while (rd_arhdr(fd, &archive_header)) { while (rd_arhdr(fd, &archive_header)) {
nextpos = lseek(fd, 0L, 1) + archive_header.ar_size; nextpos = ftell(fd) + archive_header.ar_size;
if (nextpos & 1) nextpos++; if (nextpos & 1) nextpos++;
strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name)); strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
filename = buf; filename = buf;
@ -130,7 +126,7 @@ process(fd)
printf("\n%s:\n", filename); printf("\n%s:\n", filename);
do_file(fd); do_file(fd);
} }
lseek(fd, nextpos, 0); fseek(fd, nextpos, SEEK_SET);
} }
break; break;
default: default:
@ -139,9 +135,7 @@ process(fd)
} }
} }
void static void do_file(FILE *fd)
do_file(fd)
int fd;
{ {
struct outname *nbufp = NULL; struct outname *nbufp = NULL;
struct outname nbuf; struct outname nbuf;
@ -301,8 +295,7 @@ do_file(fd)
free((char *)cbufp); free((char *)cbufp);
} }
compare(p1, p2) int compare(struct outname *p1, struct outname *p2)
struct outname *p1, *p2;
{ {
int i; int i;
@ -336,7 +329,7 @@ struct outname *p1, *p2;
return(0); return(0);
} }
rd_fatal() void rd_fatal(void)
{ {
fprintf(stderr,"read error on %s\n", filename); fprintf(stderr,"read error on %s\n", filename);
read_error = 1; read_error = 1;

View file

@ -5,18 +5,25 @@ static char rcsid[] = "$Id$";
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <sys/types.h> #include <stdarg.h>
#include <sys/stat.h> #include "object.h"
#include <object.h> #include "out.h"
#include <out.h>
#define OK 0 /* Return value of gethead if Orl Korekt. */ #define OK 0 /* Return value of gethead if Orl Korekt. */
#define BMASK 0xFF /* To extract least significant 8 bits from an int. */ #define BMASK 0xFF /* To extract least significant 8 bits from an int. */
/* Forward declarations */
static void show(register struct outhead *);
static void showflags(unsigned int);
static void showsect(void);
static void showrelo(void);
static void showname(struct outname *);
static char *myalloc(unsigned int);
static void error(char *, ...);
/* ARGSUSED */ /* ARGSUSED */
main(argc, argv) int main(int argc, char **argv)
int argc;
char *argv[];
# define prog argv[0] # define prog argv[0]
{ {
register char **arg = argv; register char **arg = argv;
@ -36,6 +43,7 @@ main(argc, argv)
} }
rd_close(); rd_close();
} }
return EXIT_SUCCESS;
} }
/* /*
@ -43,14 +51,12 @@ main(argc, argv)
* NB. The header has already been read and is in the struct outhead `headp' * NB. The header has already been read and is in the struct outhead `headp'
* points to. * points to.
*/ */
show(headp) static void show(register struct outhead *headp)
register struct outhead *headp;
{ {
register int i; register int i;
register struct outname *np; register struct outname *np;
register struct outname *name; /* Dynamically allocated name-array. */ register struct outname *name; /* Dynamically allocated name-array. */
register char *string;/* Base of string area. */ register char *string;/* Base of string area. */
extern char *myalloc();
printf("Version %d\n", headp->oh_stamp); printf("Version %d\n", headp->oh_stamp);
showflags((unsigned) headp->oh_flags); showflags((unsigned) headp->oh_flags);
@ -90,8 +96,12 @@ show(headp)
/* /*
* Now we can show all names. * Now we can show all names.
*/ */
for (np = &name[0]; np < &name[headp->oh_nname]; np++) { for (np = &name[0]; np < &name[headp->oh_nname]; np++)
printf("Name %d:\n", np - name); {
/* In C99 this should be "%td" but we are in ANSI C89,
* so we typecast explicitly to int here.
*/
printf("Name %d:\n", (int)(np - name));
showname(np); showname(np);
} }
} }
@ -99,8 +109,7 @@ show(headp)
/* /*
* Show flags from header. * Show flags from header.
*/ */
showflags(flagword) static void showflags(unsigned int flagword)
unsigned flagword;
{ {
if (flagword & HF_LINK) printf("unresolved references left\n"); if (flagword & HF_LINK) printf("unresolved references left\n");
} }
@ -108,7 +117,7 @@ showflags(flagword)
/* /*
* Show a section. * Show a section.
*/ */
showsect() static void showsect(void)
{ {
struct outsect section; struct outsect section;
@ -123,7 +132,7 @@ showsect()
/* /*
* Show a relocation record. * Show a relocation record.
*/ */
showrelo() static void showrelo(void)
{ {
struct outrelo relrec; struct outrelo relrec;
@ -171,8 +180,7 @@ showrelo()
/* /*
* Show the name in the struct `namep' points to. * Show the name in the struct `namep' points to.
*/ */
showname(namep) static void showname(struct outname *namep)
struct outname *namep;
{ {
if (namep->on_mptr) if (namep->on_mptr)
printf("\t%s\n", namep->on_mptr); printf("\t%s\n", namep->on_mptr);
@ -219,29 +227,29 @@ showname(namep)
/* /*
* Core allocation via malloc() but fatal if no core. * Core allocation via malloc() but fatal if no core.
*/ */
char * static char *myalloc(unsigned int u)
myalloc(u)
unsigned int u;
{ {
register char *rcp; register char *rcp;
rcp = malloc(u); rcp = malloc(u);
if (rcp == (char *) 0) { if (rcp == (char *) NULL) {
error("Out of core\n"); error("Out of core\n");
exit(1); exit(EXIT_FAILURE);
} }
return rcp; return rcp;
} }
/* VARARGS1 */ static void error(char *fmt, ...)
error(s, a1, a2, a3, a4)
char *s;
{ {
fflush(stdout); fflush(stdout);
fprintf(stderr, s, a1, a2, a3, a4); /* Diagnostic print, no auto NL */
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
} }
rd_fatal() void rd_fatal(void)
{ {
error("Error in reading the object file\n"); error("Error in reading the object file\n");
exit(1); exit(1);

View file

@ -14,8 +14,7 @@
*/ */
main(argc, argv) int main(int argc, char **argv)
char **argv;
{ {
struct outhead buf; struct outhead buf;
struct outsect sbuf; struct outsect sbuf;
@ -60,10 +59,10 @@ char **argv;
printf(" = %ld = 0x%lx\n", sum, sum); printf(" = %ld = 0x%lx\n", sum, sum);
rd_close(); rd_close();
} }
exit(0); exit(EXIT_SUCCESS);
} }
rd_fatal() void rd_fatal(void)
{ {
fprintf(stderr, "read error\n"); fprintf(stderr, "read error\n");
exit(2); exit(2);

View file

@ -287,8 +287,10 @@ int main(int argc, char* argv[])
if (ferror(output)) if (ferror(output))
fatal("output write error"); fatal("output write error");
if (outputfile) if (outputfile)
chmod(outputfile, 0755); {
/* mode = 0755 in standard UNIX */
chmod(outputfile, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
/* Summarise what we've done. */ /* Summarise what we've done. */
if (verbose) if (verbose)

View file

@ -4,11 +4,11 @@
*/ */
/* $Id$ */ /* $Id$ */
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <sys/stat.h>
#include "system.h"
#include "object.h" #include "object.h"
#include "out.h" #include "out.h"
@ -18,34 +18,40 @@
*/ */
char temp_name[] = "/tmp/sXXXXXX"; char tname[L_tmpnam];
char *tname;
FILE *tf; FILE *tf;
struct outhead buf; struct outhead buf;
int readerror, writeerror; int readerror, writeerror;
main(argc, argv)
char **argv; static int copy(char *, char *, long, FILE *, FILE *);
static int strip(char *);
int main(int argc, char **argv)
{ {
int status; int status;
signal(SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
signal(SIGINT, SIG_IGN); signal(SIGINT, SIG_IGN);
signal(SIGQUIT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
close(mkstemp(temp_name)); if (sys_tmpnam(tname)==NULL)
{
fprintf(stderr, "astrip: cannot create temporary filename\n");
return(1);
}
fclose(fopen(tname,"wb"));
while(--argc) { while(--argc) {
if ((status = strip(argv[argc])) > 1) if ((status = strip(argv[argc])) > 1)
break; break;
} }
unlink(temp_name); remove(tname);
exit(status); return status;
} }
strip(name) int strip(char *name)
char *name;
{ {
long size; long size;
int fw; FILE *fw;
if (! rd_open(name)) { if (! rd_open(name)) {
fprintf(stderr, "astrip: cannot open %s\n", name); fprintf(stderr, "astrip: cannot open %s\n", name);
@ -78,48 +84,46 @@ char *name;
rd_close(); rd_close();
return(1); return(1);
} }
fw = open(tname, 2); fw = fopen(tname, "ab");
if (fw < 0 || lseek(fw, (long)SZ_HEAD, 0) < 0) { if ((fw == NULL) || (fseek(fw, (long)SZ_HEAD, SEEK_SET)!=0)) {
fprintf(stderr, "astrip: cannot create temp file %s\n", tname); fprintf(stderr, "astrip: cannot create temp file %s\n", tname);
rd_close(); rd_close();
close(fw); fclose(fw);
return(2); return(2);
} }
if(copy(name, tname, size, rd_fd(), fw)) { if(copy(name, tname, size, rd_fd(), fw)) {
rd_close(); rd_close();
close(fw); fclose(fw);
return(1); return(1);
} }
rd_close(); rd_close();
close(fw); fclose(fw);
size += SZ_HEAD; size += SZ_HEAD;
if (! rd_open(tname)) { if (! rd_open(tname)) {
fprintf(stderr, "astrip: cannot read temp file %s\n", tname); fprintf(stderr, "astrip: cannot read temp file %s\n", tname);
return(2); return(2);
} }
fw = creat(name, 0777); fw = fopen(name, "wb");
if (fw < 0) { if (fw == NULL) {
fprintf(stderr, "astrip: cannot write %s\n", name); fprintf(stderr, "astrip: cannot write %s\n", name);
rd_close(); rd_close();
return(1); return(1);
} }
if(copy(tname, name, size, rd_fd(), fw)) { if(copy(tname, name, size, rd_fd(), fw)) {
close(fw); fclose(fw);
rd_close(); rd_close();
return(2); return(2);
} }
fclose(fw);
close(fw);
rd_close(); rd_close();
/* Change the mode to everything. */
chmod(name,S_IRWXU | S_IRWXG | S_IRWXO);
return(0); return(0);
} }
copy(fnam, tnam, size, fr, fw) static int copy(char *fnam, char *tnam, long size, FILE *fr, FILE *fw)
char *fnam;
char *tnam;
long size;
{ {
register s, n; register int s;
char lbuf[512]; char lbuf[512];
while(size != (long)0) { while(size != (long)0) {
@ -141,12 +145,12 @@ long size;
return(0); return(0);
} }
rd_fatal() void rd_fatal(void)
{ {
readerror = 1; readerror = 1;
} }
wr_fatal() void wr_fatal(void)
{ {
writeerror = 1; writeerror = 1;
} }