1 /* Copyright (c) 2007 Mark Nevill
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <basedir.h>
29 #include <basedir_fs.h>
30 
printAndFreeString(const char * string)31 void printAndFreeString(const char *string)
32 {
33 	printf("%s\n", string);
34 	free((char*)string);
35 }
36 
printAndFreeStringList(const char * const * strings)37 void printAndFreeStringList(const char * const *strings)
38 {
39 	const char * const *item;
40 	for (item = strings; *item; ++item)
41 	{
42 		printf("%s\n", *item);
43 		free((char*)*item);
44 	}
45 	free((const char **)strings);
46 }
47 
main(int argc,char * argv[])48 int main(int argc, char *argv[])
49 {
50 	if (argc < 3)
51 		return 1;
52 	char *datatype = argv[1];
53 	char *querytype = argv[2];
54 	if (strcmp(datatype, "data") == 0)
55 	{
56 		if (strcmp(querytype, "home") == 0)
57 			printAndFreeString(xdgDataHome(NULL));
58 		else if (strcmp(querytype, "dirs") == 0)
59 			printAndFreeStringList(xdgDataDirectories(NULL));
60 		else if (strcmp(querytype, "search") == 0)
61 			printAndFreeStringList(xdgSearchableDataDirectories(NULL));
62 		else if (strcmp(querytype, "find") == 0 && argc == 4)
63 			printAndFreeString(xdgDataFind(argv[3], NULL));
64 		else
65 			return 1;
66 	}
67 	else if (strcmp(datatype, "config") == 0)
68 	{
69 		if (strcmp(querytype, "home") == 0)
70 			printAndFreeString(xdgConfigHome(NULL));
71 		else if (strcmp(querytype, "dirs") == 0)
72 			printAndFreeStringList(xdgConfigDirectories(NULL));
73 		else if (strcmp(querytype, "search") == 0)
74 			printAndFreeStringList(xdgSearchableConfigDirectories(NULL));
75 		else if (strcmp(querytype, "find") == 0 && argc == 4)
76 			printAndFreeString(xdgConfigFind(argv[3], NULL));
77 		else
78 			return 1;
79 	}
80 	else if (strcmp(datatype, "cache") == 0)
81 	{
82 		if (strcmp(querytype, "home") == 0)
83 			printAndFreeString(xdgCacheHome(NULL));
84 		else
85 			return 1;
86 	}
87 	else if (strcmp(datatype, "runtime") == 0)
88 	{
89 		if (strcmp(querytype, "directory") == 0)
90 		{
91 			const char *rd = xdgRuntimeDirectory(NULL);
92 			if (!rd) printf("(null)\n");
93 			else printAndFreeString(rd);
94 		}
95 		else
96 			return 1;
97 	}
98 	else
99 		return 1;
100 	return 0;
101 }
102