kill: add undocumented -L for compatibility
[coreutils.git] / src / coreutils.c
blob4141346b397b215b0d0ab125259e96e757b60b2a
1 /* Copyright (C) 2014-2015 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 <http://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>. */
22 #include <config.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #if HAVE_PRCTL
26 # include <sys/prctl.h>
27 #endif
29 #include "system.h"
30 #include "error.h"
31 #include "quote.h"
33 #ifdef SINGLE_BINARY
34 /* Declare the main function on each one of the selected tools. This name
35 needs to match the one passed as CFLAGS on single-binary.mk (generated
36 by gen-single-binary.sh). */
37 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
38 int single_binary_main_##main_name (int, char **);
39 # include "coreutils.h"
40 # undef SINGLE_BINARY_PROGRAM
41 #endif
43 /* The official name of this program (e.g., no 'g' prefix). */
44 #define PROGRAM_NAME "coreutils"
46 #define AUTHORS \
47 proper_name ("Alex Deymo")
49 static struct option const long_options[] =
51 {GETOPT_HELP_OPTION_DECL},
52 {GETOPT_VERSION_OPTION_DECL},
53 {NULL, 0, NULL, 0}
57 void
58 usage (int status)
60 if (status != EXIT_SUCCESS)
61 emit_try_help ();
62 else
64 printf (_("\
65 Usage: %s --coreutils-prog=PROGRAM_NAME [PARAMETERS]... \n"),
66 program_name);
67 fputs (_("\
68 Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\
69 \n"), stdout);
70 fputs (HELP_OPTION_DESCRIPTION, stdout);
71 fputs (VERSION_OPTION_DESCRIPTION, stdout);
73 #ifdef SINGLE_BINARY
74 /* XXX: Ideally we'd like to present "install" here, not "ginstall". */
75 char const *prog_name_list =
76 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) " " prog_name_str
77 # include "coreutils.h"
78 # undef SINGLE_BINARY_PROGRAM
80 printf ("\n\
81 Built-in programs:\n\
82 %s\n", prog_name_list);
83 #endif
85 printf (_("\
86 \n\
87 Use: '%s --coreutils-prog=PROGRAM_NAME --help' for individual program help.\n"),
88 program_name);
89 emit_ancillary_info (PROGRAM_NAME);
91 exit (status);
94 static void
95 launch_program (const char *prog_name, int prog_argc, char **prog_argv)
97 int (*prog_main) (int, char **) = NULL;
99 /* Ensure that at least one parameter was passed. */
100 if (!prog_argc || !prog_argv || !prog_argv[0] || !prog_name)
101 return;
103 #ifdef SINGLE_BINARY
104 if (false);
105 /* Look up the right main program. */
106 # define SINGLE_BINARY_PROGRAM(prog_name_str, main_name) \
107 else if (STREQ (prog_name_str, prog_name)) \
108 prog_main = single_binary_main_##main_name;
109 # include "coreutils.h"
110 # undef SINGLE_BINARY_PROGRAM
111 #endif
113 if (! prog_main)
114 return;
116 #if HAVE_PRCTL && defined PR_SET_NAME
117 /* Not being able to set the program name is not a fatal error. */
118 prctl (PR_SET_NAME, prog_argv[0]);
119 #endif
120 #if HAVE_PRCTL && defined PR_SET_MM_ARG_START
121 /* Shift the beginning of the command line to prog_argv[0] (if set) so
122 /proc/pid/cmdline reflects the right value. */
123 prctl (PR_SET_MM_ARG_START, prog_argv[0]);
124 #endif
126 exit (prog_main (prog_argc, prog_argv));
130 main (int argc, char **argv)
132 char *prog_name = last_component (argv[0]);
133 int optc;
135 /* Map external name to internal name. */
136 char ginstall[] = "ginstall";
137 if (STREQ (prog_name, "install"))
138 prog_name = ginstall;
140 /* If this program is called directly as "coreutils" or if the value of
141 argv[0] is an unknown tool (which "coreutils" is), we proceed and parse
142 the options. */
143 launch_program (prog_name, argc, argv);
145 /* No known program was selected via argv[0]. Try parsing the first
146 argument as --coreutils-prog=PROGRAM to determine the program. The
147 invocation for this case should be:
148 path/to/coreutils --coreutils-prog=someprog someprog ...
149 The third argument is what the program will see as argv[0]. */
151 if (argc >= 2)
153 size_t nskip = 0;
154 char *arg_name = NULL;
156 /* If calling coreutils directly, the "script" name isn't passed.
157 Distinguish the two cases with a -shebang suffix. */
158 if (STRPREFIX (argv[1], "--coreutils-prog="))
160 nskip = 1;
161 arg_name = prog_name = argv[1] + strlen ("--coreutils-prog=");
163 else if (STRPREFIX (argv[1], "--coreutils-prog-shebang="))
165 nskip = 2;
166 prog_name = argv[1] + strlen ("--coreutils-prog-shebang=");
167 if (argc >= 3)
168 arg_name = last_component (argv[2]);
169 else
170 arg_name = prog_name;
173 if (nskip)
175 argv[nskip] = arg_name; /* XXX: Discards any specified path. */
176 launch_program (prog_name, argc - nskip, argv + nskip);
177 error (EXIT_FAILURE, 0, _("unknown program %s"),
178 quote (prog_name));
182 /* No known program was selected. From here on, we behave like any other
183 coreutils program. */
184 initialize_main (&argc, &argv);
185 set_program_name (argv[0]);
186 setlocale (LC_ALL, "");
187 bindtextdomain (PACKAGE, LOCALEDIR);
188 textdomain (PACKAGE);
189 atexit (close_stdout);
191 if ((optc = getopt_long (argc, argv, "", long_options, NULL)) != -1)
192 switch (optc)
194 case_GETOPT_HELP_CHAR;
196 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
199 /* Only print the error message when no options have been passed
200 to coreutils. */
201 if (optind == 1 && prog_name && !STREQ (prog_name, "coreutils"))
202 error (0, 0, _("unknown program %s"),
203 quote (prog_name));
205 usage (EXIT_FAILURE);