* Adapt to more ANSI C

This commit is contained in:
carl 2019-03-24 23:35:59 +08:00
parent 56e64a1fd0
commit da0e0ac257
8 changed files with 141 additions and 99 deletions

36
util/amisc/CMakeLists.txt Normal file
View file

@ -0,0 +1,36 @@
cmake_minimum_required (VERSION 2.9)
project (ack-misc)
set(SRC_MODULES abmodules.c)
set(SRC_ELFLOD aelflod.c)
set(SRC_ANM anm.c)
set(SRC_ASHOW ashow.c)
set(SRC_ASIZE asize.c)
set(SRC_ASLOD aslod.c)
set(SRC_ASTRIP astrip.c)
add_executable(anm ${SRC_ANM})
add_executable(ashow ${SRC_ASHOW})
add_executable(asize ${SRC_ASIZE})
add_executable(astrip ${SRC_ASTRIP})
add_executable(aslod ${SRC_ASLOD})
include_directories(${INCLUDE_DIRS})
# Set linking options
#target_link_libraries(ack-modules emheaders object)
target_link_libraries(anm emheaders object)
target_link_libraries(ashow emheaders object)
target_link_libraries(asize emheaders object)
target_link_libraries(astrip emheaders object system)
target_link_libraries(aslod emheaders object)
install(TARGETS anm DESTINATION bin)
install(TARGETS ashow DESTINATION bin)
install(TARGETS asize DESTINATION bin)
install(TARGETS astrip DESTINATION bin)
install(TARGETS aslod DESTINATION bin)

View file

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

View file

@ -25,6 +25,7 @@
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <sys/stat.h>
#include "out.h"
#define ASSERT(x) switch (2) { case 0: case (x): ; }
@ -859,7 +860,12 @@ int main(int argc, char* argv[])
if (ferror(output))
fatal("output write error");
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. */

View file

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

View file

@ -5,18 +5,25 @@ static char rcsid[] = "$Id$";
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <object.h>
#include <out.h>
#include <stdarg.h>
#include "object.h"
#include "out.h"
#define OK 0 /* Return value of gethead if Orl Korekt. */
#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 */
main(argc, argv)
int argc;
char *argv[];
int main(int argc, char **argv)
# define prog argv[0]
{
register char **arg = argv;
@ -36,6 +43,7 @@ main(argc, argv)
}
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'
* points to.
*/
show(headp)
register struct outhead *headp;
static void show(register struct outhead *headp)
{
register int i;
register struct outname *np;
register struct outname *name; /* Dynamically allocated name-array. */
register char *string;/* Base of string area. */
extern char *myalloc();
printf("Version %d\n", headp->oh_stamp);
showflags((unsigned) headp->oh_flags);
@ -90,8 +96,12 @@ show(headp)
/*
* Now we can show all names.
*/
for (np = &name[0]; np < &name[headp->oh_nname]; np++) {
printf("Name %d:\n", np - name);
for (np = &name[0]; np < &name[headp->oh_nname]; np++)
{
/* 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);
}
}
@ -99,8 +109,7 @@ show(headp)
/*
* Show flags from header.
*/
showflags(flagword)
unsigned flagword;
static void showflags(unsigned int flagword)
{
if (flagword & HF_LINK) printf("unresolved references left\n");
}
@ -108,7 +117,7 @@ showflags(flagword)
/*
* Show a section.
*/
showsect()
static void showsect(void)
{
struct outsect section;
@ -123,7 +132,7 @@ showsect()
/*
* Show a relocation record.
*/
showrelo()
static void showrelo(void)
{
struct outrelo relrec;
@ -171,8 +180,7 @@ showrelo()
/*
* Show the name in the struct `namep' points to.
*/
showname(namep)
struct outname *namep;
static void showname(struct outname *namep)
{
if (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.
*/
char *
myalloc(u)
unsigned int u;
static char *myalloc(unsigned int u)
{
register char *rcp;
rcp = malloc(u);
if (rcp == (char *) 0) {
if (rcp == (char *) NULL) {
error("Out of core\n");
exit(1);
exit(EXIT_FAILURE);
}
return rcp;
}
/* VARARGS1 */
error(s, a1, a2, a3, a4)
char *s;
static void error(char *fmt, ...)
{
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");
exit(1);

View file

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

View file

@ -287,8 +287,10 @@ int main(int argc, char* argv[])
if (ferror(output))
fatal("output write error");
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. */
if (verbose)

View file

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