]> www.fi.muni.cz Git - evince.git/blob - shell/xdg-user-dir-lookup.c
669fe2f01985537db86d996c0b0b3aae28b67e39
[evince.git] / shell / xdg-user-dir-lookup.c
1 /*
2   This file is not licenced under the GPL like the rest of the code.
3   Its is under the MIT license, to encourage reuse by cut-and-paste.
4
5   Copyright (c) 2007 Red Hat, inc
6
7   Permission is hereby granted, free of charge, to any person
8   obtaining a copy of this software and associated documentation files
9   (the "Software"), to deal in the Software without restriction,
10   including without limitation the rights to use, copy, modify, merge,
11   publish, distribute, sublicense, and/or sell copies of the Software,
12   and to permit persons to whom the Software is furnished to do so,
13   subject to the following conditions: 
14
15   The above copyright notice and this permission notice shall be
16   included in all copies or substantial portions of the Software. 
17
18   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25   SOFTWARE.
26 */
27
28 #include <config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33
34 char *
35 xdg_user_dir_lookup (const char *type)
36 {
37   FILE *file;
38   char *home_dir, *config_home, *config_file;
39   char buffer[512];
40   char *user_dir;
41   char *p, *d;
42   int len;
43   int relative;
44   
45   home_dir = getenv ("HOME");
46
47   if (home_dir == NULL)
48     return strdup ("/tmp");
49
50   config_home = getenv ("XDG_CONFIG_HOME");
51   if (config_home == NULL || config_home[0] == 0)
52     {
53       config_file = malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1);
54       strcpy (config_file, home_dir);
55       strcat (config_file, "/.config/user-dirs.dirs");
56     }
57   else
58     {
59       config_file = malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1);
60       strcpy (config_file, config_home);
61       strcat (config_file, "/user-dirs.dirs");
62     }
63
64   file = fopen (config_file, "r");
65   free (config_file);
66   if (file == NULL)
67     goto error;
68
69   user_dir = NULL;
70   while (fgets (buffer, sizeof (buffer), file))
71     {
72       /* Remove newline at end */
73       len = strlen (buffer);
74       if (len > 0 && buffer[len-1] == '\n')
75         buffer[len-1] = 0;
76       
77       p = buffer;
78       while (*p == ' ' || *p == '\t')
79         p++;
80       
81       if (strncmp (p, "XDG_", 4) != 0)
82         continue;
83       p += 4;
84       if (strncmp (p, type, strlen (type)) != 0)
85         continue;
86       p += strlen (type);
87       if (strncmp (p, "_DIR", 4) != 0)
88         continue;
89       p += 4;
90
91       while (*p == ' ' || *p == '\t')
92         p++;
93
94       if (*p != '=')
95         continue;
96       p++;
97       
98       while (*p == ' ' || *p == '\t')
99         p++;
100
101       if (*p != '"')
102         continue;
103       p++;
104       
105       relative = 0;
106       if (strncmp (p, "$HOME/", 6) == 0)
107         {
108           p += 6;
109           relative = 1;
110         }
111       else if (*p != '/')
112         continue;
113       
114       if (relative)
115         {
116           user_dir = malloc (strlen (home_dir) + 1 + strlen (p) + 1);
117           strcpy (user_dir, home_dir);
118           strcat (user_dir, "/");
119         }
120       else
121         {
122           user_dir = malloc (strlen (p) + 1);
123           *user_dir = 0;
124         }
125       
126       d = user_dir + strlen (user_dir);
127       while (*p && *p != '"')
128         {
129           if ((*p == '\\') && (*(p+1) != 0))
130             p++;
131           *d++ = *p++;
132         }
133       *d = 0;
134     }  
135   fclose (file);
136
137   if (user_dir)
138     return user_dir;
139
140  error:
141   /* Special case desktop for historical compatibility */
142   if (strcmp (type, "DESKTOP") == 0)
143     {
144       user_dir = malloc (strlen (home_dir) + strlen ("/Desktop") + 1);
145       strcpy (user_dir, home_dir);
146       strcat (user_dir, "/Desktop");
147       return user_dir;
148     }
149   else
150     return strdup (home_dir);
151 }
152