From 453d6fdea252b9911c7757a6cac0a6a202ddc17d Mon Sep 17 00:00:00 2001
From: Alejandro Colomar <alx@kernel.org>
Date: Sun, 21 Jul 2024 18:40:25 +0200
Subject: [PATCH] lib/, po/: Remove fgetsx() and fputsx()

It seems they never worked correctly.  Let's keep it simple and remove
support for escaped newlines.

Closes: <https://github.com/shadow-maint/shadow/issues/1055>
Reported-by: Chris Hofstaedtler <zeha@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 lib/Makefile.am     |  1 -
 lib/commonio.c      |  9 +++---
 lib/commonio.h      |  9 ------
 lib/fputsx.c        | 68 ---------------------------------------------
 lib/groupio.c       |  2 --
 lib/gshadow.c       | 10 ++-----
 lib/prototypes.h    |  5 ----
 lib/pwio.c          |  2 --
 lib/sgroupio.c      |  2 --
 lib/shadowio.c      |  2 --
 lib/subordinateio.c |  2 --
 po/POTFILES.in      |  1 -
 12 files changed, 7 insertions(+), 106 deletions(-)
 delete mode 100644 lib/fputsx.c

diff --git a/lib/Makefile.am b/lib/Makefile.am
index eb9a93da86..1baefcf2d4 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -102,7 +102,6 @@ libshadow_la_SOURCES = \
 	find_new_uid.c \
 	find_new_sub_gids.c \
 	find_new_sub_uids.c \
-	fputsx.c \
 	fs/readlink/areadlink.c \
 	fs/readlink/areadlink.h \
 	fs/readlink/readlinknul.c \
diff --git a/lib/commonio.c b/lib/commonio.c
index 1135c29493..aa41e96bd9 100644
--- a/lib/commonio.c
+++ b/lib/commonio.c
@@ -644,7 +644,7 @@ int commonio_open (struct commonio_db *db, int mode)
 	if (NULL == buf)
 		goto cleanup_errno;
 
-	while (db->ops->fgets(buf, buflen, db->fp) != NULL) {
+	while (fgets(buf, buflen, db->fp) != NULL) {
 		struct commonio_entry  *p;
 
 		while (   (strrchr (buf, '\n') == NULL)
@@ -657,9 +657,8 @@ int commonio_open (struct commonio_db *db, int mode)
 				goto cleanup_errno;
 
 			len = strlen (buf);
-			if (db->ops->fgets(buf + len, buflen - len, db->fp) == NULL) {
+			if (fgets(buf + len, buflen - len, db->fp) == NULL)
 				goto cleanup_buf;
-			}
 		}
 		stpsep(buf, "\n");
 
@@ -877,9 +876,9 @@ static int write_all (const struct commonio_db *db)
 				return -1;
 			}
 		} else if (NULL != p->line) {
-			if (db->ops->fputs (p->line, db->fp) == EOF) {
+			if (fputs(p->line, db->fp) == EOF)
 				return -1;
-			}
+
 			if (putc ('\n', db->fp) == EOF) {
 				return -1;
 			}
diff --git a/lib/commonio.h b/lib/commonio.h
index fedbefa3d9..0afa599588 100644
--- a/lib/commonio.h
+++ b/lib/commonio.h
@@ -60,15 +60,6 @@ struct commonio_ops {
 	 */
 	int (*put) (const void *, FILE *);
 
-	/*
-	 * fgets and fputs (can be replaced by versions that
-	 * understand line continuation conventions).
-	 */
-	ATTR_ACCESS(write_only, 1, 2)
-	/*@null@*/char *(*fgets)(/*@returned@*/char *restrict s, int n,
-	                         FILE *restrict stream);
-	int (*fputs) (const char *, FILE *);
-
 	/*
 	 * open_hook and close_hook.
 	 * If non NULL, these functions will be called after the database
diff --git a/lib/fputsx.c b/lib/fputsx.c
deleted file mode 100644
index 877c364277..0000000000
--- a/lib/fputsx.c
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
- * SPDX-FileCopyrightText: 1996 - 1999, Marek Michałkiewicz
- * SPDX-FileCopyrightText: 2005       , Tomasz Kłoczko
- * SPDX-FileCopyrightText: 2008       , Nicolas François
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <config.h>
-
-#include <stddef.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "defines.h"
-#include "prototypes.h"
-#include "string/strcmp/streq.h"
-
-
-/*@null@*/char *
-fgetsx(/*@returned@*/char *restrict buf, int cnt, FILE *restrict f)
-{
-	char *cp = buf;
-	char *ep;
-
-	while (cnt > 0) {
-		if (fgets(cp, cnt, f) == NULL) {
-			if (cp == buf) {
-				return NULL;
-			} else {
-				break;
-			}
-		}
-		ep = strrchr (cp, '\\');
-		if ((NULL != ep) && (*(ep + 1) == '\n')) {
-			cnt -= ep - cp;
-			if (cnt > 0)
-				cp = stpcpy(ep, "");
-		} else {
-			break;
-		}
-	}
-	return buf;
-}
-
-int fputsx (const char *s, FILE * stream)
-{
-	int i;
-
-	for (i = 0; !streq(s, ""); i++, s++) {
-		if (putc (*s, stream) == EOF) {
-			return EOF;
-		}
-
-#if 0				/* The standard getgr*() can't handle that.  --marekm */
-		if (i > (BUFSIZ / 2)) {
-			if (putc ('\\', stream) == EOF ||
-			    putc ('\n', stream) == EOF)
-				return EOF;
-
-			i = 0;
-		}
-#endif
-	}
-	return 0;
-}
-
diff --git a/lib/groupio.c b/lib/groupio.c
index 516e3ccd24..86cc5d02c6 100644
--- a/lib/groupio.c
+++ b/lib/groupio.c
@@ -99,8 +99,6 @@ static struct commonio_ops group_ops = {
 	group_getname,
 	group_parse,
 	group_put,
-	fgetsx,
-	fputsx,
 	group_open_hook,
 	group_close_hook
 };
diff --git a/lib/gshadow.c b/lib/gshadow.c
index 0272824597..a70468fe9b 100644
--- a/lib/gshadow.c
+++ b/lib/gshadow.c
@@ -145,7 +145,7 @@ sgetsgent(const char *string)
 		return NULL;
 	}
 
-	if (fgetsx(buf, buflen, fp) == NULL)
+	if (fgets(buf, buflen, fp) == NULL)
 		return NULL;
 
 	while (   (strrchr(buf, '\n') == NULL)
@@ -160,7 +160,7 @@ sgetsgent(const char *string)
 		buflen *= 2;
 
 		len = strlen (buf);
-		if (fgetsx(&buf[len], buflen - len, fp) == NULL)
+		if (fgets(&buf[len], buflen - len, fp) == NULL)
 			return NULL;
 	}
 	stpsep(buf, "\n");
@@ -259,11 +259,7 @@ int putsgent (const struct sgrp *sgrp, FILE * fp)
 	}
 	stpcpy(cp, "\n");
 
