ref: 4ddf15e2297213dc199c80371ef96fc700fe936f
parent: 41f9e6315b75a8ea422d1c0dbf0ed1999b80a7eb
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Mar 15 12:08:58 EDT 2021
getenv: map XDG_* and USER variables to something
--- a/libnpe/getenv.c
+++ b/libnpe/getenv.c
@@ -2,6 +2,20 @@
#undef getenv
+static char *
+home(char *subpath)
+{
+ char *s, *e;
+
+ if((s = getenv("home")) != nil){
+ e = cleanname(smprint("%s/%s", s, subpath));
+ free(s);
+ }else
+ e = strdup("/tmp");
+
+ return e;
+}
+
char *
npe_getenv(char *s)
{
@@ -8,7 +22,22 @@
static char *e;
free(e);
- e = getenv(strcmp(s, "HOME") == 0 ? "home" : s);
+ e = nil;
+
+ if((e = getenv(s)) != nil)
+ return e;
+
+ if(strcmp(s, "XDG_RUNTIME_DIR") == 0 || strcmp(s, "XDG_CACHE_HOME") == 0)
+ return "/tmp";
+ if(strcmp(s, "XDG_CONFIG_HOME") == 0 || strcmp(s, "XDG_DATA_HOME") == 0)
+ return home("lib");
+
+ if(strcmp(s, "HOME") == 0)
+ s = "home";
+ else if(strcmp(s, "USER") == 0)
+ s = "user";
+
+ e = getenv(s);
return e;
}