Back to index

openldap  2.4.31
setproctitle.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted only as authorized by the OpenLDAP
00009  * Public License.
00010  *
00011  * A copy of this license is available in the file LICENSE in the
00012  * top-level directory of the distribution or, alternatively, at
00013  * <http://www.OpenLDAP.org/license.html>.
00014  */
00015 /* Portions Copyright (c) 1990,1991 Regents of the University of Michigan.
00016  * All rights reserved.
00017  *
00018  * Redistribution and use in source and binary forms are permitted
00019  * provided that this notice is preserved and that due credit is given
00020  * to the University of Michigan at Ann Arbor. The name of the University
00021  * may not be used to endorse or promote products derived from this
00022  * software without specific prior written permission. This software
00023  * is provided ``as is'' without express or implied warranty.
00024  */
00025 
00026 #include "portable.h"
00027 
00028 #ifndef HAVE_SETPROCTITLE
00029 
00030 #include <stdio.h>
00031 
00032 #include <ac/stdlib.h>
00033 
00034 #include <ac/setproctitle.h>
00035 #include <ac/string.h>
00036 #include <ac/stdarg.h>
00037 
00038 char   **Argv;              /* pointer to original (main's) argv */
00039 int    Argc;         /* original argc */
00040 
00041 /*
00042  * takes a printf-style format string (fmt) and up to three parameters (a,b,c)
00043  * this clobbers the original argv...
00044  */
00045 
00046 /* VARARGS */
00047 void setproctitle( const char *fmt, ... )
00048 {
00049        static char *endargv = (char *)0;
00050        char   *s;
00051        int           i;
00052        char   buf[ 1024 ];
00053        va_list       ap;
00054 
00055        va_start(ap, fmt);
00056 
00057        buf[sizeof(buf) - 1] = '\0';
00058        vsnprintf( buf, sizeof(buf)-1, fmt, ap );
00059 
00060        va_end(ap);
00061 
00062        if ( endargv == (char *)0 ) {
00063               /* set pointer to end of original argv */
00064               endargv = Argv[ Argc-1 ] + strlen( Argv[ Argc-1 ] );
00065        }
00066        /* make ps print "([prog name])" */
00067        s = Argv[0];
00068        *s++ = '-';
00069        i = strlen( buf );
00070        if ( i > endargv - s - 2 ) {
00071               i = endargv - s - 2;
00072               buf[ i ] = '\0';
00073        }
00074        strcpy( s, buf );
00075        s += i;
00076        while ( s < endargv ) *s++ = ' ';
00077 }
00078 #endif /* NOSETPROCTITLE */