4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <avahi-common/malloc.h>
34 #include "setproctitle.h"
36 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
37 static char** argv_buffer = NULL;
38 static size_t argv_size = 0;
40 #if !HAVE_DECL_ENVIRON
41 extern char **environ;
46 void avahi_init_proc_title(int argc, char **argv) {
48 #if !defined(HAVE_SETPROCTITLE) && defined(__linux__)
51 char **new_environ, *endptr;
53 /* This code is really really ugly. We make some memory layout
54 * assumptions and reuse the environment array as memory to store
55 * our process title in */
57 for (i = 0; environ[i]; i++);
59 endptr = i ? environ[i-1] + strlen(environ[i-1]) : argv[argc-1] + strlen(argv[argc-1]);
62 argv_size = endptr - argv_buffer[0];
64 /* Make a copy of environ */
66 new_environ = avahi_malloc(sizeof(char*) * (i + 1));
67 for (i = 0; environ[i]; i++)
68 new_environ[i] = avahi_strdup(environ[i]);
69 new_environ[i] = NULL;
71 environ = new_environ;
76 void avahi_set_proc_title(const char *fmt,...) {
77 #ifdef HAVE_SETPROCTITLE
82 vsnprintf(t, sizeof(t), fmt, ap);
85 setproctitle("-%s", t);
94 vsnprintf(argv_buffer[0], argv_size, fmt, ap);
97 l = strlen(argv_buffer[0]);
99 memset(argv_buffer[0] + l, 0, argv_size - l);
100 argv_buffer[1] = NULL;