| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <lib/ac/assert.h> |
| 21 |
#include <lib/ac/stdlib.h> |
| 22 |
#include <lib/ac/string.h> |
| 23 |
#include <lib/ac/unistd.h> |
| 24 |
|
| 25 |
#include <lib/program_name.h> |
| 26 |
|
| 27 |
|
| 28 |
static char progname[256]; |
| 29 |
|
| 30 |
|
| 31 |
static void |
| 32 |
fstrcmp_program_name_set_real(const char *name) |
| 33 |
{ |
| 34 |
const char *cp; |
| 35 |
|
| 36 |
if (!name) |
| 37 |
name = ""; |
| 38 |
progname[0] = '\0'; |
| 39 |
if (!name) |
| 40 |
return; |
| 41 |
|
| 42 |
cp = name; |
| 43 |
for (;;) |
| 44 |
{ |
| 45 |
char *pnp; |
| 46 |
|
| 47 |
if (*cp == '/') |
| 48 |
{ |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
++cp; |
| 54 |
continue; |
| 55 |
} |
| 56 |
if (*cp == '\0') |
| 57 |
break; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
if (0 == memcmp(cp, "lt-", 3)) |
| 64 |
cp += 3; |
| 65 |
|
| 66 |
pnp = progname; |
| 67 |
for (;;) |
| 68 |
{ |
| 69 |
if (pnp < progname + sizeof(progname) - 1) |
| 70 |
*pnp++ = *cp; |
| 71 |
++cp; |
| 72 |
if (*cp == '\0' || *cp == '/') |
| 73 |
break; |
| 74 |
} |
| 75 |
*pnp = '\0'; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
const char * |
| 81 |
fstrcmp_program_name_get(void) |
| 82 |
{ |
| 83 |
if (progname[0]) |
| 84 |
return progname; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
{ |
| 90 |
int n; |
| 91 |
char buf[2000]; |
| 92 |
|
| 93 |
n = readlink("/proc/self/exe", buf, sizeof(buf) - 1); |
| 94 |
if (n > 0) |
| 95 |
{ |
| 96 |
buf[n] = 0; |
| 97 |
fstrcmp_program_name_set_real(buf); |
| 98 |
} |
| 99 |
} |
| 100 |
if (progname[0]) |
| 101 |
return progname; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
fstrcmp_program_name_set_real(getenv("_")); |
| 108 |
if (progname[0]) |
| 109 |
return progname; |
| 110 |
|
| 111 |
return ""; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void |
| 116 |
fstrcmp_program_name_set(const char *name) |
| 117 |
{ |
| 118 |
fstrcmp_program_name_set_real(name); |
| 119 |
} |