patch: make *outfile extern
[platform/upstream/cdrkit.git] / librols / snprintf.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)snprintf.c       1.9 04/05/09 Copyright 1985, 1996-2004 J. Schilling */
14 /*
15  *      Copyright (c) 1985, 1996-2004 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #define snprintf __nothing__    /* prototype may be wrong (e.g. IRIX) */
33 #include <mconfig.h>
34 #include <unixstd.h>            /* include <sys/types.h> try to get size_t */
35 #include <stdio.h>              /* Try again for size_t */
36 #include <stdxlib.h>            /* Try again for size_t */
37 #include <vadefs.h>
38 #include <standard.h>
39 #include <schily.h>
40 #undef  snprintf
41
42 EXPORT  int snprintf __PR((char *, size_t maxcnt, const char *, ...));
43
44 typedef struct {
45         char    *ptr;
46         int     count;
47 } *BUF, _BUF;
48
49 #ifdef  PROTOTYPES
50 static void _cput(char c, long l)
51 #else
52 static void _cput(c, l)
53         char    c;
54         long    l;
55 #endif
56 {
57         register BUF    bp = (BUF)l;
58
59         if (--bp->count > 0) {
60                 *bp->ptr++ = c;
61         } else {
62                 /*
63                  * Make sure that there will never be a negative overflow.
64                  */
65                 bp->count++;
66         }
67 }
68
69 /* VARARGS2 */
70 #ifdef  PROTOTYPES
71 EXPORT int
72 snprintf(char *buf, size_t maxcnt, const char *form, ...)
73 #else
74 EXPORT int
75 snprintf(buf, maxcnt, form, va_alist)
76         char    *buf;
77         unsigned maxcnt;
78         char    *form;
79         va_dcl
80 #endif
81 {
82         va_list args;
83         int     cnt;
84         _BUF    bb;
85
86         bb.ptr = buf;
87         bb.count = maxcnt;
88
89 #ifdef  PROTOTYPES
90         va_start(args, form);
91 #else
92         va_start(args);
93 #endif
94         cnt = format(_cput, (long)&bb, form,  args);
95         va_end(args);
96         if (maxcnt > 0)
97                 *(bb.ptr) = '\0';
98         if (bb.count < 0)
99                 return (-1);
100
101         return (cnt);
102 }