Changeset 1c6c4d3 for util.c


Ignore:
Timestamp:
Jun 30, 2002, 4:58:09 PM (19 years ago)
Author:
Erik Nygren <nygren@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
507d5aa
Parents:
262422c
Message:
	Added owl_sprintf which returns the formatted string, or NULL.
	        The caller must free this string.
		This will allocate enough memory and thus
		avoid potential some buffer overrun situations.
	Started fixing some potential buffer overrun situations.
	Simple implementation of 'zwrite -m' (doesn't yet log an outgoing
	        message as having been sent.)
	The "Not logged in or subscribing to messages" error
	        now includes the name of the recipient.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r1aee7d9 r1c6c4d3  
    2828    strcpy(buff, " (-/-) ");
    2929  } else {
    30     sprintf(buff, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
     30    snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
    3131            owl_view_get_size(v),
    3232            owl_messagelist_get_size(ml));
     
    6262    getyx(sepwin, y, x);
    6363    wmove(sepwin, y, x+2);
    64     sprintf(buff, " right: %i ", owl_global_get_rightshift(&g));
     64    snprintf(buff, 1024, " right: %i ", owl_global_get_rightshift(&g));
    6565    waddstr(sepwin, buff);
    6666  }
     
    316316}
    317317
     318/* allocates memory and returns the string or null.
     319 * caller must free the string.
     320 * from Linux sprintf man page.
     321 */
     322char *owl_sprintf(const char *fmt, ...) {
     323  int n, size = 100;
     324  char *p;
     325  va_list ap;
     326  if ((p = owl_malloc (size)) == NULL)
     327    return NULL;
     328  while (1) {
     329    /* Try to print in the allocated space. */
     330    va_start(ap, fmt);
     331    n = vsnprintf (p, size, fmt, ap);
     332    va_end(ap);
     333    /* If that worked, return the string. */
     334    if (n > -1 && n < size)
     335      return p;
     336    /* Else try again with more space. */
     337    if (n > -1)    /* glibc 2.1 */
     338      size = n+1; /* precisely what is needed */
     339    else           /* glibc 2.0 */
     340      size *= 2;  /* twice the old size */
     341    if ((p = owl_realloc (p, size)) == NULL)
     342      return NULL;
     343  }
     344}
     345
    318346char *pretty_sender(char *in) {
    319347  char *out, *ptr;
     
    331359
    332360char *long_sender(char *in) {
    333   char *out, *ptr;
     361  char *ptr;
    334362
    335363  /* the caller must free the return */
    336   out=owl_malloc(strlen(in)+100);
    337   strcpy(out, in);
    338   ptr=strchr(out, '@');
    339   if (ptr) return(out);
    340   sprintf(out, "%s@%s", out, ZGetRealm());
    341   return(out);
     364  if (NULL != (ptr=strchr(in, '@'))) {
     365    return owl_strdup(in);
     366  } else {
     367    return owl_sprintf("%s@%s", in, ZGetRealm());
     368  }
    342369}
    343370                 
Note: See TracChangeset for help on using the changeset viewer.