2006-11-28 Miguel de Icaza <miguel@novell.com>
[mono-project.git] / support / macros.c
blobcba5e23b55ba90cdb7d2436547ce4e3e237de849
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>
13 int wifexited (int status)
15 return WIFEXITED (status);
18 int wexitstatus (int status)
20 return WEXITSTATUS (status);
23 int wifsignaled (int status)
25 return WIFSIGNALED (status);
28 int wtermsig (int status)
30 return WTERMSIG (status);
33 int wifstopped (int status)
35 return WIFSTOPPED (status);
38 int wstopsig (int status)
40 return WSTOPSIG (status);
43 int helper_Mono_Posix_Stat(const char *filename, int dereference,
44 int *device,
45 int *inode,
46 int *mode,
47 int *nlinks,
48 int *uid,
49 int *gid,
50 int *rdev,
51 gint64 *size,
52 gint64 *blksize,
53 gint64 *blocks,
54 gint64 *atime,
55 gint64 *mtime,
56 gint64 *ctime
57 ) {
58 int ret;
59 struct stat buf;
61 if (!dereference)
62 ret = stat(filename, &buf);
63 else
64 ret = lstat(filename, &buf);
66 if (ret) return ret;
68 *device = buf.st_dev;
69 *inode = buf.st_ino;
70 *mode = buf.st_mode;
71 *nlinks = buf.st_nlink;
72 *uid = buf.st_uid;
73 *gid = buf.st_gid;
74 *rdev = buf.st_rdev;
75 *size = buf.st_size;
76 *blksize = buf.st_blksize;
77 *blocks = buf.st_blocks;
78 *atime = buf.st_atime;
79 *mtime = buf.st_mtime;
80 *ctime = buf.st_ctime;
81 return 0;
84 char *helper_Mono_Posix_GetUserName(int uid) {
85 struct passwd *p = getpwuid(uid);
86 if (p == NULL) return NULL;
87 return strdup (p->pw_name);
89 char *helper_Mono_Posix_GetGroupName(int gid) {
90 struct group *p = getgrgid(gid);
91 if (p == NULL) return NULL;
92 return strdup (p->gr_name);
95 char *helper_Mono_Posix_readdir(void *dir) {
96 struct dirent* e = readdir((DIR*) dir);
97 if (e == NULL) return NULL;
98 return strdup (e->d_name);
101 int helper_Mono_Posix_getpwnamuid (int mode, char *in_name, int in_uid,
102 char **account,
103 char **password,
104 int *uid,
105 int *gid,
106 char **name,
107 char **home,
108 char **shell
111 struct passwd pw, *pwp;
112 char buf[4096];
113 int ret;
115 if (mode == 0)
116 ret = getpwnam_r (in_name, &pw, buf, 4096, &pwp);
117 else
118 ret = getpwuid_r (in_uid, &pw, buf, 4096, &pwp);
120 if (ret == 0 && pwp == NULL) {
121 // Don't know why this happens, but it does.
122 // ret == 0, errno == 0, but no record was found.
123 ret = ENOENT;
126 if (ret) {
127 *account = NULL; // prevent marshalling unset pointers
128 *password = NULL;
129 *uid = 0;
130 *gid = 0;
131 *name = NULL;
132 *home = NULL;
133 *shell = NULL;
134 return ret;
137 *account = pwp->pw_name;
138 *password = pwp->pw_passwd;
139 *uid = pwp->pw_uid;
140 *gid = pwp->pw_gid;
141 *name = pwp->pw_gecos;
142 *home = pwp->pw_dir;
143 *shell = pwp->pw_shell;
145 return 0;