1 /*
2  * Copyright (c) 1992-1998 Michael A. Cooper.
3  * This software may be freely used and distributed provided it is not
4  * sold for profit or used in part or in whole for commercial gain
5  * without prior written agreement, and the author is credited
6  * appropriately.
7  */
8 /*
9  * Copyright (c) 1983 Regents of the University of California.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #ifndef lint
42 static char RCSid[] =
43 "$Id: setargs.c,v 6.5 1998/11/10 04:15:56 mcooper Exp $";
44 
45 static char sccsid[] = "@(#)setargs.c";
46 
47 static char copyright[] =
48 "Copyright (c) 1992-1998 Michael A. Cooper.\n\
49 @(#) Copyright (c) 1983 Regents of the University of California.\n\
50  All rights reserved.\n";
51 #endif /* not lint */
52 
53 #include "defs.h"
54 
55 #if	defined(SETARGS)
56 
57 /*
58  * Set process argument functions
59  */
60 
61 #define MAXUSERENVIRON 		40
62 char 			      **Argv = NULL;
63 char 			       *LastArgv = NULL;
64 char 			       *UserEnviron[MAXUSERENVIRON+1];
65 
66 /*
67  * Settup things for using setproctitle()
68  */
setargs_settup(argc,argv,envp)69 setargs_settup(argc, argv, envp)
70 	int			argc;
71 	char		      **argv;
72 	char		      **envp;
73 {
74 	register int 		i;
75 	extern char 	      **environ;
76 
77   	/* Remember the User Environment */
78 
79 	for (i = 0; i < MAXUSERENVIRON && envp[i] != NULL; i++)
80 		UserEnviron[i] = strdup(envp[i]);
81 	UserEnviron[i] = NULL;
82 	environ = UserEnviron;
83 
84   	/* Save start and extent of argv for setproctitle */
85 	Argv = argv;
86 	if (i > 0)
87 		LastArgv = envp[i-1] + strlen(envp[i-1]);
88 	else
89 		LastArgv = argv[argc-1] + strlen(argv[argc-1]);
90 }
91 
92 #ifndef HAVE_SETPROCTITLE
93 
94 /*
95  * Set process title
96  */
97 extern void _setproctitle(msg)
98         char *msg;
99 {
100 	register int i;
101 	char *p;
102 
103 	p = Argv[0];
104 
105 	/* Make ps print "(program)" */
106 	*p++ = '-';
107 
108 	i = strlen(msg);
109 	if (i > LastArgv - p - 2) {
110 		i = LastArgv - p - 2;
111 		msg[i] = '\0';
112 	}
113 	(void) strcpy(p, msg);
114 	p += i;
115 	while (p < LastArgv) {
116 		*p++ = ' ';
117 	}
118 }
119 
120 #if	defined(ARG_TYPE) && ARG_TYPE == ARG_VARARGS
121 /*
122  * Varargs front-end to _setproctitle()
123  */
setproctitle(va_alist)124 extern void setproctitle(va_alist)
125 	va_dcl
126 {
127 	static char buf[BUFSIZ];
128 	va_list args;
129 	char *fmt;
130 
131 	va_start(args);
132 	fmt = va_arg(args, char *);
133 	(void) vsprintf(buf, fmt, args);
134 	va_end(args);
135 
136 	_setproctitle(buf);
137 }
138 #endif	/* ARG_VARARGS */
139 #if	defined(ARG_TYPE) && ARG_TYPE == ARG_STDARG
140 /*
141  * Stdarg front-end to _setproctitle()
142  */
setproctitle(char * fmt,...)143 extern void setproctitle(char *fmt, ...)
144 {
145 	static char buf[BUFSIZ];
146 	va_list args;
147 
148 	va_start(args, fmt);
149 	(void) vsprintf(buf, fmt, args);
150 	va_end(args);
151 
152 	_setproctitle(buf);
153 }
154 #endif	/* ARG_STDARG */
155 #if	!defined(ARG_TYPE)
156 /*
157  * Non-Varargs front-end to _setproctitle()
158  */
159 /*VARARGS1*/
160 extern void setproctitle(fmt, a1, a2, a3, a4, a5, a6)
161 	char *fmt;
162 {
163 	static char buf[BUFSIZ];
164 
165 	(void) sprintf(buf, fmt, a1, a2, a3, a4, a5, a6);
166 
167 	_setproctitle(buf);
168 }
169 #endif	/* !ARG_TYPE */
170 
171 #endif /* !HAVE_SETPROCTITLE */
172 
173 #endif 	/* SETARGS */
174