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