File: b.c

package info (click to toggle)
sn 0.3.4a-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 784 kB
  • ctags: 826
  • sloc: ansic: 9,023; sh: 339; makefile: 208
file content (47 lines) | stat: -rw-r--r-- 990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
 * This file is part of the sn package.
 * Distribution of sn is covered by the GNU GPL. See file COPYING.
 * Copyright  1998-2000 Harold Tay.
 * Copyright  2000- Patrik Rdman.
 */

/*
 * Dynamic buffer routines.
 */

#include <stdlib.h>
#include <string.h>
#include "b.h"

static const char rcsid[] = "$Id$";

int b_appendl (register struct b *bp, char *str, int len)
{
   if (NULL == bp->buf)
      if (NULL == (bp->buf = malloc(bp->size = 248)))
         return (-1);
      else
         bp->used = 0;

   if (len + bp->used >= bp->size)
   {
      int newsize;
      char *tmp;

      if (len > bp->size)
         newsize = bp->size + len;
      else
         newsize = bp->size * 2;
      newsize += 16;
      tmp = malloc(newsize);
      if (NULL == tmp)
         return (-1);
      memcpy(tmp, bp->buf, bp->used + 1);
      bp->buf = tmp;
      bp->size = newsize;
   }
   memcpy(bp->buf + bp->used, str, len);
   bp->used += len;
   bp->buf[bp->used] = '\0';
   return (0);
}