Update Serbian translation from master branch
[wmaker-crm.git] / src / osdep_darwin.c
blob0191749167c918aa41f0c63f0ccdfb1df0caf196
1 #include <sys/types.h>
2 #include <sys/sysctl.h>
4 #include <assert.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include <WINGs/WUtil.h>
10 #include "wconfig.h"
11 #include "osdep.h"
14 * copy argc and argv for an existing process identified by `pid'
15 * into suitable storage given in ***argv and *argc.
17 * subsequent calls use the same static area for argv and argc.
19 * returns 0 for failure, in which case argc := 0 and argv := NULL
20 * returns 1 for success
22 Bool GetCommandForPid(int pid, char ***argv, int *argc)
23 #ifdef KERN_PROCARGS2
25 int mib[4];
26 unsigned int idx;
27 size_t count;
28 static char *args = NULL;
29 static int argmax = 0;
31 *argv = NULL;
32 *argc = 0;
34 /* the system-wide limit */
35 if (argmax == 0) { /* it hopefully doesn't change at runtime *g* */
36 mib[0] = CTL_KERN;
37 mib[1] = KERN_ARGMAX;
38 mib[2] = 0;
39 mib[3] = 0;
41 count = sizeof(argmax);
42 if (sysctl(mib, 2, &argmax, &count, NULL, 0) == -1)
43 return False;
46 /* if argmax is still 0, something went very seriously wrong */
47 assert(argmax > 0);
49 /* space for args; no need to free before returning even on errors */
50 if (args == NULL)
51 args = (char *)wmalloc(argmax);
53 /* get process args */
54 mib[0] = CTL_KERN;
55 mib[1] = KERN_PROCARGS2;
56 mib[2] = pid;
58 count = argmax;
59 if (sysctl(mib, 3, args, &count, NULL, 0) == -1 || count == 0)
60 return False;
62 /* get argc, skip */
63 memcpy(argc, args, sizeof(*argc));
64 idx = sizeof(*argc);
66 while (args[idx++] != '\0') /* skip execname */
68 while (args[idx] == '\0') /* padding too */
69 idx++;
70 /* args[idx] is at at begininng of args now */
72 int found = 0;
73 char *p = &args[idx];
74 while(found < *argc)
76 while(*p != '\0') p++; // look for the next \0
77 while(*p == '\0') p++; // skip over padding \0s
78 found++;
80 // Don’t overrun!
81 if (p-args >= argmax)
83 return False;
86 // At this point, p points to the last \0 in the source array.
88 // Buffer needed for the strings
89 unsigned stringbuf_size = p - &args[idx];
91 // Buffer needed for the pointers (plus one terminating NULL)
92 unsigned pointerbuf_size = sizeof(char *) * (*argc + 1);
94 *argv = wmalloc(pointerbuf_size + stringbuf_size);
95 char* stringstart = (char *)(*argv) + pointerbuf_size;
97 memcpy(stringstart, &args[idx], stringbuf_size);
99 found = 0;
100 p = stringstart;
101 while(found < *argc)
103 (*argv)[found] = p;
105 while(*p != '\0') p++; // look for the next \0
106 while(*p == '\0') p++; // skip over padding \0s
108 found++;
110 (*argv)[found] = NULL; // Terminating NULL
112 return True;
114 #else /* !KERN_PROCARGS2 */
116 *argv = NULL;
117 *argc = 0;
119 return False;
121 #endif