exit(1);
}
-void exec_child(char **command, char *argument, int replace_cb) {
+void exec_child(char **command, char **arguments, int replace_cb, int nargs) {
char **argv;
int argc = 0;
int i;
}
if (replace_cb == 0)
argc++;
- argv = calloc(sizeof(char*), argc+1);
+ argv = calloc(sizeof(char*), argc + nargs);
for (i = 0; i < argc; i++) {
argv[i] = command[i];
if (replace_cb && (strcmp(argv[i], "{}") == 0))
- argv[i] = argument;
+ argv[i] = arguments[0];
}
if (replace_cb == 0)
- argv[i-1] = argument;
+ memcpy(argv + i - 1, arguments, nargs * sizeof(char *));
if (fork() == 0) {
/* Child */
execvp(argv[0], argv);
int maxjobs = -1;
int curjobs = 0;
int maxload = -1;
+ int argsatonce = 1;
int opt;
char **command = calloc(sizeof(char*), argc);
char **arguments = NULL;
int argidx = 0;
+ int arglen = 0;
int cidx = 0;
int returncode = 0;
int replace_cb = 0;
char *t;
- while ((opt = getopt(argc, argv, "+hij:l:")) != -1) {
+ while ((opt = getopt(argc, argv, "+hij:l:n:")) != -1) {
switch (opt) {
case 'h':
usage();
exit(2);
}
break;
+ case 'n':
+ errno = 0;
+ argsatonce = strtoul(optarg, &t, 0);
+ if (errno != 0 || argsatonce < 1 || (t-optarg) != strlen(optarg)) {
+ fprintf(stderr, "option '%s' is not a positive number\n",
+ optarg);
+ exit(2);
+ }
+ break;
default: /* ’?’ */
usage();
break;
}
}
+ if (replace_cb && argsatonce > 1) {
+ fprintf(stderr, "options -i and -n are incomaptible\n");
+ exit(2);
+ }
+
if (maxjobs < 0) {
#ifdef _SC_NPROCESSORS_ONLN
maxjobs = sysconf(_SC_NPROCESSORS_ONLN);
while (optind < argc) {
if (strcmp(argv[optind], "--") == 0) {
int i;
- size_t mem = (sizeof (char*)) * (argc - optind);
- arguments = malloc(mem);
+ optind++;
+ arglen = argc - optind;
+ arguments = calloc(sizeof(char *), arglen);
if (! arguments) {
exit(1);
}
- memset(arguments, 0, mem);
- optind++; /* Skip the -- separator, skip after
- * malloc so we have a trailing
- * null */
- for (i = 0; i < argc - optind; i++) {
+ for (i = 0; i < arglen; i++) {
arguments[i] = strdup(argv[optind + i]);
}
optind += i;
optind++;
}
- while (arguments[argidx] != 0) {
+ while (argidx < arglen) {
double load;
getloadavg(&load, 1);
if ((maxjobs > 0 && curjobs < maxjobs) ||
(maxload > 0 && load < maxload)) {
- exec_child(command, arguments[argidx], replace_cb);
- argidx++;
+ if (argsatonce > arglen - argidx)
+ argsatonce = arglen - argidx;
+ exec_child(command, arguments + argidx,
+ replace_cb, argsatonce);
+ argidx += argsatonce;
curjobs++;
}