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