12 #include <WINGs/WUtil.h>
19 * copy argc and argv for an existing process identified by `pid'
20 * into suitable storage given in ***argv and *argc.
22 * subsequent calls use the same static area for argv and argc.
24 * returns 0 for failure, in which case argc := 0 and argv := NULL
25 * returns 1 for success
27 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
29 static char buf
[_POSIX_ARG_MAX
];
36 /* cmdline is a flattened series of null-terminated strings */
37 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
39 /* not switching this to stdio yet, as this does not need
40 * to be portable, and i'm lazy */
41 if ((fd
= open(buf
, O_RDONLY
)) != -1)
49 if ((count
= read(fd
, buf
, sizeof(buf
))) != -1)
59 for (i
= 0; i
< count
; i
++)
66 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
+ 1 /* term. null ptr */));
69 /* go through buf, set argv[$next] to the beginning of each string */
70 for (i
= 0, j
= 1; i
< count
; i
++) {
74 (*argv
)[j
++] = &buf
[i
+ 1];
79 /* the list of arguments must be terminated by a null pointer */