Prepare Mono for Android NDK with unified headers (#5680)
[mono.git] / support / macros.c
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 "mph.h" /* Don't remove or move after map.h! Works around issues with Android SDK unified headers */
13 #include "map.h"
14
15 int wifexited (int status)
16 {
17         return WIFEXITED (status);
18 }
19
20 int wexitstatus (int status)
21 {
22         return WEXITSTATUS (status);
23 }
24
25 int wifsignaled (int status)
26 {
27         return WIFSIGNALED (status);
28 }
29
30 int wtermsig (int status)
31 {
32         return WTERMSIG (status);
33 }
34
35 int wifstopped (int status)
36 {
37         return WIFSTOPPED (status);
38 }
39
40 int wstopsig (int status)
41 {
42         return WSTOPSIG (status);
43 }
44
45 int helper_Mono_Posix_Stat(const char *filename, int dereference, 
46         int *device,
47         int *inode,
48         int *mode,
49         int *nlinks,
50         int *uid,
51         int *gid,
52         int *rdev,
53         gint64 *size,
54         gint64 *blksize,
55         gint64 *blocks,
56         gint64 *atime,
57         gint64 *mtime,
58         gint64 *ctime
59         ) {
60         int ret;
61         struct stat buf;
62         
63         if (!dereference)
64                 ret = stat(filename, &buf);
65         else
66                 ret = lstat(filename, &buf);
67         
68         if (ret) return ret;
69         
70         *device = buf.st_dev;
71         *inode = buf.st_ino;
72         *mode = buf.st_mode;
73         *nlinks = buf.st_nlink;
74         *uid = buf.st_uid;
75         *gid = buf.st_gid;
76         *rdev = buf.st_rdev;
77         *size = buf.st_size;
78         *blksize = buf.st_blksize;
79         *blocks = buf.st_blocks;
80         *atime = buf.st_atime;
81         *mtime = buf.st_mtime;
82         *ctime = buf.st_ctime;
83         return 0;
84 }
85
86 char *helper_Mono_Posix_GetUserName(int uid) {
87         struct passwd *p = getpwuid(uid);
88         if (p == NULL) return NULL;
89         return strdup (p->pw_name);
90 }
91 char *helper_Mono_Posix_GetGroupName(int gid) {
92         struct group *p = getgrgid(gid);
93         if (p == NULL) return NULL;
94         return strdup (p->gr_name);
95 }
96
97 char *helper_Mono_Posix_readdir(void *dir) {
98         struct dirent* e = readdir((DIR*) dir);
99         if (e == NULL) return NULL;
100         return strdup (e->d_name);
101 }
102
103 #if HAVE_GETPWNAM_R
104 int helper_Mono_Posix_getpwnamuid (int mode, char *in_name, int in_uid,
105         char **account,
106         char **password,
107         int *uid,
108         int *gid,
109         char **name,
110         char **home,
111         char **shell);
112
113 int helper_Mono_Posix_getpwnamuid (int mode, char *in_name, int in_uid,
114         char **account,
115         char **password,
116         int *uid,
117         int *gid,
118         char **name,
119         char **home,
120         char **shell
121         ) {
122
123         struct passwd pw, *pwp;
124         char buf[4096];
125         int ret;
126
127         if (mode == 0)
128                 ret = getpwnam_r (in_name, &pw, buf, 4096, &pwp);
129         else
130                 ret = getpwuid_r (in_uid, &pw, buf, 4096, &pwp);
131
132         if (ret == 0 && pwp == NULL) {
133                 // Don't know why this happens, but it does.
134                 // ret == 0, errno == 0, but no record was found.
135                 ret = ENOENT;
136         }
137
138         if (ret) {
139                 *account = NULL; // prevent marshalling unset pointers
140                 *password = NULL;
141                 *uid = 0;
142                 *gid = 0;
143                 *name = NULL;
144                 *home = NULL;
145                 *shell = NULL;
146                 return ret;
147         }
148
149         *account = pwp->pw_name;
150         *password = pwp->pw_passwd;
151         *uid = pwp->pw_uid;
152         *gid = pwp->pw_gid;
153         *name = pwp->pw_gecos;
154         *home = pwp->pw_dir;
155         *shell = pwp->pw_shell;
156
157         return 0;
158 }
159 #endif  /* def HAVE_GETPWNAM_R */
160