Change to the linux kernel coding style
[wmaker-crm.git] / util / wdwrite.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* wdwrite.c - write key/value to 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
6830b057 dan2004-10-12 21:28:27 +00006 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00007 * 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.
11 *
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.
16 *
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
6830b057 dan2004-10-12 21:28:27 +000019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
9d2e6ef9 scottc1998-09-29 22:36:29 +000020 * USA.
21 */
22
931a37b1 dan1999-01-29 08:11:17 +000023#define PROG_VERSION "wdwrite (Window Maker) 0.2"
24
9d2e6ef9 scottc1998-09-29 22:36:29 +000025/*
26 * WindowMaker defaults DB writer
27 */
28
9d2e6ef9 scottc1998-09-29 22:36:29 +000029#include "../src/wconfig.h"
30
31#include <stdlib.h>
32#include <string.h>
33#include <stdio.h>
34#include <unistd.h>
35
33cc542e dan2001-10-04 03:07:34 +000036#include <WINGs/WUtil.h>
37
9d2e6ef9 scottc1998-09-29 22:36:29 +000038#include <pwd.h>
39
9d2e6ef9 scottc1998-09-29 22:36:29 +000040char *ProgName;
41
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042char *gethomedir()
9d2e6ef9 scottc1998-09-29 22:36:29 +000043{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020044 char *home = getenv("HOME");
45 struct passwd *user;
46
47 if (home)
48 return home;
49
50 user = getpwuid(getuid());
51 if (!user) {
52 perror(ProgName);
53 return "/";
54 }
55 if (!user->pw_dir) {
56 return "/";
57 } else {
58 return user->pw_dir;
59 }
9d2e6ef9 scottc1998-09-29 22:36:29 +000060}
61
9d2e6ef9 scottc1998-09-29 22:36:29 +000062void wAbort()
63{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020064 exit(0);
9d2e6ef9 scottc1998-09-29 22:36:29 +000065}
66
67void help()
68{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020069 printf("Syntax:\n%s [OPTIONS] <domain> <option> <value>\n", ProgName);
70 puts("");
71 puts(" --help display this help message");
72 puts(" --version output version information and exit");
73 exit(1);
9d2e6ef9 scottc1998-09-29 22:36:29 +000074}
75
9d2e6ef9 scottc1998-09-29 22:36:29 +000076int main(int argc, char **argv)
77{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020078 char *path;
79 WMPropList *dom, *key, *value, *dict;
80 char *gsdir;
81 int i;
82
83 ProgName = argv[0];
84
85 for (i = 1; i < argc; i++) {
86 if (strcmp("--help", argv[i]) == 0) {
87 help();
88 exit(0);
89 } else if (strcmp("--version", argv[i]) == 0) {
90 puts(PROG_VERSION);
91 exit(0);
92 }
93 }
94
95 if (argc < 4) {
96 printf("%s: invalid argument format\n", ProgName);
97 printf("Try '%s --help' for more information\n", ProgName);
98 exit(1);
99 }
100
101 dom = WMCreatePLString(argv[1]);
102 key = WMCreatePLString(argv[2]);
103 value = WMCreatePropListFromDescription(argv[3]);
104 if (!value) {
105 printf("%s:syntax error in value \"%s\"", ProgName, argv[3]);
106 exit(1);
107 }
108 gsdir = getenv("GNUSTEP_USER_ROOT");
109 if (gsdir) {
110 path = wstrdup(gsdir);
111 } else {
112 path = wstrdup(gethomedir());
113 path = wstrappend(path, "/GNUstep");
114 }
115 path = wstrappend(path, "/");
116 path = wstrappend(path, DEFAULTS_DIR);
117 path = wstrappend(path, "/");
118 path = wstrappend(path, argv[1]);
119
120 dict = WMReadPropListFromFile(path);
121 if (!dict) {
122 dict = WMCreatePLDictionary(key, value, NULL);
123 } else {
124 WMPutInPLDictionary(dict, key, value);
125 }
126
127 WMWritePropListToFile(dict, path, True);
128 wfree(path);
129
130 return 0;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000131}