about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--xe.13
-rw-r--r--xe.c25
3 files changed, 28 insertions, 1 deletions
diff --git a/README.md b/README.md
index 84ec599..0c7d1ee 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,7 @@ Over apply:
 * `-N`: pass upto *maxargs* arguments to each COMMAND (default: 1).
   `-N0` will pass as many arguments as possible.
 * `-j`: run up to *maxjobs* processes concurrently.
+  `-j0` will run as many processes as there are CPU cores running.
 * `COMMAND...`: default operation: each command line argument is
   passed as-is, `{}` is replaced by the argument (not with `-N` > 1).
 * `-s SHELLSCRIPT`: The argument `SHELLSCRIPT` is evaluated using `/bin/sh`
diff --git a/xe.1 b/xe.1
index 86c07c0..8c232d6 100644
--- a/xe.1
+++ b/xe.1
@@ -102,6 +102,9 @@ will pass as many arguments as possible.
 Run up to
 .Ar maxjobs
 processes concurrently.
+Using
+.Fl j0
+will run as many processes as there are CPU cores running.
 .El
 .Sh ENVIRONMENT
 The environment variable
diff --git a/xe.c b/xe.c
index 0fc55a8..b967820 100644
--- a/xe.c
+++ b/xe.c
@@ -226,6 +226,29 @@ toolong()
 }
 
 int
+parse_jobs(char *s)
+{
+	char *e;
+	int n;
+
+	errno = 0;
+	n = strtol(s, &e, 10);
+	if (errno != 0 || *e) {
+		fprintf(stderr, "xe: can't parse number '%s'.\n", s);
+		exit(1);
+	}
+
+#ifdef _SC_NPROCESSORS_ONLN
+	if (n <= 0)
+		n = (int)sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+	if (n <= 0)
+		n = 1;
+
+	return n;
+}
+
+int
 main(int argc, char *argv[], char *envp[])
 {
 	char c;
@@ -260,7 +283,7 @@ main(int argc, char *argv[], char *envp[])
 		case 'N': maxatonce = atoi(optarg); break;
 		case 'R': Rflag++; break;
 		case 'a': aflag++; break;
-		case 'j': maxjobs = atoi(optarg); break;
+		case 'j': maxjobs = parse_jobs(optarg); break;
 		case 'k': kflag++; break;
 		case 'n': nflag++; break;
 		case 's': sflag = optarg; break;