LCOV - code coverage report
Current view: top level - src/platform/posix - path.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 26 26
Test Date: 2026-04-20 19:54:22 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         2619 : const char *platform_home_dir(void) {
      22         2619 :     const char *h = getenv("HOME");
      23         2619 :     if (!h || !*h) {
      24            2 :         struct passwd *pw = getpwuid(getuid());
      25            2 :         if (pw) h = pw->pw_dir;
      26              :     }
      27         2619 :     if (!h) return NULL;
      28         2619 :     snprintf(g_home, sizeof(g_home), "%s", h);
      29         2619 :     return g_home;
      30              : }
      31              : 
      32          155 : const char *platform_cache_dir(void) {
      33          155 :     const char *xdg = getenv("XDG_CACHE_HOME");
      34          155 :     if (xdg && *xdg) {
      35            5 :         snprintf(g_cache, sizeof(g_cache), "%s", xdg);
      36            5 :         return g_cache;
      37              :     }
      38          150 :     const char *home = platform_home_dir();
      39          150 :     if (!home) return NULL;
      40          150 :     snprintf(g_cache, sizeof(g_cache), "%s/.cache", home);
      41          150 :     return g_cache;
      42              : }
      43              : 
      44         3967 : const char *platform_config_dir(void) {
      45         3967 :     const char *xdg = getenv("XDG_CONFIG_HOME");
      46         3967 :     if (xdg && *xdg) {
      47         1502 :         snprintf(g_config, sizeof(g_config), "%s", xdg);
      48         1502 :         return g_config;
      49              :     }
      50         2465 :     const char *home = platform_home_dir();
      51         2465 :     if (!home) return NULL;
      52         2465 :     snprintf(g_config, sizeof(g_config), "%s/.config", home);
      53         2465 :     return g_config;
      54              : }
        

Generated by: LCOV version 2.0-1