From 5f97dd2c44662784d63e62862a38ef7164d8758e Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 12 Jul 2007 15:28:30 +0000 Subject: Translate all file descriptors received from assuan. --- common/ChangeLog | 6 ++++++ common/sysutils.c | 38 +++++++++++++++++++++++++------------- common/sysutils.h | 17 ++++++++++++++++- 3 files changed, 47 insertions(+), 14 deletions(-) (limited to 'common') diff --git a/common/ChangeLog b/common/ChangeLog index 3d1f47160..7dd887cdc 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,9 @@ +2007-07-12 Werner Koch + + * sysutils.h (gnupg_fd_t): New. + * sysutils.c (translate_sys2libc_fd): Use that type instead of int. + (translate_sys2libc_fd_int): New. + 2007-07-09 Werner Koch * t-gettime.c (test_isotime2epoch): Use time_t and not u32. diff --git a/common/sysutils.c b/common/sysutils.c index 47031a152..6f6a9cbf4 100644 --- a/common/sysutils.c +++ b/common/sysutils.c @@ -278,24 +278,36 @@ gnupg_sleep (unsigned int seconds) translates system file handles to libc file handles. FOR_WRITE gives the direction of the handle. */ int -translate_sys2libc_fd (int fd, int for_write) +translate_sys2libc_fd (gnupg_fd_t fd, int for_write) { #ifdef HAVE_W32_SYSTEM int x; - - if (fd <= 2) - return fd; /* Do not do this for error, stdin, stdout, stderr. - (This also ignores an fd of -1.) */ - x = _open_osfhandle (fd, for_write ? 1 : 0); + if (fd == GNUPG_INVALID_FD) + return -1; + + /* Note that _open_osfhandle is currently defined to take and return + a long. */ + x = _open_osfhandle ((long)fd, for_write ? 1 : 0); if (x == -1) log_error ("failed to translate osfhandle %p\n", (void *) fd); - else - { -/* log_info ("_open_osfhandle %p yields %d%s\n", */ -/* (void*)fd, x, for_write? " for writing":"" ); */ - fd = x; - } -#endif /* HAVE_W32_SYSTEM */ + return x; +#else /*!HAVE_W32_SYSTEM */ return fd; +#endif +} + +/* This is the same as translate_sys2libc_fd but takes an integer + which is assumet to be such an system handle. */ +int +translate_sys2libc_fd_int (int fd, int for_write) +{ +#ifdef HAVE_W32_SYSTEM + if (fd <= 2) + return fd; /* Do not do this for error, stdin, stdout, stderr. */ + + return translate_sys2libc_fd ((void*)fd, for_write); +#else + return fd; +#endif } diff --git a/common/sysutils.h b/common/sysutils.h index cc7a50d03..42bf58b9c 100644 --- a/common/sysutils.h +++ b/common/sysutils.h @@ -20,13 +20,28 @@ #ifndef GNUPG_COMMON_SYSUTILS_H #define GNUPG_COMMON_SYSUTILS_H +/* Because we use system handles and not libc low level file + descriptors on W32, we need to declare them as HANDLE (which + actually is a plain pointer). This is required to eventually + support 64 bits Windows systems. */ +#ifdef HAVE_W32_SYSTEM +typedef void *gnupg_fd_t; +#define GNUPG_INVALID_FD ((void*)(-1)) +#else +typedef int gnupg_fd_t; +#define GNUPG_INVALID_FD (-1) +#endif + + void trap_unaligned (void); int disable_core_dumps (void); int enable_core_dumps (void); const unsigned char *get_session_marker (size_t *rlen); /*int check_permissions (const char *path,int extension,int checkonly);*/ void gnupg_sleep (unsigned int seconds); -int translate_sys2libc_fd (int fd, int for_write); +int translate_sys2libc_fd (gnupg_fd_t fd, int for_write); +int translate_sys2libc_fd_int (int fd, int for_write); + #ifdef HAVE_W32_SYSTEM -- cgit v1.2.3