snprintf.c - vx32 - Local 9vx git repository for patches.
 (HTM) git clone git://r-36.net/vx32
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       snprintf.c (705B)
       ---
            1 
            2 #include <stdio.h>
            3 #include <errno.h>
            4 
            5 #include "ioprivate.h"
            6 
            7 struct snprintbuf {
            8         char *buf;
            9         char *ebuf;
           10 };
           11 
           12 static int
           13 sprintputch(int ch, struct snprintbuf *b)
           14 {
           15         if (b->buf < b->ebuf)
           16                 *b->buf++ = ch;
           17         return 0;
           18 }
           19 
           20 int
           21 vsnprintf(char *buf, int n, const char *fmt, va_list ap)
           22 {
           23         struct snprintbuf b = {buf, buf+n-1};
           24 
           25         if (buf == NULL || n < 1) {
           26                 errno = EINVAL;
           27                 return -1;
           28         }
           29 
           30         // print the string to the buffer
           31         int cnt = vprintfmt((void*)sprintputch, &b, fmt, ap);
           32 
           33         // null terminate the buffer
           34         *b.buf = '\0';
           35 
           36         return cnt;
           37 }
           38 
           39 int
           40 snprintf(char *buf, int n, const char *fmt, ...)
           41 {
           42         va_list ap;
           43         int rc;
           44 
           45         va_start(ap, fmt);
           46         rc = vsnprintf(buf, n, fmt, ap);
           47         va_end(ap);
           48 
           49         return rc;
           50 }
           51