small update
[wmaker-crm.git] / util / wdwrite.c
blob571699e3792c5b445dd179bfb3014bcabebab378
1 /* wdwrite.c - write key/value to defaults database
3 * WindowMaker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #define PROG_VERSION "wdwrite (Window Maker) 0.2"
27 * WindowMaker defaults DB writer
31 #include "../src/wconfig.h"
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <unistd.h>
38 #include <WINGs/WUtil.h>
40 #include <pwd.h>
43 char *ProgName;
45 char*
46 gethomedir()
48 char *home = getenv("HOME");
49 struct passwd *user;
51 if (home)
52 return home;
54 user = getpwuid(getuid());
55 if (!user) {
56 perror(ProgName);
57 return "/";
59 if (!user->pw_dir) {
60 return "/";
61 } else {
62 return user->pw_dir;
68 void wAbort()
70 exit(0);
73 void help()
75 printf("Syntax:\n%s [OPTIONS] <domain> <option> <value>\n", ProgName);
76 puts("");
77 puts(" --help display this help message");
78 puts(" --version output version information and exit");
79 exit(1);
83 int main(int argc, char **argv)
85 char *path;
86 WMPropList *dom, *key, *value, *dict;
87 char *gsdir;
88 int i;
90 ProgName = argv[0];
92 for (i = 1; i < argc; i++) {
93 if (strcmp("--help", argv[i])==0) {
94 help();
95 exit(0);
96 } else if (strcmp("--version", argv[i])==0) {
97 puts(PROG_VERSION);
98 exit(0);
102 if (argc<4) {
103 printf("%s: invalid argument format\n", ProgName);
104 printf("Try '%s --help' for more information\n", ProgName);
105 exit(1);
108 dom = WMCreatePLString(argv[1]);
109 key = WMCreatePLString(argv[2]);
110 value = WMCreatePropListFromDescription(argv[3]);
111 if (!value) {
112 printf("%s:syntax error in value \"%s\"", ProgName, argv[3]);
113 exit(1);
115 gsdir = getenv("GNUSTEP_USER_ROOT");
116 if (gsdir) {
117 path = wstrdup(gsdir);
118 } else {
119 path = wstrdup(gethomedir());
120 path = wstrappend(path, "/GNUstep");
122 path = wstrappend(path, "/");
123 path = wstrappend(path, DEFAULTS_DIR);
124 path = wstrappend(path, "/");
125 path = wstrappend(path, argv[1]);
127 dict = WMReadPropListFromFile(path);
128 if (!dict) {
129 dict = WMCreatePLDictionary(key, value, NULL);
130 } else {
131 WMPutInPLDictionary(dict, key, value);
134 WMWritePropListToFile(dict, path, True);
135 wfree(path);
137 return 0;