[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: [Libguestfs] [PATCH nbdkit 05/10] freebsd: Provide alternative for glibc get_current_dir_name function.



On 08/12/2018 03:51 AM, Richard W.M. Jones wrote:
---
  configure.ac |  4 ++++
  src/utils.c  | 13 +++++++++++++
  2 files changed, 17 insertions(+)

+#ifndef HAVE_GET_CURRENT_DIR_NAME
+static char *
+get_current_dir_name (void)
+{
+  char *ret = malloc (PATH_MAX);
+
+  ret = getcwd (ret, PATH_MAX);
+  ret = realloc (ret, strlen (ret) + 1);
+  return ret;

PATH_MAX need not be defined (think GNU Hurd, for example), or may be insanely large where you are wasting time overallocating then shrinking in the common case. A more robust implementation starts with a smaller size, then repeatedly grows the buffer as long as getcwd() fails with ERANGE. Or, you could use realpath(".", NULL), to get an equivalent (but not necessarily identical) name, with the malloc() taken care of on your behalf.

But I don't think we need to worry about that for BSD, and that your implementation works fine for now.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]