Found this lingering in my ChangeLog
[mono/afaerber.git] / support / macros.c
blob03f60745c5de388fbba0e6d6dbc8a6f352b07f01
1 #include "mph.h"
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <pwd.h>
7 #include <grp.h>
8 #include <errno.h>
9 #include <dirent.h>
10 #include <string.h>
11 #include <glib.h>
12 #include "map.h"
14 int wifexited (int status)
16 return WIFEXITED (status);
19 int wexitstatus (int status)
21 return WEXITSTATUS (status);
24 int wifsignaled (int status)
26 return WIFSIGNALED (status);
29 int wtermsig (int status)
31 return WTERMSIG (status);
34 int wifstopped (int status)
36 return WIFSTOPPED (status);
39 int wstopsig (int status)
41 return WSTOPSIG (status);
44 int helper_Mono_Posix_Stat(const char *filename, int dereference,
45 int *device,
46 int *inode,
47 int *mode,
48 int *nlinks,
49 int *uid,
50 int *gid,
51 int *rdev,
52 gint64 *size,
53 gint64 *blksize,
54 gint64 *blocks,
55 gint64 *atime,
56 gint64 *mtime,
57 gint64 *ctime
58 ) {
59 int ret;
60 struct stat buf;
62 if (!dereference)
63 ret = stat(filename, &buf);
64 else
65 ret = lstat(filename, &buf);
67 if (ret) return ret;
69 *device = buf.st_dev;
70 *inode = buf.st_ino;
71 *mode = buf.st_mode;
72 *nlinks = buf.st_nlink;
73 *uid = buf.st_uid;
74 *gid = buf.st_gid;
75 *rdev = buf.st_rdev;
76 *size = buf.st_size;
77 *blksize = buf.st_blksize;
78 *blocks = buf.st_blocks;
79 *atime = buf.st_atime;
80 *mtime = buf.st_mtime;
81 *ctime = buf.st_ctime;
82 return 0;
85 char *helper_Mono_Posix_GetUserName(int uid) {
86 struct passwd *p = getpwuid(uid);
87 if (p == NULL) return NULL;
88 return strdup (p->pw_name);
90 char *helper_Mono_Posix_GetGroupName(int gid) {
91 struct group *p = getgrgid(gid);
92 if (p == NULL) return NULL;
93 return strdup (p->gr_name);
96 char *helper_Mono_Posix_readdir(void *dir) {
97 struct dirent* e = readdir((DIR*) dir);
98 if (e == NULL) return NULL;
99 return strdup (e->d_name);
102 int helper_Mono_Posix_getpwnamuid (int mode, char *in_name, int in_uid,
103 char **account,
104 char **password,
105 int *uid,
106 int *gid,
107 char **name,
108 char **home,
109 char **shell
112 struct passwd pw, *pwp;
113 char buf[4096];
114 int ret;
116 if (mode == 0)
117 ret = getpwnam_r (in_name, &pw, buf, 4096, &pwp);
118 else
119 ret = getpwuid_r (in_uid, &pw, buf, 4096, &pwp);
121 if (ret == 0 && pwp == NULL) {
122 // Don't know why this happens, but it does.
123 // ret == 0, errno == 0, but no record was found.
124 ret = ENOENT;
127 if (ret) {
128 *account = NULL; // prevent marshalling unset pointers
129 *password = NULL;
130 *uid = 0;
131 *gid = 0;
132 *name = NULL;
133 *home = NULL;
134 *shell = NULL;
135 return ret;
138 *account = pwp->pw_name;
139 *password = pwp->pw_passwd;
140 *uid = pwp->pw_uid;
141 *gid = pwp->pw_gid;
142 *name = pwp->pw_gecos;
143 *home = pwp->pw_dir;
144 *shell = pwp->pw_shell;
146 return 0;