Mercurial > retraceit
comparison src/util_linux.c @ 0:147b08bc7d64
Initial commit of a basic Application framework.
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 23 Mar 2015 12:41:52 +0100 |
parents | |
children | 9daf778feaf1 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:147b08bc7d64 |
---|---|
1 #ifndef WIN32 | |
2 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik | |
3 * Software engineering by Intevation GmbH | |
4 * | |
5 * This file is Free Software under the GNU GPL (v>=2) | |
6 * and comes with ABSOLUTELY NO WARRANTY! | |
7 * See LICENSE.txt for details. | |
8 */ | |
9 | |
10 /**@file Linux implementation of functions declared in util.h */ | |
11 | |
12 #include "util.h" | |
13 #include "string.h" | |
14 #include "strhelp.h" | |
15 #include "unistd.h" | |
16 #include <sys/types.h> | |
17 #include <pwd.h> | |
18 | |
19 char * | |
20 get_proc_install_dir(const char *proc) | |
21 { | |
22 char *retval = NULL, | |
23 *procpath = NULL, | |
24 *p = NULL, | |
25 buf[MAX_PATH_LINUX]; | |
26 ssize_t ret; | |
27 size_t path_len = 0; | |
28 | |
29 if (!proc) | |
30 { | |
31 return NULL; | |
32 } | |
33 | |
34 xasprintf(&procpath, "/proc/%s/exe", proc); | |
35 | |
36 ret = readlink (procpath, buf, MAX_PATH_LINUX); | |
37 xfree(procpath); | |
38 procpath = NULL; | |
39 | |
40 if (ret <= 0) | |
41 { | |
42 return NULL; | |
43 } | |
44 | |
45 buf[ret] = '\0'; | |
46 | |
47 /* cut off the filename */ | |
48 p = strrchr (buf, '/'); | |
49 if (p == NULL) | |
50 { | |
51 return NULL; | |
52 } | |
53 *(p + 1) = '\0'; | |
54 | |
55 path_len = strlen (buf); | |
56 retval = xmalloc (path_len + 1); | |
57 strncpy (retval, buf, path_len); | |
58 retval[path_len] = '\0'; | |
59 | |
60 return retval; | |
61 } | |
62 | |
63 char * | |
64 get_install_dir() | |
65 { | |
66 return get_proc_install_dir("self"); | |
67 } | |
68 | |
69 | |
70 #endif |