|
| 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 |
0 commit comments