]> Savannah Git Hosting - gnulib.git/commitdiff
getusershell: Split file by lines instead of spaces.
authorCollin Funk <collin.funk1@gmail.com>
Tue, 21 May 2024 23:35:09 +0000 (16:35 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Wed, 22 May 2024 00:51:52 +0000 (17:51 -0700)
* lib/getusershell.c: Include string.h and filename.h
(GNULIB_GETUSERSHELL_SINGLE_THREAD): Remove conditional to include
unlocked stdio functions that are no longer used.
(readname): Remove function.
(getusershell): Use getline and process the string instead of using
readname. Return the first absolute file name.
* modules/getusershell (Depends-on): Remove unlocked-io-internal.
Add getline and filename.
* doc/multithread.texi (Multithreading Optimizations): Don't mention
GNULIB_GETUSERSHELL_SINGLE_THREAD.

ChangeLog
doc/multithread.texi
lib/getusershell.c
modules/getusershell

index 0baff3aecce8f917da3b68c93864572f35093e46..f94a39a2bbae5956696d4ba9c1b1681807ff8190 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-05-21  Collin Funk  <collin.funk1@gmail.com>
+
+       getusershell: Split file by lines instead of spaces.
+       * lib/getusershell.c: Include string.h and filename.h
+       (GNULIB_GETUSERSHELL_SINGLE_THREAD): Remove conditional to include
+       unlocked stdio functions that are no longer used.
+       (readname): Remove function.
+       (getusershell): Use getline and process the string instead of using
+       readname. Return the first absolute file name.
+       * modules/getusershell (Depends-on): Remove unlocked-io-internal.
+       Add getline and filename.
+       * doc/multithread.texi (Multithreading Optimizations): Don't mention
+       GNULIB_GETUSERSHELL_SINGLE_THREAD.
+
 2024-05-21  Paul Eggert  <eggert@cs.ucla.edu>
 
        boot-time: port to Alpine 3.20.0_rc2
index 7d8126e8f30ed72623b014fdf4305297fa4bd910..424e1d2a7596d93dd92aa613a52fa6b8ca9a0dab 100644 (file)
@@ -293,10 +293,6 @@ This macro optimizes the functions @code{mbrtowc}, @code{mbrtoc32}, and
 You can get this macro defined by including the Gnulib module
 @code{wchar-single}.
 @item
-You may define the C macro @code{GNULIB_GETUSERSHELL_SINGLE_THREAD}, if all the
-programs in your package invoke the functions @code{setusershell},
-@code{getusershell}, @code{endusershell} only from a single thread.
-@item
 You may define the C macro @code{GNULIB_EXCLUDE_SINGLE_THREAD}, if all the
 programs in your package invoke the functions of the @code{exclude} module
 only from a single thread.
index 4da5bff37f340e9b7f1b5c8c136d9af8ab3f0ea4..356b8023d5ed685f25f2463003648db0f129543b 100644 (file)
 #endif
 
 #include <stdlib.h>
+#include <string.h>
 #include <ctype.h>
 
 #include "stdio--.h"
+#include "filename.h"
 #include "xalloc.h"
 
-#if GNULIB_GETUSERSHELL_SINGLE_THREAD
-# include "unlocked-io.h"
-#endif
-
-static idx_t readname (char **, idx_t *, FILE *);
-
 #if ! defined ADDITIONAL_DEFAULT_SHELLS && defined __MSDOS__
 # define ADDITIONAL_DEFAULT_SHELLS \
   "c:/dos/command.com", "c:/windows/command.com", "c:/command.com",
@@ -70,7 +66,7 @@ static FILE *shellstream = NULL;
 static char *line = NULL;
 
 /* Number of bytes allocated for 'line'. */
-static idx_t line_size = 0;
+static size_t line_size = 0;
 \f
 /* Return an entry from the shells file, ignoring comment lines.
    If the file doesn't exist, use the list in DEFAULT_SHELLS (above).
@@ -99,12 +95,46 @@ getusershell (void)
         }
     }
 
-  while (readname (&line, &line_size, shellstream))
+  for (;;)
     {
-      if (*line != '#')
-        return line;
+      ssize_t nread = getline (&line, &line_size, shellstream);
+
+      /* End of file.  */
+      if (nread == -1)
+        return NULL;
+      /* Skip empty lines. */
+      else if (nread > 1)
+        {
+          char *start = line;
+          char *comment = strchr (start, '#');
+          char *end;
+
+          if (comment != NULL)
+            {
+              /* Trim the comment mark.  */
+              comment = '\0';
+              end = comment;
+            }
+          else
+            {
+              /* Trim the newline.  */
+              end = start + nread;
+              if (end[-1] == '\n')
+                *--end = '\0';
+            }
+
+          /* Skip leading whitespace.  */
+          while (start < end && isspace ((unsigned char) start[0]))
+            start++;
+          /* Trim trailing whitespace.  */
+          while (start < end && isspace ((unsigned char) end[-1]))
+            *--end = '\0';
+
+          /* Only return absolute file names. */
+          if (start < end && IS_ABSOLUTE_FILE_NAME (start))
+            return start;
+        }
     }
-  return NULL;                  /* End of file. */
 }
 
 /* Rewind the shells file. */
@@ -129,37 +159,6 @@ endusershell (void)
     }
 }
 
-/* Read a line from STREAM, removing any newline at the end.
-   Place the result in *NAME, which is malloc'd
-   and/or realloc'd as necessary and can start out NULL,
-   and whose size is passed and returned in *SIZE.
-
-   Return the number of bytes placed in *NAME
-   if some nonempty sequence was found, otherwise 0.  */
-
-static idx_t
-readname (char **name, idx_t *size, FILE *stream)
-{
-  int c;
-  size_t name_index = 0;
-
-  /* Skip blank space.  */
-  while ((c = getc (stream)) != EOF && isspace (c))
-    /* Do nothing. */ ;
-
-  for (;;)
-    {
-      if (*size <= name_index)
-        *name = xpalloc (*name, size, 1, -1, sizeof **name);
-      if (c == EOF || isspace (c))
-        break;
-      (*name)[name_index++] = c;
-      c = getc (stream);
-    }
-  (*name)[name_index] = '\0';
-  return name_index;
-}
-
 #ifdef TEST
 int
 main (void)
index 2f47c62e3df826303d8045142fa48dd55de164e4..a4a40852ac8f1e579462fa2d8f41dbdba05c2aae 100644 (file)
@@ -9,7 +9,8 @@ Depends-on:
 unistd
 extensions
 fopen-safer          [test $HAVE_GETUSERSHELL = 0 || test $REPLACE_GETUSERSHELL = 1]
-unlocked-io-internal [test $HAVE_GETUSERSHELL = 0 || test $REPLACE_GETUSERSHELL = 1]
+getline              [test $HAVE_GETUSERSHELL = 0 || test $REPLACE_GETUSERSHELL = 1]
+filename             [test $HAVE_GETUSERSHELL = 0 || test $REPLACE_GETUSERSHELL = 1]
 xalloc               [test $HAVE_GETUSERSHELL = 0 || test $REPLACE_GETUSERSHELL = 1]
 
 configure.ac: