From: Jonathan Pryor Date: Mon, 17 Dec 2007 22:49:20 +0000 (-0000) Subject: * map.h: Flush; add Mono_Posix_Syscall__Utsname, Mono_Posix_Syscall_uname() X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=aaf6f0a81667378a517259bd7356b88be5ec8c02;p=mono.git * map.h: Flush; add Mono_Posix_Syscall__Utsname, Mono_Posix_Syscall_uname() declarations. * Makefile.am: Add sys-utsname.c to the build. * sys-utsname.c: Added; binding for uname(2). * mph.h: Change definition of _mph_copy_structure_strings() so that the offset array specifies whether the string is a pointer (char*) or inline array (char[]) within the structure. Necessary because most bound structure string members are pointers, but `struct utsname' uses arrays. * fstab.c, pwd.c: Update offset array generation due to above. * x-struct-str.c: Update _mph_copy_structure_strings() as above. svn path=/trunk/mono/; revision=91500 --- diff --git a/support/ChangeLog b/support/ChangeLog index c0fa91d6f54..5c0aca4ea97 100644 --- a/support/ChangeLog +++ b/support/ChangeLog @@ -1,3 +1,16 @@ +2007-12-17 Jonathan Pryor + + * map.h: Flush; add Mono_Posix_Syscall__Utsname, Mono_Posix_Syscall_uname() + declarations. + * Makefile.am: Add sys-utsname.c to the build. + * sys-utsname.c: Added; binding for uname(2). + * mph.h: Change definition of _mph_copy_structure_strings() so that the + offset array specifies whether the string is a pointer (char*) or inline + array (char[]) within the structure. Necessary because most bound + structure string members are pointers, but `struct utsname' uses arrays. + * fstab.c, pwd.c: Update offset array generation due to above. + * x-struct-str.c: Update _mph_copy_structure_strings() as above. + 2007-11-01 Geoff Norton * stdio.c: Handle Leopards lack of L_cuserid. diff --git a/support/Makefile.am b/support/Makefile.am index 96cc2002e28..1f6a51c1a8f 100644 --- a/support/Makefile.am +++ b/support/Makefile.am @@ -40,6 +40,7 @@ MPH_UNIX_SOURCE = \ sys-stat.c \ sys-statvfs.c \ sys-time.c \ + sys-utsname.c \ sys-wait.c \ sys-xattr.c \ syslog.c \ diff --git a/support/fstab.c b/support/fstab.c index e81821b18c6..010342704a1 100644 --- a/support/fstab.c +++ b/support/fstab.c @@ -32,40 +32,40 @@ G_BEGIN_DECLS typedef struct checklist mph_fstab; -static const size_t +static const mph_string_offset_t fstab_offsets[] = { - offsetof (struct checklist, fs_spec), - offsetof (struct checklist, fs_dir), - offsetof (struct checklist, fs_type) + MPH_STRING_OFFSET (struct checklist, fs_spec, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct checklist, fs_dir, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct checklist, fs_type, MPH_STRING_OFFSET_PTR) }; -static const size_t +static const mph_string_offset_t mph_fstab_offsets[] = { - offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_file), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_type) + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR) }; #elif defined (HAVE_FSTAB_H) typedef struct fstab mph_fstab; -static const size_t +static const mph_string_offset_t fstab_offsets[] = { - offsetof (struct fstab, fs_spec), - offsetof (struct fstab, fs_file), - offsetof (struct fstab, fs_vfstype), - offsetof (struct fstab, fs_mntops), - offsetof (struct fstab, fs_type) + MPH_STRING_OFFSET (struct fstab, fs_spec, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct fstab, fs_file, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct fstab, fs_vfstype, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct fstab, fs_mntops, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct fstab, fs_type, MPH_STRING_OFFSET_PTR) }; -static const size_t +static const mph_string_offset_t mph_fstab_offsets[] = { - offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_file), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_vfstype), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_mntops), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_type) + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_type, MPH_STRING_OFFSET_PTR) }; #endif /* def HAVE_FSTAB_H */ @@ -110,20 +110,20 @@ copy_fstab (struct Mono_Posix_Syscall__Fstab *to, mph_fstab *from) typedef struct vfstab mph_fstab; -static const size_t +static const mph_string_offset_t vfstab_offsets[] = { - offsetof (struct vfstab, vfs_special), - offsetof (struct vfstab, vfs_mountp), - offsetof (struct vfstab, vfs_fstype), - offsetof (struct vfstab, vfs_mntopts) + MPH_STRING_OFFSET (struct vfstab, vfs_special, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct vfstab, vfs_mountp, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct vfstab, vfs_fstype, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct vfstab, vfs_mntopts, MPH_STRING_OFFSET_PTR) }; -static const size_t +static const mph_string_offset_t mph_fstab_offsets[] = { - offsetof (struct Mono_Posix_Syscall__Fstab, fs_spec), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_file), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_vfstype), - offsetof (struct Mono_Posix_Syscall__Fstab, fs_mntops) + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_spec, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_file, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_vfstype, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Fstab, fs_mntops, MPH_STRING_OFFSET_PTR) }; /* diff --git a/support/map.h b/support/map.h index 302b08d9c17..179defe372b 100644 --- a/support/map.h +++ b/support/map.h @@ -1372,6 +1372,7 @@ struct Mono_Posix_Syscall__Dirent; struct Mono_Posix_Syscall__Fstab; struct Mono_Posix_Syscall__Group; struct Mono_Posix_Syscall__Passwd; +struct Mono_Posix_Syscall__Utsname; struct Mono_Posix_Timeval; struct Mono_Posix_Timezone; struct Mono_Posix_Utimbuf; @@ -1491,6 +1492,16 @@ struct Mono_Posix_Syscall__Passwd { void* _pw_buf_; }; +struct Mono_Posix_Syscall__Utsname { + void* sysname; + void* nodename; + void* release; + void* version; + void* machine; + void* domainname; + void* _buf_; +}; + struct Mono_Posix_Timeval { gint64 tv_sec; /* time_t */ gint64 tv_usec; /* suseconds_t */ @@ -1678,6 +1689,7 @@ gint64 Mono_Posix_Syscall_telldir (void* dir); gint64 Mono_Posix_Syscall_time (gint64* t); int Mono_Posix_Syscall_truncate (const char* path, gint64 length); int Mono_Posix_Syscall_ttyname_r (int fd, char* buf, guint64 buflen); +int Mono_Posix_Syscall_uname (struct Mono_Posix_Syscall__Utsname* buf); int Mono_Posix_Syscall_utime (const char* filename, struct Mono_Posix_Utimbuf* buf, int use_buf); int Mono_Posix_Syscall_utimes (const char* filename, struct Mono_Posix_Timeval* tvp); int Mono_Posix_Syscall_WEXITSTATUS (int status); diff --git a/support/mph.h b/support/mph.h index daa5828d6db..224f0109333 100644 --- a/support/mph.h +++ b/support/mph.h @@ -27,6 +27,7 @@ #include +#include /* offsetof */ #include /* LONG_MAX, ULONG_MAX */ #include /* for ERANGE */ #include /* for g* types, etc. */ @@ -198,10 +199,20 @@ recheck_range (int ret) return 0; } +typedef unsigned int mph_string_offset_t; + +enum { + MPH_STRING_OFFSET_PTR = 0x0, + MPH_STRING_OFFSET_ARRAY = 0x1, + MPH_STRING_OFFSET_MASK = 0x1 +}; + +#define MPH_STRING_OFFSET(type,member,kind) ((offsetof(type,member) << 1) | kind) + MPH_INTERNAL char* _mph_copy_structure_strings ( - void *to, const size_t *to_offsets, - const void *from, const size_t *from_offsets, + void *to, const mph_string_offset_t *to_offsets, + const void *from, const mph_string_offset_t *from_offsets, size_t num_strings); #endif /* ndef INC_mph_H */ diff --git a/support/pwd.c b/support/pwd.c index e622027458d..13bccef9b55 100644 --- a/support/pwd.c +++ b/support/pwd.c @@ -18,22 +18,22 @@ G_BEGIN_DECLS -static const size_t +static const mph_string_offset_t passwd_offsets[] = { - offsetof (struct passwd, pw_name), - offsetof (struct passwd, pw_passwd), - offsetof (struct passwd, pw_gecos), - offsetof (struct passwd, pw_dir), - offsetof (struct passwd, pw_shell) + MPH_STRING_OFFSET (struct passwd, pw_name, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct passwd, pw_passwd, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct passwd, pw_gecos, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct passwd, pw_dir, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct passwd, pw_shell, MPH_STRING_OFFSET_PTR) }; -static const size_t +static const mph_string_offset_t mph_passwd_offsets[] = { - offsetof (struct Mono_Posix_Syscall__Passwd, pw_name), - offsetof (struct Mono_Posix_Syscall__Passwd, pw_passwd), - offsetof (struct Mono_Posix_Syscall__Passwd, pw_gecos), - offsetof (struct Mono_Posix_Syscall__Passwd, pw_dir), - offsetof (struct Mono_Posix_Syscall__Passwd, pw_shell) + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd, pw_name, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd, pw_passwd, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd, pw_gecos, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd, pw_dir, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET (struct Mono_Posix_Syscall__Passwd, pw_shell, MPH_STRING_OFFSET_PTR) }; /* diff --git a/support/sys-utsname.c b/support/sys-utsname.c new file mode 100644 index 00000000000..deccad5a07b --- /dev/null +++ b/support/sys-utsname.c @@ -0,0 +1,67 @@ +/* + * wrapper functions. + * + * Authors: + * Jonathan Pryor (jonpryor@vt.edu) + * + * Copyright (C) 2004 Jonathan Pryor + */ + +#include +#include +#include + +#include "map.h" +#include "mph.h" + +#include + +G_BEGIN_DECLS + +static const mph_string_offset_t +utsname_offsets[] = { + MPH_STRING_OFFSET(struct utsname, sysname, MPH_STRING_OFFSET_ARRAY), + MPH_STRING_OFFSET(struct utsname, nodename, MPH_STRING_OFFSET_ARRAY), + MPH_STRING_OFFSET(struct utsname, release, MPH_STRING_OFFSET_ARRAY), + MPH_STRING_OFFSET(struct utsname, version, MPH_STRING_OFFSET_ARRAY), + MPH_STRING_OFFSET(struct utsname, machine, MPH_STRING_OFFSET_ARRAY) +}; + +static const mph_string_offset_t +mph_utsname_offsets[] = { + MPH_STRING_OFFSET(struct Mono_Posix_Syscall__Utsname, sysname, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct Mono_Posix_Syscall__Utsname, nodename, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct Mono_Posix_Syscall__Utsname, release, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct Mono_Posix_Syscall__Utsname, version, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct Mono_Posix_Syscall__Utsname, machine, MPH_STRING_OFFSET_PTR) +}; + +int +Mono_Posix_Syscall_uname (struct Mono_Posix_Syscall__Utsname *buf) +{ + struct utsname _buf; + int r; + + if (!buf) { + errno = EFAULT; + return -1; + } + + r = uname (&_buf); + if (r == 0) { + buf->_buf_ = _mph_copy_structure_strings (buf, mph_utsname_offsets, + &_buf, utsname_offsets, sizeof(utsname_offsets)/sizeof(utsname_offsets[0])); + buf->domainname = NULL; + if (!buf->_buf_) { + errno = ENOMEM; + return -1; + } + } + return r; +} + +G_END_DECLS + +/* + * vim: noexpandtab + */ diff --git a/support/x-struct-str.c b/support/x-struct-str.c index 68925ed2cf8..89d5a61b120 100644 --- a/support/x-struct-str.c +++ b/support/x-struct-str.c @@ -10,12 +10,20 @@ #define MAX_OFFSETS 10 -#define str_at(p, n) (*(char**)(((char*)p)+n)) +#define OFFSET_SHIFT 1 + +#define lstr_at(p, n) (*(char**)(((char*)(p))+(n >> OFFSET_SHIFT))) + +#define str_at(p, n) ( \ + (((n) & MPH_STRING_OFFSET_MASK) == MPH_STRING_OFFSET_ARRAY) \ + ? (char*)(p) + (n >> OFFSET_SHIFT) \ + : lstr_at(p, n) \ +) char* MPH_INTERNAL _mph_copy_structure_strings ( - void *to, const size_t *to_offsets, - const void *from, const size_t *from_offsets, + void *to, const mph_string_offset_t *to_offsets, + const void *from, const mph_string_offset_t *from_offsets, size_t num_strings) { int i; @@ -26,7 +34,7 @@ _mph_copy_structure_strings ( g_assert (num_strings < MAX_OFFSETS); for (i = 0; i < num_strings; ++i) { - str_at (to, to_offsets[i]) = NULL; + lstr_at (to, to_offsets[i]) = NULL; } buflen = num_strings; @@ -45,7 +53,7 @@ _mph_copy_structure_strings ( for (i = 0; i < num_strings; ++i) { if (len[i] > 0) { - str_at (to, to_offsets[i]) = + lstr_at (to, to_offsets[i]) = strcpy (cur, str_at (from, from_offsets[i])); cur += (len[i] +1); } @@ -62,6 +70,7 @@ struct foo { char *a; int b; char *c; + char d[10]; }; struct bar { @@ -69,22 +78,32 @@ struct bar { char *a; double d; char *c; + char *e; }; int main () { /* test copying foo to bar */ - struct foo f = {"hello", 42, "world"}; + struct foo f = {"hello", 42, "world", "!!"}; struct bar b; - size_t foo_offsets[] = {offsetof(struct foo, a), offsetof(struct foo, c)}; - size_t bar_offsets[] = {offsetof(struct bar, a), offsetof(struct bar, c)}; + mph_string_offset_t foo_offsets[] = { + MPH_STRING_OFFSET(struct foo, a, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct foo, c, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct foo, d, MPH_STRING_OFFSET_ARRAY) + }; + mph_string_offset_t bar_offsets[] = { + MPH_STRING_OFFSET(struct bar, a, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct bar, c, MPH_STRING_OFFSET_PTR), + MPH_STRING_OFFSET(struct bar, e, MPH_STRING_OFFSET_PTR) + }; char *buf; buf = _mph_copy_structure_strings (&b, bar_offsets, - &f, foo_offsets, 2); + &f, foo_offsets, 3); printf ("b.a=%s\n", b.a); printf ("b.c=%s\n", b.c); + printf ("b.e=%s\n", b.e); return 0; }