-
Notifications
You must be signed in to change notification settings - Fork 710
Expand file tree
/
Copy pathstdio.h
More file actions
110 lines (92 loc) · 2.03 KB
/
stdio.h
File metadata and controls
110 lines (92 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for stdio.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Busacker <[email protected]>
*/
#pragma once
#include <stdio.h>
#if defined(_WIN32)
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
/* stdio.h POSIX extensions */
/* dprintf implementation for Windows */
static inline int dprintf(int fd, const char *format, ...)
{
va_list args;
char buffer[4096];
int result;
va_start(args, format);
result = vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
if (fd == STDERR_FILENO)
fputs(buffer, stderr);
else if (fd == STDOUT_FILENO)
fputs(buffer, stdout);
return result;
}
/* getline implementation for Windows */
static inline ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;
if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}
bufptr = *lineptr;
size = *n;
c = fgetc(stream);
if (c == EOF)
return -1;
if (bufptr == NULL) {
bufptr = (char *)malloc(128);
if (bufptr == NULL) {
errno = ENOMEM;
return -1;
}
size = 128;
}
p = bufptr;
while (c != EOF) {
if ((size_t)(p - bufptr) + 1 >= size) {
size_t pos = (size_t)(p - bufptr);
size = size + 128;
bufptr = (char *)realloc(bufptr, size);
if (bufptr == NULL) {
errno = ENOMEM;
return -1;
}
p = bufptr + pos;
}
*p++ = c;
if (c == '\n')
break;
c = fgetc(stream);
}
*p = '\0';
*lineptr = bufptr;
*n = size;
return p - bufptr;
}
/* open_memstream workaround for Windows - returns a temporary file instead */
static inline FILE *open_memstream(char **ptr, size_t *sizeloc)
{
FILE *f = tmpfile();
if (ptr)
*ptr = NULL;
if (sizeloc)
*sizeloc = 0;
return f;
}
#endif