X-Git-Url: https://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-cloexec.c;h=374de3038672af3ca72599558dff97bc4f41cabf;hb=1bb384c44db25aef8f8b455cef22af32d8401abc;hp=8df733a70bfeeb85d56de8e87e3a8dc0e6d31459;hpb=d8ccc13ba3ba05654f9be09bba5492cc8da30c3b;p=gnulib.git diff --git a/tests/test-cloexec.c b/tests/test-cloexec.c index 8df733a70..374de3038 100644 --- a/tests/test-cloexec.c +++ b/tests/test-cloexec.c @@ -1,5 +1,5 @@ /* Test duplicating non-inheritable file descriptors. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009-2011 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,8 +22,6 @@ #include #include -#include -#include #include #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ @@ -32,17 +30,8 @@ # include #endif -#define ASSERT(expr) \ - do \ - { \ - if (!(expr)) \ - { \ - fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ - fflush (stderr); \ - abort (); \ - } \ - } \ - while (0) +#include "binary-io.h" +#include "macros.h" /* Return non-zero if FD is open and inheritable across exec/spawn. */ static int @@ -66,6 +55,21 @@ is_inheritable (int fd) #endif } +#if !O_BINARY +# define setmode(f,m) zero () +static int zero (void) { return 0; } +#endif + +/* Return non-zero if FD is open in the given MODE, which is either + O_TEXT or O_BINARY. */ +static int +is_mode (int fd, int mode) +{ + int value = setmode (fd, O_BINARY); + setmode (fd, value); + return mode == value; +} + int main (void) { @@ -94,6 +98,21 @@ main (void) ASSERT (!is_inheritable (fd)); ASSERT (close (fd2) == 0); + /* On systems that distinguish between text and binary mode, + dup_cloexec reuses the mode of the source. */ + setmode (fd, O_BINARY); + ASSERT (is_mode (fd, O_BINARY)); + fd2 = dup_cloexec (fd); + ASSERT (fd < fd2); + ASSERT (is_mode (fd2, O_BINARY)); + ASSERT (close (fd2) == 0); + setmode (fd, O_TEXT); + ASSERT (is_mode (fd, O_TEXT)); + fd2 = dup_cloexec (fd); + ASSERT (fd < fd2); + ASSERT (is_mode (fd2, O_TEXT)); + ASSERT (close (fd2) == 0); + /* Test error handling. */ errno = 0; ASSERT (set_cloexec_flag (-1, false) == -1);