WPrefs.app: Add ability to set behavior when dragging a maximized window.
[wmaker-crm.git] / src / osdep_darwin.c
blobb3a7b4facbdfa7e67f7e27fd879fc80c28036630
2 #include <sys/types.h>
3 #include <sys/sysctl.h>
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include <WINGs/WUtil.h>
11 #include "wconfig.h"
12 #include "osdep.h"
15 * copy argc and argv for an existing process identified by `pid'
16 * into suitable storage given in ***argv and *argc.
18 * subsequent calls use the same static area for argv and argc.
20 * returns 0 for failure, in which case argc := 0 and argv := NULL
21 * returns 1 for success
23 Bool GetCommandForPid(int pid, char ***argv, int *argc)
24 #ifdef KERN_PROCARGS2
26 int j, mib[4];
27 unsigned int i, idx;
28 size_t count;
29 static char *args = NULL;
30 static int argmax = 0;
32 *argv = NULL;
33 *argc = 0;
35 /* the system-wide limit */
36 if (argmax == 0) { /* it hopefully doesn't change at runtime *g* */
37 mib[0] = CTL_KERN;
38 mib[1] = KERN_ARGMAX;
39 mib[2] = 0;
40 mib[3] = 0;
42 count = sizeof(argmax);
43 if (sysctl(mib, 2, &argmax, &count, NULL, 0) == -1)
44 return False;
47 /* if argmax is still 0, something went very seriously wrong */
48 assert(argmax > 0);
50 /* space for args; no need to free before returning even on errors */
51 if (args == NULL)
52 args = (char *)wmalloc(argmax);
54 /* get process args */
55 mib[0] = CTL_KERN;
56 mib[1] = KERN_PROCARGS2;
57 mib[2] = pid;
59 count = argmax;
60 if (sysctl(mib, 3, args, &count, NULL, 0) == -1 || count == 0)
61 return False;
63 /* get argc, skip */
64 memcpy(argc, args, sizeof(*argc));
65 idx = sizeof(*argc);
67 while (args[idx++] != '\0') /* skip execname */
69 while (args[idx] == '\0') /* padding too */
70 idx++;
71 /* args[idx] is at at begininng of args now */
73 *argv = (char **)wmalloc(sizeof(char *) * (*argc + 1 /* term. null ptr */));
74 (*argv)[0] = args + idx;
76 /* go through args, set argv[$next] to the beginning of each string */
77 for (i = 0, j = 1; i < count - idx /* do not overrun */; i++) {
78 if (args[idx + i] != '\0')
79 continue;
80 if (args[idx + i] == '\0')
81 (*argv)[j++] = &args[idx + i + 1];
82 if (j == *argc)
83 break;
86 /* the list of arguments must be terminated by a null pointer */
87 (*argv)[j] = NULL;
88 return True;
90 #else /* !KERN_PROCARGS2 */
92 *argv = NULL;
93 *argc = 0;
95 return False;
97 #endif