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