[GRASS5] snprintf and other compile errors on IRIX

Eric G. Miller egm2 at jps.net
Thu May 2 20:51:54 EDT 2002


I've got a sort of cheat for asprintf. We can't use vsnprintf for the
same reason we can't use snprintf ;-)  Comments welcome (I'm not too
sure tmpfile() is safe.  It's apparently an anonymous file on my
machine, as nothing shows up in /tmp or the current directory...)

---------------------- <snip> ----------------------------------
/* BEGIN HEADER */
#ifndef G_ASPRINTF_H 
#define G_ASPRINTF_H

#if !defined _GNU_SOURCE
int
G_asprintf (char ** /* out */, const char * /* fmt */, ...);
#else
#if defined __STDC__VERSION__ + 0 >= 199900L
#define G_asprintf(pp,fmt,...) asprintf(pp, fmt, __VA_ARGS__)
#else
#define G_asprintf(pp,fmt,args...) asprintf(pp, fmt, args)
#endif /* __STDC_VERSION__ of variadic macro */
#endif /* older GNU version of variadic macro */

#endif
/* END HEADER */

/* BEGIN SOURCE */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include "g_asprintf.h"

/* Make sure the macro doesn't impact our function, if it is defined */
#undef G_asprintf

/* We cheat by printing to a tempfile via vfprintf and then reading it
 * back in.  Not the most efficient way, probably and tmpfile() is
 * not safe?
 */
int
G_asprintf (char **out, const char *fmt, ...)
{
    va_list ap;
    int ret_status = EOF;
    int count = 0;
    FILE *fp = NULL;
    char *work = NULL;
    
    assert (out != NULL && fmt != NULL);
    
    va_start (ap, fmt);
    if ((fp = tmpfile()))
    {
        count = vfprintf (fp, fmt, ap);
        if (count >= 0)
        {
            work = calloc (count + 1, 1);
            if (work != NULL)
            {
                rewind (fp);
                ret_status = fread (work, 1, count, fp);
                if (ret_status != count)
                {
                    ret_status = EOF;
                    free (work);
                    work = NULL;
                }
            }
        }
        fclose (fp);
    }
    va_end (ap);
    *out = work;
    return ret_status;
}
/* END SOURCE */
----------------------------- <snip> ------------------------------
-- 
Eric G. Miller <egm2 at jps.net>



More information about the grass-dev mailing list