bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* filenamecat-lgpl: set errno upon failure
@ 2020-06-27  0:41 Bruno Haible
  2020-06-27  0:46 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Bruno Haible @ 2020-06-27  0:41 UTC (permalink / raw)
  To: bug-gnulib

POSIX <https://pubs.opengroup.org/onlinepubs/9699919799/functions/opendir.html>
says that when opendir() fails, it return NULL with errno set.

To implement this in gnulib for native Windows,
  - the function _gl_register_fd must set errno upon failure,
  - therefore get_name must set errno upon failure,
  - therefore mfile_name_concat must set errno upon failure.


2020-06-26  Bruno Haible  <bruno@clisp.org>

	filenamecat-lgpl: Set errno upon failure.
	* lib/filenamecat-lgpl.c (mfile_name_concat): Document the failure
	return convention.
	* modules/filenamecat-lgpl (Depends-on): Add malloc-posix.

diff --git a/lib/filenamecat-lgpl.c b/lib/filenamecat-lgpl.c
index 6f666f2..d97abfa 100644
--- a/lib/filenamecat-lgpl.c
+++ b/lib/filenamecat-lgpl.c
@@ -41,7 +41,7 @@
    *BASE_IN_RESULT to point to the copy of BASE at the end of the
    returned concatenation.
 
-   Return NULL if malloc fails.  */
+   If malloc fails, return NULL with errno set.  */
 
 char *
 mfile_name_concat (char const *dir, char const *base, char **base_in_result)
@@ -68,20 +68,22 @@ mfile_name_concat (char const *dir, char const *base, char **base_in_result)
     }
 
   char *p_concat = malloc (dirlen + (sep != '\0')  + baselen + 1);
-  char *p;
-
   if (p_concat == NULL)
     return NULL;
 
-  p = mempcpy (p_concat, dir, dirlen);
-  *p = sep;
-  p += sep != '\0';
+  {
+    char *p;
+
+    p = mempcpy (p_concat, dir, dirlen);
+    *p = sep;
+    p += sep != '\0';
 
-  if (base_in_result)
-    *base_in_result = p;
+    if (base_in_result)
+      *base_in_result = p;
 
-  p = mempcpy (p, base, baselen);
-  *p = '\0';
+    p = mempcpy (p, base, baselen);
+    *p = '\0';
+  }
 
   return p_concat;
 }
diff --git a/modules/filenamecat-lgpl b/modules/filenamecat-lgpl
index 09965a1..a6bf60b 100644
--- a/modules/filenamecat-lgpl
+++ b/modules/filenamecat-lgpl
@@ -9,6 +9,7 @@ m4/filenamecat.m4
 Depends-on:
 c99
 dirname-lgpl
+malloc-posix
 
 configure.ac:
 gl_FILE_NAME_CONCAT_LGPL



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: filenamecat-lgpl: set errno upon failure
  2020-06-27  0:41 filenamecat-lgpl: set errno upon failure Bruno Haible
@ 2020-06-27  0:46 ` Bruno Haible
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2020-06-27  0:46 UTC (permalink / raw)
  To: bug-gnulib

Based on this patch, _gl_register_fd is now guaranteed to set errno upon
failure.


2020-06-26  Bruno Haible  <bruno@clisp.org>

	fchdir: Improve documentation.
	* lib/fchdir.c (get_name, _gl_register_fd): Document the failure return
	convention.

diff --git a/lib/fchdir.c b/lib/fchdir.c
index 20ecd3c..1fa9a7e 100644
--- a/lib/fchdir.c
+++ b/lib/fchdir.c
@@ -84,7 +84,8 @@ ensure_dirs_slot (size_t fd)
   return true;
 }
 
-/* Return an absolute name of DIR in malloc'd storage.  */
+/* Return an absolute name of DIR in malloc'd storage.
+   Upon failure, return NULL with errno set.  */
 static char *
 get_name (char const *dir)
 {
@@ -125,8 +126,8 @@ _gl_unregister_fd (int fd)
 /* Mark FD as visiting FILENAME.  FD must be non-negative, and refer
    to an open file descriptor.  If REPLACE_OPEN_DIRECTORY is non-zero,
    this should only be called if FD is visiting a directory.  Close FD
-   and return -1 if there is insufficient memory to track the
-   directory name; otherwise return FD.  */
+   and return -1 with errno set if there is insufficient memory to track
+   the directory name; otherwise return FD.  */
 int
 _gl_register_fd (int fd, const char *filename)
 {



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-06-27  0:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-27  0:41 filenamecat-lgpl: set errno upon failure Bruno Haible
2020-06-27  0:46 ` Bruno Haible

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 read-only IMAP folder(s) and NNTP newsgroup(s).