Back to index

tetex-bin  3.0
Functions
my-vsnprintf.c File Reference
#include <string.h>
#include <stdio.h>
#include "xdvi-config.h"
#include "xdvi.h"
#include "my-vsnprintf.h"

Go to the source code of this file.

Functions

int my_vsnprintf (char *buf, int len, const char *format, va_list argp)

Function Documentation

int my_vsnprintf ( char *  buf,
int  len,
const char *  format,
va_list  argp 
)

Definition at line 54 of file my-vsnprintf.c.

{
    int pipe_fd[2];
    FILE *fd;
    int size;
#ifdef DEBUG
    printf("=============my_vsnprintf called!\n");
#endif
    len--;    /* for the trailing '\0' */
    
    /* create a pipe for reading/writing */
    if (xpipe(pipe_fd) == -1) {
       perror("my_vsnprintf: pipe");
       xdvi_exit(EXIT_FAILURE);
    }
    if ((fd = try_fdopen(pipe_fd[1], "w")) == NULL) {
       perror("my_vsnprintf: fdopen");
       xdvi_exit(EXIT_FAILURE);
    }
    /* instead of setting fd to non-buffering, flush it explicitly: */
    /*  setvbuf(fd, NULL, _IONBF, BUFSIZ); */
    size = vfprintf(fd, format, argp);
    fflush(fd);

    /* wrote less characters than len -> adjust len for the subsequent read */
    if (size < len) {
       len = size;
    }
    read(pipe_fd[0], buf, len);
    buf[len] = '\0';     /* always null-terminate */

    fclose(fd);
    close(pipe_fd[0]);
    close(pipe_fd[1]);

    return size;
}

Here is the call graph for this function: