Update Serbian translation from master branch
[wmaker-crm.git] / src / osdep_linux.c
blob890ba6a243fba1d9e54a1269eea0863ed481bd7c
2 #include <sys/types.h>
3 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include <WINGs/WUtil.h>
14 #include "wconfig.h"
15 #include "osdep.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];
30 int fd, i, j;
31 ssize_t count;
33 *argv = NULL;
34 *argc = 0;
36 /* cmdline is a flattened series of null-terminated strings */
37 snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
38 while (1) {
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);
42 if (fd != -1)
43 break;
44 if (errno == EINTR)
45 continue;
46 return False;
49 while (1) {
50 count = read(fd, buf, sizeof(buf));
51 if (count != -1)
52 break;
53 if (errno == EINTR)
54 continue;
55 close(fd);
56 return False;
58 close(fd);
60 /* count args */
61 for (i = 0; i < count; i++)
62 if (buf[i] == '\0')
63 (*argc)++;
65 if (*argc == 0)
66 return False;
68 *argv = (char **)wmalloc(sizeof(char *) * (*argc + 1 /* term. null ptr */));
69 (*argv)[0] = buf;
71 /* go through buf, set argv[$next] to the beginning of each string */
72 for (i = 0, j = 1; i < count; i++) {
73 if (buf[i] != '\0')
74 continue;
75 if (i < count - 1)
76 (*argv)[j++] = &buf[i + 1];
77 if (j == *argc)
78 break;
81 /* the list of arguments must be terminated by a null pointer */
82 (*argv)[j] = NULL;
83 return True;