@@ -94,6 +94,7 @@ static bool do_interactive_shell = false;
94
94
static bool fakelogin = false;
95
95
static /*@observer@*/ const char * shellstr ;
96
96
static /*@null@*/ char * command = NULL ;
97
+ static /*@null@*/ char * exec_command = NULL ;
97
98
static int optidx ;
98
99
99
100
@@ -440,12 +441,14 @@ static void usage (int status)
440
441
"\n"
441
442
"Options:\n"
442
443
" -c, --command COMMAND pass COMMAND to the invoked shell\n"
444
+ " -e, --exec PATH run PATH without shell, follow -- with args\n"
443
445
" -h, --help display this help message and exit\n"
444
446
" -, -l, --login make the shell a login shell\n"
445
447
" -m, -p,\n"
446
448
" --preserve-environment do not reset environment variables, and\n"
447
449
" keep the same shell\n"
448
450
" -s, --shell SHELL use SHELL instead of the default in passwd\n"
451
+ " -- pass all subsequent arguments on as-is\n"
449
452
"\n"
450
453
"If no username is given, assume root.\n" ), (E_SUCCESS != status ) ? stderr : stdout );
451
454
exit (status );
@@ -820,6 +823,12 @@ static void process_flags (int argc, char **argv)
820
823
}
821
824
822
825
command = argv [++ optidx ];
826
+ } else if (flags_match (arg , "--exec" , "-e" , NULL )) {
827
+ if (optidx == argc - 1 ) {
828
+ flag_arg_required (arg );
829
+ }
830
+
831
+ exec_command = argv [++ optidx ];
823
832
} else if (flags_match (arg , "--help" , "-h" , NULL )) {
824
833
usage (E_SUCCESS );
825
834
} else if (flags_match (arg , "--login" , "-l" , "-" )) {
@@ -843,6 +852,17 @@ static void process_flags (int argc, char **argv)
843
852
}
844
853
}
845
854
855
+ if (NULL != exec_command && NULL != command ) {
856
+ fprintf (stderr ,
857
+ _ ("%s: COMMAND and PATH are mutually exclusive\n" ),
858
+ argv [0 ]);
859
+ usage (E_USAGE );
860
+ }
861
+
862
+ if (NULL != exec_command ) {
863
+ command = exec_command ;
864
+ }
865
+
846
866
/* if next arg is not "--", treat as USER */
847
867
if (optidx < argc && strcmp (argv [optidx ], "--" )) {
848
868
STRFCPY (name , argv [optidx ++ ]); /* use this login id */
@@ -1226,10 +1246,18 @@ int main (int argc, char **argv)
1226
1246
* with the rest of the command line included.
1227
1247
*/
1228
1248
argv [-1 ] = cp ;
1229
- execve_shell (shellstr , & argv [-1 ], environ );
1230
- err = errno ;
1231
- (void ) fprintf (stderr ,
1232
- _ ("Cannot execute %s\n" ), shellstr );
1249
+
1250
+ if (NULL != exec_command ) {
1251
+ (void ) execve (command , & argv [1 ], environ );
1252
+ err = errno ;
1253
+ (void ) fprintf (stderr ,
1254
+ _ ("Cannot execute \'%s\'\n" ), command );
1255
+ } else {
1256
+ execve_shell (shellstr , & argv [-1 ], environ );
1257
+ err = errno ;
1258
+ (void ) fprintf (stderr ,
1259
+ _ ("Cannot execute \'%s\'\n" ), shellstr );
1260
+ }
1233
1261
errno = err ;
1234
1262
} else {
1235
1263
(void ) shell (shellstr , cp , environ );
0 commit comments