VirtualBox

source: kBuild/vendor/grep/2.12/gnulib-tests/test-open.h@ 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.9 KB
Line 
1/* Test of opening a file descriptor.
2 Copyright (C) 2007-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 Bruno Haible <bruno@clisp.org>, 2007. */
18
19/* This file is designed to test both open(n,buf[,mode]) and
20 openat(AT_FDCWD,n,buf[,mode]). FUNC is the function to test.
21 Assumes that BASE and ASSERT are already defined, and that
22 appropriate headers are already included. If PRINT, warn before
23 skipping symlink tests with status 77. */
24
25static int
26test_open (int (*func) (char const *, int, ...), bool print)
27{
28 int fd;
29 /* Remove anything from prior partial run. */
30 unlink (BASE "file");
31
32 /* Cannot create directory. */
33 errno = 0;
34 ASSERT (func ("nonexist.ent/", O_CREAT | O_RDONLY, 0600) == -1);
35 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == ENOENT
36 || errno == EINVAL);
37
38 /* Create a regular file. */
39 fd = func (BASE "file", O_CREAT | O_RDONLY, 0600);
40 ASSERT (0 <= fd);
41 ASSERT (close (fd) == 0);
42
43 /* Trailing slash handling. */
44 errno = 0;
45 ASSERT (func (BASE "file/", O_RDONLY) == -1);
46 ASSERT (errno == ENOTDIR || errno == EISDIR || errno == EINVAL);
47
48 /* Directories cannot be opened for writing. */
49 errno = 0;
50 ASSERT (func (".", O_WRONLY) == -1);
51 ASSERT (errno == EISDIR || errno == EACCES);
52
53 /* /dev/null must exist, and be writable. */
54 fd = func ("/dev/null", O_RDONLY);
55 ASSERT (0 <= fd);
56 {
57 char c;
58 ASSERT (read (fd, &c, 1) == 0);
59 }
60 ASSERT (close (fd) == 0);
61 fd = func ("/dev/null", O_WRONLY);
62 ASSERT (0 <= fd);
63 ASSERT (write (fd, "c", 1) == 1);
64 ASSERT (close (fd) == 0);
65
66 /* Although O_NONBLOCK on regular files can be ignored, it must not
67 cause a failure. */
68 fd = func (BASE "file", O_NONBLOCK | O_RDONLY);
69 ASSERT (0 <= fd);
70 ASSERT (close (fd) == 0);
71
72 /* Symlink handling, where supported. */
73 if (symlink (BASE "file", BASE "link") != 0)
74 {
75 ASSERT (unlink (BASE "file") == 0);
76 if (print)
77 fputs ("skipping test: symlinks not supported on this file system\n",
78 stderr);
79 return 77;
80 }
81 errno = 0;
82 ASSERT (func (BASE "link/", O_RDONLY) == -1);
83 ASSERT (errno == ENOTDIR);
84 fd = func (BASE "link", O_RDONLY);
85 ASSERT (0 <= fd);
86 ASSERT (close (fd) == 0);
87
88 /* Cleanup. */
89 ASSERT (unlink (BASE "file") == 0);
90 ASSERT (unlink (BASE "link") == 0);
91
92 return 0;
93}
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