Merge pull request #12860 from monojenkins/backport-pr-12856-to-2019-02
[mono-project.git] / support / macros.c
blobe15fa76b7edb0a35774956c4e1070f10e366fd35
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"
15 int wifexited (int status)
17 return WIFEXITED (status);
20 int wexitstatus (int status)
22 return WEXITSTATUS (status);
25 int wifsignaled (int status)
27 return WIFSIGNALED (status);
30 int wtermsig (int status)
32 return WTERMSIG (status);
35 int wifstopped (int status)
37 return WIFSTOPPED (status);
40 int wstopsig (int status)
42 return WSTOPSIG (status);
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;
63 if (!dereference)
64 ret = stat(filename, &buf);
65 else
66 ret = lstat(filename, &buf);
68 if (ret) return ret;
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;
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);
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);
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);
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);
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
123 struct passwd pw, *pwp;
124 char buf[4096];
125 int ret;
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);
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;
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;
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;
157 return 0;
159 #endif /* def HAVE_GETPWNAM_R */