LCOV - code coverage report
Current view: top level - src/platform/posix - path.c (source / functions) Coverage Total Hit
Test: coverage-functional.info Lines: 92.3 % 26 24
Test Date: 2026-04-20 19:54:24 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /* SPDX-License-Identifier: GPL-3.0-or-later */
       2              : /* Copyright 2026 Peter Csaszar */
       3              : 
       4              : /**
       5              :  * POSIX path implementation.
       6              :  * Uses $HOME / $XDG_CACHE_HOME / $XDG_CONFIG_HOME, falling back to
       7              :  * getpwuid(getuid()) for the home directory.
       8              :  */
       9              : #include "../path.h"
      10              : #include <stdlib.h>
      11              : #include <stdio.h>
      12              : #include <string.h>
      13              : #include <pwd.h>
      14              : #include <unistd.h>
      15              : #include <sys/types.h>
      16              : 
      17              : static char g_home[4096];
      18              : static char g_cache[8192];
      19              : static char g_config[8192];
      20              : 
      21         1214 : const char *platform_home_dir(void) {
      22         1214 :     const char *h = getenv("HOME");
      23         1214 :     if (!h || !*h) {
      24            0 :         struct passwd *pw = getpwuid(getuid());
      25            0 :         if (pw) h = pw->pw_dir;
      26              :     }
      27         1214 :     if (!h) return NULL;
      28         1214 :     snprintf(g_home, sizeof(g_home), "%s", h);
      29         1214 :     return g_home;
      30              : }
      31              : 
      32           39 : const char *platform_cache_dir(void) {
      33           39 :     const char *xdg = getenv("XDG_CACHE_HOME");
      34           39 :     if (xdg && *xdg) {
      35            2 :         snprintf(g_cache, sizeof(g_cache), "%s", xdg);
      36            2 :         return g_cache;
      37              :     }
      38           37 :     const char *home = platform_home_dir();
      39           37 :     if (!home) return NULL;
      40           37 :     snprintf(g_cache, sizeof(g_cache), "%s/.cache", home);
      41           37 :     return g_cache;
      42              : }
      43              : 
      44         1889 : const char *platform_config_dir(void) {
      45         1889 :     const char *xdg = getenv("XDG_CONFIG_HOME");
      46         1889 :     if (xdg && *xdg) {
      47          712 :         snprintf(g_config, sizeof(g_config), "%s", xdg);
      48          712 :         return g_config;
      49              :     }
      50         1177 :     const char *home = platform_home_dir();
      51         1177 :     if (!home) return NULL;
      52         1177 :     snprintf(g_config, sizeof(g_config), "%s/.config", home);
      53         1177 :     return g_config;
      54              : }
        

Generated by: LCOV version 2.0-1