groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

xasprintf() NIH


From: Alejandro Colomar
Subject: xasprintf() NIH
Date: Wed, 17 Jan 2024 21:06:30 +0100

Hi Branden,

I just saw some function in groff that looks like gnulib's xasprintf().
You may want to just use that instead, or at least rename it to
my_xasprintf() to be more clear about what it is.

        $ grepc -B8 make_string .
        ./src/preproc/html/pre-html.cpp-/*
        ./src/preproc/html/pre-html.cpp- *  make_string - Create a string via 
`malloc()`, place the variadic
        ./src/preproc/html/pre-html.cpp- *                arguments as 
formatted by `fmt` into it, and return
        ./src/preproc/html/pre-html.cpp- *                it.  Adapted from 
Linux man-pages' printf(3) example.
        ./src/preproc/html/pre-html.cpp- *                We never return a 
null pointer, instead treating
        ./src/preproc/html/pre-html.cpp- *                failure as invariably 
fatal.
        ./src/preproc/html/pre-html.cpp- */
        ./src/preproc/html/pre-html.cpp-
        ./src/preproc/html/pre-html.cpp:char *make_string(const char *fmt, ...)
        {
          size_t size = 0;
          char *p = 0 /* nullptr */;
          va_list ap;
          va_start(ap, fmt);
          int n = vsnprintf(p, size, fmt, ap);
          va_end(ap);
          if (n < 0)
            sys_fatal("vsnprintf");
          size = static_cast<size_t>(n) + 1 /* '\0' */;
          p = static_cast<char *>(malloc(size));
          if (0 /* nullptr */ == p)
            sys_fatal("vsnprintf");
          va_start(ap, fmt);
          n = vsnprintf(p, size, fmt, ap);
          va_end(ap);
          if (n < 0)
            sys_fatal("vsnprintf");
          assert(p != 0 /* nullptr */);
          return p;
        }

Cheers,
Alex

-- 
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]