189 lines
4.5 KiB
Diff
189 lines
4.5 KiB
Diff
|
diff -Paur --no-dereference -- xargs.upstream/Makefile xargs/Makefile
|
||
|
--- xargs.upstream/Makefile
|
||
|
+++ xargs/Makefile
|
||
|
@@ -0,0 +1,25 @@
|
||
|
+include ../../../build-aux/compiler.mak
|
||
|
+include ../../../build-aux/version.mak
|
||
|
+include ../../../build-aux/dirs.mak
|
||
|
+
|
||
|
+OPTLEVEL?=-g -O2
|
||
|
+CFLAGS?=$(OPTLEVEL)
|
||
|
+
|
||
|
+CFLAGS:=$(CXXFLAGS) -Wall -Wextra
|
||
|
+CPPFLAGS:=$(CPPFLAGS)
|
||
|
+
|
||
|
+BINARY:=xargs
|
||
|
+
|
||
|
+all: $(BINARY)
|
||
|
+
|
||
|
+.PHONY: all install clean
|
||
|
+
|
||
|
+%: %.c
|
||
|
+ $(CC) -std=gnu11 $(CFLAGS) $(CPPFLAGS) $< -o $@ $(LIBS)
|
||
|
+
|
||
|
+install: all
|
||
|
+ mkdir -p $(DESTDIR)$(BINDIR)
|
||
|
+ install $(BINARY) $(DESTDIR)$(BINDIR)
|
||
|
+
|
||
|
+clean:
|
||
|
+ rm -f $(BINARY)
|
||
|
diff -Paur --no-dereference -- xargs.upstream/xargs.c xargs/xargs.c
|
||
|
--- xargs.upstream/xargs.c
|
||
|
+++ xargs/xargs.c
|
||
|
@@ -38,12 +38,10 @@
|
||
|
#include <sys/wait.h>
|
||
|
|
||
|
#include <ctype.h>
|
||
|
-#include <err.h>
|
||
|
+#include <error.h>
|
||
|
#include <errno.h>
|
||
|
#include <fcntl.h>
|
||
|
-#include <langinfo.h>
|
||
|
#include <locale.h>
|
||
|
-#include <paths.h>
|
||
|
#include <regex.h>
|
||
|
#include <signal.h>
|
||
|
#include <stdio.h>
|
||
|
@@ -51,7 +49,62 @@
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
-#include "pathnames.h"
|
||
|
+#define err(eval, ...) error(eval, errno, __VA_ARGS__)
|
||
|
+#define errx(eval, ...) error(eval, 0, __VA_ARGS__)
|
||
|
+#define warn(...) error(0, errno, __VA_ARGS__)
|
||
|
+#define warnx(...) error(0, 0, __VA_ARGS__)
|
||
|
+
|
||
|
+/*
|
||
|
+ * Replaces str with a string consisting of str with match replaced with
|
||
|
+ * replstr as many times as can be done before the constructed string is
|
||
|
+ * maxsize bytes large. It does not free the string pointed to by str, it
|
||
|
+ * is up to the calling program to be sure that the original contents of
|
||
|
+ * str as well as the new contents are handled in an appropriate manner.
|
||
|
+ * If replstr is NULL, then that internally is changed to a nil-string, so
|
||
|
+ * that we can still pretend to do somewhat meaningful substitution.
|
||
|
+ * No value is returned.
|
||
|
+ */
|
||
|
+void
|
||
|
+strnsubst(char **str, const char *match, const char *replstr, size_t maxsize)
|
||
|
+{
|
||
|
+ char *s1, *s2, *this;
|
||
|
+ size_t matchlen, repllen, s2len;
|
||
|
+ int n;
|
||
|
+
|
||
|
+ (void) repllen;
|
||
|
+
|
||
|
+ if ((s1 = *str) == NULL)
|
||
|
+ return;
|
||
|
+ if ((s2 = malloc(maxsize)) == NULL)
|
||
|
+ err(1, NULL);
|
||
|
+
|
||
|
+ if (replstr == NULL)
|
||
|
+ replstr = "";
|
||
|
+
|
||
|
+ if (match == NULL || *match == '\0' || strlen(s1) >= maxsize) {
|
||
|
+ strlcpy(s2, s1, maxsize);
|
||
|
+ goto done;
|
||
|
+ }
|
||
|
+
|
||
|
+ *s2 = '\0';
|
||
|
+ s2len = 0;
|
||
|
+ matchlen = strlen(match);
|
||
|
+ repllen = strlen(replstr);
|
||
|
+ for (;;) {
|
||
|
+ if ((this = strstr(s1, match)) == NULL)
|
||
|
+ break;
|
||
|
+ n = snprintf(s2 + s2len, maxsize - s2len, "%.*s%s",
|
||
|
+ (int)(this - s1), s1, replstr);
|
||
|
+ if (n == -1 || n + s2len + strlen(this + matchlen) >= maxsize)
|
||
|
+ break; /* out of room */
|
||
|
+ s2len += n;
|
||
|
+ s1 = this + matchlen;
|
||
|
+ }
|
||
|
+ strlcpy(s2 + s2len, s1, maxsize - s2len);
|
||
|
+done:
|
||
|
+ *str = s2;
|
||
|
+ return;
|
||
|
+}
|
||
|
|
||
|
static void parse_input(int, char *[]);
|
||
|
static void prerun(int, char *[]);
|
||
|
@@ -100,8 +153,9 @@
|
||
|
* probably not worthwhile.
|
||
|
*/
|
||
|
nargs = 5000;
|
||
|
- if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
|
||
|
- errx(1, "sysconf(_SC_ARG_MAX) failed");
|
||
|
+ arg_max = 4096 * 16;
|
||
|
+ //if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
|
||
|
+ // errx(1, "sysconf(_SC_ARG_MAX) failed");
|
||
|
nline = arg_max - 4 * 1024;
|
||
|
while (*ep != NULL) {
|
||
|
/* 1 byte for each '\0' */
|
||
|
@@ -195,7 +249,7 @@
|
||
|
* arguments.
|
||
|
*/
|
||
|
if (*argv == NULL)
|
||
|
- cnt = strlen(*bxp++ = _PATH_ECHO);
|
||
|
+ cnt = strlen(*bxp++ = "/bin/echo");
|
||
|
else {
|
||
|
do {
|
||
|
if (Jflag && strcmp(*argv, replstr) == 0) {
|
||
|
@@ -515,17 +569,17 @@
|
||
|
(void)fflush(stderr);
|
||
|
}
|
||
|
exec:
|
||
|
- switch (pid = vfork()) {
|
||
|
+ switch (pid = fork()) {
|
||
|
case -1:
|
||
|
- err(1, "vfork");
|
||
|
+ err(1, "fork");
|
||
|
case 0:
|
||
|
if (oflag) {
|
||
|
- if ((fd = open(_PATH_TTY, O_RDONLY)) == -1) {
|
||
|
+ if ((fd = open("/dev/tty", O_RDONLY)) == -1) {
|
||
|
warn("can't open /dev/tty");
|
||
|
_exit(1);
|
||
|
}
|
||
|
} else {
|
||
|
- fd = open(_PATH_DEVNULL, O_RDONLY);
|
||
|
+ fd = open("/dev/null", O_RDONLY);
|
||
|
}
|
||
|
if (fd > STDIN_FILENO) {
|
||
|
if (dup2(fd, STDIN_FILENO) != 0) {
|
||
|
@@ -572,7 +626,7 @@
|
||
|
if (WTERMSIG(status) != SIGPIPE) {
|
||
|
if (WTERMSIG(status) < NSIG)
|
||
|
warnx("%s terminated by SIG%s", name,
|
||
|
- sys_signame[WTERMSIG(status)]);
|
||
|
+ strsignal(WTERMSIG(status)));
|
||
|
else
|
||
|
warnx("%s terminated by signal %d",
|
||
|
name, WTERMSIG(status));
|
||
|
@@ -590,13 +644,14 @@
|
||
|
static int
|
||
|
prompt(void)
|
||
|
{
|
||
|
+#if 0
|
||
|
regex_t cre;
|
||
|
size_t rsize;
|
||
|
int match;
|
||
|
char *response;
|
||
|
FILE *ttyfp;
|
||
|
|
||
|
- if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
|
||
|
+ if ((ttyfp = fopen("/dev/tty", "r")) == NULL)
|
||
|
return (2); /* Indicate that the TTY failed to open. */
|
||
|
(void)fprintf(stderr, "?...");
|
||
|
(void)fflush(stderr);
|
||
|
@@ -610,6 +665,9 @@
|
||
|
(void)fclose(ttyfp);
|
||
|
regfree(&cre);
|
||
|
return (match == 0);
|
||
|
+#else
|
||
|
+ return 1;
|
||
|
+#endif
|
||
|
}
|
||
|
|
||
|
static void
|