summaryrefslogtreecommitdiff
path: root/usr.bin/ranlib
diff options
context:
space:
mode:
authorMarc Espie <espie@cvs.openbsd.org>1999-09-21 13:15:44 +0000
committerMarc Espie <espie@cvs.openbsd.org>1999-09-21 13:15:44 +0000
commit4ecdc1954f4fbdcb4552591930031f4e3396fbe8 (patch)
treea53dd5479f3236a73e41aa449c54a6df41483f63 /usr.bin/ranlib
parent1b50297182d0c8ea155fe81bf00514b453081f08 (diff)
Build some proper prototypes, fix minor nits.
Add some proper error handling to object file parsing, deciding whether it's a bad object file or something ranlib should not care about, so that archives that mix non object files with object files don't get killed by the `mixed object archive' check.
Diffstat (limited to 'usr.bin/ranlib')
-rw-r--r--usr.bin/ranlib/build.c68
-rw-r--r--usr.bin/ranlib/extern.h53
-rw-r--r--usr.bin/ranlib/misc.c31
-rw-r--r--usr.bin/ranlib/ranlib.c11
-rw-r--r--usr.bin/ranlib/touch.c10
5 files changed, 125 insertions, 48 deletions
diff --git a/usr.bin/ranlib/build.c b/usr.bin/ranlib/build.c
index c953492a694..633def9dbe6 100644
--- a/usr.bin/ranlib/build.c
+++ b/usr.bin/ranlib/build.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: build.c,v 1.6 1999/05/10 16:14:07 espie Exp $ */
+/* $OpenBSD: build.c,v 1.7 1999/09/21 13:15:43 espie Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@@ -38,7 +38,7 @@
#ifndef lint
/*static char sccsid[] = "from: @(#)build.c 5.3 (Berkeley) 3/12/91";*/
-static char rcsid[] = "$OpenBSD: build.c,v 1.6 1999/05/10 16:14:07 espie Exp $";
+static char rcsid[] = "$OpenBSD: build.c,v 1.7 1999/09/21 13:15:43 espie Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -52,14 +52,15 @@ static char rcsid[] = "$OpenBSD: build.c,v 1.6 1999/05/10 16:14:07 espie Exp $";
#include <limits.h>
#include <ranlib.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <archive.h>
+#include <err.h>
#include "byte.c"
+#include "extern.h"
extern CHDR chdr; /* converted header */
-extern char *archive; /* archive name */
-extern char *tname; /* temporary file "name" */
typedef struct _rlib {
struct _rlib *next; /* next structure */
@@ -75,17 +76,16 @@ static long tsymlen; /* total string length */
static int rexec();
static void symobj();
-extern void *emalloc();
+int
build()
{
CF cf;
int afd, tfd;
- int check_mid;
int current_mid;
off_t size;
- check_mid = 0;
+ current_mid = -1;
afd = open_archive(O_RDWR);
fp = fdopen(afd, "r+");
tfd = tmp();
@@ -103,11 +103,13 @@ build()
continue;
}
new_mid = rexec(afd, tfd);
- if (check_mid && new_mid != current_mid)
- errx(1, "Mixed object format archive: %d / %d",
- new_mid, current_mid);
- current_mid = new_mid;
- check_mid = 1;
+ if (new_mid != -1) {
+ if (current_mid == -1)
+ current_mid = new_mid;
+ else if (new_mid != current_mid)
+ errx(1, "Mixed object format archive: %d / %d",
+ new_mid, current_mid);
+ }
put_arobj(&cf, (struct stat *)NULL);
}
*pnext = NULL;
@@ -133,6 +135,9 @@ build()
* rexec
* Read the exec structure; ignore any files that don't look
* exactly right. Return MID.
+ * return -1 for files that don't look right.
+ * XXX it's hard to be sure when to ignore files, and when to error
+ * out.
*/
static int
rexec(rfd, wfd)
@@ -142,11 +147,13 @@ rexec(rfd, wfd)
register RLIB *rp;
register long nsyms;
register int nr, symlen;
- register char *strtab, *sym;
+ register char *strtab = 0;
+ char *sym;
struct exec ebuf;
struct nlist nl;
off_t r_off, w_off;
long strsize;
+ int result = -1;
/* Get current offsets for original and tmp files. */
r_off = lseek(rfd, (off_t)0, SEEK_CUR);
@@ -155,37 +162,40 @@ rexec(rfd, wfd)
/* Read in exec structure. */
nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
if (nr != sizeof(struct exec))
- goto badread;
+ goto bad;
/* Check magic number and symbol count. */
if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
- goto bad1;
+ goto bad;
fix_header_order(&ebuf);
/* Seek to string table. */
- if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
- error(archive);
+ if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1) {
+ if (errno == EINVAL)
+ goto bad;
+ else
+ error(archive);
+ }
/* Read in size of the string table. */
nr = read(rfd, (char *)&strsize, sizeof(strsize));
if (nr != sizeof(strsize))
- goto badread;
+ goto bad;
strsize = fix_long_order(strsize, N_GETMID(ebuf));
+
/* Read in the string table. */
strsize -= sizeof(strsize);
strtab = (char *)emalloc(strsize);
nr = read(rfd, strtab, strsize);
- if (nr != strsize) {
-badread: if (nr < 0)
- error(archive);
- goto bad2;
- }
+ if (nr != strsize)
+ goto bad;
/* Seek to symbol table. */
if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
- goto bad2;
+ goto bad;
+ result = N_GETMID(ebuf);
/* For each symbol read the nlist entry and save it as necessary. */
nsyms = ebuf.a_syms / sizeof(struct nlist);
while (nsyms--) {
@@ -194,7 +204,7 @@ badread: if (nr < 0)
badfmt();
error(archive);
}
- fix_nlist_order(&nl, N_GETMID(ebuf));
+ fix_nlist_order(&nl, N_GETMID(ebuf));
/* Ignore if no name or local. */
if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
@@ -225,9 +235,11 @@ badread: if (nr < 0)
tsymlen += symlen;
}
-bad2: free(strtab);
-bad1: (void)lseek(rfd, (off_t)r_off, SEEK_SET);
- return N_GETMID(ebuf);
+bad: if (nr < 0)
+ error(archive);
+ free(strtab);
+ (void)lseek(rfd, (off_t)r_off, SEEK_SET);
+ return result;
}
/*
diff --git a/usr.bin/ranlib/extern.h b/usr.bin/ranlib/extern.h
new file mode 100644
index 00000000000..b88aa81294c
--- /dev/null
+++ b/usr.bin/ranlib/extern.h
@@ -0,0 +1,53 @@
+/* $OpenBSD: extern.h,v 1.1 1999/09/21 13:15:43 espie Exp $ */
+
+/*-
+ * Copyright (c) 1990 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* misc.c */
+extern int tmp __P((void));
+extern void *emalloc __P((size_t));
+extern void badfmt __P((void));
+extern void error __P((const char *));
+extern const char *rname __P((const char *));
+extern char *tname; /* temporary file "name" */
+
+/* touch.c */
+extern int touch __P((void));
+extern void settime __P((int));
+
+/* build.c */
+extern int build __P((void));
+
+/* main.c */
+extern char *archive; /* archive name */
+
diff --git a/usr.bin/ranlib/misc.c b/usr.bin/ranlib/misc.c
index 3b64347e2c7..6582cbdf48f 100644
--- a/usr.bin/ranlib/misc.c
+++ b/usr.bin/ranlib/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.4 1999/08/27 08:43:22 fgsch Exp $ */
+/* $OpenBSD: misc.c,v 1.5 1999/09/21 13:15:43 espie Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@@ -38,7 +38,7 @@
#ifndef lint
/*static char sccsid[] = "from: @(#)misc.c 5.2 (Berkeley) 2/26/91";*/
-static char rcsid[] = "$OpenBSD: misc.c,v 1.4 1999/08/27 08:43:22 fgsch Exp $";
+static char rcsid[] = "$OpenBSD: misc.c,v 1.5 1999/09/21 13:15:43 espie Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -49,10 +49,11 @@ static char rcsid[] = "$OpenBSD: misc.c,v 1.4 1999/08/27 08:43:22 fgsch Exp $";
#include <stdlib.h>
#include <string.h>
#include "pathnames.h"
+#include "extern.h"
-extern char *archive; /* archive name */
-char *tname = "temporary file";
+char *tname = "temporary file";
+int
tmp()
{
static char *envtmp;
@@ -80,40 +81,42 @@ tmp()
(void)sigprocmask(SIG_BLOCK, &set, &oset);
if ((fd = mkstemp(path)) == -1)
error(tname);
- (void)unlink(path);
+ (void)unlink(path);
(void)sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
return(fd);
}
void *
emalloc(len)
- int len;
+ size_t len;
{
- char *p;
+ void *p;
- if (!(p = malloc((u_int)len)))
+ if (!(p = malloc(len)))
error(archive);
return(p);
}
-char *
+const char *
rname(path)
- char *path;
+ const char *path;
{
- register char *ind;
+ register const char *ind;
return((ind = strrchr(path, '/')) ? ind + 1 : path);
}
+void
badfmt()
{
errno = EFTYPE;
error(archive);
}
+void
error(name)
- char *name;
+ const char *name;
{
- (void)fprintf(stderr, "ranlib: %s: %s\n", name, strerror(errno));
- exit(1);
+
+ err(1, "%s", name);
}
diff --git a/usr.bin/ranlib/ranlib.c b/usr.bin/ranlib/ranlib.c
index 17e9817a557..0940ee125e4 100644
--- a/usr.bin/ranlib/ranlib.c
+++ b/usr.bin/ranlib/ranlib.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ranlib.c,v 1.3 1997/01/15 23:43:04 millert Exp $ */
+/* $OpenBSD: ranlib.c,v 1.4 1999/09/21 13:15:43 espie Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@@ -44,7 +44,7 @@ char copyright[] =
#ifndef lint
/*static char sccsid[] = "from: @(#)ranlib.c 5.6 (Berkeley) 2/26/91";*/
-static char rcsid[] = "$OpenBSD: ranlib.c,v 1.3 1997/01/15 23:43:04 millert Exp $";
+static char rcsid[] = "$OpenBSD: ranlib.c,v 1.4 1999/09/21 13:15:43 espie Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -52,11 +52,17 @@ static char rcsid[] = "$OpenBSD: ranlib.c,v 1.3 1997/01/15 23:43:04 millert Exp
#include <stdio.h>
#include <stdlib.h>
#include <archive.h>
+#include "extern.h"
CHDR chdr;
u_int options; /* UNUSED -- keep open_archive happy */
+
char *archive;
+static void
+usage();
+
+int
main(argc, argv)
int argc;
char **argv;
@@ -84,6 +90,7 @@ main(argc, argv)
exit(eval);
}
+static void
usage()
{
(void)fprintf(stderr, "usage: ranlib [-t] archive ...\n");
diff --git a/usr.bin/ranlib/touch.c b/usr.bin/ranlib/touch.c
index d3440fa6019..9841039207c 100644
--- a/usr.bin/ranlib/touch.c
+++ b/usr.bin/ranlib/touch.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: touch.c,v 1.2 1996/06/26 05:38:07 deraadt Exp $ */
+/* $OpenBSD: touch.c,v 1.3 1999/09/21 13:15:43 espie Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
@@ -38,7 +38,7 @@
#ifndef lint
/*static char sccsid[] = "from: @(#)touch.c 5.3 (Berkeley) 3/12/91";*/
-static char rcsid[] = "$OpenBSD: touch.c,v 1.2 1996/06/26 05:38:07 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: touch.c,v 1.3 1999/09/21 13:15:43 espie Exp $";
#endif /* not lint */
#include <sys/types.h>
@@ -51,10 +51,11 @@ static char rcsid[] = "$OpenBSD: touch.c,v 1.2 1996/06/26 05:38:07 deraadt Exp $
#include <stdio.h>
#include <string.h>
#include <archive.h>
+#include "extern.h"
extern CHDR chdr; /* converted header */
-extern char *archive; /* archive name */
+int
touch()
{
int afd;
@@ -72,6 +73,7 @@ touch()
return(0);
}
+void
settime(afd)
int afd;
{
@@ -82,7 +84,7 @@ settime(afd)
size = SARMAG + sizeof(hdr->ar_name);
if (lseek(afd, size, SEEK_SET) == (off_t)-1)
error(archive);
- (void)sprintf(buf, "%-12ld", time((time_t *)NULL) + RANLIBSKEW);
+ (void)sprintf(buf, "%-12ld", (long int)time((time_t *)NULL) + RANLIBSKEW);
if (write(afd, buf, sizeof(hdr->ar_date)) != sizeof(hdr->ar_date))
error(archive);
}