]> git.ipfire.org Git - ipfire-2.x.git/blame - src/setup/timezone.c
core130: Ship SSH Agent Forwarding changes
[ipfire-2.x.git] / src / setup / timezone.c
CommitLineData
c6fbd6ea
MT
1/* SmoothWall setup program.
2 *
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
5 *
6 * (c) Lawrence Manning, 2001
7 * Stuff for setting the timezone.
8 *
9 * $Id: timezone.c,v 1.4.2.1 2004/04/14 22:05:41 gespinasse Exp $
10 *
11 */
12
e1457ba0
MT
13// Translation
14#include <libintl.h>
15#define _(x) dgettext("setup", x)
16
c6fbd6ea
MT
17#include "setup.h"
18
19extern FILE *flog;
20extern char *mylog;
21
c6fbd6ea
MT
22extern int automode;
23
24#define MAX_FILENAMES 5000
25#define ZONEFILES "/usr/share/zoneinfo/posix"
26
27static int filenamecount;
28static char *filenames[MAX_FILENAMES];
29static char *displaynames[MAX_FILENAMES];
30
31static int process(char *prefix, char *path);
32static int cmp(const void *s1, const void *s2);
33
34int handletimezone(void)
35{
36 int c;
37 int choice;
38 char *temp;
39 struct keyvalue *kv = initkeyvalues();
40 int rc;
41 int result;
42 char timezone[STRING_SIZE];
43
44 filenamecount = 0;
45
46 process(ZONEFILES, "");
47 filenames[filenamecount] = NULL;
48 qsort(filenames, filenamecount, sizeof(char *), cmp);
49
50 for (c = 0; filenames[c]; c++)
51 {
52 displaynames[c] = malloc(STRING_SIZE);
53 if ((temp = strstr(filenames[c], ZONEFILES)))
54 strcpy(displaynames[c], temp + strlen(ZONEFILES) + 1);
55 else
56 strcpy(displaynames[c], filenames[c]);
57 }
58 displaynames[c] = NULL;
59
60 if (!(readkeyvalues(kv, CONFIG_ROOT "/main/settings")))
61 {
62 freekeyvalues(kv);
e1457ba0 63 errorbox(_("Unable to open settings file"));
c6fbd6ea
MT
64 return 0;
65 }
66
67 strcpy(timezone, ZONEFILES "/Europe/Berlin");
68 findkey(kv, "TIMEZONE", timezone);
69
70 choice = 0;
71 for (c = 0; filenames[c]; c++)
72 {
73 if (strcmp(timezone, filenames[c]) == 0)
74 choice = c;
75 }
76
e1457ba0
MT
77 rc = newtWinMenu(_("Timezone"), _("Choose the timezone you are in from the list below."),
78 50, 5, 5, 6, displaynames, &choice, _("OK"), _("Cancel"), NULL);
c6fbd6ea
MT
79
80 strcpy(timezone, filenames[choice]);
81
82 if (rc != 2)
83 {
84 replacekeyvalue(kv, "TIMEZONE", timezone);
85 writekeyvalues(kv, CONFIG_ROOT "/main/settings");
86 unlink("/etc/localtime");
87 link(timezone, "/etc/localtime");
88 result = 1;
89 }
90 else
91 result = 0;
92
93 for (c = 0; filenames[c]; c++)
94 {
95 free(filenames[c]);
96 free(displaynames[c]);
97 }
98 freekeyvalues(kv);
99
100 return result;
101}
102
103static int process(char *prefix, char *path)
104{
105 DIR *dir;
106 struct dirent *de;
107 char newpath[PATH_MAX];
108
109 snprintf(newpath, PATH_MAX, "%s%s", prefix, path);
110
111 if (!(dir = opendir(newpath)))
112 {
113 if (filenamecount > MAX_FILENAMES)
114 return 1;
115
116 filenames[filenamecount] = (char *) strdup(newpath);
117 filenamecount++;
118 return 0;
119 }
120
121 while ((de = readdir(dir)))
122 {
123 if (de->d_name[0] == '.') continue;
124 snprintf(newpath, PATH_MAX, "%s/%s", path, de->d_name);
125 process(prefix, newpath);
126 }
127 closedir(dir);
128
129 return 1;
130}
131
132/* Small wrapper for use with qsort(). */
133static int cmp(const void *s1, const void *s2)
134{
135 return (strcmp(* (char **) s1, * (char **) s2));
136}