cvs commit: ALFS/nALFS/src comm.c

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Tue Feb 10 21:59:55 PST 2004


kpfleming    04/02/10 22:59:55

  Modified:    nALFS/src comm.c
  Log:
  clean up and optimize message sending code
  
  Revision  Changes    Path
  1.10      +24 -29    ALFS/nALFS/src/comm.c
  
  Index: comm.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/comm.c,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- comm.c	11 Feb 2004 03:43:59 -0000	1.9
  +++ comm.c	11 Feb 2004 05:59:55 -0000	1.10
  @@ -380,47 +380,42 @@
   int comm_send_ctrl_msg(
   	socket_e s, ctrl_msg_type_e t, const char *format, ...)
   {
  -	size_t i;
   	ssize_t ret;
   	va_list ap;
   	char *full_message;
  -	char *size, *type, raw_msg[MAX_CTRL_MSG_LEN + 1];
  -
  +	int result;
  +	char *buf;
  +	int buf_size = 1024;
   
   	/* Create raw message string. */
  -	va_start(ap, format);
  -	vsnprintf(raw_msg, sizeof raw_msg, format, ap); /* TODO: Check. */
  -	va_end(ap);
  -
  -	/* Create type string. */
  -	type = xmalloc(number_len(t) + 2);
  -	sprintf(type, "%d|", (int)t);
  -
  -	i = strlen(type) + strlen(raw_msg);
  -
  -	/* Create size string. */
  -	size = xmalloc(number_len(i) + 2);
  -	sprintf(size, "%lu|", (unsigned long)i);
  -
  -
  -	full_message = xmalloc(strlen(size) + i + 1);
  -
  -	strcpy(full_message, size);
  -	strcat(full_message, type);
  -	strcat(full_message, raw_msg);
  -
  -	xfree(size);
  -	xfree(type);
  +	if ((buf = xmalloc(buf_size)) == NULL) {
  +		Nprint_err("xmalloc() failed: %s", strerror(errno));
  +		return -1;
  +	}
  +	while (1) {
  +		va_start(ap, format);
  +		result = vsnprintf(buf, buf_size, format, ap);
  +		va_end(ap);
  +		if ((result < buf_size) && (result > -1))
  +			break;
  +		if (result > -1)
  +			buf_size += 1;
  +		else
  +			buf_size *= 2;
  +		if ((buf = xrealloc(buf, buf_size)) == NULL) {
  +			Nprint_err("xrealloc() failed: %s", strerror(errno));
  +			return -1;
  +		}
  +	}
   
  +	full_message = xmalloc(result + 15);
  +	sprintf(full_message, "%08d|%04d|%s", result + 6, t, buf);
   	ret = write(comm_get_socket(s), full_message, strlen(full_message));
  -
   	xfree(full_message);
  -
   	if (ret == -1) {
   		Nprint_err("write() failed: %s", strerror(errno));
   		return -1;
   	}
  -
   	return 0;
   }
   
  
  
  



More information about the alfs-log mailing list