]> www.fi.muni.cz Git - evince.git/blob - shell/xdg-user-dir-lookup.c
00bcefa6a76646adc58b69397312416b80415caa
[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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32
33 char *
34 xdg_user_dir_lookup (const char *type)
35 {
36   FILE *file;
37   char *home_dir, *config_home, *config_file;
38   char buffer[512];
39   char *user_dir;
40   char *p, *d;
41   int len;
42   int relative;
43   
44   home_dir = getenv ("HOME");
45
46   if (home_dir == NULL)
47     return strdup ("/tmp");
48
49   config_home = getenv ("XDG_CONFIG_HOME");
50   if (config_home == NULL || config_home[0] == 0)
51     {
52       config_file = malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1);
53       strcpy (config_file, home_dir);
54       strcat (config_file, "/.config/user-dirs.dirs");
55     }
56   else
57     {
58       config_file = malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1);
59       strcpy (config_file, config_home);
60       strcat (config_file, "/user-dirs.dirs");
61     }
62
63   file = fopen (config_file, "r");
64   free (config_file);
65   if (file == NULL)
66     goto error;
67
68   user_dir = NULL;
69   while (fgets (buffer, sizeof (buffer), file))
70     {
71       /* Remove newline at end */
72       len = strlen (buffer);
73       if (len > 0 && buffer[len-1] == '\n')
74         buffer[len-1] = 0;
75       
76       p = buffer;
77       while (*p == ' ' || *p == '\t')
78         p++;
79       
80       if (strncmp (p, "XDG_", 4) != 0)
81         continue;
82       p += 4;
83       if (strncmp (p, type, strlen (type)) != 0)
84         continue;
85       p += strlen (type);
86       if (strncmp (p, "_DIR", 4) != 0)
87         continue;
88       p += 4;
89
90       while (*p == ' ' || *p == '\t')
91         p++;
92
93       if (*p != '=')
94         continue;
95       p++;
96       
97       while (*p == ' ' || *p == '\t')
98         p++;
99
100       if (*p != '"')
101         continue;
102       p++;
103       
104       relative = 0;
105       if (strncmp (p, "$HOME/", 6) == 0)
106         {
107           p += 6;
108           relative = 1;
109         }
110       else if (*p != '/')
111         continue;
112       
113       if (relative)
114         {
115           user_dir = malloc (strlen (home_dir) + 1 + strlen (p) + 1);
116           strcpy (user_dir, home_dir);
117           strcat (user_dir, "/");
118         }
119       else
120         {
121           user_dir = malloc (strlen (p) + 1);
122           *user_dir = 0;
123         }
124       
125       d = user_dir + strlen (user_dir);
126       while (*p && *p != '"')
127         {
128           if ((*p == '\\') && (*(p+1) != 0))
129             p++;
130           *d++ = *p++;
131         }
132       *d = 0;
133     }  
134   fclose (file);
135
136   if (user_dir)
137     return user_dir;
138
139  error:
140   /* Special case desktop for historical compatibility */
141   if (strcmp (type, "DESKTOP") == 0)
142     {
143       user_dir = malloc (strlen (home_dir) + strlen ("/Desktop") + 1);
144       strcpy (user_dir, home_dir);
145       strcat (user_dir, "/Desktop");
146       return user_dir;
147     }
148   else
149     return strdup (home_dir);
150 }
151