1*c2c66affSColin Finck /* @(#)jssnprintf.c 1.14 17/08/03 Copyright 1985, 1995-2017 J. Schilling */
2*c2c66affSColin Finck /*
3*c2c66affSColin Finck * Copyright (c) 1985, 1995-2017 J. Schilling
4*c2c66affSColin Finck */
5*c2c66affSColin Finck /*
6*c2c66affSColin Finck * The contents of this file are subject to the terms of the
7*c2c66affSColin Finck * Common Development and Distribution License, Version 1.0 only
8*c2c66affSColin Finck * (the "License"). You may not use this file except in compliance
9*c2c66affSColin Finck * with the License.
10*c2c66affSColin Finck *
11*c2c66affSColin Finck * See the file CDDL.Schily.txt in this distribution for details.
12*c2c66affSColin Finck * A copy of the CDDL is also available via the Internet at
13*c2c66affSColin Finck * http://www.opensource.org/licenses/cddl1.txt
14*c2c66affSColin Finck *
15*c2c66affSColin Finck * When distributing Covered Code, include this CDDL HEADER in each
16*c2c66affSColin Finck * file and include the License file CDDL.Schily.txt from this distribution.
17*c2c66affSColin Finck */
18*c2c66affSColin Finck
19*c2c66affSColin Finck #include <schily/mconfig.h>
20*c2c66affSColin Finck #include <schily/unistd.h> /* include <sys/types.h> try to get size_t */
21*c2c66affSColin Finck #include <schily/stdio.h> /* Try again for size_t */
22*c2c66affSColin Finck #include <schily/stdlib.h> /* Try again for size_t */
23*c2c66affSColin Finck #include <schily/varargs.h>
24*c2c66affSColin Finck #include <schily/standard.h>
25*c2c66affSColin Finck #include <schily/schily.h>
26*c2c66affSColin Finck
27*c2c66affSColin Finck EXPORT int js_snprintf __PR((char *, size_t maxcnt, const char *, ...));
28*c2c66affSColin Finck
29*c2c66affSColin Finck typedef struct {
30*c2c66affSColin Finck char *ptr;
31*c2c66affSColin Finck int count;
32*c2c66affSColin Finck } *BUF, _BUF;
33*c2c66affSColin Finck
34*c2c66affSColin Finck #ifdef PROTOTYPES
35*c2c66affSColin Finck static void
_cput(char c,void * l)36*c2c66affSColin Finck _cput(char c, void *l)
37*c2c66affSColin Finck #else
38*c2c66affSColin Finck static void
39*c2c66affSColin Finck _cput(c, l)
40*c2c66affSColin Finck char c;
41*c2c66affSColin Finck void *l;
42*c2c66affSColin Finck #endif
43*c2c66affSColin Finck {
44*c2c66affSColin Finck register BUF bp = (BUF)l;
45*c2c66affSColin Finck
46*c2c66affSColin Finck if (--bp->count > 0) {
47*c2c66affSColin Finck *bp->ptr++ = c;
48*c2c66affSColin Finck } else {
49*c2c66affSColin Finck /*
50*c2c66affSColin Finck * Make sure that there will never be a negative overflow.
51*c2c66affSColin Finck */
52*c2c66affSColin Finck bp->count++;
53*c2c66affSColin Finck }
54*c2c66affSColin Finck }
55*c2c66affSColin Finck
56*c2c66affSColin Finck /* VARARGS2 */
57*c2c66affSColin Finck #ifdef PROTOTYPES
58*c2c66affSColin Finck EXPORT int
js_snprintf(char * buf,size_t maxcnt,const char * form,...)59*c2c66affSColin Finck js_snprintf(char *buf, size_t maxcnt, const char *form, ...)
60*c2c66affSColin Finck #else
61*c2c66affSColin Finck EXPORT int
62*c2c66affSColin Finck js_snprintf(buf, maxcnt, form, va_alist)
63*c2c66affSColin Finck char *buf;
64*c2c66affSColin Finck unsigned maxcnt;
65*c2c66affSColin Finck char *form;
66*c2c66affSColin Finck va_dcl
67*c2c66affSColin Finck #endif
68*c2c66affSColin Finck {
69*c2c66affSColin Finck va_list args;
70*c2c66affSColin Finck int cnt;
71*c2c66affSColin Finck _BUF bb;
72*c2c66affSColin Finck
73*c2c66affSColin Finck bb.ptr = buf;
74*c2c66affSColin Finck bb.count = maxcnt;
75*c2c66affSColin Finck
76*c2c66affSColin Finck #ifdef PROTOTYPES
77*c2c66affSColin Finck va_start(args, form);
78*c2c66affSColin Finck #else
79*c2c66affSColin Finck va_start(args);
80*c2c66affSColin Finck #endif
81*c2c66affSColin Finck cnt = format(_cput, &bb, form, args);
82*c2c66affSColin Finck va_end(args);
83*c2c66affSColin Finck if (maxcnt > 0)
84*c2c66affSColin Finck *(bb.ptr) = '\0';
85*c2c66affSColin Finck if (bb.count < 0)
86*c2c66affSColin Finck return (-1);
87*c2c66affSColin Finck
88*c2c66affSColin Finck return (cnt);
89*c2c66affSColin Finck }
90