-	/*
-	 * Output using the function which understands the line
-	 * continuation conventions.
-	 */
-	if (fputsx (buf, fp) == EOF) {
+	if (fputs(buf, fp) == EOF) {
 		free (buf);
 		return -1;
 	}
diff --git a/lib/prototypes.h b/lib/prototypes.h
index 6b978a9751..2f6b0c5ac6 100644
--- a/lib/prototypes.h
+++ b/lib/prototypes.h
@@ -157,11 +157,6 @@ extern int getrange (const char *range,
 /* gettime.c */
 extern time_t gettime (void);
 
-/* fputsx.c */
-ATTR_ACCESS(write_only, 1, 2)
-extern /*@null@*/char *fgetsx(/*@returned@*/char *restrict, int, FILE *restrict);
-extern int fputsx (const char *, FILE *);
-
 /* groupio.c */
 extern void __gr_del_entry (const struct commonio_entry *ent);
 extern /*@observer@*/const struct commonio_db *__gr_get_db (void);
diff --git a/lib/pwio.c b/lib/pwio.c
index 3497c7545e..1e9231ebc9 100644
--- a/lib/pwio.c
+++ b/lib/pwio.c
@@ -73,8 +73,6 @@ static struct commonio_ops passwd_ops = {
 	passwd_getname,
 	passwd_parse,
 	passwd_put,
-	fgets,
-	fputs,
 	NULL,			/* open_hook */
 	NULL			/* close_hook */
 };
diff --git a/lib/sgroupio.c b/lib/sgroupio.c
index acb140d87e..229cc2364b 100644
--- a/lib/sgroupio.c
+++ b/lib/sgroupio.c
@@ -197,8 +197,6 @@ static struct commonio_ops gshadow_ops = {
 	gshadow_getname,
 	gshadow_parse,
 	gshadow_put,
-	fgetsx,
-	fputsx,
 	NULL,			/* open_hook */
 	NULL			/* close_hook */
 };
diff --git a/lib/shadowio.c b/lib/shadowio.c
index d2c3b4730b..5fe6c14430 100644
--- a/lib/shadowio.c
+++ b/lib/shadowio.c
@@ -72,8 +72,6 @@ static struct commonio_ops shadow_ops = {
 	shadow_getname,
 	shadow_parse,
 	shadow_put,
-	fgets,
-	fputs,
 	NULL,			/* open_hook */
 	NULL			/* close_hook */
 };
diff --git a/lib/subordinateio.c b/lib/subordinateio.c
index 97993e7e96..3d7d5eadef 100644
--- a/lib/subordinateio.c
+++ b/lib/subordinateio.c
@@ -149,8 +149,6 @@ static struct commonio_ops subordinate_ops = {
 	NULL,			/* getname */
 	subordinate_parse,	/* parse */
 	subordinate_put,	/* put */
-	fgets,			/* fgets */
-	fputs,			/* fputs */
 	NULL,			/* open_hook */
 	NULL,			/* close_hook */
 };
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 3aff87b2d4..6d6ae48aba 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -21,7 +21,6 @@ lib/find_new_gid.c
 lib/find_new_sub_gids.c
 lib/find_new_sub_uids.c
 lib/find_new_uid.c
-lib/fputsx.c
 lib/getdef.c
 lib/getgr_nam_gid.c
 lib/getrange.c