Back to index

citadel  8.12
Functions
snprintf.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

Go to the source code of this file.

Functions

static int needed (const char *fmt, va_list argp)
int vsnprintf (char *buf, size_t max, const char *fmt, va_list argp)
int snprintf (char *buf, size_t max, const char *fmt,...)

Function Documentation

static int needed ( const char *  fmt,
va_list  argp 
) [static]

Definition at line 27 of file snprintf.c.

{
  static FILE *sink = NULL;

  /* ok, there's a small race here that could result in the sink being
   * opened more than once if we're threaded, but I'd rather ignore it than
   * spend cycles synchronizing :-) */

  if (sink == NULL)
    {
      if ((sink = fopen("/dev/null", "w")) == NULL)
       {
         perror("/dev/null");
         exit(1);
       }
    }

  return vfprintf(sink, fmt, argp);
}

Here is the caller graph for this function:

int snprintf ( char *  buf,
size_t  max,
const char *  fmt,
  ... 
)

Definition at line 69 of file snprintf.c.

{
  va_list argp;
  int bytes;

  va_start(argp, fmt);
  bytes = vsnprintf(buf, max, fmt, argp);
  va_end(argp);

  return bytes;
}

Here is the call graph for this function:

int vsnprintf ( char *  buf,
size_t  max,
const char *  fmt,
va_list  argp 
)

Definition at line 48 of file snprintf.c.

{
  char *p;
  int size;

  if ((p = malloc(needed(fmt, argp) + 1)) == NULL)
    {
      fprintf(stderr, "vsnprintf: malloc failed, aborting\n");
      abort();
    }

  if ((size = vsprintf(p, fmt, argp)) >= max)
    size = -1;

  strncpy(buf, p, max);
  buf[max - 1] = 0;
  free(p);
  return size;
}

Here is the call graph for this function:

Here is the caller graph for this function: