]> git.tuebingen.mpg.de Git - dss.git/blobdiff - str.c
Merge topic branch t/realpath into pu
[dss.git] / str.c
diff --git a/str.c b/str.c
index 557eaa38ca6352dab2c435ae619ef0fc60700047..eeb635cfa97dd7b09096b405df651bcb52a5deb1 100644 (file)
--- a/str.c
+++ b/str.c
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 2004-2010 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* SPDX-License-Identifier: GPL-2.0 */
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -147,28 +143,27 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
                n = vsnprintf(msg, size, fmt, ap);
                va_end(ap);
                /* If that worked, return the string. */
-               if (n > -1 && n < size)
-                       break;
+               if (n < size)
+                       return msg;
                /* Else try again with more space. */
-               if (n > -1) /* glibc 2.1 */
-                       size = n + 1; /* precisely what is needed */
-               else /* glibc 2.0 */
-                       size *= 2; /* twice the old size */
+               size = n + 1; /* precisely what is needed */
                msg = dss_realloc(msg, size);
        }
-       return msg;
 }
 
-/**
+/*
  * Get the home directory of the current user.
  *
- * \return A dynamically allocated string that must be freed by the caller. If
- * the home directory could not be found, this function returns "/tmp".
+ * Returns a dynamically allocated string that must be freed by the caller. If
+ * the HOME environment variable is unset or empty, the function aborts.
  */
 __must_check __malloc char *get_homedir(void)
 {
-       struct passwd *pw = getpwuid(getuid());
-       return dss_strdup(pw? pw->pw_dir : "/tmp");
+       const char *home = getenv("HOME");
+       if (home && *home)
+               return dss_strdup(home);
+       DSS_EMERG_LOG(("fatal: HOME is unset or empty\n"));
+       exit(EXIT_FAILURE);
 }
 
 /**
@@ -200,19 +195,18 @@ int dss_atoi64(const char *str, int64_t *value)
        return 1;
 }
 
-/**
+/*
  * Get the logname of the current user.
  *
- * \return A dynamically allocated string that must be freed by the caller. On
- * errors, the string "unknown user" is returned, i.e. this function never
- * returns \p NULL.
- *
- * \sa getpwuid(3).
+ * Returns a dynamically allocated string that must be freed by the caller. On
+ * errors, the string "unknown" is returned. This function never returns NULL.
  */
 __must_check __malloc char *dss_logname(void)
 {
-       struct passwd *pw = getpwuid(getuid());
-       return dss_strdup(pw? pw->pw_name : "unknown_user");
+       const char *logname = getenv("LOGNAME");
+       if (!logname && !*logname)
+               logname = "unknown";
+       return dss_strdup(logname);
 }
 
 /**
@@ -220,20 +214,21 @@ __must_check __malloc char *dss_logname(void)
  *
  * \param args The string to be split.
  * \param argv_ptr Pointer to the list of substrings.
- * \param delim Delimiter.
  *
- * This function modifies \a args by replacing each occurrence of \a delim by
- * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
- * and these pointers are initialized to point to the broken-up substrings
- * within \a args. A pointer to this array is returned via \a argv_ptr.
+ * This function modifies the string given by the first argument by replacing
+ * all occurrences of space and '\t' characters by '\0'. A NULL-terminated
+ * array of pointers to char * is allocated dynamically, and these pointers are
+ * initialized to point to the broken-up substrings.  A pointer to this array
+ * is returned via the last argument.
  *
- * \return The number of substrings found in \a args.
+ * \return The number of substrings found.
  */
-unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
+unsigned split_args(char *args, char *** const argv_ptr)
 {
        char *p;
        char **argv;
        size_t n = 0, i, j;
+       const char delim[] = " \t";
 
        p = args + strspn(args, delim);
        for (;;) {