]> git.sur5r.net Git - openldap/blob - libraries/liblutil/setproctitle.c
6578a8089504925f1c8da910ae11ed5342de9635
[openldap] / libraries / liblutil / setproctitle.c
1 #include "portable.h"
2
3 #ifndef HAVE_SETPROCTITLE
4
5 #include <stdio.h>
6
7 #include <ac/stdlib.h>
8
9 #include <ac/setproctitle.h>
10 #include <ac/string.h>
11 #include <ac/stdarg.h>
12
13 /*
14  * Copyright (c) 1990,1991 Regents of the University of Michigan.
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms are permitted
18  * provided that this notice is preserved and that due credit is given
19  * to the University of Michigan at Ann Arbor. The name of the University
20  * may not be used to endorse or promote products derived from this
21  * software without specific prior written permission. This software
22  * is provided ``as is'' without express or implied warranty.
23  */
24
25 char    **Argv;         /* pointer to original (main's) argv */
26 int     Argc;           /* original argc */
27
28 /*
29  * takes a printf-style format string (fmt) and up to three parameters (a,b,c)
30  * this clobbers the original argv...
31  */
32
33 /* VARARGS */
34 void setproctitle
35 #if defined( HAVE_STDARG )
36         ( const char *fmt, ... )
37 #else
38         ( va_alist )
39 va_dcl
40 #endif
41 {
42         static char *endargv = (char *)0;
43         char    *s;
44         int             i;
45         char    buf[ 1024 ];
46         va_list ap;
47
48 #if defined( HAVE_STDARG )
49         va_start(ap, fmt);
50 #else
51         const char *fmt;
52
53         va_start(ap);
54         fmt = va_arg(ap, const char *);
55 #endif
56
57 #ifdef HAVE_VSNPRINTF
58         buf[sizeof(buf) - 1] = '\0';
59         vsnprintf( buf, sizeof(buf)-1, fmt, ap );
60 #elif HAVE_VPRINTF
61         vsprintf( buf, fmt, ap ); /* hope it's not too long */
62 #else
63         /* use doprnt() */
64         chokeme = "choke me!  I don't have a doprnt() manual handy";
65 #endif
66
67         va_end(ap);
68
69         if ( endargv == (char *)0 ) {
70                 /* set pointer to end of original argv */
71                 endargv = Argv[ Argc-1 ] + strlen( Argv[ Argc-1 ] );
72         }
73         /* make ps print "([prog name])" */
74         s = Argv[0];
75         *s++ = '-';
76         i = strlen( buf );
77         if ( i > endargv - s - 2 ) {
78                 i = endargv - s - 2;
79                 buf[ i ] = '\0';
80         }
81         strcpy( s, buf );
82         s += i;
83         while ( s < endargv ) *s++ = ' ';
84 }
85 #endif /* NOSETPROCTITLE */