xref: /illumos-gate/usr/src/cmd/make/lib/mksh/mksh.cc (revision ee3c499ad1e4fc884a11b2bc6490787b788bf84a)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  *	mksh.cc
29  *
30  *	Execute the command(s) of one Make or DMake rule
31  */
32 
33 /*
34  * Included files
35  */
36 #include <mksh/dosys.h>		/* redirect_io() */
37 #include <mksh/misc.h>		/* retmem() */
38 #include <mksh/mksh.h>
39 #include <errno.h>
40 #include <signal.h>
41 
42 
43 /*
44  * Workaround for NFS bug. Sometimes, when running 'chdir' on a remote
45  * dmake server, it fails with "Stale NFS file handle" error.
46  * The second attempt seems to work.
47  */
48 int
49 my_chdir(char * dir) {
50 	int res = chdir(dir);
51 	if (res != 0 && (errno == ESTALE || errno == EAGAIN)) {
52 		/* Stale NFS file handle. Try again */
53 		res = chdir(dir);
54 	}
55 	return res;
56 }
57 
58 
59 /*
60  * File table of contents
61  */
62 static void	change_sunpro_dependencies_value(char *oldpath, char *newpath);
63 static void	init_mksh_globals(char *shell);
64 static void	set_env_vars(char *env_list[]);
65 
66 
67 static void
68 set_env_vars(char *env_list[])
69 {
70 	char			**env_list_p;
71 
72 	for (env_list_p = env_list;
73 	     *env_list_p != (char *) NULL;
74 	     env_list_p++) {
75 		putenv(*env_list_p);
76 	}
77 }
78 
79 static void
80 init_mksh_globals(char *shell)
81 {
82 /*
83 	MBSTOWCS(wcs_buffer, "SHELL");
84 	shell_name = GETNAME(wcs_buffer, FIND_LENGTH);
85 	MBSTOWCS(wcs_buffer, shell);
86 	(void) SETVAR(shell_name, GETNAME(wcs_buffer, FIND_LENGTH), false);
87  */
88 	char * dmake_shell;
89 	if ((dmake_shell = getenv("DMAKE_SHELL")) == NULL) {
90 		dmake_shell = shell;
91 	}
92 	MBSTOWCS(wcs_buffer, dmake_shell);
93 	shell_name = GETNAME(wcs_buffer, FIND_LENGTH);
94 }
95 
96 /*
97  * Change the pathname in the value of the SUNPRO_DEPENDENCIES env variable
98  * from oldpath to newpath.
99  */
100 static void
101 change_sunpro_dependencies_value(char *oldpath, char *newpath)
102 {
103 	char		buf[MAXPATHLEN];
104 	static char	*env;
105 	int		length;
106 	int		oldpathlen;
107 	char		*sp_dep_value;
108 
109 	/* check if SUNPRO_DEPENDENCIES is set in the environment */
110 	if ((sp_dep_value = getenv("SUNPRO_DEPENDENCIES")) != NULL) {
111 		oldpathlen = strlen(oldpath);
112 		/* check if oldpath is indeed in the value of SUNPRO_DEPENDENCIES */
113 		if (strncmp(oldpath, sp_dep_value, oldpathlen) == 0) {
114 			(void) sprintf(buf,
115 				       "%s%s",
116 				       newpath,
117 				       sp_dep_value + oldpathlen);
118 			length = 2 +
119 				strlen("SUNPRO_DEPENDENCIES") +
120 				strlen(buf);
121 			env = getmem(length);
122 			(void) sprintf(env,
123 				       "%s=%s",
124 				       "SUNPRO_DEPENDENCIES",
125 				       buf);
126 			(void) putenv(env);
127 		}
128 	}
129 }
130 
131 
132