aboutsummaryrefslogtreecommitdiffstats
path: root/libfonts_gethome__.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-01-08 16:23:56 +0100
committerMattias Andrée <maandree@kth.se>2023-01-08 16:23:56 +0100
commit627e5b9789c663343c74dac6535724ddc63aae1c (patch)
treeaf78890b546b774267ff82520f0756dd159f8d86 /libfonts_gethome__.c
parentImprove libfonts_get_default_font_name and draft implementation of libfonts_get_default_font (diff)
downloadlibfonts-627e5b9789c663343c74dac6535724ddc63aae1c.tar.gz
libfonts-627e5b9789c663343c74dac6535724ddc63aae1c.tar.bz2
libfonts-627e5b9789c663343c74dac6535724ddc63aae1c.tar.xz
Add environment spoofing
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libfonts_gethome__.c')
-rw-r--r--libfonts_gethome__.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/libfonts_gethome__.c b/libfonts_gethome__.c
new file mode 100644
index 0000000..902029f
--- /dev/null
+++ b/libfonts_gethome__.c
@@ -0,0 +1,40 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+char *
+libfonts_gethome__(struct libfonts_context *ctx)
+{
+ uid_t uid = (ctx && ctx->use_context_uid) ? ctx->uid : getuid();
+ size_t size = 512;
+ char *buf = NULL, *new;
+ char *ret = NULL;
+ struct passwd pw, *pwp;
+ int err;
+
+ goto beginning;
+
+ do {
+ if (size > SIZE_MAX / 2)
+ goto enomem;
+ size *= 2;
+ beginning:
+ new = realloc(buf, size);
+ if (!new) {
+ enomem:
+ free(buf);
+ errno = ENOMEM;
+ return NULL;
+ }
+ buf = new;
+ } while ((err = getpwuid_r(uid, &pw, buf, size, &pwp)) == ERANGE);
+ if (err) {
+ free(buf);
+ return NULL;
+ }
+
+ if (pwp && pwp->pw_dir)
+ ret = strdup(pwp->pw_dir);
+ free(buf);
+ return ret;
+}