]> Joshua Wise's Git repositories - netwatch.git/blob - lib/sprintf.c
More ICH2-specific code diked out.
[netwatch.git] / lib / sprintf.c
1 /* 
2  * Mach Operating System
3  * Copyright (c) 1991,1990,1989 Carnegie Mellon University
4  * All Rights Reserved.
5  * 
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  * 
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  * 
16  * Carnegie Mellon requests users of this software to return to
17  * 
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  * 
23  * any improvements or extensions that they make and grant Carnegie Mellon
24  * the rights to redistribute these changes.
25  */
26
27 #include <stdarg.h>
28 #include "doprnt.h"
29
30 #define SPRINTF_UNLIMITED -1
31 struct sprintf_state {
32       char *buf;
33       unsigned int len;
34       unsigned int max;
35 };
36
37 static void
38 savechar(char *arg, int c)
39 {
40         struct sprintf_state *state = (struct sprintf_state *)arg;
41         
42         if (state->max != SPRINTF_UNLIMITED)
43         {
44                 if (state->len == state->max)
45                         return;
46         }
47
48         state->len++;
49         *state->buf = c;
50         state->buf++;
51 }
52
53 int vsprintf(char *s, const char *fmt, va_list args)
54 {
55         struct sprintf_state state;
56         state.max = SPRINTF_UNLIMITED;
57         state.len = 0;
58         state.buf = s;
59
60         _doprnt(fmt, args, 0, (void (*)()) savechar, (char *) &state);
61         *(state.buf) = '\0';
62
63         return state.len;
64 }
65
66 int vsnprintf(char *s, int size, const char *fmt, va_list args)
67 {
68         struct sprintf_state state;
69         state.max = size;
70         state.len = 0;
71         state.buf = s;
72
73         _doprnt(fmt, args, 0, (void (*)()) savechar, (char *) &state);
74         *(state.buf) = '\0';
75
76         return state.len;
77 }
78
79 int sprintf(char *s, const char *fmt, ...)
80 {
81         va_list args;
82         int err;
83
84         va_start(args, fmt);
85         err = vsprintf(s, fmt, args);
86         va_end(args);
87
88         return err;
89 }
90
91 int snprintf(char *s, int size, const char *fmt, ...)
92 {
93         va_list args;
94         int err;
95
96         va_start(args, fmt);
97         err = vsnprintf(s, size, fmt, args);
98         va_end(args);
99
100         return err;
101 }
102
This page took 0.044782 seconds and 4 git commands to generate.