Initial revision
[wmaker-crm.git] / util / wdwrite.c
blob80383b2a8565e768981cf6eee45b5bc77922be16
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.
25 * WindowMaker defaults DB writer
29 #include "../src/wconfig.h"
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <unistd.h>
36 #include <proplist.h>
37 #include <pwd.h>
40 char *ProgName;
42 char*
43 gethomedir()
45 char *home = getenv("HOME");
46 struct passwd *user;
48 if (home)
49 return home;
51 user = getpwuid(getuid());
52 if (!user) {
53 perror(ProgName);
54 return "/";
56 if (!user->pw_dir) {
57 return "/";
58 } else {
59 return user->pw_dir;
65 void wAbort()
67 exit(0);
70 void help()
72 printf("Syntax:\n%s <domain> <option> <value>\n", ProgName);
73 exit(1);
77 int main(int argc, char **argv)
79 char path[256];
80 proplist_t dom, key, value, dict;
81 char *gsdir;
83 ProgName = argv[0];
85 if (argc<4) {
86 help();
89 dom = PLMakeString(argv[1]);
90 key = PLMakeString(argv[2]);
91 value = PLGetProplistWithDescription(argv[3]);
92 if (!value) {
93 printf("%s:syntax error in value \"%s\"", ProgName, argv[3]);
94 exit(1);
96 gsdir = getenv("GNUSTEP_USER_ROOT");
97 if (gsdir) {
98 strcpy(path, gsdir);
99 } else {
100 strcpy(path, gethomedir());
101 strcat(path, "/GNUstep");
103 strcat(path, "/");
104 strcat(path, DEFAULTS_DIR);
105 strcat(path, "/");
106 strcat(path, argv[1]);
108 dict = PLGetProplistWithPath(path);
109 if (!dict) {
110 dict = PLMakeDictionaryFromEntries(key, value, NULL);
111 PLSetFilename(dict, PLMakeString(path));
112 } else {
113 PLRemoveDictionaryEntry(dict, key);
114 PLInsertDictionaryEntry(dict, key, value);
117 PLSave(dict, YES);
119 return 0;