xref: /illumos-gate/usr/src/cmd/make/bin/main.cc (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 2006 Sun Microsystems, Inc. All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright 2019, Joyent, Inc.
26  * Copyright 2019 RackTop Systems.
27  */
28 
29 /*
30  *	main.cc
31  *
32  *	make program main routine plus some helper routines
33  */
34 
35 /*
36  * Included files
37  */
38 #include <bsd/bsd.h>		/* bsd_signal() */
39 
40 
41 #include <locale.h>		/* setlocale() */
42 #include <libgen.h>
43 #include <mk/defs.h>
44 #include <mksh/macro.h>		/* getvar() */
45 #include <mksh/misc.h>		/* getmem(), setup_char_semantics() */
46 
47 #include <pwd.h>		/* getpwnam() */
48 #include <setjmp.h>
49 #include <signal.h>
50 #include <stdlib.h>
51 #include <sys/errno.h>		/* ENOENT */
52 #include <sys/stat.h>		/* fstat() */
53 #include <fcntl.h>		/* open() */
54 
55 #	include <sys/systeminfo.h>	/* sysinfo() */
56 
57 #include <sys/types.h>		/* stat() */
58 #include <sys/wait.h>		/* wait() */
59 #include <unistd.h>		/* execv(), unlink(), access() */
60 #include <vroot/report.h>	/* report_dependency(), get_report_file() */
61 
62 // From read2.cc
63 extern	Name		normalize_name(register wchar_t *name_string, register int length);
64 
65 extern void job_adjust_fini();
66 
67 
68 /*
69  * Defined macros
70  */
71 #define	LD_SUPPORT_ENV_VAR	"SGS_SUPPORT_32"
72 #define	LD_SUPPORT_ENV_VAR_32	"SGS_SUPPORT_32"
73 #define	LD_SUPPORT_ENV_VAR_64	"SGS_SUPPORT_64"
74 #define	LD_SUPPORT_MAKE_LIB	"libmakestate.so.1"
75 #ifdef __x86
76 #define	LD_SUPPORT_MAKE_ARCH	"i386"
77 #elif __sparc
78 #define	LD_SUPPORT_MAKE_ARCH	"sparc"
79 #else
80 #error "Unsupported architecture"
81 #endif
82 
83 /*
84  * typedefs & structs
85  */
86 
87 /*
88  * Static variables
89  */
90 static	char		*argv_zero_string;
91 static	Boolean		build_failed_ever_seen;
92 static	Boolean		continue_after_error_ever_seen;	/* `-k' */
93 static	Boolean		dmake_group_specified;		/* `-g' */
94 static	Boolean		dmake_max_jobs_specified;	/* `-j' */
95 static	Boolean		dmake_mode_specified;		/* `-m' */
96 static	Boolean		dmake_add_mode_specified;	/* `-x' */
97 static	Boolean		dmake_output_mode_specified;	/* `-x DMAKE_OUTPUT_MODE=' */
98 static	Boolean		dmake_compat_mode_specified;	/* `-x SUN_MAKE_COMPAT_MODE=' */
99 static	Boolean		dmake_odir_specified;		/* `-o' */
100 static	Boolean		dmake_rcfile_specified;		/* `-c' */
101 static	Boolean		env_wins;			/* `-e' */
102 static	Boolean		ignore_default_mk;		/* `-r' */
103 static	Boolean		list_all_targets;		/* `-T' */
104 static	int		mf_argc;
105 static	char		**mf_argv;
106 static	Dependency_rec  not_auto_depen_struct;
107 static	Dependency 	not_auto_depen = &not_auto_depen_struct;
108 static	Boolean		pmake_cap_r_specified;		/* `-R' */
109 static	Boolean		pmake_machinesfile_specified;	/* `-M' */
110 static	Boolean		stop_after_error_ever_seen;	/* `-S' */
111 static	Boolean		trace_status;			/* `-p' */
112 
113 #ifdef DMAKE_STATISTICS
114 static	Boolean		getname_stat = false;
115 #endif
116 
117 	static	time_t		start_time;
118 	static	int		g_argc;
119 	static	char		**g_argv;
120 
121 /*
122  * File table of contents
123  */
124 	extern "C" void		cleanup_after_exit(void);
125 
126 extern "C" {
127 	extern	void		dmake_exit_callback(void);
128 	extern	void		dmake_message_callback(char *);
129 }
130 
131 extern	Name		normalize_name(register wchar_t *name_string, register int length);
132 
133 extern	int		main(int, char * []);
134 
135 static	void		append_makeflags_string(Name, String);
136 static	void		doalarm(int);
137 static	void		enter_argv_values(int , char **, ASCII_Dyn_Array *);
138 static	void		make_targets(int, char **, Boolean);
139 static	int		parse_command_option(char);
140 static	void		read_command_options(int, char **);
141 static	void		read_environment(Boolean);
142 static	void		read_files_and_state(int, char **);
143 static	Boolean		read_makefile(Name, Boolean, Boolean, Boolean);
144 static	void		report_recursion(Name);
145 static	void		set_sgs_support(void);
146 static	void		setup_for_projectdir(void);
147 static	void		setup_makeflags_argv(void);
148 static	void		report_dir_enter_leave(Boolean entering);
149 
150 extern void expand_value(Name, register String , Boolean);
151 
152 static const char	verstring[] = "illumos make";
153 
154 jmp_buf jmpbuffer;
155 
156 /*
157  *	main(argc, argv)
158  *
159  *	Parameters:
160  *		argc			You know what this is
161  *		argv			You know what this is
162  *
163  *	Static variables used:
164  *		list_all_targets	make -T seen
165  *		trace_status		make -p seen
166  *
167  *	Global variables used:
168  *		debug_level		Should we trace make actions?
169  *		keep_state		Set if .KEEP_STATE seen
170  *		makeflags		The Name "MAKEFLAGS", used to get macro
171  *		remote_command_name	Name of remote invocation cmd ("on")
172  *		running_list		List of parallel running processes
173  *		stdout_stderr_same	true if stdout and stderr are the same
174  *		auto_dependencies	The Name "SUNPRO_DEPENDENCIES"
175  *		temp_file_directory	Set to the dir where we create tmp file
176  *		trace_reader		Set to reflect tracing status
177  *		working_on_targets	Set when building user targets
178  */
179 int
180 main(int argc, char *argv[])
181 {
182 	/*
183 	 * cp is a -> to the value of the MAKEFLAGS env var,
184 	 * which has to be regular chars.
185 	 */
186 	register char		*cp;
187 	char 			make_state_dir[MAXPATHLEN];
188 	Boolean			parallel_flag = false;
189 	Boolean			argv_zero_relative = false;
190 	char			*prognameptr;
191 	char 			*slash_ptr;
192 	mode_t			um;
193 	int			i;
194 	struct itimerval	value;
195 	char			def_dmakerc_path[MAXPATHLEN];
196 	Name			dmake_name, dmake_name2;
197 	Name			dmake_value, dmake_value2;
198 	Property		prop, prop2;
199 	struct stat		statbuf;
200 	int			statval;
201 
202 	struct stat		out_stat, err_stat;
203 	hostid = gethostid();
204 	bsd_signals();
205 
206 	(void) setlocale(LC_ALL, "");
207 
208 
209 #ifdef DMAKE_STATISTICS
210 	if (getenv("DMAKE_STATISTICS")) {
211 		getname_stat = true;
212 	}
213 #endif
214 
215 #ifndef TEXT_DOMAIN
216 #define	TEXT_DOMAIN	"SYS_TEST"
217 #endif
218 	textdomain(TEXT_DOMAIN);
219 
220 	g_argc = argc;
221 	g_argv = (char **) malloc((g_argc + 1) * sizeof(char *));
222 	for (i = 0; i < argc; i++) {
223 		g_argv[i] = argv[i];
224 	}
225 	g_argv[i] = NULL;
226 
227 	/*
228 	 * Set argv_zero_string to some form of argv[0] for
229 	 * recursive MAKE builds.
230 	 */
231 
232 	if (*argv[0] == (int) slash_char) {
233 		/* argv[0] starts with a slash */
234 		argv_zero_string = strdup(argv[0]);
235 	} else if (strchr(argv[0], (int) slash_char) == NULL) {
236 		/* argv[0] contains no slashes */
237 		argv_zero_string = strdup(argv[0]);
238 	} else {
239 		/*
240 		 * argv[0] contains at least one slash,
241 		 * but doesn't start with a slash
242 		 */
243 		char	*tmp_current_path;
244 		char	*tmp_string;
245 
246 		tmp_current_path = get_current_path();
247 		tmp_string = getmem(strlen(tmp_current_path) + 1 +
248 		                    strlen(argv[0]) + 1);
249 		(void) sprintf(tmp_string,
250 		               "%s/%s",
251 		               tmp_current_path,
252 		               argv[0]);
253 		argv_zero_string = strdup(tmp_string);
254 		retmem_mb(tmp_string);
255 		argv_zero_relative = true;
256 	}
257 
258 	/*
259 	 * The following flags are reset if we don't have the
260 	 * (.nse_depinfo or .make.state) files locked and only set
261 	 * AFTER the file has been locked. This ensures that if the user
262 	 * interrupts the program while file_lock() is waiting to lock
263 	 * the file, the interrupt handler doesn't remove a lock
264 	 * that doesn't belong to us.
265 	 */
266 	make_state_lockfile = NULL;
267 	make_state_locked = false;
268 
269 
270 	/*
271 	 * look for last slash char in the path to look at the binary
272 	 * name. This is to resolve the hard link and invoke make
273 	 * in svr4 mode.
274 	 */
275 
276 	/* Sun OS make standart */
277 	svr4 = false;
278 	posix = false;
279 	if(!strcmp(argv_zero_string, "/usr/xpg4/bin/make")) {
280 		svr4 = false;
281 		posix = true;
282 	} else {
283 		prognameptr = strrchr(argv[0], '/');
284 		if(prognameptr) {
285 			prognameptr++;
286 		} else {
287 			prognameptr = argv[0];
288 		}
289 		if(!strcmp(prognameptr, "svr4.make")) {
290 			svr4 = true;
291 			posix = false;
292 		}
293 	}
294 	if (getenv(USE_SVR4_MAKE) || getenv("USE_SVID")){
295 	   svr4 = true;
296 	   posix = false;
297 	}
298 
299 	/*
300 	 * Find the dmake_compat_mode: posix, sun, svr4, or gnu_style, .
301 	 */
302 	char * dmake_compat_mode_var = getenv("SUN_MAKE_COMPAT_MODE");
303 	if (dmake_compat_mode_var != NULL) {
304 		if (0 == strcasecmp(dmake_compat_mode_var, "GNU")) {
305 			gnu_style = true;
306 		}
307 		//svr4 = false;
308 		//posix = false;
309 	}
310 
311 	/*
312 	 * Temporary directory set up.
313 	 */
314 	char * tmpdir_var = getenv("TMPDIR");
315 	if (tmpdir_var != NULL && *tmpdir_var == '/' && strlen(tmpdir_var) < MAXPATHLEN) {
316 		strcpy(mbs_buffer, tmpdir_var);
317 		for (tmpdir_var = mbs_buffer+strlen(mbs_buffer);
318 			*(--tmpdir_var) == '/' && tmpdir_var > mbs_buffer;
319 			*tmpdir_var = '\0');
320 		if (strlen(mbs_buffer) + 32 < MAXPATHLEN) { /* 32 = strlen("/dmake.stdout.%d.%d.XXXXXX") */
321 			sprintf(mbs_buffer2, "%s/dmake.tst.%d.XXXXXX",
322 				mbs_buffer, getpid());
323 			int fd = mkstemp(mbs_buffer2);
324 			if (fd >= 0) {
325 				close(fd);
326 				unlink(mbs_buffer2);
327 				tmpdir = strdup(mbs_buffer);
328 			}
329 		}
330 	}
331 
332 	/* find out if stdout and stderr point to the same place */
333 	if (fstat(1, &out_stat) < 0) {
334 		fatal(gettext("fstat of standard out failed: %s"), errmsg(errno));
335 	}
336 	if (fstat(2, &err_stat) < 0) {
337 		fatal(gettext("fstat of standard error failed: %s"), errmsg(errno));
338 	}
339 	if ((out_stat.st_dev == err_stat.st_dev) &&
340 	    (out_stat.st_ino == err_stat.st_ino)) {
341 		stdout_stderr_same = true;
342 	} else {
343 		stdout_stderr_same = false;
344 	}
345 	/* Make the vroot package scan the path using shell semantics */
346 	set_path_style(0);
347 
348 	setup_char_semantics();
349 
350 	/*
351 	 * If running with .KEEP_STATE, curdir will be set with
352 	 * the connected directory.
353 	 */
354 	(void) atexit(cleanup_after_exit);
355 
356 	load_cached_names();
357 
358 /*
359  *	Set command line flags
360  */
361 	setup_makeflags_argv();
362 	read_command_options(mf_argc, mf_argv);
363 	read_command_options(argc, argv);
364 	if (debug_level > 0) {
365 		cp = getenv(makeflags->string_mb);
366 		(void) printf(gettext("MAKEFLAGS value: %s\n"), cp == NULL ? "" : cp);
367 	}
368 
369 	/*
370 	 * Reset argv_zero_string if it was built from a relative path and the
371 	 * -C option was specified.
372 	 */
373 	if (argv_zero_relative && rebuild_arg0) {
374 		char	*tmp_current_path;
375 		char	*tmp_string;
376 
377 		free(argv_zero_string);
378 		tmp_current_path = get_current_path();
379 		tmp_string = getmem(strlen(tmp_current_path) + 1 +
380 		                    strlen(argv[0]) + 1);
381 		(void) sprintf(tmp_string,
382 		               "%s/%s",
383 		               tmp_current_path,
384 		               argv[0]);
385 		argv_zero_string = strdup(tmp_string);
386 		retmem_mb(tmp_string);
387 	}
388 
389 	setup_for_projectdir();
390 
391 	setup_interrupt(handle_interrupt);
392 
393 	read_files_and_state(argc, argv);
394 
395 	/*
396 	 * Find the dmake_output_mode: TXT1, TXT2 or HTML1.
397 	 */
398 	MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
399 	dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
400 	prop2 = get_prop(dmake_name2->prop, macro_prop);
401 	if (prop2 == NULL) {
402 		/* DMAKE_OUTPUT_MODE not defined, default to TXT1 mode */
403 		output_mode = txt1_mode;
404 	} else {
405 		dmake_value2 = prop2->body.macro.value;
406 		if ((dmake_value2 == NULL) ||
407 		    (IS_EQUAL(dmake_value2->string_mb, "TXT1"))) {
408 			output_mode = txt1_mode;
409 		} else if (IS_EQUAL(dmake_value2->string_mb, "TXT2")) {
410 			output_mode = txt2_mode;
411 		} else if (IS_EQUAL(dmake_value2->string_mb, "HTML1")) {
412 			output_mode = html1_mode;
413 		} else {
414 			warning(gettext("Unsupported value `%s' for DMAKE_OUTPUT_MODE after -x flag (ignored)"),
415 			      dmake_value2->string_mb);
416 		}
417 	}
418 	/*
419 	 * Find the dmake_mode: parallel, or serial.
420 	 */
421     if ((!pmake_cap_r_specified) &&
422         (!pmake_machinesfile_specified)) {
423 	char *s, *b;
424 
425 	if ((s = strdup(argv[0])) == NULL)
426 		fatal(gettext("Out of memory"));
427 
428 	b = basename(s);
429 
430 	MBSTOWCS(wcs_buffer, "DMAKE_MODE");
431 	dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
432 	prop2 = get_prop(dmake_name2->prop, macro_prop);
433 	// If we're invoked as 'make' run serially, regardless of DMAKE_MODE
434 	// If we're invoked as 'make' but passed -j, run parallel
435 	// If we're invoked as 'dmake', without DMAKE_MODE, default parallel
436 	// If we're invoked as 'dmake' and DMAKE_MODE is set, honour it.
437 	if ((strcmp(b, "make") == 0) &&
438 	    !dmake_max_jobs_specified) {
439 		dmake_mode_type = serial_mode;
440 		no_parallel = true;
441 	} else if (prop2 == NULL) {
442 		/* DMAKE_MODE not defined, default based on our name */
443 		if (strcmp(b, "dmake") == 0) {
444 			dmake_mode_type = parallel_mode;
445 			no_parallel = false;
446 		}
447 	} else {
448 		dmake_value2 = prop2->body.macro.value;
449 		if (IS_EQUAL(dmake_value2->string_mb, "parallel")) {
450 			dmake_mode_type = parallel_mode;
451 			no_parallel = false;
452 		} else if (IS_EQUAL(dmake_value2->string_mb, "serial")) {
453 			dmake_mode_type = serial_mode;
454 			no_parallel = true;
455 		} else {
456 			fatal(gettext("Unknown dmake mode argument `%s' after -m flag"), dmake_value2->string_mb);
457 		}
458 	}
459 	free(s);
460     }
461 
462 	parallel_flag = true;
463 	putenv(strdup("DMAKE_CHILD=TRUE"));
464 
465 //
466 // If dmake is running with -t option, set dmake_mode_type to serial.
467 // This is done because doname() calls touch_command() that runs serially.
468 // If we do not do that, maketool will have problems.
469 //
470 	if(touch) {
471 		dmake_mode_type = serial_mode;
472 		no_parallel = true;
473 	}
474 
475 	/*
476 	 * Check whether stdout and stderr are physically same.
477 	 * This is in order to decide whether we need to redirect
478 	 * stderr separately from stdout.
479 	 * This check is performed only if __DMAKE_SEPARATE_STDERR
480 	 * is not set. This variable may be used in order to preserve
481 	 * the 'old' behaviour.
482 	 */
483 	out_err_same = true;
484 	char * dmake_sep_var = getenv("__DMAKE_SEPARATE_STDERR");
485 	if (dmake_sep_var == NULL || (0 != strcasecmp(dmake_sep_var, "NO"))) {
486 		struct stat stdout_stat;
487 		struct stat stderr_stat;
488 		if( (fstat(1, &stdout_stat) == 0)
489 		 && (fstat(2, &stderr_stat) == 0) )
490 		{
491 			if( (stdout_stat.st_dev != stderr_stat.st_dev)
492 			 || (stdout_stat.st_ino != stderr_stat.st_ino) )
493 			{
494 				out_err_same = false;
495 			}
496 		}
497 	}
498 
499 
500 /*
501  *	Enable interrupt handler for alarms
502  */
503         (void) bsd_signal(SIGALRM, (SIG_PF)doalarm);
504 
505 /*
506  *	Check if make should report
507  */
508 	if (getenv(sunpro_dependencies->string_mb) != NULL) {
509 		FILE	*report_file;
510 
511 		report_dependency("");
512 		report_file = get_report_file();
513 		if ((report_file != NULL) && (report_file != (FILE*)-1)) {
514 			(void) fprintf(report_file, "\n");
515 		}
516 	}
517 
518 /*
519  *	Make sure SUNPRO_DEPENDENCIES is exported (or not) properly.
520  */
521 	if (keep_state) {
522 		maybe_append_prop(sunpro_dependencies, macro_prop)->
523 		  body.macro.exported = true;
524 	} else {
525 		maybe_append_prop(sunpro_dependencies, macro_prop)->
526 		  body.macro.exported = false;
527 	}
528 
529 	working_on_targets = true;
530 	if (trace_status) {
531 		dump_make_state();
532 		fclose(stdout);
533 		fclose(stderr);
534 		exit_status = 0;
535 		exit(0);
536 	}
537 	if (list_all_targets) {
538 		dump_target_list();
539 		fclose(stdout);
540 		fclose(stderr);
541 		exit_status = 0;
542 		exit(0);
543 	}
544 	trace_reader = false;
545 
546  	/*
547  	 * Set temp_file_directory to the directory the .make.state
548  	 * file is written to.
549  	 */
550  	if ((slash_ptr = strrchr(make_state->string_mb, (int) slash_char)) == NULL) {
551  		temp_file_directory = strdup(get_current_path());
552  	} else {
553  		*slash_ptr = (int) nul_char;
554  		(void) strcpy(make_state_dir, make_state->string_mb);
555  		*slash_ptr = (int) slash_char;
556 		   /* when there is only one slash and it's the first
557 		   ** character, make_state_dir should point to '/'.
558 		   */
559 		if(make_state_dir[0] == '\0') {
560 		   make_state_dir[0] = '/';
561 		   make_state_dir[1] = '\0';
562 		}
563  		if (make_state_dir[0] == (int) slash_char) {
564  			temp_file_directory = strdup(make_state_dir);
565  		} else {
566  			char	tmp_current_path2[MAXPATHLEN];
567 
568  			(void) sprintf(tmp_current_path2,
569  			               "%s/%s",
570  			               get_current_path(),
571  			               make_state_dir);
572  			temp_file_directory = strdup(tmp_current_path2);
573  		}
574  	}
575 
576 
577 	report_dir_enter_leave(true);
578 
579 	make_targets(argc, argv, parallel_flag);
580 
581 	report_dir_enter_leave(false);
582 
583 	if (build_failed_ever_seen) {
584 		if (posix) {
585 			exit_status = 1;
586 		}
587 		exit(1);
588 	}
589 	exit_status = 0;
590 	exit(0);
591 	/* NOTREACHED */
592 }
593 
594 /*
595  *	cleanup_after_exit()
596  *
597  *	Called from exit(), performs cleanup actions.
598  *
599  *	Parameters:
600  *		status		The argument exit() was called with
601  *		arg		Address of an argument vector to
602  *				cleanup_after_exit()
603  *
604  *	Global variables used:
605  *		command_changed	Set if we think .make.state should be rewritten
606  *		current_line	Is set we set commands_changed
607  *		do_not_exec_rule
608  *				True if -n flag on
609  *		done		The Name ".DONE", rule we run
610  *		keep_state	Set if .KEEP_STATE seen
611  *		parallel	True if building in parallel
612  *		quest		If -q is on we do not run .DONE
613  *		report_dependencies
614  *				True if -P flag on
615  *		running_list	List of parallel running processes
616  *		temp_file_name	The temp file is removed, if any
617  */
618 extern "C" void
619 cleanup_after_exit(void)
620 {
621 	Running		rp;
622 
623 extern long	getname_bytes_count;
624 extern long	getname_names_count;
625 extern long	getname_struct_count;
626 extern long	freename_bytes_count;
627 extern long	freename_names_count;
628 extern long	freename_struct_count;
629 extern long	other_alloc;
630 
631 extern long	env_alloc_num;
632 extern long	env_alloc_bytes;
633 
634 
635 #ifdef DMAKE_STATISTICS
636 if(getname_stat) {
637 	printf(">>> Getname statistics:\n");
638 	printf("  Allocated:\n");
639 	printf("        Names: %ld\n", getname_names_count);
640 	printf("      Strings: %ld Kb (%ld bytes)\n", getname_bytes_count/1000, getname_bytes_count);
641 	printf("      Structs: %ld Kb (%ld bytes)\n", getname_struct_count/1000, getname_struct_count);
642 	printf("  Total bytes: %ld Kb (%ld bytes)\n", getname_struct_count/1000 + getname_bytes_count/1000, getname_struct_count + getname_bytes_count);
643 
644 	printf("\n  Unallocated: %ld\n", freename_names_count);
645 	printf("        Names: %ld\n", freename_names_count);
646 	printf("      Strings: %ld Kb (%ld bytes)\n", freename_bytes_count/1000, freename_bytes_count);
647 	printf("      Structs: %ld Kb (%ld bytes)\n", freename_struct_count/1000, freename_struct_count);
648 	printf("  Total bytes: %ld Kb (%ld bytes)\n", freename_struct_count/1000 + freename_bytes_count/1000, freename_struct_count + freename_bytes_count);
649 
650 	printf("\n  Total used: %ld Kb (%ld bytes)\n", (getname_struct_count/1000 + getname_bytes_count/1000) - (freename_struct_count/1000 + freename_bytes_count/1000), (getname_struct_count + getname_bytes_count) - (freename_struct_count + freename_bytes_count));
651 
652 	printf("\n>>> Other:\n");
653 	printf(
654 		"       Env (%ld): %ld Kb (%ld bytes)\n",
655 		env_alloc_num,
656 		env_alloc_bytes/1000,
657 		env_alloc_bytes
658 	);
659 
660 }
661 #endif
662 
663 	parallel = false;
664 	/* If we used the SVR4_MAKE, don't build .DONE or .FAILED */
665 	if (!getenv(USE_SVR4_MAKE)){
666 	    /* Build the target .DONE or .FAILED if we caught an error */
667 	    if (!quest && !list_all_targets) {
668 		Name		failed_name;
669 
670 		MBSTOWCS(wcs_buffer, ".FAILED");
671 		failed_name = GETNAME(wcs_buffer, FIND_LENGTH);
672 		if ((exit_status != 0) && (failed_name->prop != NULL)) {
673 			/*
674 			 * [tolik] switch DMake to serial mode
675 			 */
676 			dmake_mode_type = serial_mode;
677 			no_parallel = true;
678 			(void) doname(failed_name, false, true);
679 		} else {
680 		    if (!trace_status) {
681 			/*
682 			 * Switch DMake to serial mode
683 			 */
684 			dmake_mode_type = serial_mode;
685 			no_parallel = true;
686 			(void) doname(done, false, true);
687 		    }
688 		}
689 	    }
690 	}
691 	/*
692 	 * Remove the temp file utilities report dependencies thru if it
693 	 * is still around
694 	 */
695 	if (temp_file_name != NULL) {
696 		(void) unlink(temp_file_name->string_mb);
697 	}
698 	/*
699 	 * Do not save the current command in .make.state if make
700 	 * was interrupted.
701 	 */
702 	if (current_line != NULL) {
703 		command_changed = true;
704 		current_line->body.line.command_used = NULL;
705 	}
706 	/*
707 	 * For each parallel build process running, remove the temp files
708 	 * and zap the command line so it won't be put in .make.state
709 	 */
710 	for (rp = running_list; rp != NULL; rp = rp->next) {
711 		if (rp->temp_file != NULL) {
712 			(void) unlink(rp->temp_file->string_mb);
713 		}
714 		if (rp->stdout_file != NULL) {
715 			(void) unlink(rp->stdout_file);
716 			retmem_mb(rp->stdout_file);
717 			rp->stdout_file = NULL;
718 		}
719 		if (rp->stderr_file != NULL) {
720 			(void) unlink(rp->stderr_file);
721 			retmem_mb(rp->stderr_file);
722 			rp->stderr_file = NULL;
723 		}
724 		command_changed = true;
725 /*
726 		line = get_prop(rp->target->prop, line_prop);
727 		if (line != NULL) {
728 			line->body.line.command_used = NULL;
729 		}
730  */
731 	}
732 	/* Remove the statefile lock file if the file has been locked */
733 	if ((make_state_lockfile != NULL) && (make_state_locked)) {
734 		(void) unlink(make_state_lockfile);
735 		make_state_lockfile = NULL;
736 		make_state_locked = false;
737 	}
738 	/* Write .make.state */
739 	write_state_file(1, (Boolean) 1);
740 
741 	job_adjust_fini();
742 }
743 
744 /*
745  *	handle_interrupt()
746  *
747  *	This is where C-C traps are caught.
748  *
749  *	Parameters:
750  *
751  *	Global variables used (except DMake 1.0):
752  *		current_target		Sometimes the current target is removed
753  *		do_not_exec_rule	But not if -n is on
754  *		quest			or -q
755  *		running_list		List of parallel running processes
756  *		touch			Current target is not removed if -t on
757  */
758 void
759 handle_interrupt(int)
760 {
761 	Property		member;
762 	Running			rp;
763 
764 	(void) fflush(stdout);
765 	if (childPid > 0) {
766 		kill(childPid, SIGTERM);
767 		childPid = -1;
768 	}
769 	for (rp = running_list; rp != NULL; rp = rp->next) {
770 		if (rp->state != build_running) {
771 			continue;
772 		}
773 		if (rp->pid > 0) {
774 			kill(rp->pid, SIGTERM);
775 			rp->pid = -1;
776 		}
777 	}
778 	if (getpid() == getpgrp()) {
779 		bsd_signal(SIGTERM, SIG_IGN);
780 		kill (-getpid(), SIGTERM);
781 	}
782 	/* Clean up all parallel children already finished */
783         finish_children(false);
784 
785 	/* Make sure the processes running under us terminate first */
786 
787 	while (wait((int *) NULL) != -1);
788 	/* Delete the current targets unless they are precious */
789 	if ((current_target != NULL) &&
790 	    current_target->is_member &&
791 	    ((member = get_prop(current_target->prop, member_prop)) != NULL)) {
792 		current_target = member->body.member.library;
793 	}
794 	if (!do_not_exec_rule &&
795 	    !touch &&
796 	    !quest &&
797 	    (current_target != NULL) &&
798 	    !(current_target->stat.is_precious || all_precious)) {
799 
800 /* BID_1030811 */
801 /* azv 16 Oct 95 */
802 		current_target->stat.time = file_no_time;
803 
804 		if (exists(current_target) != file_doesnt_exist) {
805 			(void) fprintf(stderr,
806 				       "\n*** %s ",
807 				       current_target->string_mb);
808 			if (current_target->stat.is_dir) {
809 				(void) fprintf(stderr,
810 					       gettext("not removed.\n"),
811 					       current_target->string_mb);
812 			} else if (unlink(current_target->string_mb) == 0) {
813 				(void) fprintf(stderr,
814 					       gettext("removed.\n"),
815 					       current_target->string_mb);
816 			} else {
817 				(void) fprintf(stderr,
818 					       gettext("could not be removed: %s.\n"),
819 					       current_target->string_mb,
820 					       errmsg(errno));
821 			}
822 		}
823 	}
824 	for (rp = running_list; rp != NULL; rp = rp->next) {
825 		if (rp->state != build_running) {
826 			continue;
827 		}
828 		if (rp->target->is_member &&
829 		    ((member = get_prop(rp->target->prop, member_prop)) !=
830 		     NULL)) {
831 			rp->target = member->body.member.library;
832 		}
833 		if (!do_not_exec_rule &&
834 		    !touch &&
835 		    !quest &&
836 		    !(rp->target->stat.is_precious || all_precious)) {
837 
838 			rp->target->stat.time = file_no_time;
839 			if (exists(rp->target) != file_doesnt_exist) {
840 				(void) fprintf(stderr,
841 					       "\n*** %s ",
842 					       rp->target->string_mb);
843 				if (rp->target->stat.is_dir) {
844 					(void) fprintf(stderr,
845 						       gettext("not removed.\n"),
846 						       rp->target->string_mb);
847 				} else if (unlink(rp->target->string_mb) == 0) {
848 					(void) fprintf(stderr,
849 						       gettext("removed.\n"),
850 						       rp->target->string_mb);
851 				} else {
852 					(void) fprintf(stderr,
853 						       gettext("could not be removed: %s.\n"),
854 						       rp->target->string_mb,
855 						       errmsg(errno));
856 				}
857 			}
858 		}
859 	}
860 
861 
862 	/* Have we locked .make.state or .nse_depinfo? */
863 	if ((make_state_lockfile != NULL) && (make_state_locked)) {
864 		unlink(make_state_lockfile);
865 		make_state_lockfile = NULL;
866 		make_state_locked = false;
867 	}
868 	/*
869 	 * Re-read .make.state file (it might be changed by recursive make)
870 	 */
871 	check_state(NULL);
872 
873 	report_dir_enter_leave(false);
874 
875 	exit_status = 2;
876 	exit(2);
877 }
878 
879 /*
880  *	doalarm(sig, ...)
881  *
882  *	Handle the alarm interrupt but do nothing.  Side effect is to
883  *	cause return from wait3.
884  *
885  *	Parameters:
886  *		sig
887  *
888  *	Global variables used:
889  */
890 /*ARGSUSED*/
891 static void
892 doalarm(int)
893 {
894 	return;
895 }
896 
897 
898 /*
899  *	read_command_options(argc, argv)
900  *
901  *	Scan the cmd line options and process the ones that start with "-"
902  *
903  *	Return value:
904  *				-M argument, if any
905  *
906  *	Parameters:
907  *		argc		You know what this is
908  *		argv		You know what this is
909  *
910  *	Global variables used:
911  */
912 static void
913 read_command_options(register int argc, register char **argv)
914 {
915 	register int		ch;
916 	int			current_optind = 1;
917 	int			last_optind_with_double_hyphen = 0;
918 	int			last_optind;
919 	int			last_current_optind;
920 	register int		i;
921 	register int		j;
922 	register int		k;
923 	register int		makefile_next = 0; /*
924 						    * flag to note options:
925 						    * -c, f, g, j, m, o
926 						    */
927 	const char		*tptr;
928 	const char		*CMD_OPTS;
929 
930 	extern char		*optarg;
931 	extern int		optind, opterr, optopt;
932 
933 #define SUNPRO_CMD_OPTS	"-~Bbc:C:Ddef:g:ij:K:kM:m:NnO:o:PpqRrSsTtuVvwx:"
934 
935 #	define SVR4_CMD_OPTS   "-c:C:ef:g:ij:km:nO:o:pqrsTtVv"
936 
937 	/*
938 	 * Added V in SVR4_CMD_OPTS also, which is going to be a hidden
939 	 * option, just to make sure that the getopt doesn't fail when some
940 	 * users leave their USE_SVR4_MAKE set and try to use the makefiles
941 	 * that are designed to issue commands like $(MAKE) -V. Anyway it
942 	 * sets the same flag but ensures that getopt doesn't fail.
943 	 */
944 
945 	opterr = 0;
946 	optind = 1;
947 	while (1) {
948 		last_optind=optind;			/* Save optind and current_optind values */
949 		last_current_optind=current_optind;	/* in case we have to repeat this round. */
950 		if (svr4) {
951 			CMD_OPTS=SVR4_CMD_OPTS;
952 			ch = getopt(argc, argv, SVR4_CMD_OPTS);
953 		} else {
954 			CMD_OPTS=SUNPRO_CMD_OPTS;
955 			ch = getopt(argc, argv, SUNPRO_CMD_OPTS);
956 		}
957 		if (ch == EOF) {
958 			if(optind < argc) {
959 				/*
960 				 * Fixing bug 4102537:
961 				 *    Strange behaviour of command make using -- option.
962 				 * Not all argv have been processed
963 				 * Skip non-flag argv and continue processing.
964 				 */
965 				optind++;
966 				current_optind++;
967 				continue;
968 			} else {
969 				break;
970 			}
971 
972 		}
973 		if (ch == '?') {
974 		 	if (optopt == '-') {
975 				/* Bug 5060758: getopt() changed behavior (s10_60),
976 				 * and now we have to deal with cases when options
977 				 * with double hyphen appear here, from -$(MAKEFLAGS)
978 				 */
979 				i = current_optind;
980 				if (argv[i][0] == '-') {
981 				  if (argv[i][1] == '-') {
982 				    if (argv[i][2] != '\0') {
983 				      /* Check if this option is allowed */
984 				      tptr = strchr(CMD_OPTS, argv[i][2]);
985 				      if (tptr) {
986 				        if (last_optind_with_double_hyphen != current_optind) {
987 				          /* This is first time we are trying to fix "--"
988 				           * problem with this option. If we come here second
989 				           * time, we will go to fatal error.
990 				           */
991 				          last_optind_with_double_hyphen = current_optind;
992 
993 				          /* Eliminate first hyphen character */
994 				          for (j=0; argv[i][j] != '\0'; j++) {
995 				            argv[i][j] = argv[i][j+1];
996 				          }
997 
998 				          /* Repeat the processing of this argument */
999 				          optind=last_optind;
1000 				          current_optind=last_current_optind;
1001 				          continue;
1002 				        }
1003 				      }
1004 				    }
1005 				  }
1006 				}
1007 			}
1008 		}
1009 
1010 		if (ch == '?') {
1011 			if (svr4) {
1012 				fprintf(stderr,
1013 					gettext("Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ][-C directory]\n"));
1014 				fprintf(stderr,
1015 					gettext("              [ -j dmake_max_jobs ][ -m dmake_mode ][ -o dmake_odir ]...\n"));
1016 				fprintf(stderr,
1017 					gettext("              [ -e ][ -i ][ -k ][ -n ][ -p ][ -q ][ -r ][ -s ][ -t ][ -v ]\n"));
1018 				tptr = strchr(SVR4_CMD_OPTS, optopt);
1019 			} else {
1020 				fprintf(stderr,
1021 					gettext("Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ][-C directory]\n"));
1022 				fprintf(stderr,
1023 					gettext("              [ -j dmake_max_jobs ][ -K statefile ][ -m dmake_mode ][ -x MODE_NAME=VALUE ][ -o dmake_odir ]...\n"));
1024 				fprintf(stderr,
1025 					gettext("              [ -d ][ -dd ][ -D ][ -DD ][ -e ][ -i ][ -k ][ -n ][ -p ][ -P ][ -u ][ -w ]\n"));
1026 				fprintf(stderr,
1027 					gettext("              [ -q ][ -r ][ -s ][ -S ][ -t ][ -v ][ -V ][ target... ][ macro=value... ][ \"macro +=value\"... ]\n"));
1028 				tptr = strchr(SUNPRO_CMD_OPTS, optopt);
1029 			}
1030 			if (!tptr) {
1031 				fatal(gettext("Unknown option `-%c'"), optopt);
1032 			} else {
1033 				fatal(gettext("Missing argument after `-%c'"), optopt);
1034 			}
1035 		}
1036 
1037 
1038 
1039 		makefile_next |= parse_command_option(ch);
1040 		/*
1041 		 * If we're done processing all of the options of
1042 		 * ONE argument string...
1043 		 */
1044 		if (current_optind < optind) {
1045 			i = current_optind;
1046 			k = 0;
1047 			/* If there's an argument for an option... */
1048 			if ((optind - current_optind) > 1) {
1049 				k = i + 1;
1050 			}
1051 			switch (makefile_next) {
1052 			case 0:
1053 				argv[i] = NULL;
1054 				/* This shouldn't happen */
1055 				if (k) {
1056 					argv[k] = NULL;
1057 				}
1058 				break;
1059 			case 1:	/* -f seen */
1060 				argv[i] = (char *)"-f";
1061 				break;
1062 			case 2:	/* -c seen */
1063 				argv[i] = (char *)"-c";
1064 				break;
1065 			case 4:	/* -g seen */
1066 				argv[i] = (char *)"-g";
1067 				break;
1068 			case 8:	/* -j seen */
1069 				argv[i] = (char *)"-j";
1070 				break;
1071 			case 16: /* -M seen */
1072 				argv[i] = (char *)"-M";
1073 				break;
1074 			case 32: /* -m seen */
1075 				argv[i] = (char *)"-m";
1076 				break;
1077 			case 128: /* -O seen */
1078 				argv[i] = (char *)"-O";
1079 				break;
1080 			case 256: /* -K seen */
1081 				argv[i] = (char *)"-K";
1082 			        break;
1083 			case 512:	/* -o seen */
1084 				argv[i] = (char *)"-o";
1085 				break;
1086 			case 1024: /* -x seen */
1087 				argv[i] = (char *)"-x";
1088 				break;
1089 			case 2048:
1090 				argv[i] = (char *)"-C";
1091 				break;
1092 			default: /* > 1 of -c, f, g, j, K, M, m, O, o, x seen */
1093 				fatal(gettext("Illegal command line. More than one option requiring\nan argument given in the same argument group"));
1094 			}
1095 
1096 			makefile_next = 0;
1097 			current_optind = optind;
1098 		}
1099 	}
1100 }
1101 
1102 static void
1103 quote_str(char *str, char *qstr)
1104 {
1105 	char		*to;
1106 	char		*from;
1107 
1108 	to = qstr;
1109 	for (from = str; *from; from++) {
1110 		switch (*from) {
1111 		case ';':	/* End of command */
1112 		case '(':	/* Start group */
1113 		case ')':	/* End group */
1114 		case '{':	/* Start group */
1115 		case '}':	/* End group */
1116 		case '[':	/* Reg expr - any of a set of chars */
1117 		case ']':	/* End of set of chars */
1118 		case '|':	/* Pipe or logical-or */
1119 		case '^':	/* Old-fashioned pipe */
1120 		case '&':	/* Background or logical-and */
1121 		case '<':	/* Redirect stdin */
1122 		case '>':	/* Redirect stdout */
1123 		case '*':	/* Reg expr - any sequence of chars */
1124 		case '?':	/* Reg expr - any single char */
1125 		case '$':	/* Variable substitution */
1126 		case '\'':	/* Singe quote - turn off all magic */
1127 		case '"':	/* Double quote - span whitespace */
1128 		case '`':	/* Backquote - run a command */
1129 		case '#':	/* Comment */
1130 		case ' ':	/* Space (for MACRO=value1 value2  */
1131 		case '\\':	/* Escape char - turn off magic of next char */
1132 			*to++ = '\\';
1133 			break;
1134 
1135 		default:
1136 			break;
1137 		}
1138 		*to++ = *from;
1139 	}
1140 	*to = '\0';
1141 }
1142 
1143 static void
1144 unquote_str(char *str, char *qstr)
1145 {
1146 	char		*to;
1147 	char		*from;
1148 
1149 	to = qstr;
1150 	for (from = str; *from; from++) {
1151 		if (*from == '\\') {
1152 			from++;
1153 		}
1154 		*to++ = *from;
1155 	}
1156 	*to = '\0';
1157 }
1158 
1159 /*
1160  * Convert the MAKEFLAGS string value into a vector of char *, similar
1161  * to argv.
1162  */
1163 static void
1164 setup_makeflags_argv()
1165 {
1166 	char		*cp;
1167 	char		*cp1;
1168 	char		*cp2;
1169 	char		*cp3;
1170 	char		*cp_orig;
1171 	Boolean		add_hyphen;
1172 	int		i;
1173 	char		tmp_char;
1174 
1175 	mf_argc = 1;
1176 	cp = getenv(makeflags->string_mb);
1177 	cp_orig = cp;
1178 
1179 	if (cp) {
1180 		/*
1181 		 * If new MAKEFLAGS format, no need to add hyphen.
1182 		 * If old MAKEFLAGS format, add hyphen before flags.
1183 		 */
1184 
1185 		if ((strchr(cp, (int) hyphen_char) != NULL) ||
1186 		    (strchr(cp, (int) equal_char) != NULL)) {
1187 
1188 			/* New MAKEFLAGS format */
1189 
1190 			add_hyphen = false;
1191 
1192 			/* Check if MAKEFLAGS value begins with multiple
1193 			 * hyphen characters, and remove all duplicates.
1194 			 * Usually it happens when the next command is
1195 			 * used: $(MAKE) -$(MAKEFLAGS)
1196 			 *
1197 			 * This was a workaround for BugID 5060758, but
1198 			 * appears to have survived as a fix in make.
1199 			 */
1200 			while (*cp) {
1201 				if (*cp != (int) hyphen_char) {
1202 					break;
1203 				}
1204 				cp++;
1205 				if (*cp == (int) hyphen_char) {
1206 					/* There are two hyphens. Skip one */
1207 					cp_orig = cp;
1208 					cp++;
1209 				}
1210 				if (!(*cp)) {
1211 					/* There are hyphens only. Skip all */
1212 					cp_orig = cp;
1213 					break;
1214 				}
1215 			}
1216 		} else {
1217 
1218 			/* Old MAKEFLAGS format */
1219 
1220 			add_hyphen = true;
1221 		}
1222 	}
1223 
1224 	/* Find the number of arguments in MAKEFLAGS */
1225 	while (cp && *cp) {
1226 		/* Skip white spaces */
1227 		while (cp && *cp && isspace(*cp)) {
1228 			cp++;
1229 		}
1230 		if (cp && *cp) {
1231 			/* Increment arg count */
1232 			mf_argc++;
1233 			/* Go to next white space */
1234 			while (cp && *cp && !isspace(*cp)) {
1235 				if(*cp == (int) backslash_char) {
1236 					cp++;
1237 				}
1238 				cp++;
1239 			}
1240 		}
1241 	}
1242 	/* Allocate memory for the new MAKEFLAGS argv */
1243 	mf_argv = (char **) malloc((mf_argc + 1) * sizeof(char *));
1244 	mf_argv[0] = (char *)"MAKEFLAGS";
1245 	/*
1246 	 * Convert the MAKEFLAGS string value into a vector of char *,
1247 	 * similar to argv.
1248 	 */
1249 	cp = cp_orig;
1250 	for (i = 1; i < mf_argc; i++) {
1251 		/* Skip white spaces */
1252 		while (cp && *cp && isspace(*cp)) {
1253 			cp++;
1254 		}
1255 		if (cp && *cp) {
1256 			cp_orig = cp;
1257 			/* Go to next white space */
1258 			while (cp && *cp && !isspace(*cp)) {
1259 				if(*cp == (int) backslash_char) {
1260 					cp++;
1261 				}
1262 				cp++;
1263 			}
1264 			tmp_char = *cp;
1265 			*cp = (int) nul_char;
1266 			if (add_hyphen) {
1267 				mf_argv[i] = getmem(2 + strlen(cp_orig));
1268 				mf_argv[i][0] = '\0';
1269 				(void) strcat(mf_argv[i], "-");
1270 				// (void) strcat(mf_argv[i], cp_orig);
1271 				unquote_str(cp_orig, mf_argv[i]+1);
1272 			} else {
1273 				mf_argv[i] = getmem(2 + strlen(cp_orig));
1274 				//mf_argv[i] = strdup(cp_orig);
1275 				unquote_str(cp_orig, mf_argv[i]);
1276 			}
1277 			*cp = tmp_char;
1278 		}
1279 	}
1280 	mf_argv[i] = NULL;
1281 }
1282 
1283 /*
1284  *	parse_command_option(ch)
1285  *
1286  *	Parse make command line options.
1287  *
1288  *	Return value:
1289  *				Indicates if any -f -c or -M were seen
1290  *
1291  *	Parameters:
1292  *		ch		The character to parse
1293  *
1294  *	Static variables used:
1295  *		dmake_group_specified	Set for make -g
1296  *		dmake_max_jobs_specified	Set for make -j
1297  *		dmake_mode_specified	Set for make -m
1298  *		dmake_add_mode_specified	Set for make -x
1299  *		dmake_compat_mode_specified	Set for make -x SUN_MAKE_COMPAT_MODE=
1300  *		dmake_output_mode_specified	Set for make -x DMAKE_OUTPUT_MODE=
1301  *		dmake_odir_specified	Set for make -o
1302  *		dmake_rcfile_specified	Set for make -c
1303  *		env_wins		Set for make -e
1304  *		ignore_default_mk	Set for make -r
1305  *		trace_status		Set for make -p
1306  *
1307  *	Global variables used:
1308  *		.make.state path & name set for make -K
1309  *		continue_after_error	Set for make -k
1310  *		debug_level		Set for make -d
1311  *		do_not_exec_rule	Set for make -n
1312  *		filter_stderr		Set for make -X
1313  *		ignore_errors_all	Set for make -i
1314  *		no_parallel		Set for make -R
1315  *		quest			Set for make -q
1316  *		read_trace_level	Set for make -D
1317  *		report_dependencies	Set for make -P
1318  *		silent_all		Set for make -s
1319  *		touch			Set for make -t
1320  */
1321 static int
1322 parse_command_option(register char ch)
1323 {
1324 	static int		invert_next = 0;
1325 	int			invert_this = invert_next;
1326 
1327 	invert_next = 0;
1328 	switch (ch) {
1329 	case '-':			 /* Ignore "--" */
1330 		return 0;
1331 	case '~':			 /* Invert next option */
1332 		invert_next = 1;
1333 		return 0;
1334 	case 'B':			 /* Obsolete */
1335 		return 0;
1336 	case 'b':			 /* Obsolete */
1337 		return 0;
1338 	case 'c':			 /* Read alternative dmakerc file */
1339 		if (invert_this) {
1340 			dmake_rcfile_specified = false;
1341 		} else {
1342 			dmake_rcfile_specified = true;
1343 		}
1344 		return 2;
1345 	case 'C':			/* Change directory */
1346 		return 2048;
1347 	case 'D':			 /* Show lines read */
1348 		if (invert_this) {
1349 			read_trace_level--;
1350 		} else {
1351 			read_trace_level++;
1352 		}
1353 		return 0;
1354 	case 'd':			 /* Debug flag */
1355 		if (invert_this) {
1356 			debug_level--;
1357 		} else {
1358 			debug_level++;
1359 		}
1360 		return 0;
1361 	case 'e':			 /* Environment override flag */
1362 		if (invert_this) {
1363 			env_wins = false;
1364 		} else {
1365 			env_wins = true;
1366 		}
1367 		return 0;
1368 	case 'f':			 /* Read alternative makefile(s) */
1369 		return 1;
1370 	case 'g':			 /* Use alternative DMake group */
1371 		if (invert_this) {
1372 			dmake_group_specified = false;
1373 		} else {
1374 			dmake_group_specified = true;
1375 		}
1376 		return 4;
1377 	case 'i':			 /* Ignore errors */
1378 		if (invert_this) {
1379 			ignore_errors_all = false;
1380 		} else {
1381 			ignore_errors_all = true;
1382 		}
1383 		return 0;
1384 	case 'j':			 /* Use alternative DMake max jobs */
1385 		if (invert_this) {
1386 			dmake_max_jobs_specified = false;
1387 		} else {
1388 			dmake_mode_type = parallel_mode;
1389 			no_parallel = false;
1390 			dmake_max_jobs_specified = true;
1391 		}
1392 		return 8;
1393 	case 'K':			 /* Read alternative .make.state */
1394 		return 256;
1395 	case 'k':			 /* Keep making even after errors */
1396 		if (invert_this) {
1397 			continue_after_error = false;
1398 		} else {
1399 			continue_after_error = true;
1400 			continue_after_error_ever_seen = true;
1401 		}
1402 		return 0;
1403 	case 'M':			 /* Read alternative make.machines file */
1404 		if (invert_this) {
1405 			pmake_machinesfile_specified = false;
1406 		} else {
1407 			pmake_machinesfile_specified = true;
1408 			dmake_mode_type = parallel_mode;
1409 			no_parallel = false;
1410 		}
1411 		return 16;
1412 	case 'm':			 /* Use alternative DMake build mode */
1413 		if (invert_this) {
1414 			dmake_mode_specified = false;
1415 		} else {
1416 			dmake_mode_specified = true;
1417 		}
1418 		return 32;
1419 	case 'x':			 /* Use alternative DMake mode */
1420 		if (invert_this) {
1421 			dmake_add_mode_specified = false;
1422 		} else {
1423 			dmake_add_mode_specified = true;
1424 		}
1425 		return 1024;
1426 	case 'N':			 /* Reverse -n */
1427 		if (invert_this) {
1428 			do_not_exec_rule = true;
1429 		} else {
1430 			do_not_exec_rule = false;
1431 		}
1432 		return 0;
1433 	case 'n':			 /* Print, not exec commands */
1434 		if (invert_this) {
1435 			do_not_exec_rule = false;
1436 		} else {
1437 			do_not_exec_rule = true;
1438 		}
1439 		return 0;
1440 	case 'O':			 /* Integrate with maketool, obsolete */
1441 		return 0;
1442 	case 'o':			 /* Use alternative dmake output dir */
1443 		if (invert_this) {
1444 			dmake_odir_specified = false;
1445 		} else {
1446 			dmake_odir_specified = true;
1447 		}
1448 		return 512;
1449 	case 'P':			 /* Print for selected targets */
1450 		if (invert_this) {
1451 			report_dependencies_level--;
1452 		} else {
1453 			report_dependencies_level++;
1454 		}
1455 		return 0;
1456 	case 'p':			 /* Print description */
1457 		if (invert_this) {
1458 			trace_status = false;
1459 			do_not_exec_rule = false;
1460 		} else {
1461 			trace_status = true;
1462 			do_not_exec_rule = true;
1463 		}
1464 		return 0;
1465 	case 'q':			 /* Question flag */
1466 		if (invert_this) {
1467 			quest = false;
1468 		} else {
1469 			quest = true;
1470 		}
1471 		return 0;
1472 	case 'R':			 /* Don't run in parallel */
1473 		if (invert_this) {
1474 			pmake_cap_r_specified = false;
1475 			no_parallel = false;
1476 		} else {
1477 			pmake_cap_r_specified = true;
1478 			dmake_mode_type = serial_mode;
1479 			no_parallel = true;
1480 		}
1481 		return 0;
1482 	case 'r':			 /* Turn off internal rules */
1483 		if (invert_this) {
1484 			ignore_default_mk = false;
1485 		} else {
1486 			ignore_default_mk = true;
1487 		}
1488 		return 0;
1489 	case 'S':			 /* Reverse -k */
1490 		if (invert_this) {
1491 			continue_after_error = true;
1492 		} else {
1493 			continue_after_error = false;
1494 			stop_after_error_ever_seen = true;
1495 		}
1496 		return 0;
1497 	case 's':			 /* Silent flag */
1498 		if (invert_this) {
1499 			silent_all = false;
1500 		} else {
1501 			silent_all = true;
1502 		}
1503 		return 0;
1504 	case 'T':			 /* Print target list */
1505 		if (invert_this) {
1506 			list_all_targets = false;
1507 			do_not_exec_rule = false;
1508 		} else {
1509 			list_all_targets = true;
1510 			do_not_exec_rule = true;
1511 		}
1512 		return 0;
1513 	case 't':			 /* Touch flag */
1514 		if (invert_this) {
1515 			touch = false;
1516 		} else {
1517 			touch = true;
1518 		}
1519 		return 0;
1520 	case 'u':			 /* Unconditional flag */
1521 		if (invert_this) {
1522 			build_unconditional = false;
1523 		} else {
1524 			build_unconditional = true;
1525 		}
1526 		return 0;
1527 	case 'V':			/* SVR4 mode */
1528 		svr4 = true;
1529 		return 0;
1530 	case 'v':			/* Version flag */
1531 		if (invert_this) {
1532 		} else {
1533 			fprintf(stdout, "%s: %s\n", getprogname(), verstring);
1534 			exit_status = 0;
1535 			exit(0);
1536 		}
1537 		return 0;
1538 	case 'w':			 /* Unconditional flag */
1539 		if (invert_this) {
1540 			report_cwd = false;
1541 		} else {
1542 			report_cwd = true;
1543 		}
1544 		return 0;
1545 #if 0
1546 	case 'X':			/* Filter stdout */
1547 		if (invert_this) {
1548 			filter_stderr = false;
1549 		} else {
1550 			filter_stderr = true;
1551 		}
1552 		return 0;
1553 #endif
1554 	default:
1555 		break;
1556 	}
1557 	return 0;
1558 }
1559 
1560 /*
1561  *	setup_for_projectdir()
1562  *
1563  *	Read the PROJECTDIR variable, if defined, and set the sccs path
1564  *
1565  *	Parameters:
1566  *
1567  *	Global variables used:
1568  *		sccs_dir_path	Set to point to SCCS dir to use
1569  */
1570 static void
1571 setup_for_projectdir(void)
1572 {
1573 static char	path[MAXPATHLEN];
1574 char		cwdpath[MAXPATHLEN];
1575 uid_t uid;
1576 int   done=0;
1577 
1578 	/* Check if we should use PROJECTDIR when reading the SCCS dir. */
1579 	sccs_dir_path = getenv("PROJECTDIR");
1580 	if ((sccs_dir_path != NULL) &&
1581 	    (sccs_dir_path[0] != (int) slash_char)) {
1582 		struct passwd *pwent;
1583 
1584 	     {
1585 		uid = getuid();
1586 		pwent = getpwuid(uid);
1587 		if (pwent == NULL) {
1588 		   fatal(gettext("Bogus USERID "));
1589 		}
1590 		if ((pwent = getpwnam(sccs_dir_path)) == NULL) {
1591 			/*empty block : it'll go & check cwd  */
1592 		}
1593 		else {
1594 		  (void) sprintf(path, "%s/src", pwent->pw_dir);
1595 		  if (access(path, F_OK) == 0) {
1596 			sccs_dir_path = path;
1597 			done = 1;
1598 		  } else {
1599 			(void) sprintf(path, "%s/source", pwent->pw_dir);
1600 			if (access(path, F_OK) == 0) {
1601 				sccs_dir_path = path;
1602 				done = 1;
1603 			}
1604 		     }
1605 		}
1606 		if (!done) {
1607 		    if (getcwd(cwdpath, MAXPATHLEN - 1 )) {
1608 
1609 		       (void) sprintf(path, "%s/%s", cwdpath,sccs_dir_path);
1610 		       if (access(path, F_OK) == 0) {
1611 		        	sccs_dir_path = path;
1612 				done = 1;
1613 		        } else {
1614 		  	       	fatal(gettext("Bogus PROJECTDIR '%s'"), sccs_dir_path);
1615 		        }
1616 		    }
1617 		}
1618 	   }
1619 	}
1620 }
1621 
1622 char *
1623 make_install_prefix(void)
1624 {
1625 	int ret;
1626 	char origin[PATH_MAX];
1627 	char *dir;
1628 
1629 	if ((ret = readlink("/proc/self/path/a.out", origin,
1630 	    PATH_MAX - 1)) < 0)
1631 		fatal("failed to read origin from /proc\n");
1632 
1633 
1634 	origin[ret] = '\0';
1635 	return strdup(dirname(origin));
1636 }
1637 
1638 static char *
1639 add_to_env(const char *var, const char *value, const char *fallback)
1640 {
1641 	const char *oldpath;
1642 	char *newpath;
1643 
1644 	oldpath = getenv(var);
1645 	if (oldpath == NULL) {
1646 		if (value != NULL) {
1647 			asprintf(&newpath, "%s=%s",
1648 			    var, value);
1649 		} else {
1650 			asprintf(&newpath, "%s=%s",
1651 			    var, fallback);
1652 		}
1653 	} else {
1654 		if (value != NULL) {
1655 			asprintf(&newpath, "%s=%s:%s",
1656 			    var, oldpath, value);
1657 		} else {
1658 			asprintf(&newpath, "%s=%s:%s",
1659 			    var, oldpath, fallback);
1660 		}
1661 	}
1662 
1663 	return (newpath);
1664 }
1665 
1666 /*
1667  *	set_sgs_support()
1668  *
1669  *	Add the libmakestate.so.1 lib to the env var SGS_SUPPORT
1670  *	  if it's not already in there.
1671  *	The SGS_SUPPORT env var and libmakestate.so.1 is used by
1672  *	  the linker ld to report .make.state info back to make.
1673  *
1674  * In the new world we always will set the 32-bit and 64-bit versions of this
1675  * variable explicitly so that we can take into account the correct isa and our
1676  * prefix. So say that the prefix was /opt/local. Then we would want to search
1677  * /opt/local/lib/libmakestate.so.1:libmakestate.so.1. We still want to search
1678  * the original location just as a safety measure.
1679  */
1680 static void
1681 set_sgs_support()
1682 {
1683 	int		len;
1684 	char		*newpath, *newpath64;
1685 	char 		*lib32, *lib64;
1686 	static char	*prev_path, *prev_path64;
1687 	char		*origin = make_install_prefix();
1688 	struct stat st;
1689 
1690 	asprintf(&lib32, "%s/%s/%s", origin, "../lib",
1691 	    LD_SUPPORT_MAKE_LIB);
1692 
1693 	if (stat(lib32, &st) != 0) {
1694 		free(lib32);
1695 		// Try the tools path
1696 		asprintf(&lib32, "%s/%s/%s/%s", origin, "../../lib/",
1697 		    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
1698 
1699 		if (stat(lib32, &st) != 0) {
1700 			free(lib32);
1701 			lib32 = NULL;
1702 		}
1703 	}
1704 
1705 	asprintf(&lib64, "%s/%s/64/%s", origin, "../lib",
1706 	    LD_SUPPORT_MAKE_LIB);
1707 
1708 	if (stat(lib64, &st) != 0) {
1709 		free(lib64);
1710 		// Try the tools path
1711 		asprintf(&lib64, "%s/%s/%s/64/%s", origin, "../../lib/",
1712 		    LD_SUPPORT_MAKE_ARCH, LD_SUPPORT_MAKE_LIB);
1713 
1714 		if (stat(lib64, &st) != 0) {
1715 			free(lib64);
1716 			lib64 = NULL;
1717 		}
1718 	}
1719 
1720 	newpath = add_to_env(LD_SUPPORT_ENV_VAR_32, lib32, LD_SUPPORT_MAKE_LIB);
1721 	newpath64 = add_to_env(LD_SUPPORT_ENV_VAR_64, lib64, LD_SUPPORT_MAKE_LIB);
1722 
1723 	putenv(newpath);
1724 	if (prev_path) {
1725 		free(prev_path);
1726 	}
1727 	prev_path = newpath;
1728 
1729 	putenv(newpath64);
1730 	if (prev_path64) {
1731 		free(prev_path64);
1732 	}
1733 	prev_path64 = newpath64;
1734 	free(lib32);
1735 	free(lib64);
1736 	free(origin);
1737 }
1738 
1739 /*
1740  *	read_files_and_state(argc, argv)
1741  *
1742  *	Read the makefiles we care about and the environment
1743  *	Also read the = style command line options
1744  *
1745  *	Parameters:
1746  *		argc		You know what this is
1747  *		argv		You know what this is
1748  *
1749  *	Static variables used:
1750  *		env_wins	make -e, determines if env vars are RO
1751  *		ignore_default_mk make -r, determines if make.rules is read
1752  *		not_auto_depen	dwight
1753  *
1754  *	Global variables used:
1755  *		default_target_to_build	Set to first proper target from file
1756  *		do_not_exec_rule Set to false when makfile is made
1757  *		dot		The Name ".", used to read current dir
1758  *		empty_name	The Name "", use as macro value
1759  *		keep_state	Set if KEEP_STATE is in environment
1760  *		make_state	The Name ".make.state", used to read file
1761  *		makefile_type	Set to type of file being read
1762  *		makeflags	The Name "MAKEFLAGS", used to set macro value
1763  *		not_auto	dwight
1764  *		read_trace_level Checked to se if the reader should trace
1765  *		report_dependencies If -P is on we do not read .make.state
1766  *		trace_reader	Set if reader should trace
1767  *		virtual_root	The Name "VIRTUAL_ROOT", used to check value
1768  */
1769 static void
1770 read_files_and_state(int argc, char **argv)
1771 {
1772 	wchar_t			buffer[1000];
1773 	wchar_t			buffer_posix[1000];
1774 	register char		ch;
1775 	register char		*cp;
1776 	Property		def_make_macro = NULL;
1777 	Name			def_make_name;
1778 	Name			default_makefile;
1779 	String_rec		dest;
1780 	wchar_t			destbuffer[STRING_BUFFER_LENGTH];
1781 	register int		i;
1782 	register int		j;
1783 	Name			keep_state_name;
1784 	int			length;
1785 	Name			Makefile;
1786 	register Property	macro;
1787 	struct stat		make_state_stat;
1788 	Name			makefile_name;
1789         register int		makefile_next = 0;
1790 	register Boolean	makefile_read = false;
1791 	String_rec		makeflags_string;
1792 	String_rec		makeflags_string_posix;
1793 	String_rec *		makeflags_string_current;
1794 	Name			makeflags_value_saved;
1795 	register Name		name;
1796 	Name			new_make_value;
1797 	Boolean			save_do_not_exec_rule;
1798 	static wchar_t		state_file_str;
1799 	static char		state_file_str_mb[MAXPATHLEN];
1800 	static struct _Name	state_filename;
1801 	Boolean			temp;
1802 	char			tmp_char;
1803 	wchar_t			*tmp_wcs_buffer;
1804 	register Name		value;
1805 	ASCII_Dyn_Array		makeflags_and_macro;
1806 	Boolean			is_xpg4;
1807 
1808 /*
1809  *	Remember current mode. It may be changed after reading makefile
1810  *	and we will have to correct MAKEFLAGS variable.
1811  */
1812 	is_xpg4 = posix;
1813 
1814 	MBSTOWCS(wcs_buffer, "KEEP_STATE");
1815 	keep_state_name = GETNAME(wcs_buffer, FIND_LENGTH);
1816 	MBSTOWCS(wcs_buffer, "Makefile");
1817 	Makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1818 	MBSTOWCS(wcs_buffer, "makefile");
1819 	makefile_name = GETNAME(wcs_buffer, FIND_LENGTH);
1820 
1821 /*
1822  *	initialize global dependency entry for .NOT_AUTO
1823  */
1824 	not_auto_depen->next = NULL;
1825 	not_auto_depen->name = not_auto;
1826 	not_auto_depen->automatic = not_auto_depen->stale = false;
1827 
1828 /*
1829  *	Read internal definitions and rules.
1830  */
1831 	if (read_trace_level > 1) {
1832 		trace_reader = true;
1833 	}
1834 	if (!ignore_default_mk) {
1835 		if (svr4) {
1836 			MBSTOWCS(wcs_buffer, "svr4.make.rules");
1837 			default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1838 		} else {
1839 			MBSTOWCS(wcs_buffer, "make.rules");
1840 			default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1841 		}
1842 		default_makefile->stat.is_file = true;
1843 
1844 		(void) read_makefile(default_makefile,
1845 				     true,
1846 				     false,
1847 				     true);
1848 	}
1849 
1850 	/*
1851 	 * If the user did not redefine the MAKE macro in the
1852 	 * default makefile (make.rules), then we'd like to
1853 	 * change the macro value of MAKE to be some form
1854 	 * of argv[0] for recursive MAKE builds.
1855 	 */
1856 	MBSTOWCS(wcs_buffer, "MAKE");
1857 	def_make_name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
1858 	def_make_macro = get_prop(def_make_name->prop, macro_prop);
1859 	if ((def_make_macro != NULL) &&
1860 	    (IS_EQUAL(def_make_macro->body.macro.value->string_mb,
1861 	              "make"))) {
1862 		MBSTOWCS(wcs_buffer, argv_zero_string);
1863 		new_make_value = GETNAME(wcs_buffer, wcslen(wcs_buffer));
1864 		(void) SETVAR(def_make_name,
1865 		              new_make_value,
1866 		              false);
1867 	}
1868 
1869 	default_target_to_build = NULL;
1870 	trace_reader = false;
1871 
1872 /*
1873  *	Read environment args. Let file args which follow override unless
1874  *	-e option seen. If -e option is not mentioned.
1875  */
1876 	read_environment(env_wins);
1877 	if (getvar(virtual_root)->hash.length == 0) {
1878 		maybe_append_prop(virtual_root, macro_prop)
1879 		  ->body.macro.exported = true;
1880 		MBSTOWCS(wcs_buffer, "/");
1881 		(void) SETVAR(virtual_root,
1882 			      GETNAME(wcs_buffer, FIND_LENGTH),
1883 			      false);
1884 	}
1885 
1886 /*
1887  * We now scan mf_argv and argv to see if we need to set
1888  * any of the DMake-added options/variables in MAKEFLAGS.
1889  */
1890 
1891 	makeflags_and_macro.start = 0;
1892 	makeflags_and_macro.size = 0;
1893 	enter_argv_values(mf_argc, mf_argv, &makeflags_and_macro);
1894 	enter_argv_values(argc, argv, &makeflags_and_macro);
1895 
1896 /*
1897  *	Set MFLAGS and MAKEFLAGS
1898  *
1899  *	Before reading makefile we do not know exactly which mode
1900  *	(posix or not) is used. So prepare two MAKEFLAGS strings
1901  *	for both posix and solaris modes because they are different.
1902  */
1903 	INIT_STRING_FROM_STACK(makeflags_string, buffer);
1904 	INIT_STRING_FROM_STACK(makeflags_string_posix, buffer_posix);
1905 	append_char((int) hyphen_char, &makeflags_string);
1906 	append_char((int) hyphen_char, &makeflags_string_posix);
1907 
1908 	switch (read_trace_level) {
1909 	case 2:
1910 		append_char('D', &makeflags_string);
1911 		append_char('D', &makeflags_string_posix);
1912 		/* FALLTHROUGH */
1913 	case 1:
1914 		append_char('D', &makeflags_string);
1915 		append_char('D', &makeflags_string_posix);
1916 	}
1917 	switch (debug_level) {
1918 	case 2:
1919 		append_char('d', &makeflags_string);
1920 		append_char('d', &makeflags_string_posix);
1921 		/* FALLTHROUGH */
1922 	case 1:
1923 		append_char('d', &makeflags_string);
1924 		append_char('d', &makeflags_string_posix);
1925 	}
1926 	if (env_wins) {
1927 		append_char('e', &makeflags_string);
1928 		append_char('e', &makeflags_string_posix);
1929 	}
1930 	if (ignore_errors_all) {
1931 		append_char('i', &makeflags_string);
1932 		append_char('i', &makeflags_string_posix);
1933 	}
1934 	if (continue_after_error) {
1935 		if (stop_after_error_ever_seen) {
1936 			append_char('S', &makeflags_string_posix);
1937 			append_char((int) space_char, &makeflags_string_posix);
1938 			append_char((int) hyphen_char, &makeflags_string_posix);
1939 		}
1940 		append_char('k', &makeflags_string);
1941 		append_char('k', &makeflags_string_posix);
1942 	} else {
1943 		if (stop_after_error_ever_seen
1944 		    && continue_after_error_ever_seen) {
1945 			append_char('k', &makeflags_string_posix);
1946 			append_char((int) space_char, &makeflags_string_posix);
1947 			append_char((int) hyphen_char, &makeflags_string_posix);
1948 			append_char('S', &makeflags_string_posix);
1949 		}
1950 	}
1951 	if (do_not_exec_rule) {
1952 		append_char('n', &makeflags_string);
1953 		append_char('n', &makeflags_string_posix);
1954 	}
1955 	switch (report_dependencies_level) {
1956 	case 4:
1957 		append_char('P', &makeflags_string);
1958 		append_char('P', &makeflags_string_posix);
1959 		/* FALLTHROUGH */
1960 	case 3:
1961 		append_char('P', &makeflags_string);
1962 		append_char('P', &makeflags_string_posix);
1963 		/* FALLTHROUGH */
1964 	case 2:
1965 		append_char('P', &makeflags_string);
1966 		append_char('P', &makeflags_string_posix);
1967 		/* FALLTHROUGH */
1968 	case 1:
1969 		append_char('P', &makeflags_string);
1970 		append_char('P', &makeflags_string_posix);
1971 	}
1972 	if (trace_status) {
1973 		append_char('p', &makeflags_string);
1974 		append_char('p', &makeflags_string_posix);
1975 	}
1976 	if (quest) {
1977 		append_char('q', &makeflags_string);
1978 		append_char('q', &makeflags_string_posix);
1979 	}
1980 	if (silent_all) {
1981 		append_char('s', &makeflags_string);
1982 		append_char('s', &makeflags_string_posix);
1983 	}
1984 	if (touch) {
1985 		append_char('t', &makeflags_string);
1986 		append_char('t', &makeflags_string_posix);
1987 	}
1988 	if (build_unconditional) {
1989 		append_char('u', &makeflags_string);
1990 		append_char('u', &makeflags_string_posix);
1991 	}
1992 	if (report_cwd) {
1993 		append_char('w', &makeflags_string);
1994 		append_char('w', &makeflags_string_posix);
1995 	}
1996 	/* -c dmake_rcfile */
1997 	if (dmake_rcfile_specified) {
1998 		MBSTOWCS(wcs_buffer, "DMAKE_RCFILE");
1999 		dmake_rcfile = GETNAME(wcs_buffer, FIND_LENGTH);
2000 		append_makeflags_string(dmake_rcfile, &makeflags_string);
2001 		append_makeflags_string(dmake_rcfile, &makeflags_string_posix);
2002 	}
2003 	/* -g dmake_group */
2004 	if (dmake_group_specified) {
2005 		MBSTOWCS(wcs_buffer, "DMAKE_GROUP");
2006 		dmake_group = GETNAME(wcs_buffer, FIND_LENGTH);
2007 		append_makeflags_string(dmake_group, &makeflags_string);
2008 		append_makeflags_string(dmake_group, &makeflags_string_posix);
2009 	}
2010 	/* -j dmake_max_jobs */
2011 	if (dmake_max_jobs_specified) {
2012 		MBSTOWCS(wcs_buffer, "DMAKE_MAX_JOBS");
2013 		dmake_max_jobs = GETNAME(wcs_buffer, FIND_LENGTH);
2014 		append_makeflags_string(dmake_max_jobs, &makeflags_string);
2015 		append_makeflags_string(dmake_max_jobs, &makeflags_string_posix);
2016 	}
2017 	/* -m dmake_mode */
2018 	if (dmake_mode_specified) {
2019 		MBSTOWCS(wcs_buffer, "DMAKE_MODE");
2020 		dmake_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2021 		append_makeflags_string(dmake_mode, &makeflags_string);
2022 		append_makeflags_string(dmake_mode, &makeflags_string_posix);
2023 	}
2024 	/* -x dmake_compat_mode */
2025 //	if (dmake_compat_mode_specified) {
2026 //		MBSTOWCS(wcs_buffer, "SUN_MAKE_COMPAT_MODE");
2027 //		dmake_compat_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2028 //		append_makeflags_string(dmake_compat_mode, &makeflags_string);
2029 //		append_makeflags_string(dmake_compat_mode, &makeflags_string_posix);
2030 //	}
2031 	/* -x dmake_output_mode */
2032 	if (dmake_output_mode_specified) {
2033 		MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
2034 		dmake_output_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2035 		append_makeflags_string(dmake_output_mode, &makeflags_string);
2036 		append_makeflags_string(dmake_output_mode, &makeflags_string_posix);
2037 	}
2038 	/* -o dmake_odir */
2039 	if (dmake_odir_specified) {
2040 		MBSTOWCS(wcs_buffer, "DMAKE_ODIR");
2041 		dmake_odir = GETNAME(wcs_buffer, FIND_LENGTH);
2042 		append_makeflags_string(dmake_odir, &makeflags_string);
2043 		append_makeflags_string(dmake_odir, &makeflags_string_posix);
2044 	}
2045 	/* -M pmake_machinesfile */
2046 	if (pmake_machinesfile_specified) {
2047 		MBSTOWCS(wcs_buffer, "PMAKE_MACHINESFILE");
2048 		pmake_machinesfile = GETNAME(wcs_buffer, FIND_LENGTH);
2049 		append_makeflags_string(pmake_machinesfile, &makeflags_string);
2050 		append_makeflags_string(pmake_machinesfile, &makeflags_string_posix);
2051 	}
2052 	/* -R */
2053 	if (pmake_cap_r_specified) {
2054 		append_char((int) space_char, &makeflags_string);
2055 		append_char((int) hyphen_char, &makeflags_string);
2056 		append_char('R', &makeflags_string);
2057 		append_char((int) space_char, &makeflags_string_posix);
2058 		append_char((int) hyphen_char, &makeflags_string_posix);
2059 		append_char('R', &makeflags_string_posix);
2060 	}
2061 
2062 /*
2063  *	Make sure MAKEFLAGS is exported
2064  */
2065 	maybe_append_prop(makeflags, macro_prop)->
2066 	  body.macro.exported = true;
2067 
2068 	if (makeflags_string.buffer.start[1] != (int) nul_char) {
2069 		if (makeflags_string.buffer.start[1] != (int) space_char) {
2070 			MBSTOWCS(wcs_buffer, "MFLAGS");
2071 			(void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2072 				      GETNAME(makeflags_string.buffer.start,
2073 					      FIND_LENGTH),
2074 				      false);
2075 		} else {
2076 			MBSTOWCS(wcs_buffer, "MFLAGS");
2077 			(void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2078 				      GETNAME(makeflags_string.buffer.start + 2,
2079 					      FIND_LENGTH),
2080 				      false);
2081 		}
2082 	}
2083 
2084 /*
2085  *	Add command line macro to POSIX makeflags_string
2086  */
2087 	if (makeflags_and_macro.start) {
2088 		tmp_char = (char) space_char;
2089 		cp = makeflags_and_macro.start;
2090 		do {
2091 			append_char(tmp_char, &makeflags_string_posix);
2092 		} while ( tmp_char = *cp++ );
2093 		retmem_mb(makeflags_and_macro.start);
2094 	}
2095 
2096 /*
2097  *	Now set the value of MAKEFLAGS macro in accordance
2098  *	with current mode.
2099  */
2100 	macro = maybe_append_prop(makeflags, macro_prop);
2101 	temp = (Boolean) macro->body.macro.read_only;
2102 	macro->body.macro.read_only = false;
2103 	if(posix || gnu_style) {
2104 		makeflags_string_current = &makeflags_string_posix;
2105 	} else {
2106 		makeflags_string_current = &makeflags_string;
2107 	}
2108 	if (makeflags_string_current->buffer.start[1] == (int) nul_char) {
2109 		makeflags_value_saved =
2110 			GETNAME( makeflags_string_current->buffer.start + 1
2111 			       , FIND_LENGTH
2112 			       );
2113 	} else {
2114 		if (makeflags_string_current->buffer.start[1] != (int) space_char) {
2115 			makeflags_value_saved =
2116 				GETNAME( makeflags_string_current->buffer.start
2117 				       , FIND_LENGTH
2118 				       );
2119 		} else {
2120 			makeflags_value_saved =
2121 				GETNAME( makeflags_string_current->buffer.start + 2
2122 				       , FIND_LENGTH
2123 				       );
2124 		}
2125 	}
2126 	(void) SETVAR( makeflags
2127 	             , makeflags_value_saved
2128 	             , false
2129 	             );
2130 	macro->body.macro.read_only = temp;
2131 
2132 /*
2133  *	Read command line "-f" arguments and ignore -c, g, j, K, M, m, O and o args.
2134  */
2135 	save_do_not_exec_rule = do_not_exec_rule;
2136 	do_not_exec_rule = false;
2137 	if (read_trace_level > 0) {
2138 		trace_reader = true;
2139 	}
2140 
2141 	for (i = 1; i < argc; i++) {
2142 		if (argv[i] &&
2143 		    (argv[i][0] == (int) hyphen_char) &&
2144 		    (argv[i][1] == 'f') &&
2145 		    (argv[i][2] == (int) nul_char)) {
2146 			argv[i] = NULL;		/* Remove -f */
2147 			if (i >= argc - 1) {
2148 				fatal(gettext("No filename argument after -f flag"));
2149 			}
2150 			MBSTOWCS(wcs_buffer, argv[++i]);
2151 			primary_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
2152 			(void) read_makefile(primary_makefile, true, true, true);
2153 			argv[i] = NULL;		/* Remove filename */
2154 			makefile_read = true;
2155 		} else if (argv[i] &&
2156 			   (argv[i][0] == (int) hyphen_char) &&
2157 			   (argv[i][1] == 'c' ||
2158 			    argv[i][1] == 'g' ||
2159 			    argv[i][1] == 'j' ||
2160 			    argv[i][1] == 'K' ||
2161 			    argv[i][1] == 'M' ||
2162 			    argv[i][1] == 'm' ||
2163 			    argv[i][1] == 'O' ||
2164 			    argv[i][1] == 'o') &&
2165 			   (argv[i][2] == (int) nul_char)) {
2166 			argv[i] = NULL;
2167 			argv[++i] = NULL;
2168 		}
2169 	}
2170 
2171 /*
2172  *	If no command line "-f" args then look for "makefile", and then for
2173  *	"Makefile" if "makefile" isn't found.
2174  */
2175 	if (!makefile_read) {
2176 		(void) read_dir(dot,
2177 				(wchar_t *) NULL,
2178 				(Property) NULL,
2179 				(wchar_t *) NULL);
2180 	    if (!posix) {
2181 		if (makefile_name->stat.is_file) {
2182 			if (Makefile->stat.is_file) {
2183 				warning(gettext("Both `makefile' and `Makefile' exist"));
2184 			}
2185 			primary_makefile = makefile_name;
2186 			makefile_read = read_makefile(makefile_name,
2187 						      false,
2188 						      false,
2189 						      true);
2190 		}
2191 		if (!makefile_read &&
2192 		    Makefile->stat.is_file) {
2193 			primary_makefile = Makefile;
2194 			makefile_read = read_makefile(Makefile,
2195 						      false,
2196 						      false,
2197 						      true);
2198 		}
2199 	    } else {
2200 
2201 		enum sccs_stat save_m_has_sccs = NO_SCCS;
2202 		enum sccs_stat save_M_has_sccs = NO_SCCS;
2203 
2204 		if (makefile_name->stat.is_file) {
2205 			if (Makefile->stat.is_file) {
2206 				warning(gettext("Both `makefile' and `Makefile' exist"));
2207 			}
2208 		}
2209 		if (makefile_name->stat.is_file) {
2210 			if (makefile_name->stat.has_sccs == NO_SCCS) {
2211 			   primary_makefile = makefile_name;
2212 			   makefile_read = read_makefile(makefile_name,
2213 						      false,
2214 						      false,
2215 						      true);
2216 			} else {
2217 			  save_m_has_sccs = makefile_name->stat.has_sccs;
2218 			  makefile_name->stat.has_sccs = NO_SCCS;
2219 			  primary_makefile = makefile_name;
2220 			  makefile_read = read_makefile(makefile_name,
2221 						      false,
2222 						      false,
2223 						      true);
2224 			}
2225 		}
2226 		if (!makefile_read &&
2227 		    Makefile->stat.is_file) {
2228 			if (Makefile->stat.has_sccs == NO_SCCS) {
2229 			   primary_makefile = Makefile;
2230 			   makefile_read = read_makefile(Makefile,
2231 						      false,
2232 						      false,
2233 						      true);
2234 			} else {
2235 			  save_M_has_sccs = Makefile->stat.has_sccs;
2236 			  Makefile->stat.has_sccs = NO_SCCS;
2237 			  primary_makefile = Makefile;
2238 			  makefile_read = read_makefile(Makefile,
2239 						      false,
2240 						      false,
2241 						      true);
2242 			}
2243 		}
2244 		if (!makefile_read &&
2245 		        makefile_name->stat.is_file) {
2246 			   makefile_name->stat.has_sccs = save_m_has_sccs;
2247 			   primary_makefile = makefile_name;
2248 			   makefile_read = read_makefile(makefile_name,
2249 						      false,
2250 						      false,
2251 						      true);
2252 		}
2253 		if (!makefile_read &&
2254 		    Makefile->stat.is_file) {
2255 			   Makefile->stat.has_sccs = save_M_has_sccs;
2256 			   primary_makefile = Makefile;
2257 			   makefile_read = read_makefile(Makefile,
2258 						      false,
2259 						      false,
2260 						      true);
2261 		}
2262 	    }
2263 	}
2264 	do_not_exec_rule = save_do_not_exec_rule;
2265 	allrules_read = makefile_read;
2266 	trace_reader = false;
2267 
2268 /*
2269  *	Now get current value of MAKEFLAGS and compare it with
2270  *	the saved value we set before reading makefile.
2271  *	If they are different then MAKEFLAGS is subsequently set by
2272  *	makefile, just leave it there. Otherwise, if make mode
2273  *	is changed by using .POSIX target in makefile we need
2274  *	to correct MAKEFLAGS value.
2275  */
2276 	Name mf_val = getvar(makeflags);
2277 	if( (posix != is_xpg4)
2278 	 && (!strcmp(mf_val->string_mb, makeflags_value_saved->string_mb)))
2279 	{
2280 		if (makeflags_string_posix.buffer.start[1] == (int) nul_char) {
2281 			(void) SETVAR(makeflags,
2282 				      GETNAME(makeflags_string_posix.buffer.start + 1,
2283 					      FIND_LENGTH),
2284 				      false);
2285 		} else {
2286 			if (makeflags_string_posix.buffer.start[1] != (int) space_char) {
2287 				(void) SETVAR(makeflags,
2288 					      GETNAME(makeflags_string_posix.buffer.start,
2289 						      FIND_LENGTH),
2290 					      false);
2291 			} else {
2292 				(void) SETVAR(makeflags,
2293 					      GETNAME(makeflags_string_posix.buffer.start + 2,
2294 						      FIND_LENGTH),
2295 					      false);
2296 			}
2297 		}
2298 	}
2299 
2300 	if (makeflags_string.free_after_use) {
2301 		retmem(makeflags_string.buffer.start);
2302 	}
2303 	if (makeflags_string_posix.free_after_use) {
2304 		retmem(makeflags_string_posix.buffer.start);
2305 	}
2306 	makeflags_string.buffer.start = NULL;
2307 	makeflags_string_posix.buffer.start = NULL;
2308 
2309 	if (posix) {
2310 		/*
2311 		 * If the user did not redefine the ARFLAGS macro in the
2312 		 * default makefile (make.rules), then we'd like to
2313 		 * change the macro value of ARFLAGS to be in accordance
2314 		 * with "POSIX" requirements.
2315 		 */
2316 		MBSTOWCS(wcs_buffer, "ARFLAGS");
2317 		name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2318 		macro = get_prop(name->prop, macro_prop);
2319 		if ((macro != NULL) && /* Maybe (macro == NULL) || ? */
2320 		    (IS_EQUAL(macro->body.macro.value->string_mb,
2321 		              "rv"))) {
2322 			MBSTOWCS(wcs_buffer, "-rv");
2323 			value = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2324 			(void) SETVAR(name,
2325 			              value,
2326 			              false);
2327 		}
2328 	}
2329 
2330 	if (!posix && !svr4) {
2331 		set_sgs_support();
2332 	}
2333 
2334 
2335 /*
2336  *	Make sure KEEP_STATE is in the environment if KEEP_STATE is on.
2337  */
2338 	macro = get_prop(keep_state_name->prop, macro_prop);
2339 	if ((macro != NULL) &&
2340 	    macro->body.macro.exported) {
2341 		keep_state = true;
2342 	}
2343 	if (keep_state) {
2344 		if (macro == NULL) {
2345 			macro = maybe_append_prop(keep_state_name,
2346 						  macro_prop);
2347 		}
2348 		macro->body.macro.exported = true;
2349 		(void) SETVAR(keep_state_name,
2350 			      empty_name,
2351 			      false);
2352 
2353 		/*
2354 		 *	Read state file
2355 		 */
2356 
2357 		/* Before we read state, let's make sure we have
2358 		** right state file.
2359 		*/
2360 		/* just in case macro references are used in make_state file
2361 		** name, we better expand them at this stage using expand_value.
2362 		*/
2363 		INIT_STRING_FROM_STACK(dest, destbuffer);
2364 		expand_value(make_state, &dest, false);
2365 
2366 		make_state = GETNAME(dest.buffer.start, FIND_LENGTH);
2367 
2368 		if(!stat(make_state->string_mb, &make_state_stat)) {
2369 		   if(!(make_state_stat.st_mode & S_IFREG) ) {
2370 			/* copy the make_state structure to the other
2371 			** and then let make_state point to the new
2372 			** one.
2373 			*/
2374 		      memcpy(&state_filename, make_state,sizeof(state_filename));
2375 		      state_filename.string_mb = state_file_str_mb;
2376 		/* Just a kludge to avoid two slashes back to back */
2377 		      if((make_state->hash.length == 1)&&
2378 			        (make_state->string_mb[0] == '/')) {
2379 			 make_state->hash.length = 0;
2380 			 make_state->string_mb[0] = '\0';
2381 		      }
2382 	   	      sprintf(state_file_str_mb,"%s%s",
2383 		       make_state->string_mb,"/.make.state");
2384 		      make_state = &state_filename;
2385 			/* adjust the length to reflect the appended string */
2386 		      make_state->hash.length += 12;
2387 		   }
2388 		} else { /* the file doesn't exist or no permission */
2389 		   char tmp_path[MAXPATHLEN];
2390 		   char *slashp;
2391 
2392 		   if (slashp = strrchr(make_state->string_mb, '/')) {
2393 		      strncpy(tmp_path, make_state->string_mb,
2394 				(slashp - make_state->string_mb));
2395 			tmp_path[slashp - make_state->string_mb]=0;
2396 		      if(strlen(tmp_path)) {
2397 		        if(stat(tmp_path, &make_state_stat)) {
2398 			  warning(gettext("directory %s for .KEEP_STATE_FILE does not exist"),tmp_path);
2399 		        }
2400 		        if (access(tmp_path, F_OK) != 0) {
2401 			  warning(gettext("can't access dir %s"),tmp_path);
2402 		        }
2403 		      }
2404 		   }
2405 		}
2406 		if (report_dependencies_level != 1) {
2407 			Makefile_type	makefile_type_temp = makefile_type;
2408 			makefile_type = reading_statefile;
2409 			if (read_trace_level > 1) {
2410 				trace_reader = true;
2411 			}
2412 			(void) read_simple_file(make_state,
2413 						false,
2414 						false,
2415 						false,
2416 						false,
2417 						false,
2418 						true);
2419 			trace_reader = false;
2420 			makefile_type = makefile_type_temp;
2421 		}
2422 	}
2423 }
2424 
2425 /*
2426  * Scan the argv for options and "=" type args and make them readonly.
2427  */
2428 static void
2429 enter_argv_values(int argc, char *argv[], ASCII_Dyn_Array *makeflags_and_macro)
2430 {
2431 	register char		*cp;
2432 	register int		i;
2433 	int			length;
2434 	register Name		name;
2435 	int			opt_separator = argc;
2436 	char			tmp_char;
2437 	wchar_t			*tmp_wcs_buffer;
2438 	register Name		value;
2439 	Boolean			append = false;
2440 	Property		macro;
2441 	struct stat		statbuf;
2442 
2443 
2444 	/* Read argv options and "=" type args and make them readonly. */
2445 	makefile_type = reading_nothing;
2446 	for (i = 1; i < argc; ++i) {
2447 		append = false;
2448 		if (argv[i] == NULL) {
2449 			continue;
2450 		} else if (((argv[i][0] == '-') && (argv[i][1] == '-')) ||
2451 			   ((argv[i][0] == (int) ' ') &&
2452 			    (argv[i][1] == (int) '-') &&
2453 			    (argv[i][2] == (int) ' ') &&
2454 			    (argv[i][3] == (int) '-'))) {
2455 			argv[i] = NULL;
2456 			opt_separator = i;
2457 			continue;
2458 		} else if ((i < opt_separator) && (argv[i][0] == (int) hyphen_char)) {
2459 			switch (parse_command_option(argv[i][1])) {
2460 			case 1:	/* -f seen */
2461 				++i;
2462 				continue;
2463 			case 2:	/* -c seen */
2464 				if (argv[i+1] == NULL) {
2465 					fatal(gettext("No dmake rcfile argument after -c flag"));
2466 				}
2467 				MBSTOWCS(wcs_buffer, "DMAKE_RCFILE");
2468 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2469 				break;
2470 			case 4:	/* -g seen */
2471 				if (argv[i+1] == NULL) {
2472 					fatal(gettext("No dmake group argument after -g flag"));
2473 				}
2474 				MBSTOWCS(wcs_buffer, "DMAKE_GROUP");
2475 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2476 				break;
2477 			case 8:	/* -j seen */
2478 				if (argv[i+1] == NULL) {
2479 					fatal(gettext("No dmake max jobs argument after -j flag"));
2480 				}
2481 				MBSTOWCS(wcs_buffer, "DMAKE_MAX_JOBS");
2482 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2483 				break;
2484 			case 16: /* -M seen */
2485 				if (argv[i+1] == NULL) {
2486 					fatal(gettext("No pmake machinesfile argument after -M flag"));
2487 				}
2488 				MBSTOWCS(wcs_buffer, "PMAKE_MACHINESFILE");
2489 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2490 				break;
2491 			case 32: /* -m seen */
2492 				if (argv[i+1] == NULL) {
2493 					fatal(gettext("No dmake mode argument after -m flag"));
2494 				}
2495 				MBSTOWCS(wcs_buffer, "DMAKE_MODE");
2496 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2497 				break;
2498 			case 256: /* -K seen */
2499 				if (argv[i+1] == NULL) {
2500 					fatal(gettext("No makestate filename argument after -K flag"));
2501 				}
2502 				MBSTOWCS(wcs_buffer, argv[i+1]);
2503 				make_state = GETNAME(wcs_buffer, FIND_LENGTH);
2504 				keep_state = true;
2505 				argv[i] = NULL;
2506 				argv[i+1] = NULL;
2507 				continue;
2508 			case 512:	/* -o seen */
2509 				if (argv[i+1] == NULL) {
2510 					fatal(gettext("No dmake output dir argument after -o flag"));
2511 				}
2512 				MBSTOWCS(wcs_buffer, "DMAKE_ODIR");
2513 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2514 				break;
2515 			case 1024: /* -x seen */
2516 				if (argv[i+1] == NULL) {
2517 					fatal(gettext("No argument after -x flag"));
2518 				}
2519 				length = strlen( "SUN_MAKE_COMPAT_MODE=");
2520 				if (strncmp(argv[i+1], "SUN_MAKE_COMPAT_MODE=", length) == 0) {
2521 					argv[i+1] = &argv[i+1][length];
2522 					MBSTOWCS(wcs_buffer, "SUN_MAKE_COMPAT_MODE");
2523 					name = GETNAME(wcs_buffer, FIND_LENGTH);
2524 					dmake_compat_mode_specified = dmake_add_mode_specified;
2525 					break;
2526 				}
2527 				length = strlen( "DMAKE_OUTPUT_MODE=");
2528 				if (strncmp(argv[i+1], "DMAKE_OUTPUT_MODE=", length) == 0) {
2529 					argv[i+1] = &argv[i+1][length];
2530 					MBSTOWCS(wcs_buffer, "DMAKE_OUTPUT_MODE");
2531 					name = GETNAME(wcs_buffer, FIND_LENGTH);
2532 					dmake_output_mode_specified = dmake_add_mode_specified;
2533 				} else {
2534 					warning(gettext("Unknown argument `%s' after -x flag (ignored)"),
2535 					      argv[i+1]);
2536 					argv[i] = argv[i + 1] = NULL;
2537 					continue;
2538 				}
2539 				break;
2540 			case 2048: /* -C seen */
2541 				if (argv[i + 1] == NULL) {
2542 					fatal(gettext("No argument after -C flag"));
2543 				}
2544 				if (chdir(argv[i + 1]) != 0) {
2545 					fatal(gettext("failed to change to directory %s: %s"),
2546 					    argv[i + 1], strerror(errno));
2547 				}
2548 				MBSTOWCS(wcs_buffer, "DMAKE_CDIR");
2549 				name = GETNAME(wcs_buffer, FIND_LENGTH);
2550 				path_reset = true;
2551 				rebuild_arg0 = true;
2552 				(void) get_current_path();
2553 				break;
2554 			default: /* Shouldn't reach here */
2555 				argv[i] = NULL;
2556 				continue;
2557 			}
2558 			argv[i] = NULL;
2559 			if (i == (argc - 1)) {
2560 				break;
2561 			}
2562 			if ((length = strlen(argv[i+1])) >= MAXPATHLEN) {
2563 				tmp_wcs_buffer = ALLOC_WC(length + 1);
2564 				(void) mbstowcs(tmp_wcs_buffer, argv[i+1], length + 1);
2565 				value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2566 				retmem(tmp_wcs_buffer);
2567 			} else {
2568 				MBSTOWCS(wcs_buffer, argv[i+1]);
2569 				value = GETNAME(wcs_buffer, FIND_LENGTH);
2570 			}
2571 			argv[i+1] = NULL;
2572 		} else if ((cp = strchr(argv[i], (int) equal_char)) != NULL) {
2573 /*
2574  * Combine all macro in dynamic array
2575  */
2576 			if(*(cp-1) == (int) plus_char)
2577 			{
2578 				if(isspace(*(cp-2))) {
2579 					append = true;
2580 					cp--;
2581 				}
2582 			}
2583 			if(!append)
2584 				append_or_replace_macro_in_dyn_array(makeflags_and_macro, argv[i]);
2585 
2586 			while (isspace(*(cp-1))) {
2587 				cp--;
2588 			}
2589 			tmp_char = *cp;
2590 			*cp = (int) nul_char;
2591 			MBSTOWCS(wcs_buffer, argv[i]);
2592 			*cp = tmp_char;
2593 			name = GETNAME(wcs_buffer, wcslen(wcs_buffer));
2594 			while (*cp != (int) equal_char) {
2595 				cp++;
2596 			}
2597 			cp++;
2598 			while (isspace(*cp) && (*cp != (int) nul_char)) {
2599 				cp++;
2600 			}
2601 			if ((length = strlen(cp)) >= MAXPATHLEN) {
2602 				tmp_wcs_buffer = ALLOC_WC(length + 1);
2603 				(void) mbstowcs(tmp_wcs_buffer, cp, length + 1);
2604 				value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2605 				retmem(tmp_wcs_buffer);
2606 			} else {
2607 				MBSTOWCS(wcs_buffer, cp);
2608 				value = GETNAME(wcs_buffer, FIND_LENGTH);
2609 			}
2610 			argv[i] = NULL;
2611 		} else {
2612 			/* Illegal MAKEFLAGS argument */
2613 			continue;
2614 		}
2615 		if(append) {
2616 			setvar_append(name, value);
2617 			append = false;
2618 		} else {
2619 			macro = maybe_append_prop(name, macro_prop);
2620 			macro->body.macro.exported = true;
2621 			SETVAR(name, value, false)->body.macro.read_only = true;
2622 		}
2623 	}
2624 }
2625 
2626 /*
2627  * Append the DMake option and value to the MAKEFLAGS string.
2628  */
2629 static void
2630 append_makeflags_string(Name name, register String makeflags_string)
2631 {
2632 	const char	*option;
2633 
2634 	if (strcmp(name->string_mb, "DMAKE_GROUP") == 0) {
2635 		option = " -g ";
2636 	} else if (strcmp(name->string_mb, "DMAKE_MAX_JOBS") == 0) {
2637 		option = " -j ";
2638 	} else if (strcmp(name->string_mb, "DMAKE_MODE") == 0) {
2639 		option = " -m ";
2640 	} else if (strcmp(name->string_mb, "DMAKE_ODIR") == 0) {
2641 		option = " -o ";
2642 	} else if (strcmp(name->string_mb, "DMAKE_RCFILE") == 0) {
2643 		option = " -c ";
2644 	} else if (strcmp(name->string_mb, "PMAKE_MACHINESFILE") == 0) {
2645 		option = " -M ";
2646 	} else if (strcmp(name->string_mb, "DMAKE_OUTPUT_MODE") == 0) {
2647 		option = " -x DMAKE_OUTPUT_MODE=";
2648 	} else if (strcmp(name->string_mb, "SUN_MAKE_COMPAT_MODE") == 0) {
2649 		option = " -x SUN_MAKE_COMPAT_MODE=";
2650 	} else {
2651 		fatal(gettext("Internal error: name not recognized in append_makeflags_string()"));
2652 	}
2653 	Property prop = maybe_append_prop(name, macro_prop);
2654 	if( prop == 0 || prop->body.macro.value == 0 ||
2655 	    prop->body.macro.value->string_mb == 0 ) {
2656 		return;
2657 	}
2658 	char mbs_value[MAXPATHLEN + 100];
2659 	strcpy(mbs_value, option);
2660 	strcat(mbs_value, prop->body.macro.value->string_mb);
2661 	MBSTOWCS(wcs_buffer, mbs_value);
2662 	append_string(wcs_buffer, makeflags_string, FIND_LENGTH);
2663 }
2664 
2665 /*
2666  *	read_environment(read_only)
2667  *
2668  *	This routine reads the process environment when make starts and enters
2669  *	it as make macros. The environment variable SHELL is ignored.
2670  *
2671  *	Parameters:
2672  *		read_only	Should we make env vars read only?
2673  *
2674  *	Global variables used:
2675  *		report_pwd	Set if this make was started by other make
2676  */
2677 static void
2678 read_environment(Boolean read_only)
2679 {
2680 	register char		**environment;
2681 	int			length;
2682 	wchar_t			*tmp_wcs_buffer;
2683 	Boolean			alloced_tmp_wcs_buffer = false;
2684 	register wchar_t	*name;
2685 	register wchar_t	*value;
2686 	register Name		macro;
2687 	Property		val;
2688 	Boolean			read_only_saved;
2689 
2690 	reading_environment = true;
2691 	environment = environ;
2692 	for (; *environment; environment++) {
2693 		read_only_saved = read_only;
2694 		if ((length = strlen(*environment)) >= MAXPATHLEN) {
2695 			tmp_wcs_buffer = ALLOC_WC(length + 1);
2696 			alloced_tmp_wcs_buffer = true;
2697 			(void) mbstowcs(tmp_wcs_buffer, *environment, length + 1);
2698 			name = tmp_wcs_buffer;
2699 		} else {
2700 			MBSTOWCS(wcs_buffer, *environment);
2701 			name = wcs_buffer;
2702 		}
2703 		value = (wchar_t *) wcschr(name, (int) equal_char);
2704 
2705 		/*
2706 		 * Looks like there's a bug in the system, but sometimes
2707 		 * you can get blank lines in *environment.
2708 		 */
2709 		if (!value) {
2710 			continue;
2711 		}
2712 		MBSTOWCS(wcs_buffer2, "SHELL=");
2713 		if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2714 			continue;
2715 		}
2716 		MBSTOWCS(wcs_buffer2, "MAKEFLAGS=");
2717 		if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2718 			report_pwd = true;
2719 			/*
2720 			 * In POSIX mode we do not want MAKEFLAGS to be readonly.
2721 			 * If the MAKEFLAGS macro is subsequently set by the makefile,
2722 			 * it replaces the MAKEFLAGS variable currently found in the
2723 			 * environment.
2724 			 * See Assertion 50 in section 6.2.5.3 of standard P1003.3.2/D8.
2725 			 */
2726 			if(posix) {
2727 				read_only_saved = false;
2728 			}
2729 		}
2730 
2731 		/*
2732 		 * We ignore SUNPRO_DEPENDENCIES. This environment variable is
2733 		 * set by make and read by cpp which then writes info to
2734 		 * .make.dependency.xxx.  When make is invoked by another make
2735 		 * (recursive make), we don't want to read this because then
2736 		 * the child make will end up writing to the parent
2737 		 * directory's .make.state and clobbering them.
2738 		 */
2739 		MBSTOWCS(wcs_buffer2, "SUNPRO_DEPENDENCIES");
2740 		if (IS_WEQUALN(name, wcs_buffer2, wcslen(wcs_buffer2))) {
2741 			continue;
2742 		}
2743 
2744 		macro = GETNAME(name, value - name);
2745 		maybe_append_prop(macro, macro_prop)->body.macro.exported =
2746 		  true;
2747 		if ((value == NULL) || ((value + 1)[0] == (int) nul_char)) {
2748 			val = setvar_daemon(macro,
2749 					    (Name) NULL,
2750 					    false, no_daemon, false, debug_level);
2751 		} else {
2752 			val = setvar_daemon(macro,
2753 					    GETNAME(value + 1, FIND_LENGTH),
2754 					    false, no_daemon, false, debug_level);
2755 		}
2756 		val->body.macro.read_only = read_only_saved;
2757 		if (alloced_tmp_wcs_buffer) {
2758 			retmem(tmp_wcs_buffer);
2759 			alloced_tmp_wcs_buffer = false;
2760 		}
2761 	}
2762 	reading_environment = false;
2763 }
2764 
2765 /*
2766  *	read_makefile(makefile, complain, must_exist, report_file)
2767  *
2768  *	Read one makefile and check the result
2769  *
2770  *	Return value:
2771  *				false is the read failed
2772  *
2773  *	Parameters:
2774  *		makefile	The file to read
2775  *		complain	Passed thru to read_simple_file()
2776  *		must_exist	Passed thru to read_simple_file()
2777  *		report_file	Passed thru to read_simple_file()
2778  *
2779  *	Global variables used:
2780  *		makefile_type	Set to indicate we are reading main file
2781  *		recursion_level	Initialized
2782  */
2783 static Boolean
2784 read_makefile(register Name makefile, Boolean complain, Boolean must_exist, Boolean report_file)
2785 {
2786 	Boolean			b;
2787 
2788 	makefile_type = reading_makefile;
2789 	recursion_level = 0;
2790 	reading_dependencies = true;
2791 	b = read_simple_file(makefile, true, true, complain,
2792 			     must_exist, report_file, false);
2793 	reading_dependencies = false;
2794 	return b;
2795 }
2796 
2797 /*
2798  *	make_targets(argc, argv, parallel_flag)
2799  *
2800  *	Call doname on the specified targets
2801  *
2802  *	Parameters:
2803  *		argc		You know what this is
2804  *		argv		You know what this is
2805  *		parallel_flag	True if building in parallel
2806  *
2807  *	Global variables used:
2808  *		build_failed_seen Used to generated message after failed -k
2809  *		commands_done	Used to generate message "Up to date"
2810  *		default_target_to_build	First proper target in makefile
2811  *		init		The Name ".INIT", use to run command
2812  *		parallel	Global parallel building flag
2813  *		quest		make -q, suppresses messages
2814  *		recursion_level	Initialized, used for tracing
2815  *		report_dependencies make -P, regroves whole process
2816  */
2817 static void
2818 make_targets(int argc, char **argv, Boolean parallel_flag)
2819 {
2820 	int			i;
2821 	char			*cp;
2822 	Doname			result;
2823 	register Boolean	target_to_make_found = false;
2824 
2825 	(void) doname(init, true, true);
2826 	recursion_level = 1;
2827 	parallel = parallel_flag;
2828 /*
2829  *	make remaining args
2830  */
2831 /*
2832 	if ((report_dependencies_level == 0) && parallel) {
2833  */
2834 	if (parallel) {
2835 		/*
2836 		 * If building targets in parallel, start all of the
2837 		 * remaining args to build in parallel.
2838 		 */
2839 		for (i = 1; i < argc; i++) {
2840 			if ((cp = argv[i]) != NULL) {
2841 				commands_done = false;
2842 				if ((cp[0] == (int) period_char) &&
2843 				    (cp[1] == (int) slash_char)) {
2844 					cp += 2;
2845 				}
2846 				 if((cp[0] == (int) ' ') &&
2847 				    (cp[1] == (int) '-') &&
2848 				    (cp[2] == (int) ' ') &&
2849 				    (cp[3] == (int) '-')) {
2850 			            argv[i] = NULL;
2851 					continue;
2852 				}
2853 				MBSTOWCS(wcs_buffer, cp);
2854 				//default_target_to_build = GETNAME(wcs_buffer,
2855 				//				  FIND_LENGTH);
2856 				default_target_to_build = normalize_name(wcs_buffer,
2857 								  wcslen(wcs_buffer));
2858 				if (default_target_to_build == wait_name) {
2859 					if (parallel_process_cnt > 0) {
2860 						finish_running();
2861 					}
2862 					continue;
2863 				}
2864 				top_level_target = get_wstring(default_target_to_build->string_mb);
2865 				/*
2866 				 * If we can't execute the current target in
2867 				 * parallel, hold off the target processing
2868 				 * to preserve the order of the targets as they appeared
2869 				 * in command line.
2870 				 */
2871 				if (!parallel_ok(default_target_to_build, false)
2872 						&& parallel_process_cnt > 0) {
2873 					finish_running();
2874 				}
2875 				result = doname_check(default_target_to_build,
2876 						      true,
2877 						      false,
2878 						      false);
2879 				gather_recursive_deps();
2880 				if (/* !commands_done && */
2881 				    (result == build_ok) &&
2882 				    !quest &&
2883 				    (report_dependencies_level == 0) /*  &&
2884 				    (exists(default_target_to_build) > file_doesnt_exist)  */) {
2885 					if (posix) {
2886 						if (!commands_done) {
2887 							(void) printf(gettext("`%s' is updated.\n"),
2888 						 		      default_target_to_build->string_mb);
2889 						} else {
2890 							if (no_action_was_taken) {
2891 								(void) printf(gettext("`%s': no action was taken.\n"),
2892 						 			      default_target_to_build->string_mb);
2893 							}
2894 						}
2895 					} else {
2896 						default_target_to_build->stat.time = file_no_time;
2897 						if (!commands_done &&
2898 						    (exists(default_target_to_build) > file_doesnt_exist)) {
2899 							(void) printf(gettext("`%s' is up to date.\n"),
2900 								      default_target_to_build->string_mb);
2901 						}
2902 					}
2903 				}
2904 			}
2905 		}
2906 		/* Now wait for all of the targets to finish running */
2907 		finish_running();
2908 		//		setjmp(jmpbuffer);
2909 
2910 	}
2911 	for (i = 1; i < argc; i++) {
2912 		if ((cp = argv[i]) != NULL) {
2913 			target_to_make_found = true;
2914 			if ((cp[0] == (int) period_char) &&
2915 			    (cp[1] == (int) slash_char)) {
2916 				cp += 2;
2917 			}
2918 				 if((cp[0] == (int) ' ') &&
2919 				    (cp[1] == (int) '-') &&
2920 				    (cp[2] == (int) ' ') &&
2921 				    (cp[3] == (int) '-')) {
2922 			            argv[i] = NULL;
2923 					continue;
2924 				}
2925 			MBSTOWCS(wcs_buffer, cp);
2926 			default_target_to_build = normalize_name(wcs_buffer, wcslen(wcs_buffer));
2927 			top_level_target = get_wstring(default_target_to_build->string_mb);
2928 			report_recursion(default_target_to_build);
2929 			commands_done = false;
2930 			if (parallel) {
2931 				result = (Doname) default_target_to_build->state;
2932 			} else {
2933 				result = doname_check(default_target_to_build,
2934 						      true,
2935 						      false,
2936 						      false);
2937 			}
2938 			gather_recursive_deps();
2939 			if (build_failed_seen) {
2940 				build_failed_ever_seen = true;
2941 				warning(gettext("Target `%s' not remade because of errors"),
2942 					default_target_to_build->string_mb);
2943 			}
2944 			build_failed_seen = false;
2945 			if (report_dependencies_level > 0) {
2946 				print_dependencies(default_target_to_build,
2947 						   get_prop(default_target_to_build->prop,
2948 							    line_prop));
2949 			}
2950 			default_target_to_build->stat.time =
2951 			  file_no_time;
2952 			if (default_target_to_build->colon_splits > 0) {
2953 				default_target_to_build->state =
2954 				  build_dont_know;
2955 			}
2956 			if (!parallel &&
2957 			    /* !commands_done && */
2958 			    (result == build_ok) &&
2959 			    !quest &&
2960 			    (report_dependencies_level == 0) /*  &&
2961 			    (exists(default_target_to_build) > file_doesnt_exist)  */) {
2962 				if (posix) {
2963 					if (!commands_done) {
2964 						(void) printf(gettext("`%s' is updated.\n"),
2965 					 		      default_target_to_build->string_mb);
2966 					} else {
2967 						if (no_action_was_taken) {
2968 							(void) printf(gettext("`%s': no action was taken.\n"),
2969 					 			      default_target_to_build->string_mb);
2970 						}
2971 					}
2972 				} else {
2973 					if (!commands_done &&
2974 					    (exists(default_target_to_build) > file_doesnt_exist)) {
2975 						(void) printf(gettext("`%s' is up to date.\n"),
2976 							      default_target_to_build->string_mb);
2977 					}
2978 				}
2979 			}
2980 		}
2981 	}
2982 
2983 /*
2984  *	If no file arguments have been encountered,
2985  *	make the first name encountered that doesnt start with a dot
2986  */
2987 	if (!target_to_make_found) {
2988 		if (default_target_to_build == NULL) {
2989 			fatal(gettext("No arguments to build"));
2990 		}
2991 		commands_done = false;
2992 		top_level_target = get_wstring(default_target_to_build->string_mb);
2993 		report_recursion(default_target_to_build);
2994 
2995 
2996 		if (getenv("SPRO_EXPAND_ERRORS")){
2997 			(void) printf("::(%s)\n",
2998 				      default_target_to_build->string_mb);
2999 		}
3000 
3001 
3002 		result = doname_parallel(default_target_to_build, true, false);
3003 		gather_recursive_deps();
3004 		if (build_failed_seen) {
3005 			build_failed_ever_seen = true;
3006 			warning(gettext("Target `%s' not remade because of errors"),
3007 				default_target_to_build->string_mb);
3008 		}
3009 		build_failed_seen = false;
3010 		if (report_dependencies_level > 0) {
3011 			print_dependencies(default_target_to_build,
3012 					   get_prop(default_target_to_build->
3013 						    prop,
3014 						    line_prop));
3015 		}
3016 		default_target_to_build->stat.time = file_no_time;
3017 		if (default_target_to_build->colon_splits > 0) {
3018 			default_target_to_build->state = build_dont_know;
3019 		}
3020 		if (/* !commands_done && */
3021 		    (result == build_ok) &&
3022 		    !quest &&
3023 		    (report_dependencies_level == 0) /*  &&
3024 		    (exists(default_target_to_build) > file_doesnt_exist)  */) {
3025 			if (posix) {
3026 				if (!commands_done) {
3027 					(void) printf(gettext("`%s' is updated.\n"),
3028 				 		      default_target_to_build->string_mb);
3029 				} else {
3030 					if (no_action_was_taken) {
3031 						(void) printf(gettext("`%s': no action was taken.\n"),
3032 							      default_target_to_build->string_mb);
3033 					}
3034 				}
3035 			} else {
3036 				if (!commands_done &&
3037 				    (exists(default_target_to_build) > file_doesnt_exist)) {
3038 					(void) printf(gettext("`%s' is up to date.\n"),
3039 						      default_target_to_build->string_mb);
3040 				}
3041 			}
3042 		}
3043 	}
3044 }
3045 
3046 /*
3047  *	report_recursion(target)
3048  *
3049  *	If this is a recursive make and the parent make has KEEP_STATE on
3050  *	this routine reports the dependency to the parent make
3051  *
3052  *	Parameters:
3053  *		target		Target to report
3054  *
3055  *	Global variables used:
3056  *		makefiles_used		List of makefiles read
3057  *		recursive_name		The Name ".RECURSIVE", printed
3058  *		report_dependency	dwight
3059  */
3060 static void
3061 report_recursion(register Name target)
3062 {
3063 	register FILE		*report_file = get_report_file();
3064 
3065 	if ((report_file == NULL) || (report_file == (FILE*)-1)) {
3066 		return;
3067 	}
3068 	if (primary_makefile == NULL) {
3069 		/*
3070 		 * This can happen when there is no makefile and
3071 		 * only implicit rules are being used.
3072 		 */
3073 		return;
3074 	}
3075 	(void) fprintf(report_file,
3076 		       "%s: %s ",
3077 		       get_target_being_reported_for(),
3078 		       recursive_name->string_mb);
3079 	report_dependency(get_current_path());
3080 	report_dependency(target->string_mb);
3081 	report_dependency(primary_makefile->string_mb);
3082 	(void) fprintf(report_file, "\n");
3083 }
3084 
3085 /* Next function "append_or_replace_macro_in_dyn_array" must be in "misc.cc". */
3086 /* NIKMOL */
3087 extern void
3088 append_or_replace_macro_in_dyn_array(ASCII_Dyn_Array *Ar, char *macro)
3089 {
3090 	register char	*cp0;	/* work pointer in macro */
3091 	register char	*cp1;	/* work pointer in array */
3092 	register char	*cp2;	/* work pointer in array */
3093 	register char	*cp3;	/* work pointer in array */
3094 	register char	*name;	/* macro name */
3095 	register char	*value;	/* macro value */
3096 	register int 	len_array;
3097 	register int 	len_macro;
3098 
3099 	char * esc_value = NULL;
3100 	int esc_len;
3101 
3102 	if (!(len_macro = strlen(macro))) return;
3103 	name = macro;
3104 	while (isspace(*(name))) {
3105 		name++;
3106 	}
3107 	if (!(value = strchr(name, (int) equal_char))) {
3108 		/* no '=' in macro */
3109 		goto ERROR_MACRO;
3110 	}
3111 	cp0 = value;
3112 	value++;
3113 	while (isspace(*(value))) {
3114 		value++;
3115 	}
3116 	while (isspace(*(cp0-1))) {
3117 		cp0--;
3118 	}
3119 	if (cp0 <= name) goto ERROR_MACRO; /* no name */
3120 	if (!(Ar->size)) goto ALLOC_ARRAY;
3121 	cp1 = Ar->start;
3122 
3123 LOOK_FOR_NAME:
3124 	if (!(cp1 = strchr(cp1, name[0]))) goto APPEND_MACRO;
3125 	if (!(cp2 = strchr(cp1, (int) equal_char))) goto APPEND_MACRO;
3126 	if (strncmp(cp1, name, (size_t)(cp0-name))) {
3127 		/* another name */
3128 		cp1++;
3129 		goto LOOK_FOR_NAME;
3130 	}
3131 	if (cp1 != Ar->start) {
3132 		if (!isspace(*(cp1-1))) {
3133 			/* another name */
3134 			cp1++;
3135 			goto LOOK_FOR_NAME;
3136 		}
3137 	}
3138 	for (cp3 = cp1 + (cp0-name); cp3 < cp2; cp3++) {
3139 		if (isspace(*cp3)) continue;
3140 		/* else: another name */
3141 		cp1++;
3142 		goto LOOK_FOR_NAME;
3143 	}
3144 	/* Look for the next macro name in array */
3145 	cp3 = cp2+1;
3146 	if (*cp3 != (int) doublequote_char) {
3147 		/* internal error */
3148 		goto ERROR_MACRO;
3149 	}
3150 	if (!(cp3 = strchr(cp3+1, (int) doublequote_char))) {
3151 		/* internal error */
3152 		goto ERROR_MACRO;
3153 	}
3154 	cp3++;
3155 	while (isspace(*cp3)) {
3156 		cp3++;
3157 	}
3158 
3159 	cp2 = cp1;  /* remove old macro */
3160 	if ((*cp3) && (cp3 < Ar->start + Ar->size)) {
3161 		for (; cp3 < Ar->start + Ar->size; cp3++) {
3162 			*cp2++ = *cp3;
3163 		}
3164 	}
3165 	for (; cp2 < Ar->start + Ar->size; cp2++) {
3166 		*cp2 = 0;
3167 	}
3168 	if (*cp1) {
3169 		/* check next name */
3170 		goto LOOK_FOR_NAME;
3171 	}
3172 	goto APPEND_MACRO;
3173 
3174 ALLOC_ARRAY:
3175 	if (Ar->size) {
3176 		cp1 = Ar->start;
3177 	} else {
3178 		cp1 = 0;
3179 	}
3180 	Ar->size += 128;
3181 	Ar->start = getmem(Ar->size);
3182 	for (len_array=0; len_array < Ar->size; len_array++) {
3183 		Ar->start[len_array] = 0;
3184 	}
3185 	if (cp1) {
3186 		strcpy(Ar->start, cp1);
3187 		retmem((wchar_t *) cp1);
3188 	}
3189 
3190 APPEND_MACRO:
3191 	len_array = strlen(Ar->start);
3192 	esc_value = (char*)malloc(strlen(value)*2 + 1);
3193 	quote_str(value, esc_value);
3194 	esc_len = strlen(esc_value) - strlen(value);
3195 	if (len_array + len_macro + esc_len + 5 >= Ar->size) goto  ALLOC_ARRAY;
3196 	strcat(Ar->start, " ");
3197 	strncat(Ar->start, name, cp0-name);
3198 	strcat(Ar->start, "=");
3199 	strncat(Ar->start, esc_value, strlen(esc_value));
3200 	free(esc_value);
3201 	return;
3202 ERROR_MACRO:
3203 	/* Macro without '=' or with invalid left/right part */
3204 	return;
3205 }
3206 
3207 static void
3208 report_dir_enter_leave(Boolean entering)
3209 {
3210 	char	rcwd[MAXPATHLEN];
3211 static	char *	mlev = NULL;
3212 	char *	make_level_str = NULL;
3213 	int	make_level_val = 0;
3214 
3215 	make_level_str = getenv("MAKELEVEL");
3216 	if(make_level_str) {
3217 		make_level_val = atoi(make_level_str);
3218 	}
3219 	if(mlev == NULL) {
3220 		mlev = (char*) malloc(MAXPATHLEN);
3221 	}
3222 	if(entering) {
3223 		sprintf(mlev, "MAKELEVEL=%d", make_level_val + 1);
3224 	} else {
3225 		make_level_val--;
3226 		sprintf(mlev, "MAKELEVEL=%d", make_level_val);
3227 	}
3228 	putenv(mlev);
3229 
3230 	if(report_cwd) {
3231 		if(make_level_val <= 0) {
3232 			if(entering) {
3233 				sprintf(rcwd,
3234 				    gettext("%s: Entering directory `%s'\n"),
3235 				    getprogname(),
3236 				    get_current_path());
3237 			} else {
3238 				sprintf(rcwd,
3239 				    gettext("%s: Leaving directory `%s'\n"),
3240 				    getprogname(),
3241 				    get_current_path());
3242 			}
3243 		} else {
3244 			if(entering) {
3245 				sprintf(rcwd,
3246 				    gettext("%s[%d]: Entering directory `%s'\n"),
3247 				    getprogname(),
3248 				    make_level_val, get_current_path());
3249 			} else {
3250 				sprintf(rcwd,
3251 				    gettext("%s[%d]: Leaving directory `%s'\n"),
3252 				    getprogname(),
3253 				    make_level_val, get_current_path());
3254 			}
3255 		}
3256 		printf("%s", rcwd);
3257 	}
3258 }
3259