cvs commit: ALFS/nALFS/src/handlers execute.c package.c stage.c textdump.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Wed Mar 17 17:03:03 PST 2004


kpfleming    04/03/17 18:03:03

  Modified:    nALFS/src utility.c utility.h
               nALFS/src/handlers execute.c package.c stage.c textdump.c
  Log:
  add append_str_format function that takes a format string and parameters
  use append_str_format for handlers to create their display names
  
  Revision  Changes    Path
  1.9       +37 -9     ALFS/nALFS/src/utility.c
  
  Index: utility.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/utility.c,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- utility.c	2 Mar 2004 04:08:29 -0000	1.8
  +++ utility.c	18 Mar 2004 01:03:03 -0000	1.9
  @@ -87,7 +87,7 @@
   #endif
   
   	if ((value = realloc(ptr, size)) == NULL) {
  -		FATAL_ERROR("relloc() failed");
  +		FATAL_ERROR("realloc() failed (ptr: %p, size %zd)", ptr, size);
   	}
   
   	return value;
  @@ -209,22 +209,50 @@
   	return 0;
   }
   
  -void append_str(char **ptr, const char *str)
  +void append_str_format(char **ptr, const char *format, ...)
   {
  +	va_list ap;
  +	int old_length = 0;
  +	int new_length;
   	char *value;
   
  +	if (Empty_string(format)) {
  +		return;
  +	}
  +
  +	if (*ptr)
  +		old_length = strlen(*ptr);
  +
  +	va_start(ap, format);
  +	new_length = vsnprintf(NULL, 0, format, ap);
  +	va_end(ap);
  +
  +      	value = xrealloc(*ptr, new_length + old_length + 1);
  +	va_start(ap, format);
  +	vsnprintf(value + old_length, new_length + 1, format, ap);
  +	va_end(ap);
  +
  +	*ptr = value;
  +}
  +
  +
  +void append_str(char **ptr, const char *str)
  +{
  +	char *value;
  +	int old_length = 0;
  +	int new_length;
   
   	if (Empty_string(str)) {
   		return;
   	}
   
  -	if (*ptr) {
  -		value = xrealloc(*ptr, strlen(*ptr) + strlen(str) + 1);
  -		strcat(value, str);
  -	} else {
  -		value = xmalloc(strlen(str) + 1);
  -		strcpy(value, str);
  -	}
  +	if (*ptr)
  +		old_length = strlen(*ptr);
  +
  +	new_length = strlen(str);
  +
  +      	value = xrealloc(*ptr, new_length + old_length + 1);
  +	memcpy(value + old_length, str, new_length + 1);
   
   	*ptr = value;
   }
  
  
  
  1.5       +1 -0      ALFS/nALFS/src/utility.h
  
  Index: utility.h
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/utility.h,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- utility.h	29 Oct 2003 00:09:12 -0000	1.4
  +++ utility.h	18 Mar 2004 01:03:03 -0000	1.5
  @@ -77,6 +77,7 @@
   char *xstrcasestr(const char *haystack, const char *needle);
   char *alloc_trimmed_str(const char *s);
   int remove_new_line(char *buf);
  +void append_str_format(char **ptr, const char *format, ...);
   void append_str(char **ptr, const char *str);
   
   /*
  
  
  
  1.29      +3 -7      ALFS/nALFS/src/handlers/execute.c
  
  Index: execute.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/execute.c,v
  retrieving revision 1.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- execute.c	16 Mar 2004 04:21:59 -0000	1.28
  +++ execute.c	18 Mar 2004 01:03:03 -0000	1.29
  @@ -239,14 +239,10 @@
   	switch (data_requested) {
   	case HDATA_DISPLAY_NAME:
   	{
  -		char *display;
  +		char *display = NULL;
   
  -		display = xstrdup("Execute ");
  -		if (data->command) {
  -			append_str(&display, data->command);
  -		} else if (data->content) {
  -			append_str(&display, "script");
  -		}
  +		append_str_format(&display, "Execute %s",
  +				  data->command ? data->command : "script");
   
   		return display;
   	}
  
  
  
  1.24      +4 -12     ALFS/nALFS/src/handlers/package.c
  
  Index: package.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/package.c,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- package.c	16 Mar 2004 04:21:59 -0000	1.23
  +++ package.c	18 Mar 2004 01:03:03 -0000	1.24
  @@ -193,19 +193,11 @@
   		break;
   	case HDATA_DISPLAY_NAME:
   	{
  -		char *display;
  +		char *display = NULL;
   
  -		display = xstrdup("Package");
  -
  -		if (data->name) {
  -			append_str(&display, " ");
  -			append_str(&display, data->name);
  -		}
  -
  -		if (data->version) {
  -			append_str(&display, " ");
  -			append_str(&display, data->version);
  -		}
  +		append_str_format(&display, "Package %s %s",
  +				  data->name ? data->name : "",
  +				  data->version ? data->version : "");
   
   		return display;
   	}
  
  
  
  1.34      +3 -5      ALFS/nALFS/src/handlers/stage.c
  
  Index: stage.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/stage.c,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- stage.c	16 Mar 2004 04:21:59 -0000	1.33
  +++ stage.c	18 Mar 2004 01:03:03 -0000	1.34
  @@ -397,11 +397,9 @@
   	{
   		char *display = NULL;
   
  -		if (*opt_display_stage_header)
  -			append_str(&display, "Enter stage: ");
  -
  -		if (data->name)
  -			append_str(&display, data->name);
  +		append_str_format(&display, "%s%s",
  +				  *opt_display_stage_header ? "Enter stage: " : "",
  +				  data->name);
   
   		return display;
   	}
  
  
  
  1.27      +2 -4      ALFS/nALFS/src/handlers/textdump.c
  
  Index: textdump.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/handlers/textdump.c,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- textdump.c	16 Mar 2004 04:21:59 -0000	1.26
  +++ textdump.c	18 Mar 2004 01:03:03 -0000	1.27
  @@ -231,11 +231,9 @@
   	switch (data_requested) {
   	case HDATA_DISPLAY_NAME:
   	{
  -		char *display;
  +		char *display = NULL;
   
  -		display = xstrdup("Dump text (");
  -		append_str(&display, data->file);
  -		append_str(&display, ")");
  +		append_str_format(&display, "Dump text (%s)", data->file);
   
   		return display;
   	}
  
  
  



More information about the alfs-log mailing list