stdio: New module, initially to provide working [v]snprintf() on Windows.
[cascardo/ovs.git] / lib / stdio.c
1 /*
2  * Copyright (c) 2013 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <stdio.h>
20
21 #ifdef _WIN32
22 #undef snprintf
23 #undef vsnprintf
24
25 int
26 ovs_snprintf(char *s, size_t n, const char *format, ... )
27 {
28     va_list args;
29     int len;
30
31     va_start(args, format);
32     len = ovs_vsnprintf(s, n, format, args);
33     va_end(args);
34
35     return len;
36 }
37
38 int
39 ovs_vsnprintf(char *s, size_t n, const char *format, va_list args)
40 {
41     int needed = _vscprintf(format, args);
42     if (s && n) {
43         vsnprintf(s, n, format, args);
44         s[n - 1] = '\0';
45     }
46     return needed;
47 }
48 #endif  /* _WIN32 */