Tito wrote:
> On Friday 22 February 2008 19:13:51 walter harms wrote:
>> hi list,
>> this simplifies the coding of the watch utility.
>> I does not save much bytes but it thing it is easier to read
>> now. I have changed the date time display as the old one eat
>> to much space.
>>
>> Note:
>> perhaps it even make sense to make the time display dynamic
>> if someone is bored ,,,
>>
>>
>> re,
>>  wh
>>
>>
>>
> 
> Hi,
> 
>> int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
>> int watch_main(int argc, char **argv)
>> {
>>         unsigned opt;
>>         unsigned period = 2;
>>         char *header = NULL;
>>         char *cmd;
>>
>>         opt_complementary = "-1"; // at least one param please
>>         opt = getopt32(argv, "+dtn:", &cmd);
>>
>>       if (opt & 0x4) period = xatou(cmd);
> 
>       getopt32 can do this for us:
> "a+:"  A plus after a char in opt_complementary means that the parameter
>         for this option is a nonnegative integer. It will be processed
>         with xatoi_u() - allowed range is 0..INT_MAX.
> 

i tried and failed:
        opt_complementary = "-1:n+";
        opt = getopt32(argv, "+dtn:", &period);
        
the '+' option is not mentioned in the  libbb/getopt32.c of 1.9.1
did i miss something ?


>         int param;  // "unsigned param;" will also work
>         opt_complementary = "p+";
>         getopt32(argv, "p:", &param);  
> 
>>         argv += optind;
>>         cmd=NULL;
>    
> Maybe char *xasprintf(const char *format, ...) because it checks 
> for malloc errors and is portable on systems that don't have asprintf...
>      
>>         asprintf(&cmd,"%s",*argv );
>>         while (*++argv)
>>             asprintf(&cmd,"%s %s",cmd,*argv );
> 
>>         while (1) {
> 

done, actualy this was already an older version of my watch.c seems i mixed 
something up :)

re,
 wh

/* vi: set sw=4 ts=4: */
/*
 * Mini watch implementation for busybox
 *
 * Copyright (C) 2001 by Michael Habermann <[EMAIL PROTECTED]>
 * Copyrigjt (C) Mar 16, 2003 Manuel Novoa III   ([EMAIL PROTECTED])
 *
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */

/* BB_AUDIT SUSv3 N/A */
/* BB_AUDIT GNU defects -- only option -n is supported. */

#include "libbb.h"

// procps 2.0.18:
// watch [-d] [-n seconds]
//   [--differences[=cumulative]] [--interval=seconds] command
//
// procps-3.2.3:
// watch [-dt] [-n seconds]
//   [--differences[=cumulative]] [--interval=seconds] [--no-title] command
//
// (procps 3.x and procps 2.x are forks, not newer/older versions of the same)

int watch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int watch_main(int argc, char **argv)
{
	unsigned opt;
	unsigned period = 2;
	char *header = NULL;
	char *cmd;

	opt_complementary = "-1"; // at least one param please
	opt = getopt32(argv, "+dtn:",&cmd);

	if (opt & 0x4) period = xatou(cmd);
	argv += optind;
	cmd=NULL;
	
	cmd=xasprintf("%s",*argv );
        while (*++argv)
            cmd=xasprintf("%s %s",cmd,*argv );

	while (1) {
		printf("\033[H\033[J");
		if (!(opt & 0x2)) { // no -t
			int width;
			int len=20; /* size of "%Y-%m-%d %H:%M:%S" */
			time_t t;

			get_terminal_width_height(STDIN_FILENO, &width, 0);
			header=xasprintf("Every %ds: %-*s", period, width, cmd);
			time(&t);
			if (len < width)
			  strftime(header + width - len, len, "%Y-%m-%d %H:%M:%S", localtime(&t) );

			puts(header);
		}
		fflush(stdout);
		// TODO: 'real' watch pipes cmd's output to itself
		// and does not allow it to overflow the screen
		// (taking into account linewrap!)
		system(cmd);
		sleep(period);
	}
	return 0; // gcc thinks we can reach this :)
}
_______________________________________________
busybox mailing list
[email protected]
http://busybox.net/cgi-bin/mailman/listinfo/busybox

Reply via email to