1 /* Copyright (C) 2014-2023 Free Software Foundation, Inc.
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16 /* coreutils.c aggregates the functionality of every other tool into a single
17 binary multiplexed by the value of argv[0]. This is enabled by passing
18 --enable-single-binary to configure.
20 Written by Alex Deymo <deymo@chromium.org>. */
26 # include <sys/prctl.h>
35 /* Declare the main function on each one of the selected tools. This name
36 needs to match the one passed as CFLAGS on single-binary.mk (generated
37 by gen-single-binary.sh). */
38 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
39 int single_binary_main_##main_name (int, char **);
40 # include "coreutils.h"
41 # undef SINGLE_BINARY_PROGRAM
44 /* The official name of this program (e.g., no 'g' prefix). */
45 #define PROGRAM_NAME "coreutils"
48 proper_name ("Alex Deymo")
50 static struct option
const long_options
[] =
52 {GETOPT_HELP_OPTION_DECL
},
53 {GETOPT_VERSION_OPTION_DECL
},
61 if (status
!= EXIT_SUCCESS
)
66 Usage: %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]... \n"),
69 Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\
71 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
72 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
75 /* XXX: Ideally we'd like to present "install" here, not "ginstall". */
76 char const *prog_name_list
=
77 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) " " prog_name_str
78 # include "coreutils.h"
79 # undef SINGLE_BINARY_PROGRAM
83 %s\n", prog_name_list
);
88 Use: '%s --coreutils-prog=PROGRAM_NAME --help' for individual program help.\n"),
90 emit_ancillary_info (PROGRAM_NAME
);
96 launch_program (char const *prog_name
, int prog_argc
, char **prog_argv
)
98 int (*prog_main
) (int, char **) = NULL
;
100 /* Ensure that at least one parameter was passed. */
101 if (!prog_argc
|| !prog_argv
|| !prog_argv
[0] || !prog_name
)
106 /* Look up the right main program. */
107 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
108 else if (STREQ (prog_name_str, prog_name)) \
109 prog_main = single_binary_main_##main_name;
110 # include "coreutils.h"
111 # undef SINGLE_BINARY_PROGRAM
117 #if HAVE_PRCTL && defined PR_SET_NAME
118 /* Not being able to set the program name is not a fatal error. */
119 prctl (PR_SET_NAME
, prog_argv
[0]);
121 #if HAVE_PRCTL && defined PR_SET_MM_ARG_START
122 /* Shift the beginning of the command line to prog_argv[0] (if set) so
123 /proc/$pid/cmdline reflects a more specific value. Note one needs
124 CAP_SYS_RESOURCE or root privileges for this to succeed. */
125 prctl (PR_SET_MM
, PR_SET_MM_ARG_START
, prog_argv
[0], 0, 0);
128 exit (prog_main (prog_argc
, prog_argv
));
132 main (int argc
, char **argv
)
134 char *prog_name
= last_component (argv
[0]);
137 /* Map external name to internal name. */
138 char ginstall
[] = "ginstall";
139 if (STREQ (prog_name
, "install"))
140 prog_name
= ginstall
;
142 /* If this program is called directly as "coreutils" or if the value of
143 argv[0] is an unknown tool (which "coreutils" is), we proceed and parse
145 launch_program (prog_name
, argc
, argv
);
147 /* No known program was selected via argv[0]. Try parsing the first
148 argument as --coreutils-prog=PROGRAM to determine the program. The
149 invocation for this case should be:
150 path/to/coreutils --coreutils-prog=someprog someprog ...
151 The third argument is what the program will see as argv[0]. */
156 char *arg_name
= NULL
;
158 /* If calling coreutils directly, the "script" name isn't passed.
159 Distinguish the two cases with a -shebang suffix. */
160 if (STRPREFIX (argv
[1], "--coreutils-prog="))
163 arg_name
= prog_name
= argv
[1] + strlen ("--coreutils-prog=");
165 else if (STRPREFIX (argv
[1], "--coreutils-prog-shebang="))
168 prog_name
= argv
[1] + strlen ("--coreutils-prog-shebang=");
170 arg_name
= last_component (argv
[2]);
172 arg_name
= prog_name
;
177 argv
[nskip
] = arg_name
; /* XXX: Discards any specified path. */
178 launch_program (prog_name
, argc
- nskip
, argv
+ nskip
);
179 die (EXIT_FAILURE
, 0, _("unknown program %s"),
184 /* No known program was selected. From here on, we behave like any other
185 coreutils program. */
186 initialize_main (&argc
, &argv
);
187 set_program_name (argv
[0]);
188 setlocale (LC_ALL
, "");
189 bindtextdomain (PACKAGE
, LOCALEDIR
);
190 textdomain (PACKAGE
);
191 atexit (close_stdout
);
193 if ((optc
= getopt_long (argc
, argv
, "", long_options
, NULL
)) != -1)
196 case_GETOPT_HELP_CHAR
;
198 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
201 /* Only print the error message when no options have been passed
203 if (optind
== 1 && prog_name
&& !STREQ (prog_name
, "coreutils"))
204 error (0, 0, _("unknown program %s"),
207 usage (EXIT_FAILURE
);