]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/shadow/, lib/: fgetsgent(): Move to separate file
authorAlejandro Colomar <alx@kernel.org>
Sun, 10 Nov 2024 17:18:19 +0000 (18:18 +0100)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Tue, 7 Oct 2025 09:03:09 +0000 (11:03 +0200)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/gshadow.c
lib/gshadow_.h
lib/shadow/gshadow/fgetsgent.c [new file with mode: 0644]
lib/shadow/gshadow/fgetsgent.h [new file with mode: 0644]

index 5138d5b525f1748833006a277c24cacf29f861bd..c6a2ce02fc369438d63855981ec5a2e5dadec739 100644 (file)
@@ -176,6 +176,8 @@ libshadow_la_SOURCES = \
        shadow/grp/agetgroups.h \
        shadow/gshadow/endsgent.c \
        shadow/gshadow/endsgent.h \
+       shadow/gshadow/fgetsgent.c \
+       shadow/gshadow/fgetsgent.h \
        shadow/gshadow/gshadow.c \
        shadow/gshadow/gshadow.h \
        shadow/gshadow/putsgent.c \
index 072f4d475ac31d953830e9630bb181d044f8a68a..8f638f7f35c3a31fb9ea19bae556d2ee0da2c372 100644 (file)
@@ -22,6 +22,7 @@
 #include "alloc/realloc.h"
 #include "defines.h"
 #include "prototypes.h"
+#include "shadow/gshadow/fgetsgent.h"
 #include "shadow/gshadow/gshadow.h"
 #include "shadow/gshadow/setsgent.h"
 #include "shadow/gshadow/sgrp.h"
@@ -77,57 +78,6 @@ sgetsgent(const char *s)
        return &sgroup;
 }
 
-/*
- * fgetsgent - convert next line in stream to (struct sgrp)
- *
- * fgetsgent() reads the next line from the provided stream and
- * converts it to a (struct sgrp).  NULL is returned on EOF.
- */
-
-/*@observer@*//*@null@*/struct sgrp *fgetsgent (/*@null@*/FILE * fp)
-{
-       static size_t buflen = 0;
-       static char *buf = NULL;
-
-       char *cp;
-
-       if (0 == buflen) {
-               buf = MALLOC(BUFSIZ, char);
-               if (NULL == buf) {
-                       return NULL;
-               }
-               buflen = BUFSIZ;
-       }
-
-       if (NULL == fp) {
-               return NULL;
-       }
-
-       if (fgetsx(buf, buflen, fp) == NULL)
-               return NULL;
-
-       while (   (strrchr(buf, '\n') == NULL)
-              && (feof (fp) == 0)) {
-               size_t len;
-
-               cp = REALLOC(buf, buflen * 2, char);
-               if (NULL == cp) {
-                       return NULL;
-               }
-               buf = cp;
-               buflen *= 2;
-
-               len = strlen (buf);
-               if (fgetsx (&buf[len],
-                           (int) (buflen - len),
-                           fp) != &buf[len]) {
-                       return NULL;
-               }
-       }
-       stpsep(buf, "\n");
-       return (sgetsgent (buf));
-}
-
 /*
  * getsgent - get a single shadow group entry
  */
index 63223e962f17352d7b2a571365489c21a0b2b598..007652b08f81339a2f9214daa74b8a6c02c87507 100644 (file)
 
 #include <config.h>
 
-#include <stdio.h>             /* for FILE */
-
 #include "shadow/gshadow/sgrp.h"
 
 
 /*@observer@*//*@null@*/struct sgrp *getsgent (void);
 /*@observer@*//*@null@*/struct sgrp *getsgnam (const char *);
 /*@observer@*//*@null@*/struct sgrp *sgetsgent (const char *);
-/*@observer@*//*@null@*/struct sgrp *fgetsgent (/*@null@*/FILE *);
 
 #define        GSHADOW "/etc/gshadow"
 
diff --git a/lib/shadow/gshadow/fgetsgent.c b/lib/shadow/gshadow/fgetsgent.c
new file mode 100644 (file)
index 0000000..9424ab9
--- /dev/null
@@ -0,0 +1,77 @@
+// SPDX-FileCopyrightText: 1990-1994, Julianne Frances Haugh
+// SPDX-FileCopyrightText: 1996-1998, Marek Michałkiewicz
+// SPDX-FileCopyrightText: 2005, Tomasz Kłoczko
+// SPDX-FileCopyrightText: 2008-2009, Nicolas François
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include "config.h"
+
+#include "shadow/gshadow/fgetsgent.h"
+
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "alloc/malloc.h"
+#include "alloc/realloc.h"
+#include "defines.h"
+#include "prototypes.h"
+#include "shadow/gshadow/sgrp.h"
+#include "string/strtok/stpsep.h"
+
+
+/*
+ * fgetsgent - convert next line in stream to (struct sgrp)
+ *
+ * fgetsgent() reads the next line from the provided stream and
+ * converts it to a (struct sgrp).  NULL is returned on EOF.
+ */
+#if defined(SHADOWGRP) && !__has_include(<gshadow.h>)
+// from-FILE get-next shadow group entry
+struct sgrp *
+fgetsgent(FILE *fp)
+{
+       static size_t buflen = 0;
+       static char *buf = NULL;
+
+       char *cp;
+
+       if (0 == buflen) {
+               buf = MALLOC(BUFSIZ, char);
+               if (NULL == buf) {
+                       return NULL;
+               }
+               buflen = BUFSIZ;
+       }
+
+       if (NULL == fp) {
+               return NULL;
+       }
+
+       if (fgetsx(buf, buflen, fp) == NULL)
+               return NULL;
+
+       while (   (strrchr(buf, '\n') == NULL)
+              && (feof (fp) == 0)) {
+               size_t len;
+
+               cp = REALLOC(buf, buflen * 2, char);
+               if (NULL == cp) {
+                       return NULL;
+               }
+               buf = cp;
+               buflen *= 2;
+
+               len = strlen (buf);
+               if (fgetsx (&buf[len],
+                           (int) (buflen - len),
+                           fp) != &buf[len]) {
+                       return NULL;
+               }
+       }
+       stpsep(buf, "\n");
+       return (sgetsgent (buf));
+}
+#endif
diff --git a/lib/shadow/gshadow/fgetsgent.h b/lib/shadow/gshadow/fgetsgent.h
new file mode 100644 (file)
index 0000000..e40fbbd
--- /dev/null
@@ -0,0 +1,26 @@
+// SPDX-FileCopyrightText: 1988-1994, Julianne Frances Haugh
+// SPDX-FileCopyrightText: 1996-1997, Marek Michałkiewicz
+// SPDX-FileCopyrightText: 2003-2005, Tomasz Kłoczko
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_SHADOW_GSHADOW_FGETSGENT_H_
+#define SHADOW_INCLUDE_LIB_SHADOW_GSHADOW_FGETSGENT_H_
+
+
+#include "config.h"
+
+#include <stdio.h>
+
+#include "shadow/gshadow/sgrp.h"
+
+
+#if __has_include(<gshadow.h>)
+# include <gshadow.h>
+#else
+struct sgrp *fgetsgent(FILE *stream);
+#endif
+
+
+#endif  // include guard