xref: /csrg-svn/usr.bin/ranlib/misc.c (revision 62197)
146013Sbostic /*-
2*62197Sbostic  * Copyright (c) 1990, 1993
3*62197Sbostic  *	The Regents of the University of California.  All rights reserved.
446013Sbostic  *
546013Sbostic  * This code is derived from software contributed to Berkeley by
646013Sbostic  * Hugh Smith at The University of Guelph.
746013Sbostic  *
846013Sbostic  * %sccs.include.redist.c%
946013Sbostic  */
1046013Sbostic 
1146013Sbostic #ifndef lint
12*62197Sbostic static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 06/06/93";
1346013Sbostic #endif /* not lint */
1446013Sbostic 
1546013Sbostic #include <sys/param.h>
1646013Sbostic #include <sys/signal.h>
1746698Sbostic #include <errno.h>
1846698Sbostic #include <unistd.h>
1946013Sbostic #include <stdio.h>
2046013Sbostic #include <stdlib.h>
2146013Sbostic #include <string.h>
2246013Sbostic #include "pathnames.h"
2346013Sbostic 
2446013Sbostic extern char *archive;			/* archive name */
2546013Sbostic char *tname = "temporary file";		/* temporary file "name" */
2646013Sbostic 
tmp()2746013Sbostic tmp()
2846013Sbostic {
2946013Sbostic 	sigset_t set, oset;
3046013Sbostic 	int fd;
3152348Sbostic 	char *envtmp, path[MAXPATHLEN];
3246013Sbostic 
3359020Storek 	if ((envtmp = getenv("TMPDIR")) != NULL)
3459020Storek 		(void)sprintf(path, "%s%s", envtmp, strrchr(_PATH_RANTMP, '/'));
3552348Sbostic 	else
3652348Sbostic 		bcopy(_PATH_RANTMP, path, sizeof(_PATH_RANTMP));
3752348Sbostic 
3852348Sbostic 	sigfillset(&set);
3946013Sbostic 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
4046013Sbostic 	if ((fd = mkstemp(path)) == -1)
4159020Storek 		error(path);
4246013Sbostic         (void)unlink(path);
4352348Sbostic 	(void)sigprocmask(SIG_SETMASK, &oset, NULL);
4446013Sbostic 	return(fd);
4546013Sbostic }
4646013Sbostic 
4746013Sbostic void *
emalloc(len)4846013Sbostic emalloc(len)
4946013Sbostic 	int len;
5046013Sbostic {
5152348Sbostic 	void *p;
5246013Sbostic 
5352348Sbostic 	if ((p = malloc((u_int)len)) == NULL)
5446013Sbostic 		error(archive);
5546013Sbostic 	return(p);
5646013Sbostic }
5746013Sbostic 
5846013Sbostic char *
rname(path)5946013Sbostic rname(path)
6046013Sbostic 	char *path;
6146013Sbostic {
6246013Sbostic 	register char *ind;
6346013Sbostic 
6446013Sbostic 	return((ind = rindex(path, '/')) ? ind + 1 : path);
6546013Sbostic }
6646013Sbostic 
badfmt()6746013Sbostic badfmt()
6846013Sbostic {
6946013Sbostic 	errno = EFTYPE;
7046013Sbostic 	error(archive);
7146013Sbostic }
7246013Sbostic 
error(name)7346013Sbostic error(name)
7446013Sbostic 	char *name;
7546013Sbostic {
7646013Sbostic 	(void)fprintf(stderr, "ranlib: %s: %s\n", name, strerror(errno));
7746013Sbostic 	exit(1);
7846013Sbostic }
79