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