File: vsnprintf.c

package info (click to toggle)
httptunnel 3.3%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 524 kB
  • ctags: 309
  • sloc: ansic: 4,646; sh: 338; makefile: 97
file content (76 lines) | stat: -rw-r--r-- 1,109 bytes parent folder | download | duplicates (10)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
port/vsnprintf.c

Copyright (C) 1999 Lars Brinkhoff.  See COPYING for terms and conditions.
*/

#include "config.h"

#include <stdio_.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>

#ifndef HAVE_VPRINTF
#error "Must have vfprintf() and vsprintf()."
#endif

int
vsmprintf (char **s, const char *format, va_list ap)
{
  size_t n;
  FILE *f;

  f = fopen ("/dev/null", "w");
  if (f == NULL)
    return -1;

  n = vfprintf (f, format, ap);
  fclose (f);

  *s = malloc (n + 1);
  if (*s == NULL)
    return -1;

  return vsprintf (*s, format, ap);
}

#ifndef HAVE_VSNPRINTF
int
vsnprintf (char *str, size_t n, const char *format, va_list ap)
{
  char *s;
  int m, r;

  m = vsmprintf (&s, format, ap);
  if (m == -1)
    return 0;

  if (m + 1 > n)
    {
      m = n - 1;
      r = -1;
    }
  else
    {
      r = m;
    }

  memcpy (str, s, m);
  free (s);
  str[m] = 0;
  return r;
}

int
snprintf (char *s, size_t n, const char *format, ...)
{
  va_list ap;
  int m;

  va_start (ap, format);
  m = vsnprintf (s, n, format, ap);
  va_end (ap);
  return m;
}
#endif /* HAVE_VSNPRINTF */