xref: /csrg-svn/lib/libc/gen/genbuildname.c (revision 42624)
140489Smckusick /*
240489Smckusick  * Copyright (c) 1990 The Regents of the University of California.
340489Smckusick  * All rights reserved.
440489Smckusick  *
5*42624Sbostic  * %sccs.include.redist.c%
640489Smckusick  */
740489Smckusick 
840489Smckusick #if defined(LIBC_SCCS) && !defined(lint)
9*42624Sbostic static char sccsid[] = "@(#)genbuildname.c	5.3 (Berkeley) 06/01/90";
1040489Smckusick #endif LIBC_SCCS and not lint
1140489Smckusick 
1240489Smckusick #include <sys/types.h>
1340489Smckusick #include <sys/stat.h>
1442023Sbostic #include <string.h>
1540489Smckusick 
1640489Smckusick char *objdir = "obj";
1740489Smckusick 
1840489Smckusick #define	UNKNOWN	0
1940489Smckusick #define	NODIR	1
2040489Smckusick #define	USEDIR	2
2140489Smckusick 
2240489Smckusick char *
genbuildname(name)2340489Smckusick genbuildname(name)
2440489Smckusick 	char *name;
2540489Smckusick {
2640489Smckusick 	static int dirlen, chkobjdir = UNKNOWN;
2740489Smckusick 	struct stat stbuf;
2840489Smckusick 	char *newname;
2940489Smckusick 
3040489Smckusick 	if (chkobjdir == NODIR || index(name, '/') != (char *)0)
3140489Smckusick 		return (name);
3240489Smckusick 	if (chkobjdir == UNKNOWN &&
3340489Smckusick 	    (stat(objdir, &stbuf) < 0 ||
3440489Smckusick 	    (stbuf.st_mode & S_IFMT) != S_IFDIR)) {
3540489Smckusick 		chkobjdir = NODIR;
3640489Smckusick 		return (name);
3740489Smckusick 	} else {
3840489Smckusick 		chkobjdir = USEDIR;
3940489Smckusick 		dirlen = strlen(objdir) + 2;
4040489Smckusick 	}
4140489Smckusick 	newname = (char *)malloc(dirlen + strlen(name));
4240489Smckusick 	if (newname == (char *)0)
4340489Smckusick 		return (name);
4440489Smckusick 	strcpy(newname, objdir);
4540489Smckusick 	strcat(newname, "/");
4640489Smckusick 	strcat(newname, name);
4740489Smckusick 	return (newname);
4840489Smckusick }
49