Skip to content

Commit 3824169

Browse files
Refactored stdio compatibiity
Moved stdio compatibility into nvme/stdio.h and removed it from platform/windows.h
1 parent eae0756 commit 3824169

5 files changed

Lines changed: 119 additions & 87 deletions

File tree

libnvme/src/nvme/log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <time.h>
1515
#include <unistd.h>
1616

17+
#include <nvme/stdio.h>
18+
1719
#include <libnvme.h>
1820

1921
#include "cleanup.h"

libnvme/src/nvme/stdio.h

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

libnvme/src/platform/windows.h

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -80,93 +80,6 @@ struct ifaddrs {
8080
};
8181

8282

83-
/* stdio.h POSIX extensions */
84-
85-
/* dprintf implementation for Windows */
86-
static inline int dprintf(int fd, const char *format, ...)
87-
{
88-
va_list args;
89-
char buffer[4096];
90-
int result;
91-
92-
va_start(args, format);
93-
result = vsnprintf(buffer, sizeof(buffer), format, args);
94-
va_end(args);
95-
if (fd == STDERR_FILENO)
96-
fputs(buffer, stderr);
97-
else if (fd == STDOUT_FILENO)
98-
fputs(buffer, stdout);
99-
return result;
100-
}
101-
102-
/* getline implementation for Windows */
103-
static inline ssize_t getline(char **lineptr, size_t *n, FILE *stream)
104-
{
105-
char *bufptr = NULL;
106-
char *p = bufptr;
107-
size_t size;
108-
int c;
109-
110-
if (lineptr == NULL || stream == NULL || n == NULL) {
111-
errno = EINVAL;
112-
return -1;
113-
}
114-
115-
bufptr = *lineptr;
116-
size = *n;
117-
118-
c = fgetc(stream);
119-
if (c == EOF)
120-
return -1;
121-
122-
if (bufptr == NULL) {
123-
bufptr = (char *)malloc(128);
124-
if (bufptr == NULL) {
125-
errno = ENOMEM;
126-
return -1;
127-
}
128-
size = 128;
129-
}
130-
131-
p = bufptr;
132-
while (c != EOF) {
133-
if ((size_t)(p - bufptr) + 1 >= size) {
134-
size_t pos = (size_t)(p - bufptr);
135-
136-
size = size + 128;
137-
bufptr = (char *)realloc(bufptr, size);
138-
if (bufptr == NULL) {
139-
errno = ENOMEM;
140-
return -1;
141-
}
142-
p = bufptr + pos;
143-
}
144-
*p++ = c;
145-
if (c == '\n')
146-
break;
147-
c = fgetc(stream);
148-
}
149-
150-
*p = '\0';
151-
*lineptr = bufptr;
152-
*n = size;
153-
154-
return p - bufptr;
155-
}
156-
157-
/* open_memstream workaround for Windows - returns a temporary file instead */
158-
static inline FILE *open_memstream(char **ptr, size_t *sizeloc)
159-
{
160-
FILE *f = tmpfile();
161-
162-
if (ptr)
163-
*ptr = NULL;
164-
if (sizeloc)
165-
*sizeloc = 0;
166-
return f;
167-
}
168-
169-
17083
/* time.h POSIX compatibility */
17184

17285
static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)

nvme-models.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
#include <sys/stat.h>
1111
#include <sys/types.h>
12+
13+
#include <nvme/stdio.h>
14+
1215
#include "nvme-models.h"
1316
#include "nvme.h"
1417

nvme-print-stdout.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <arpa/inet.h>
1414
#endif
1515

16+
#include <nvme/stdio.h>
17+
1618
#include <ccan/strset/strset.h>
1719
#include <ccan/htable/htable_type.h>
1820
#include <ccan/htable/htable.h>

0 commit comments

Comments
 (0)