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 fd
= open(buf
, O_RDONLY
);
50 count
= read(fd
, buf
, sizeof(buf
));
61 for (i
= 0; i
< count
; i
++)
68 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
+ 1 /* term. null ptr */));
71 /* go through buf, set argv[$next] to the beginning of each string */
72 for (i
= 0, j
= 1; i
< count
; i
++) {
76 (*argv
)[j
++] = &buf
[i
+ 1];
81 /* the list of arguments must be terminated by a null pointer */