Revert "* Adapt to more ANSI C"

This reverts commit da0e0ac257.
This commit is contained in:
carl 2019-03-24 23:39:04 +08:00
parent a58ad2bc31
commit f7e4fac687
8 changed files with 99 additions and 141 deletions

View file

@ -1,36 +0,0 @@
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,18 +32,22 @@ 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(FILE *fd)
static void do_file(int 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);
@ -71,6 +75,7 @@ static void do_file(FILE *fd)
while (--n >= 0)
{
struct outname nbuf;
struct stringfragment* f;
rd_name(&nbuf, 1);
if (read_error)
@ -102,12 +107,12 @@ corrupt:
fatal("%s --- corrupt", filename);
}
static void process(FILE* fd)
static void process(int fd)
{
uint16_t magic = rd_unsigned2(fd);
switch(magic) {
case O_MAGIC:
fseek(fd, 0L, SEEK_SET);
lseek(fd, 0L, 0);
do_file(fd);
break;
@ -119,7 +124,7 @@ static void process(FILE* fd)
while (rd_arhdr(fd, &archive_header))
{
long nextpos = ftell(fd) + archive_header.ar_size;
long nextpos = lseek(fd, 0L, SEEK_CUR) + archive_header.ar_size;
if (nextpos & 1)
nextpos++;
@ -127,7 +132,7 @@ static void process(FILE* fd)
filename = buf;
if (strcmp(filename, SYMDEF) != 0)
do_file(fd);
fseek(fd, nextpos, SEEK_SET);
lseek(fd, nextpos, 0);
}
break;
}
@ -139,6 +144,7 @@ static void process(FILE* fd)
int main(int argc, char* const argv[])
{
int opt;
FILE* outputfp = NULL;
program_name = argv[0];
@ -163,15 +169,15 @@ int main(int argc, char* const argv[])
for (;;)
{
FILE *fd;
int fd;
filename = argv[optind++];
if (!filename)
break;
if ((fd = fopen(filename, "rb")) == NULL)
if ((fd = open(filename, 0)) < 0)
fatal("cannot open %s: %s", filename, strerror(errno));
process(fd);
fclose(fd);
close(fd);
}
if (outputfp)

View file

@ -25,7 +25,6 @@
#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): ; }
@ -860,12 +859,7 @@ int main(int argc, char* argv[])
if (ferror(output))
fatal("output write error");
if (outputfile)
{
/* mode = 0755 in standard UNIX */
chmod(outputfile, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}
chmod(outputfile, 0755);
/* Summarise what we've done. */

View file

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

View file

@ -5,25 +5,18 @@ static char rcsid[] = "$Id$";
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "object.h"
#include "out.h"
#include <sys/types.h>
#include <sys/stat.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 */
int main(int argc, char **argv)
main(argc, argv)
int argc;
char *argv[];
# define prog argv[0]
{
register char **arg = argv;
@ -43,7 +36,6 @@ int main(int argc, char **argv)
}
rd_close();
}
return EXIT_SUCCESS;
}
/*
@ -51,12 +43,14 @@ int main(int argc, char **argv)
* NB. The header has already been read and is in the struct outhead `headp'
* points to.
*/
static void show(register struct outhead *headp)
show(headp)
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);
@ -96,12 +90,8 @@ static void show(register struct outhead *headp)
/*
* Now we can show all names.
*/
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));
for (np = &name[0]; np < &name[headp->oh_nname]; np++) {
printf("Name %d:\n", np - name);
showname(np);
}
}
@ -109,7 +99,8 @@ static void show(register struct outhead *headp)
/*
* Show flags from header.
*/
static void showflags(unsigned int flagword)
showflags(flagword)
unsigned flagword;
{
if (flagword & HF_LINK) printf("unresolved references left\n");
}
@ -117,7 +108,7 @@ static void showflags(unsigned int flagword)
/*
* Show a section.
*/
static void showsect(void)
showsect()
{
struct outsect section;
@ -132,7 +123,7 @@ static void showsect(void)
/*
* Show a relocation record.
*/
static void showrelo(void)
showrelo()
{
struct outrelo relrec;
@ -180,7 +171,8 @@ static void showrelo(void)
/*
* Show the name in the struct `namep' points to.
*/
static void showname(struct outname *namep)
showname(namep)
struct outname *namep;
{
if (namep->on_mptr)
printf("\t%s\n", namep->on_mptr);
@ -227,29 +219,29 @@ static void showname(struct outname *namep)
/*
* Core allocation via malloc() but fatal if no core.
*/
static char *myalloc(unsigned int u)
char *
myalloc(u)
unsigned int u;
{
register char *rcp;
rcp = malloc(u);
if (rcp == (char *) NULL) {
if (rcp == (char *) 0) {
error("Out of core\n");
exit(EXIT_FAILURE);
exit(1);
}
return rcp;
}
static void error(char *fmt, ...)
/* VARARGS1 */
error(s, a1, a2, a3, a4)
char *s;
{
fflush(stdout);
/* Diagnostic print, no auto NL */
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, s, a1, a2, a3, a4);
}
void rd_fatal(void)
rd_fatal()
{
error("Error in reading the object file\n");
exit(1);

View file

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

View file

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