Change to the linux kernel coding style
[wmaker-crm.git] / util / wdread.c
Commit [+]AuthorDateLineData
543a4665 kojima2001-02-09 16:05:49 +00001/* wdread.c - read value from defaults database
2 *
3 * WindowMaker window manager
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
543a4665 kojima2001-02-09 16:05:49 +00006 * (cowardly remade from wdwrite.c; by judas@hell on Jan 26 2001)
6830b057 dan2004-10-12 21:28:27 +00007 *
543a4665 kojima2001-02-09 16:05:49 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
6830b057 dan2004-10-12 21:28:27 +000020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
543a4665 kojima2001-02-09 16:05:49 +000021 * USA.
22 */
23
24#define PROG_VERSION "wdread (Window Maker) 0.2"
25
543a4665 kojima2001-02-09 16:05:49 +000026/*
27 * WindowMaker defaults DB reader
28 */
29
543a4665 kojima2001-02-09 16:05:49 +000030#include "../src/wconfig.h"
31
32#include <stdlib.h>
33#include <string.h>
34#include <stdio.h>
35#include <unistd.h>
36
33cc542e dan2001-10-04 03:07:34 +000037#include <WINGs/WUtil.h>
38
543a4665 kojima2001-02-09 16:05:49 +000039#include <pwd.h>
40
543a4665 kojima2001-02-09 16:05:49 +000041char *ProgName;
42
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043char *gethomedir()
543a4665 kojima2001-02-09 16:05:49 +000044{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020045 char *home = getenv("HOME");
46 struct passwd *user;
47
48 if (home)
49 return home;
50
51 user = getpwuid(getuid());
52 if (!user) {
53 perror(ProgName);
54 return "/";
55 }
56 if (!user->pw_dir) {
57 return "/";
58 } else {
59 return user->pw_dir;
60 }
543a4665 kojima2001-02-09 16:05:49 +000061}
62
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020063void wAbort()
543a4665 kojima2001-02-09 16:05:49 +000064{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020065 exit(0);
543a4665 kojima2001-02-09 16:05:49 +000066}
67
68void help()
69{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020070 printf("Syntax:\n%s [OPTIONS] <domain> <option>\n", ProgName);
71 puts("");
72 puts(" --help display this help message");
73 puts(" --version output version information and exit");
74 exit(1);
543a4665 kojima2001-02-09 16:05:49 +000075}
76
543a4665 kojima2001-02-09 16:05:49 +000077int main(int argc, char **argv)
78{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020079 char path[256];
80 WMPropList *key, *value, *dict;
81 char *gsdir;
82 int i;
83
84 ProgName = argv[0];
85
86 for (i = 1; i < argc; i++) {
87 if (strcmp("--help", argv[i]) == 0) {
88 help();
89 exit(0);
90 } else if (strcmp("--version", argv[i]) == 0) {
91 puts(PROG_VERSION);
92 exit(0);
93 }
94 }
95
96 if (argc < 3) {
97 printf("%s: invalid argument format\n", ProgName);
98 printf("Try '%s --help' for more information\n", ProgName);
99 exit(1);
100 }
101
102 key = WMCreatePLString(argv[2]);
103
104 gsdir = getenv("GNUSTEP_USER_ROOT");
105 if (gsdir) {
106 strcpy(path, gsdir);
107 } else {
108 strcpy(path, gethomedir());
109 strcat(path, "/GNUstep");
110 }
111 strcat(path, "/");
112 strcat(path, DEFAULTS_DIR);
113 strcat(path, "/");
114 strcat(path, argv[1]);
115
116 if ((dict = WMReadPropListFromFile(path)) == NULL)
117 return 1; /* bad domain */
118 if ((value = WMGetFromPLDictionary(dict, key)) == NULL)
119 return 2; /* bad key */
120
121 printf("%s\n", WMGetPropListDescription(value, True));
122 return 0;
543a4665 kojima2001-02-09 16:05:49 +0000123}