#ifdef TEST
-# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
-# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
+# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
# endif
-# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
-# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
# endif
-# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
-# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
+# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
# endif
-# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
-# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
+# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
# endif
#endif
}
-typedef struct xlist_s {
+typedef struct xlist_t {
char *data;
- size_t lenght;
- struct xlist_s *link;
+ size_t length;
+ struct xlist_t *link;
} xlist_t;
-static int eof_stdin_detected;
+static smallint eof_stdin_detected;
#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
|| (c) == '\f' || (c) == '\v')
-#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
-static xlist_t *process_stdin(xlist_t * list_arg,
+#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+static xlist_t *process_stdin(xlist_t *list_arg,
const char *eof_str, size_t mc, char *buf)
{
#define NORM 0
xlist_t *cur;
xlist_t *prev;
- for (prev = cur = list_arg; cur; cur = cur->link) {
- line_l += cur->lenght; /* previous allocated */
- if (prev != cur)
- prev = prev->link;
+ cur = list_arg;
+ while (1) {
+ prev = cur;
+ if (!cur) break;
+ line_l += cur->length;
+ cur = cur->link;
}
while (!eof_stdin_detected) {
c = getchar();
if (c == EOF) {
- eof_stdin_detected++;
+ eof_stdin_detected = 1;
if (s)
goto unexpected_eof;
break;
}
/* word loaded */
if (eof_str) {
- eof_str_detected = strcmp(s, eof_str) == 0;
+ eof_str_detected = (strcmp(s, eof_str) == 0);
}
if (!eof_str_detected) {
- size_t lenght = (p - buf);
+ size_t length = (p - buf);
- cur = xmalloc(sizeof(xlist_t) + lenght);
- cur->data = memcpy(cur + 1, s, lenght);
- cur->lenght = lenght;
- cur->link = NULL;
+ cur = xzalloc(sizeof(xlist_t) + length);
+ cur->data = memcpy(cur + 1, s, length);
+ cur->length = length;
+ /*cur->link = NULL;*/
if (prev == NULL) {
list_arg = cur;
} else {
prev->link = cur;
}
prev = cur;
- line_l += lenght;
+ line_l += length;
if (line_l > mc) {
/* stop memory usage :-) */
break;
}
#else
/* The variant does not support single quotes, double quotes or backslash */
-static xlist_t *process_stdin(xlist_t * list_arg,
- const char *eof_str, size_t mc, char *buf)
+static xlist_t *process_stdin(xlist_t *list_arg,
+ const char *eof_str, size_t mc, char *buf)
{
int c; /* current char */
- int eof_str_detected = 0;
+ char eof_str_detected = 0;
char *s = NULL; /* start word */
char *p = NULL; /* pointer to end word */
size_t line_l = 0; /* size loaded args line */
xlist_t *cur;
xlist_t *prev;
- for (prev = cur = list_arg; cur; cur = cur->link) {
- line_l += cur->lenght; /* previous allocated */
- if (prev != cur)
- prev = prev->link;
+ cur = list_arg;
+ while (1) {
+ prev = cur;
+ if (!cur) break;
+ line_l += cur->length;
+ cur = cur->link;
}
while (!eof_stdin_detected) {
c = getchar();
if (c == EOF) {
- eof_stdin_detected++;
+ eof_stdin_detected = 1;
}
if (eof_str_detected)
continue;
if (c == EOF) { /* word's delimiter or EOF detected */
/* word loaded */
if (eof_str) {
- eof_str_detected = strcmp(s, eof_str) == 0;
+ eof_str_detected = (strcmp(s, eof_str) == 0);
}
if (!eof_str_detected) {
- size_t lenght = (p - buf);
+ size_t length = (p - buf);
- cur = xmalloc(sizeof(xlist_t) + lenght);
- cur->data = memcpy(cur + 1, s, lenght);
- cur->lenght = lenght;
- cur->link = NULL;
+ cur = xzalloc(sizeof(xlist_t) + length);
+ cur->data = memcpy(cur + 1, s, length);
+ cur->length = length;
+ /*cur->link = NULL;*/
if (prev == NULL) {
list_arg = cur;
} else {
prev->link = cur;
}
prev = cur;
- line_l += lenght;
+ line_l += length;
if (line_l > mc) {
/* stop memory usage :-) */
break;
}
return list_arg;
}
-#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
+#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
-#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
+#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
/* Prompt the user for a response, and
if the user responds affirmatively, return true;
- otherwise, return false. Used "/dev/tty", not stdin. */
+ otherwise, return false. Uses "/dev/tty", not stdin. */
static int xargs_ask_confirmation(void)
{
- static FILE *tty_stream;
+ FILE *tty_stream;
int c, savec;
- if (!tty_stream) {
- tty_stream = xfopen(CURRENT_TTY, "r");
- /* pranoidal security by vodz */
- fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
- }
+ tty_stream = xfopen(CURRENT_TTY, "r");
fputs(" ?...", stderr);
fflush(stderr);
c = savec = getc(tty_stream);
while (c != EOF && c != '\n')
c = getc(tty_stream);
- if (savec == 'y' || savec == 'Y')
- return 1;
- return 0;
+ fclose(tty_stream);
+ return (savec == 'y' || savec == 'Y');
}
#else
# define xargs_ask_confirmation() 1
-#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
+#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
-#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
-static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED,
- size_t mc, char *buf)
+#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
+static xlist_t *process0_stdin(xlist_t *list_arg,
+ const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
{
int c; /* current char */
char *s = NULL; /* start word */
xlist_t *cur;
xlist_t *prev;
- for (prev = cur = list_arg; cur; cur = cur->link) {
- line_l += cur->lenght; /* previous allocated */
- if (prev != cur)
- prev = prev->link;
+ cur = list_arg;
+ while (1) {
+ prev = cur;
+ if (!cur) break;
+ line_l += cur->length;
+ cur = cur->link;
}
while (!eof_stdin_detected) {
c = getchar();
if (c == EOF) {
- eof_stdin_detected++;
+ eof_stdin_detected = 1;
if (s == NULL)
break;
c = 0;
*p++ = c;
if (c == 0) { /* word's delimiter or EOF detected */
/* word loaded */
- size_t lenght = (p - buf);
+ size_t length = (p - buf);
- cur = xmalloc(sizeof(xlist_t) + lenght);
- cur->data = memcpy(cur + 1, s, lenght);
- cur->lenght = lenght;
- cur->link = NULL;
+ cur = xzalloc(sizeof(xlist_t) + length);
+ cur->data = memcpy(cur + 1, s, length);
+ cur->length = length;
+ /*cur->link = NULL;*/
if (prev == NULL) {
list_arg = cur;
} else {
prev->link = cur;
}
prev = cur;
- line_l += lenght;
+ line_l += length;
if (line_l > mc) {
/* stop memory usage :-) */
break;
}
return list_arg;
}
-#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
+#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
/* Correct regardless of combination of CONFIG_xxx */
enum {
if (opt & OPT_ZEROTERM)
USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
- argc -= optind;
argv += optind;
+ argc -= optind;
if (!argc) {
/* default behavior is to echo all the filenames */
*argv = (char*)"echo";
opt |= OPT_NO_EMPTY;
n = 0;
n_chars = 0;
-#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
+#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
for (cur = list; cur;) {
- n_chars += cur->lenght;
+ n_chars += cur->length;
n++;
cur = cur->link;
if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
}
#else
for (cur = list; cur; cur = cur->link) {
- n_chars += cur->lenght;
+ n_chars += cur->length;
n++;
if (n_chars > n_max_chars || n == n_max_arg) {
break;
}
}
-#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
+#endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
/* allocate pointers for execvp:
argc*arg, n*arg from stdin, NULL */
break;
}
}
- if (ENABLE_FEATURE_CLEAN_UP) free(max_chars);
+ if (ENABLE_FEATURE_CLEAN_UP)
+ free(max_chars);
return child_error;
}