12 #include <WINGs/WUtil.h>
18 #define RETRY( x ) do { \
20 } while (errno == EINTR);
23 * copy argc and argv for an existing process identified by `pid'
24 * into suitable storage given in ***argv and *argc.
26 * subsequent calls use the same static area for argv and argc.
28 * returns 0 for failure, in which case argc := 0 and argv := NULL
29 * returns 1 for success
31 Bool
GetCommandForPid(int pid
, char ***argv
, int *argc
)
33 static char buf
[_POSIX_ARG_MAX
];
40 /* cmdline is a flattened series of null-terminated strings */
41 snprintf(buf
, sizeof(buf
), "/proc/%d/cmdline", pid
);
43 /* not switching this to stdio yet, as this does not need
44 * to be portable, and i'm lazy */
45 if ((fd
= open(buf
, O_RDONLY
)) != -1)
53 if ((count
= read(fd
, buf
, sizeof(buf
))) != -1)
63 for (i
= 0; i
< count
; i
++)
70 *argv
= (char **)wmalloc(sizeof(char *) * (*argc
+ 1 /* term. null ptr */));
73 /* go through buf, set argv[$next] to the beginning of each string */
74 for (i
= 0, j
= 1; i
< count
; i
++) {
78 (*argv
)[j
++] = &buf
[i
+ 1];
83 /* the list of arguments must be terminated by a null pointer */