12 #include <WINGs/WUtil.h>
16 #define RETRY( x ) do { \
18 } while (errno == EINTR);
21 * copy argc and argv for an existing process identified by `pid'
22 * into suitable storage given in ***argv and *argc.
24 * subsequent calls use the same static area for argv and argc.
26 * returns 0 for failure, in which case argc := 0 and argv := NULL
27 * returns 1 for success
29 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
31 static char buf
[_POSIX_ARG_MAX
];
38 /* cmdline is a flattened series of null-terminated strings */
39 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
41 /* not switching this to stdio yet, as this does not need
42 * to be portable, and i'm lazy */
43 if ((fd
= open(buf
, O_RDONLY
)) != -1)
51 if ((count
= read(fd
, buf
, sizeof(buf
))) != -1)
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 */