On Sunday 06 January 2008 07:28:47 Denys Vlasenko wrote:
> On Wednesday 02 January 2008 15:23, Natanael Copa wrote:
> > Denys, whats the status on the tac patch?
> 
> Version which does inversion in memory is appled to svn.
> It handles non terminated last line the same as coreutils,
> but mishandles NULs.
Hi,
Denis, did you forget svn add again?  :-)

Why it mishandles NULs?
I thought that i've got it right in version 2 of my patch (attached):

              /* Get line and add it to the start of the temp linked list. */
                while ((line = xmalloc_fgets(f))) {
                        if (temp_list && !last_char_is(temp_list->data, '\n'))
                                /* Last line ended with '\0' so concatenate 
them. */
                                 temp_list->data = xasprintf("%s%s", 
temp_list->data, line);
                        else
                                llist_add_to(&temp_list, line); 
                }

this was the test file i used (attached):
055 056 057 000 057 056 055 010

result is:

GNU tac
tac prova4
789987

busybox
 ./busybox tac prova4
789987

Ciao,
TIto
> --
> vda
> 

789987
/* vi: set sw=4 ts=4: */
/*
 * tac implementation for busybox
 *
 * Copyright (C) 2003  Yang Xiaopeng  <yxp at hanwang.com.cn>
 * Copyright (C) 2007  Natanael Copa  <[EMAIL PROTECTED]>
 * Copyright (C) 2007  Tito Ragusa    <[EMAIL PROTECTED]>
 *
 * Licensed under GPLv2, see file License in this tarball for details.
 *
 */

/* tac - concatenate and print files in reverse */

/* Based on Yang Xiaopeng's (yxp at hanwang.com.cn) patch
 * http://www.uclibc.org/lists/busybox/2003-July/008813.html
 */

#include "libbb.h"

int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int tac_main(int argc, char **argv)
{
	int retval = EXIT_SUCCESS;
	FILE *f;
	char *line;
	llist_t *list = NULL;
	llist_t *temp_list = NULL;
	
	argv++;
	if (!*argv)
		*--argv = (char *)"-";

	do {
		f = fopen_or_warn_stdin(*argv);
		if (f == NULL) {
			retval = EXIT_FAILURE;
			continue;
		}

		/* reset errno */
		errno = 0;
		/* Get line and add it to the start of the temp linked list. */
		while ((line = xmalloc_fgets(f))) {
			if (temp_list && !last_char_is(temp_list->data, '\n'))
				/* Last line ended with '\0' so concatenate them. */
				 temp_list->data = xasprintf("%s%s", temp_list->data, line);
			else
				llist_add_to(&temp_list, line);	
		}
		/* xmalloc_fgets uses getc and returns NULL on error or EOF.   */
		/* It 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)
			bb_simple_perror_msg(*argv);
		/* Remove first element from the temp list and  */
		/* add it to the end of the real linked list.   */
		while ((line = llist_pop(&temp_list)))
			llist_add_to_end(&list, line);
		temp_list = NULL;
	} while (*++argv);

	/* Remove first element from the real list, print it and free it */
	while ((line = llist_pop(&list))) {
		printf("%s", line);
		free(line);
	}
	
	return retval;	
}

_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to