diff --git a/xargs/xargs.1 b/xargs/xargs.1 index ebcb1e0..b216c61 100644 --- a/xargs/xargs.1 +++ b/xargs/xargs.1 @@ -65,7 +65,10 @@ than there were items in the input. This will normally have significant performance benefits. Some commands can usefully be executed in parallel too; see the .B \-P -option. +option. To help commands form easily sequenced output file names, GNU +.B xargs +exports to commands an environment variable XARGS_SEQ containing the +invocation number (padded with leading zeros). .P Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks diff --git a/xargs/xargs.c b/xargs/xargs.c index 5fd93cd..b634461 100644 --- a/xargs/xargs.c +++ b/xargs/xargs.c @@ -369,7 +369,7 @@ main (int argc, char **argv) int (*read_args) (void) = read_line; void (*act_on_init_result)(void) = noop; enum BC_INIT_STATUS bcstatus; - enum { XARGS_POSIX_HEADROOM = 2048u }; + enum { XARGS_POSIX_HEADROOM = 2076u }; /* 2048 +28 for $XARGS_SEQ usage */ struct sigaction sigact; if (argv[0]) @@ -1155,6 +1155,7 @@ prep_child_for_exec (void) static int xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv) { + static unsigned int xargs_seq_num; pid_t child; int fd[2]; int buf; @@ -1164,6 +1165,7 @@ xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char * (void) argc; (void) usercontext; + xargs_seq_num++; /* count command invocations for user */ if (!query_before_executing || print_args (true)) { if (proc_max) @@ -1203,6 +1205,12 @@ xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char * case 0: /* Child. */ { + char env_var[24]; /* For <=9 digit numbers max is 10+10 bytes */ + sprintf(env_var, "XARGS_SEQ=%09u", xargs_seq_num); + putenv(env_var); /* export XARGS_SEQ; ENOMEM failure is + * conceivable, but neither failure nor falling + * back makes much sense in that situation. */ + close (fd[0]); child_error = EXIT_SUCCESS;