diff --git a/src/Makefile.am b/src/Makefile.am index 90a6c134..dbab5a2e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -45,6 +45,7 @@ wmaker_SOURCES = \ menu.c \ menu.h \ misc.c \ + osdep/linux.c \ monitor.c \ motif.c \ motif.h \ diff --git a/src/misc.c b/src/misc.c index 3e5a8cfc..a7de66b1 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1073,51 +1073,6 @@ char *StrConcatDot(char *a, char *b) return str; } -#define MAX_CMD_SIZE 4096 - -Bool GetCommandForPid(int pid, char ***argv, int *argc) -{ - static char buf[MAX_CMD_SIZE]; - FILE *fPtr; - int count, i, j; - Bool ok = False; - - sprintf(buf, "/proc/%d/cmdline", pid); - fPtr = fopen(buf, "r"); - if (fPtr) { - count = read(fileno(fPtr), buf, MAX_CMD_SIZE); - if (count > 0) { - buf[count - 1] = 0; - for (i = 0, *argc = 0; i < count; i++) { - if (buf[i] == 0) { - (*argc)++; - } - } - if ((*argc) == 0) { - *argv = NULL; - ok = False; - } else { - *argv = (char **)wmalloc(sizeof(char *) * (*argc)); - (*argv)[0] = buf; - for (i = 0, j = 1; i < count; i++) { - if (buf[i] != 0) - continue; - if (i < count - 1) { - (*argv)[j++] = &buf[i + 1]; - } - if (j == *argc) { - break; - } - } - ok = True; - } - } - fclose(fPtr); - } - - return ok; -} - static char *getCommandForWindow(Window win, int elements) { char **argv, *command = NULL; diff --git a/src/osdep/linux.c b/src/osdep/linux.c new file mode 100644 index 00000000..9f87a3cd --- /dev/null +++ b/src/osdep/linux.c @@ -0,0 +1,65 @@ + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "../wconfig.h" + +/* + * copy argc and argv for an existing process identified by `pid' + * into suitable storage given in ***argv and *argc. + * + * returns 0 for failure, in which case argc := 0 and arv := NULL + * returns 1 for success + */ +Bool GetCommandForPid(int pid, char ***argv, int *argc) +{ + static char buf[_POSIX_ARG_MAX]; + int fd, i, j; + ssize_t count; + + *argv = NULL; + *argc = 0; + + /* cmdline is a flattened series of null-terminated strings */ + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + if ((fd = open(buf, O_RDONLY)) == -1) + return False; + + /* XXX: read/close errors */ + if ((count = read(fd, buf, sizeof(buf))) == -1) { + close(fd); + return False; + } + close(fd); + + for (i = 0; i < count; i++) + if (buf[i] == '\0') + (*argc)++; + + if (*argc == 0) + return False; + + *argv = (char **)wmalloc(sizeof(char *) * *argc); + (*argv)[0] = buf; + + /* go through buf, set argv[$next] to the beginning of each string */ + for (i = 0, j = 1; i < count; i++) { + if (buf[i] != '\0') + continue; + if (i < count - 1) + (*argv)[j++] = &buf[i + 1]; + if (j == *argc) + break; + } + + return True; +}