1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2011 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
14 */
15/* Portions Copyright (c) 1990,1991 Regents of the University of Michigan.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
24 */
25
26#include "portable.h"
27
28#ifndef HAVE_SETPROCTITLE
29
30#include <stdio.h>
31
32#include <ac/stdlib.h>
33
34#include <ac/setproctitle.h>
35#include <ac/string.h>
36#include <ac/stdarg.h>
37
38char	**Argv;		/* pointer to original (main's) argv */
39int	Argc;		/* original argc */
40
41/*
42 * takes a printf-style format string (fmt) and up to three parameters (a,b,c)
43 * this clobbers the original argv...
44 */
45
46/* VARARGS */
47void setproctitle( const char *fmt, ... )
48{
49	static char *endargv = (char *)0;
50	char	*s;
51	int		i;
52	char	buf[ 1024 ];
53	va_list	ap;
54
55	va_start(ap, fmt);
56
57	buf[sizeof(buf) - 1] = '\0';
58	vsnprintf( buf, sizeof(buf)-1, fmt, ap );
59
60	va_end(ap);
61
62	if ( endargv == (char *)0 ) {
63		/* set pointer to end of original argv */
64		endargv = Argv[ Argc-1 ] + strlen( Argv[ Argc-1 ] );
65	}
66	/* make ps print "([prog name])" */
67	s = Argv[0];
68	*s++ = '-';
69	i = strlen( buf );
70	if ( i > endargv - s - 2 ) {
71		i = endargv - s - 2;
72		buf[ i ] = '\0';
73	}
74	strcpy( s, buf );
75	s += i;
76	while ( s < endargv ) *s++ = ' ';
77}
78#endif /* NOSETPROCTITLE */
79