Avoid GCC 11 -Warray-parameter warnings [BZ #26686].
[glibc.git] / stdlib / tst-system.c
blobeddea33f4c8b700c7e52cb2269ef3d308e00d3d7
1 /* Copyright (C) 2002-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <signal.h>
23 #include <paths.h>
25 #include <support/capture_subprocess.h>
26 #include <support/check.h>
27 #include <support/temp_file.h>
28 #include <support/support.h>
30 static char *tmpdir;
31 static long int namemax;
33 static void
34 do_prepare (int argc, char *argv[])
36 tmpdir = support_create_temp_directory ("tst-system-");
37 /* Include the last '/0'. */
38 namemax = pathconf (tmpdir, _PC_NAME_MAX) + 1;
39 TEST_VERIFY_EXIT (namemax != -1);
41 #define PREPARE do_prepare
43 struct args
45 const char *command;
46 int exit_status;
47 int term_sig;
48 const char *path;
51 static void
52 call_system (void *closure)
54 struct args *args = (struct args *) closure;
55 int ret;
57 if (args->path != NULL)
58 TEST_COMPARE (setenv ("PATH", args->path, 1), 0);
59 ret = system (args->command);
60 if (args->term_sig == 0)
62 /* Expect regular termination. */
63 TEST_VERIFY (WIFEXITED (ret) != 0);
64 TEST_COMPARE (WEXITSTATUS (ret), args->exit_status);
66 else
68 /* status_or_signal < 0. Expect termination by signal. */
69 TEST_VERIFY (WIFSIGNALED (ret) != 0);
70 TEST_COMPARE (WTERMSIG (ret), args->term_sig);
74 static int
75 do_test (void)
77 TEST_VERIFY (system (NULL) != 0);
80 char cmd[namemax];
81 memset (cmd, 'a', sizeof(cmd));
82 cmd[sizeof(cmd) - 1] = '\0';
84 struct support_capture_subprocess result;
85 result = support_capture_subprocess (call_system,
86 &(struct args) {
87 cmd, 127, 0, tmpdir
88 });
89 support_capture_subprocess_check (&result, "system", 0, sc_allow_stderr);
91 char *returnerr = xasprintf ("%s: execing %s failed: "
92 "No such file or directory",
93 basename(_PATH_BSHELL), cmd);
94 TEST_COMPARE_STRING (result.err.buffer, returnerr);
95 free (returnerr);
99 char cmd[namemax + 1];
100 memset (cmd, 'a', sizeof(cmd));
101 cmd[sizeof(cmd) - 1] = '\0';
103 struct support_capture_subprocess result;
104 result = support_capture_subprocess (call_system,
105 &(struct args) {
106 cmd, 127, 0, tmpdir
108 support_capture_subprocess_check (&result, "system", 0, sc_allow_stderr);
110 char *returnerr = xasprintf ("%s: execing %s failed: "
111 "File name too long",
112 basename(_PATH_BSHELL), cmd);
113 TEST_COMPARE_STRING (result.err.buffer, returnerr);
114 free (returnerr);
118 struct support_capture_subprocess result;
119 result = support_capture_subprocess (call_system,
120 &(struct args) {
121 "kill $$", 0, SIGTERM
123 support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
127 struct support_capture_subprocess result;
128 result = support_capture_subprocess (call_system,
129 &(struct args) { "echo ...", 0 });
130 support_capture_subprocess_check (&result, "system", 0, sc_allow_stdout);
131 TEST_COMPARE_STRING (result.out.buffer, "...\n");
135 struct support_capture_subprocess result;
136 result = support_capture_subprocess (call_system,
137 &(struct args) { "exit 1", 1 });
138 support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
141 TEST_COMPARE (system (""), 0);
143 return 0;
146 #include <support/test-driver.c>