Coverage Report

Created: 2026-10-14 08:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib-2.88.3/glib/gutilsprivate.c
Line
Count
Source
1
/* adapted from xdg-user-dir-lookup.c
2
 *
3
 * Copyright (C) 2007 Red Hat Inc.
4
 *
5
 * Permission is hereby granted, free of charge, to any person
6
 * obtaining a copy of this software and associated documentation files
7
 * (the "Software"), to deal in the Software without restriction,
8
 * including without limitation the rights to use, copy, modify, merge,
9
 * publish, distribute, sublicense, and/or sell copies of the Software,
10
 * and to permit persons to whom the Software is furnished to do so,
11
 * subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
static void
26
load_user_special_dirs_from_string (const gchar *string, const gchar *home_dir, gchar **special_dirs)
27
0
{
28
0
  gchar **lines;
29
0
  gint n_lines, i;
30
0
  size_t min_len;
31
32
0
  lines = g_strsplit (string, "\n", -1);
33
0
  n_lines = g_strv_length (lines);
34
35
0
  for (i = 0; i < n_lines; i++)
36
0
    {
37
0
      gchar *buffer = lines[i];
38
0
      gchar *d, *p;
39
0
      size_t len;
40
0
      gboolean is_relative = FALSE;
41
0
      GUserDirectory directory;
42
43
      /* Remove newline at end */
44
0
      len = strlen (buffer);
45
0
      if (len > 0 && buffer[len - 1] == '\n')
46
0
        buffer[len - 1] = 0;
47
48
0
      p = buffer;
49
0
      while (*p == ' ' || *p == '\t')
50
0
        p++;
51
52
0
      if (strncmp (p, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
53
0
        {
54
0
          directory = G_USER_DIRECTORY_DESKTOP;
55
0
          p += strlen ("XDG_DESKTOP_DIR");
56
0
        }
57
0
      else if (strncmp (p, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
58
0
        {
59
0
          directory = G_USER_DIRECTORY_DOCUMENTS;
60
0
          p += strlen ("XDG_DOCUMENTS_DIR");
61
0
        }
62
0
      else if (strncmp (p, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
63
0
        {
64
0
          directory = G_USER_DIRECTORY_DOWNLOAD;
65
0
          p += strlen ("XDG_DOWNLOAD_DIR");
66
0
        }
67
0
      else if (strncmp (p, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
68
0
        {
69
0
          directory = G_USER_DIRECTORY_MUSIC;
70
0
          p += strlen ("XDG_MUSIC_DIR");
71
0
        }
72
0
      else if (strncmp (p, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
73
0
        {
74
0
          directory = G_USER_DIRECTORY_PICTURES;
75
0
          p += strlen ("XDG_PICTURES_DIR");
76
0
        }
77
0
      else if (strncmp (p, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
78
0
        {
79
0
          directory = G_USER_DIRECTORY_PUBLIC_SHARE;
80
0
          p += strlen ("XDG_PUBLICSHARE_DIR");
81
0
        }
82
0
      else if (strncmp (p, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
83
0
        {
84
0
          directory = G_USER_DIRECTORY_TEMPLATES;
85
0
          p += strlen ("XDG_TEMPLATES_DIR");
86
0
        }
87
0
      else if (strncmp (p, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
88
0
        {
89
0
          directory = G_USER_DIRECTORY_VIDEOS;
90
0
          p += strlen ("XDG_VIDEOS_DIR");
91
0
        }
92
0
      else
93
0
        continue;
94
95
0
      while (*p == ' ' || *p == '\t')
96
0
        p++;
97
98
0
      if (*p != '=')
99
0
        continue;
100
0
      p++;
101
102
0
      while (*p == ' ' || *p == '\t')
103
0
        p++;
104
105
0
      if (*p != '"')
106
0
        continue;
107
0
      p++;
108
109
0
      if (strncmp (p, "$HOME", 5) == 0)
110
0
        {
111
0
          p += 5;
112
0
          if (*p != '/' && *p != '"')
113
0
            continue;
114
0
          is_relative = TRUE;
115
0
        }
116
0
      else if (*p != '/')
117
0
        continue;
118
119
0
      d = strrchr (p, '"');
120
0
      if (d < p)
121
0
        continue;
122
0
      *d = 0;
123
124
0
      d = p;
125
126
      /* remove trailing slashes, but keep first slash in absolute path */
127
0
      min_len = is_relative ? 0 : 1;
128
0
      for (len = strlen (d); len > min_len && d[len - 1] == '/'; len--)
129
0
        d[len - 1] = 0;
130
131
      /* Duplicates override the previous value. This is not explicit in the
132
       * spec, but given that the spec[1] is designed to allow user-dirs.dirs to
133
       * be sourced in a shell, overriding is the behaviour that would imply.
134
       *
135
       * [1]: https://www.freedesktop.org/wiki/Software/xdg-user-dirs/ */
136
0
      g_clear_pointer (&special_dirs[directory], g_free);
137
138
0
      if (is_relative)
139
0
        special_dirs[directory] = g_build_filename (home_dir, d, NULL);
140
0
      else
141
0
        special_dirs[directory] = g_strdup (d);
142
0
    }
143
144
0
  g_strfreev (lines);
145
0
}