xref: /original-bsd/lib/libc/gen/genbuildname.c (revision bcb80f39)
1 /*
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)genbuildname.c	5.3 (Berkeley) 06/01/90";
10 #endif LIBC_SCCS and not lint
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <string.h>
15 
16 char *objdir = "obj";
17 
18 #define	UNKNOWN	0
19 #define	NODIR	1
20 #define	USEDIR	2
21 
22 char *
genbuildname(name)23 genbuildname(name)
24 	char *name;
25 {
26 	static int dirlen, chkobjdir = UNKNOWN;
27 	struct stat stbuf;
28 	char *newname;
29 
30 	if (chkobjdir == NODIR || index(name, '/') != (char *)0)
31 		return (name);
32 	if (chkobjdir == UNKNOWN &&
33 	    (stat(objdir, &stbuf) < 0 ||
34 	    (stbuf.st_mode & S_IFMT) != S_IFDIR)) {
35 		chkobjdir = NODIR;
36 		return (name);
37 	} else {
38 		chkobjdir = USEDIR;
39 		dirlen = strlen(objdir) + 2;
40 	}
41 	newname = (char *)malloc(dirlen + strlen(name));
42 	if (newname == (char *)0)
43 		return (name);
44 	strcpy(newname, objdir);
45 	strcat(newname, "/");
46 	strcat(newname, name);
47 	return (newname);
48 }
49