VirtualBox

source: kBuild/vendor/grep/2.12/gnulib-tests/test-openat.c@ 3627

Last change on this file since 3627 was 2595, checked in by bird, 13 years ago

gnu grep version 2.12 (grep-2.12.tar.xz, md5sum=8d2f0346d08b13c18afb81f0e8aa1e2f)

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1/* Test that openat works.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17/* Written by Eric Blake <ebb9@byu.net>, 2009. */
18
19#include <config.h>
20
21#include <fcntl.h>
22
23#include "signature.h"
24SIGNATURE_CHECK (openat, int, (int, char const *, int, ...));
25
26#include <errno.h>
27#include <stdarg.h>
28#include <stdbool.h>
29#include <stdio.h>
30#include <unistd.h>
31
32#include "progname.h"
33#include "macros.h"
34
35#define BASE "test-openat.t"
36
37#include "test-open.h"
38
39static int dfd = AT_FDCWD;
40
41/* Wrapper around openat to test open behavior. */
42static int
43do_open (char const *name, int flags, ...)
44{
45 if (flags & O_CREAT)
46 {
47 mode_t mode = 0;
48 va_list arg;
49 va_start (arg, flags);
50
51 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
52 creates crashing code when 'mode_t' is smaller than 'int'. */
53 mode = va_arg (arg, PROMOTED_MODE_T);
54
55 va_end (arg);
56 return openat (dfd, name, flags, mode);
57 }
58 return openat (dfd, name, flags);
59}
60
61int
62main (int argc _GL_UNUSED, char *argv[])
63{
64 int result;
65
66 set_program_name (argv[0]);
67
68 /* Test behaviour for invalid file descriptors. */
69 {
70 errno = 0;
71 ASSERT (openat (-1, "foo", O_RDONLY) == -1);
72 ASSERT (errno == EBADF);
73 }
74 {
75 errno = 0;
76 ASSERT (openat (99, "foo", O_RDONLY) == -1);
77 ASSERT (errno == EBADF);
78 }
79
80 /* Basic checks. */
81 result = test_open (do_open, false);
82 dfd = open (".", O_RDONLY);
83 ASSERT (0 <= dfd);
84 ASSERT (test_open (do_open, false) == result);
85 ASSERT (close (dfd) == 0);
86
87 /* Check that even when *-safer modules are in use, plain openat can
88 land in fd 0. Do this test last, since it is destructive to
89 stdin. */
90 ASSERT (close (STDIN_FILENO) == 0);
91 ASSERT (openat (AT_FDCWD, ".", O_RDONLY) == STDIN_FILENO);
92 {
93 dfd = open (".", O_RDONLY);
94 ASSERT (STDIN_FILENO < dfd);
95 ASSERT (chdir ("..") == 0);
96 ASSERT (close (STDIN_FILENO) == 0);
97 ASSERT (openat (dfd, ".", O_RDONLY) == STDIN_FILENO);
98 ASSERT (close (dfd) == 0);
99 }
100 return result;
101}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette