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