tac: handle NULs properly. +145 bytes
This commit is contained in:
@ -20,12 +20,17 @@
|
|||||||
|
|
||||||
/* This is a NOEXEC applet. Be very careful! */
|
/* This is a NOEXEC applet. Be very careful! */
|
||||||
|
|
||||||
|
struct lstring {
|
||||||
|
int size;
|
||||||
|
char buf[];
|
||||||
|
};
|
||||||
|
|
||||||
int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
||||||
int tac_main(int argc, char **argv)
|
int tac_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char **name;
|
char **name;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char *line;
|
struct lstring *line = NULL;
|
||||||
llist_t *list = NULL;
|
llist_t *list = NULL;
|
||||||
int retval = EXIT_SUCCESS;
|
int retval = EXIT_SUCCESS;
|
||||||
|
|
||||||
@ -38,6 +43,8 @@ int tac_main(int argc, char **argv)
|
|||||||
name++;
|
name++;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
int ch, i;
|
||||||
|
|
||||||
name--;
|
name--;
|
||||||
f = fopen_or_warn_stdin(*name);
|
f = fopen_or_warn_stdin(*name);
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
@ -45,14 +52,26 @@ int tac_main(int argc, char **argv)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = 0;
|
errno = i = 0;
|
||||||
/* FIXME: NUL bytes are mishandled. */
|
do {
|
||||||
while ((line = xmalloc_fgets(f)) != NULL)
|
ch = fgetc(f);
|
||||||
|
if (ch != EOF) {
|
||||||
|
if (!(i & 0x7f))
|
||||||
|
/* Grow on every 128th char */
|
||||||
|
line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
|
||||||
|
line->buf[i++] = ch;
|
||||||
|
}
|
||||||
|
if ((ch == '\n' || ch == EOF) && i) {
|
||||||
|
line = xrealloc(line, i + sizeof(int));
|
||||||
|
line->size = i;
|
||||||
llist_add_to(&list, line);
|
llist_add_to(&list, line);
|
||||||
|
line = NULL;
|
||||||
/* xmalloc_fgets uses getc and returns NULL on error or EOF. */
|
i = 0;
|
||||||
/* It sets errno to ENOENT on EOF, but fopen_or_warn_stdin would */
|
}
|
||||||
/* catch this error so we can filter it out here. */
|
} while (ch != EOF);
|
||||||
|
/* fgetc sets errno to ENOENT on EOF, but */
|
||||||
|
/* fopen_or_warn_stdin would catch this error */
|
||||||
|
/* so we can filter it out here. */
|
||||||
if (errno && errno != ENOENT) {
|
if (errno && errno != ENOENT) {
|
||||||
bb_simple_perror_msg(*name);
|
bb_simple_perror_msg(*name);
|
||||||
retval = EXIT_FAILURE;
|
retval = EXIT_FAILURE;
|
||||||
@ -60,9 +79,14 @@ int tac_main(int argc, char **argv)
|
|||||||
} while (name != argv);
|
} while (name != argv);
|
||||||
|
|
||||||
while (list) {
|
while (list) {
|
||||||
printf("%s", list->data);
|
line = (struct lstring *)list->data;
|
||||||
|
xwrite(STDOUT_FILENO, line->buf, line->size);
|
||||||
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
||||||
|
free(llist_pop(&list));
|
||||||
|
} else {
|
||||||
list = list->link;
|
list = list->link;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user