From: Andre Muezerie <andremue@linux.microsoft.com>
To: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>,
Tyler Retzlaff <roretzla@linux.microsoft.com>,
Thomas Monjalon <thomas@monjalon.net>
Cc: dev@dpdk.org, Andre Muezerie <andremue@linux.microsoft.com>
Subject: [PATCH] eal: add getline() function for Windows
Date: Mon, 5 May 2025 13:40:07 -0700 [thread overview]
Message-ID: <1746477607-14961-1-git-send-email-andremue@linux.microsoft.com> (raw)
Existing DPDK code uses getline(), which is a POSIX function and is
not available in the Windows APIs.
Instead of rewriting it or coming up with some other replacement, this
patch makes use of the implementation provided by NetBSD to make it
possible to compile code dependent on getline on Windows.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
lib/eal/windows/getline.c | 61 +++++++++++++++++++++++++++++++
lib/eal/windows/include/getline.h | 28 ++++++++++++++
lib/eal/windows/meson.build | 1 +
license/exceptions.txt | 2 +
4 files changed, 92 insertions(+)
create mode 100644 lib/eal/windows/getline.c
create mode 100644 lib/eal/windows/include/getline.h
diff --git a/lib/eal/windows/getline.c b/lib/eal/windows/getline.c
new file mode 100644
index 0000000000..955452b50e
--- /dev/null
+++ b/lib/eal/windows/getline.c
@@ -0,0 +1,61 @@
+/* SPDX-License-Identifier: BSD-2-Clause
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ */
+
+#include <malloc.h>
+
+#include "getline.h"
+
+ssize_t
+getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
+{
+ char *ptr, *eptr;
+
+ if (*buf == NULL || *bufsiz == 0) {
+ *bufsiz = BUFSIZ;
+ *buf = malloc(*bufsiz);
+ if (buf == NULL)
+ return -1;
+ }
+
+ for (ptr = *buf, eptr = *buf + *bufsiz;;) {
+ int c = fgetc(fp);
+ if (c == -1) {
+ if (feof(fp)) {
+ ssize_t diff = (ssize_t)(ptr - *buf);
+ if (diff != 0) {
+ *ptr = '\0';
+ return diff;
+ }
+ }
+ return -1;
+ }
+ *ptr++ = c;
+ if (c == delimiter) {
+ *ptr = '\0';
+ return ptr - *buf;
+ }
+ if (ptr + 2 >= eptr) {
+ char *nbuf;
+ size_t nbufsiz = *bufsiz * 2;
+ ssize_t d = ptr - *buf;
+ nbuf = realloc(*buf, nbufsiz);
+ if (nbuf == NULL)
+ return -1;
+ *buf = nbuf;
+ *bufsiz = nbufsiz;
+ eptr = nbuf + nbufsiz;
+ ptr = nbuf + d;
+ }
+ }
+}
+
+ssize_t
+getline(char **buf, size_t *bufsiz, FILE *fp)
+{
+ return getdelim(buf, bufsiz, '\n', fp);
+}
diff --git a/lib/eal/windows/include/getline.h b/lib/eal/windows/include/getline.h
new file mode 100644
index 0000000000..2931ce2a02
--- /dev/null
+++ b/lib/eal/windows/include/getline.h
@@ -0,0 +1,28 @@
+/* SPDX-License-Identifier: BSD-2-Clause
+ * Copyright (c) 2011 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ */
+
+/**
+ * @file
+ * getline compat.
+ *
+ * This module provides getline() and getdelim().
+ */
+
+#pragma once
+
+#include <stdio.h>
+
+#ifndef ssize_t
+#define ssize_t ptrdiff_t
+#endif
+
+ssize_t
+getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp);
+
+ssize_t
+getline(char **buf, size_t *bufsiz, FILE *fp);
diff --git a/lib/eal/windows/meson.build b/lib/eal/windows/meson.build
index 7756d417be..c526ede405 100644
--- a/lib/eal/windows/meson.build
+++ b/lib/eal/windows/meson.build
@@ -17,6 +17,7 @@ sources += files(
'eal_mp.c',
'eal_thread.c',
'eal_timer.c',
+ 'getline.c',
'getopt.c',
'rte_thread.c',
)
diff --git a/license/exceptions.txt b/license/exceptions.txt
index d12fac2034..2785a4f335 100644
--- a/license/exceptions.txt
+++ b/license/exceptions.txt
@@ -16,4 +16,6 @@ MIT | 10/23/2019 | 02/10/2020 | lib/eal/windows/inc
BSD-2-Clause | 10/23/2019 | 12/18/2019 | lib/eal/windows/include/getopt.h
ISC AND BSD-2-Clause | 10/23/2019 | 12/18/2019 | lib/eal/windows/getopt.c
MIT | 10/19/2022 | 10/18/2022 | drivers/net/gve/base/*
+BSD-2-Clause | XX/XX/XXXX | XX/XX/XXXX | lib/eal/windows/include/getline.h
+BSD-2-Clause | XX/XX/XXXX | XX/XX/XXXX | lib/eal/windows/getline.c
---------------------------------------------------------------------------------------------------
--
2.49.0.vfs.0.0
reply other threads:[~2025-05-05 20:40 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1746477607-14961-1-git-send-email-andremue@linux.microsoft.com \
--to=andremue@linux.microsoft.com \
--cc=dev@dpdk.org \
--cc=dmitry.kozliuk@gmail.com \
--cc=roretzla@linux.microsoft.com \
--cc=thomas@monjalon.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).