sfprints.c 1.46 KB
Newer Older
ellson's avatar
ellson committed
1 2 3
/* $Id$ $Revision$ */
/* vim:set shiftwidth=4 ts=8: */

ellson's avatar
ellson committed
4 5 6 7 8 9 10 11 12
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/
ellson's avatar
ellson committed
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

#include	"sfhdr.h"

/*	Construct a string with the given format and data.
**	This function allocates space as necessary to store the string.
**	This avoids overflow problems typical with sprintf() in stdio.
**
**	Written by Kiem-Phong Vo.
*/

#if __STD_C
char *sfprints(const char *form, ...)
#else
char *sfprints(va_alist)
va_dcl
#endif
{
    va_list args;
    reg int rv;
    static Sfio_t *f;

#if __STD_C
    va_start(args, form);
#else
    reg char *form;
    va_start(args);
    form = va_arg(args, char *);
#endif

    /* make a fake stream */
    if (!f &&
	!(f = sfnew(NIL(Sfio_t *), NIL(char *), (size_t) SF_UNBOUND,
45 46 47 48
		    -1, SF_WRITE | SF_STRING))) {
	va_end(args);
	return NIL(char *);
    }
ellson's avatar
ellson committed
49 50 51 52 53 54 55 56 57 58 59

    sfseek(f, (Sfoff_t) 0, 0);
    rv = sfvprintf(f, form, args);
    va_end(args);

    if (rv < 0 || sfputc(f, '\0') < 0)
	return NIL(char *);

    _Sfi = (f->next - f->data) - 1;
    return (char *) f->data;
}