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