monit-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

alert.c


From: Rory Toma
Subject: alert.c
Date: 18 Oct 2002 12:20:53 -0700

In this snippet of code from alert.c:

static void replace(char **src, char *old, char *new) {

  int i= count(*src, old);
  int d= strlen(new)-strlen(old);

  if(i==0)
      return;
  if(d>0)
      d*= i;
  else
      d= 0;
  
  {
    char *p, *q;
    int l= strlen(old);
    char buf[strlen(*src)+d+1];

    q= *src;
    *buf= '\0';
    
    while((p= strstr(q, old))) {
      
      *p= '\0';
      strcat(buf, q);
      strcat(buf, new);
      p+= l;
      q= p;
      
    }
    
    strcat(buf, q);
    free(*src);
    *src= xstrdup(buf);

  }
  
}

I have a few questions. Why are we using a fixed sized buffer for buf?
Wouldn't it be easier and better to malloc it?

Shouldn't we be using strncat?

Why do we free(*src) and then use it right after?

-- 
Rory Toma               address@hidden
VP of Run Level 5       http://www.trs80.net
Digeo Digital           http://www.digeo.com

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]