1 /* Child program invoked by test-execute-main.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
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, or (at your option)
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* If the user's config.h happens to include <sys/stat.h>, let it include only
18 the system's <sys/stat.h> here. */
19 #define __need_system_sys_stat_h
22 /* Get the original definition of fstat. It might be defined as a macro.
23 Also, 'stat' might be defined as a macro. */
24 #include <sys/types.h>
26 #undef __need_system_sys_stat_h
28 /* Return non-zero if FD is opened to a device. */
34 #if defined _WIN32 && ! defined __CYGWIN__
39 && !S_ISREG (st
.st_mode
);
42 /* Now include the other header files. */
51 #if defined _WIN32 && ! defined __CYGWIN__
52 /* Get declarations of the native Windows API functions. */
53 # define WIN32_LEAN_AND_MEAN
55 /* Get _get_osfhandle, _isatty, _chdir, _getcwd. */
59 /* In this file, we use only system functions, no overrides from gnulib. */
75 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
77 gl_msvc_invalid_parameter_handler (const wchar_t *expression
,
78 const wchar_t *function
,
86 /* Return non-zero if FD is open. */
90 #if defined _WIN32 && ! defined __CYGWIN__
91 /* On native Windows, the initial state of unassigned standard file
92 descriptors is that they are open but point to an
93 INVALID_HANDLE_VALUE, and there is no fcntl. */
94 return (HANDLE
) _get_osfhandle (fd
) != INVALID_HANDLE_VALUE
;
97 # error Please port fcntl to your platform
99 return 0 <= fcntl (fd
, F_GETFL
);
104 main (int argc
, char *argv
[])
107 /* Check an invocation without arguments. Check the exit code. */
110 int test
= atoi (argv
[1]);
114 /* Check argument passing. */
116 && strcmp (argv
[2], "abc def") == 0
117 && strcmp (argv
[3], "abc\"def\"ghi") == 0
118 && strcmp (argv
[4], "xyz\"") == 0
119 && strcmp (argv
[5], "abc\\def\\ghi") == 0
120 && strcmp (argv
[6], "xyz\\") == 0
121 && strcmp (argv
[7], "???") == 0
122 && strcmp (argv
[8], "***") == 0
123 && strcmp (argv
[9], "") == 0
124 && strcmp (argv
[10], "foo") == 0
125 && strcmp (argv
[11], "") == 0);
126 #if !(defined _WIN32 && !defined __CYGWIN__)
128 /* Check SIGPIPE handling with ignore_sigpipe = false. */
130 /* Check SIGPIPE handling with ignore_sigpipe = true. */
135 /* Check other signal. */
139 /* Check stdin is inherited. */
140 return !(fgetc (stdin
) == 'F' && fgetc (stdin
) == 'o');
142 /* Check null_stdin = true. */
143 return !(fgetc (stdin
) == EOF
);
145 /* Check stdout is inherited, part 1 (regular file). */
146 return !(fputs ("bar", stdout
) != EOF
&& fflush (stdout
) == 0);
148 /* Check stdout is inherited, part 2 (device). */
150 /* Check null_stdout = true. */
151 return !is_device (STDOUT_FILENO
);
153 /* Check stderr is inherited, part 1 (regular file). */
154 return !(fputs ("bar", stderr
) != EOF
&& fflush (stderr
) == 0);
156 /* Check stderr is inherited, part 2 (device). */
158 /* Check null_stderr = true. */
159 return !is_device (STDERR_FILENO
);
162 /* Check file descriptors >= 3 can be inherited. */
164 /* Check file descriptors >= 3 with O_CLOEXEC bit are not inherited. */
165 #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
166 /* Avoid exceptions from within _get_osfhandle. */
167 _set_invalid_parameter_handler (gl_msvc_invalid_parameter_handler
);
174 for (fd
= 0; fd
< 20; fd
++)
180 sprintf (p
, "%d ", fd
);
183 const char *expected
= (test
< 16 ? "0 1 2 10 " : "0 1 2 ");
184 if (strcmp (buf
, expected
) == 0)
188 fprintf (stderr
, "Test case %d: %s\n", test
, buf
); fflush (stderr
);
193 /* Check that file descriptors >= 3, open for reading, can be inherited,
194 including the file position. */
197 int n
= read (10, buf
, sizeof (buf
));
198 return !(n
== 4 && memcmp (buf
, "obar", 4) == 0);
201 /* Check that file descriptors >= 3, open for writing, can be inherited,
202 including the file position. */
204 int n
= write (10, "bar", 3);
208 /* Check that file descriptors >= 3, when inherited, preserve their
209 isatty() property, part 1 (regular file). */
211 /* Check that file descriptors >= 3, when inherited, preserve their
212 isatty() property, part 2 (character devices). */
214 #if defined _WIN32 && ! defined __CYGWIN__
215 return 4 + 2 * (_isatty (10) != 0) + (_isatty (11) != 0);
217 return 4 + 2 * (isatty (10) != 0) + (isatty (11) != 0);
221 /* Check execution in a different directory. */
224 #if defined _WIN32 && ! defined __CYGWIN__
225 if (_chdir ("..") != 0)
227 if (_getcwd (cwd
, sizeof (cwd
)) == NULL
)
230 if (chdir ("..") != 0)
232 if (getcwd (cwd
, sizeof (cwd
)) == NULL
)
235 return (argc
== 3 && strcmp (argv
[2], cwd
) == 0 ? 0 : 3);