so far this is just a slight shuffling-around of things existing

>From 6e1e133b4759d8031a96c5e9f5056068c47d4ec6 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Fri, 19 Mar 2010 14:09:48 +0100
Subject: [PATCH] Introduce OS-dependent stuff

- move GetCommandForPid() into a separate file
- slightly rewrite it to be more readable along the way
---
 src/Makefile.am   |    1 +
 src/misc.c        |   45 ------------------------------------
 src/osdep/linux.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 45 deletions(-)
 create mode 100644 src/osdep/linux.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 64c6fde..4c0be48 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 c063f18..777e675 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 0000000..b49d706
--- /dev/null
+++ b/src/osdep/linux.c
@@ -0,0 +1,66 @@
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <WINGs/WUtil.h>
+
+#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;
+}
+
-- 
1.7.0


-- 
[-]

mkdir /nonexistent
From 6e1e133b4759d8031a96c5e9f5056068c47d4ec6 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Fri, 19 Mar 2010 14:09:48 +0100
Subject: [PATCH] Introduce OS-dependent stuff

- move GetCommandForPid() into a separate file
- slightly rewrite it to be more readable along the way
---
 src/Makefile.am   |    1 +
 src/misc.c        |   45 ------------------------------------
 src/osdep/linux.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 45 deletions(-)
 create mode 100644 src/osdep/linux.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 64c6fde..4c0be48 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 c063f18..777e675 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 0000000..b49d706
--- /dev/null
+++ b/src/osdep/linux.c
@@ -0,0 +1,66 @@
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <WINGs/WUtil.h>
+
+#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;
+}
+
-- 
1.7.0

Reply via email to