installer: Correct typo in a comment.
[dragonfly.git] / contrib / bsdinstaller-1.1.6 / src / backend / installer / fn_zonetab.c
1 /*
2  * Copyright (c)2004 The DragonFly Project. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *   Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  *   Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  *   Neither the name of the DragonFly Project nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 /*
35  * fn_parsers.c
36  * Installer Function : Parse /usr/share/zoneinfo/zone.tab.
37  * $Id: fn_zonetab.c,v 1.3 2004/07/16 21:04:20 adonijah Exp $
38  */
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "fn_zonetab.h"
47
48 char *
49 zt_readfile(char *buf, int len, FILE *f) {
50         int i;
51         char *cs;
52         
53         cs = buf;
54         while (--len > 0 && (i = getc(f)) != EOF) 
55                 *(cs++) = i;
56         *cs = '\0';
57         return((i == EOF && buf == cs) ? NULL : buf);
58 }
59
60 void
61 zt_get_token(struct zt_parse *zp) {
62         int i = 0;
63
64         while (*(zp->buf) != '\0' && *(zp->buf) != '\n' && *(zp->buf) != '\t') {
65                 switch (*(zp->buf)) {
66                 case '#':
67                         while (*(zp->buf) != '\n')
68                                 zp->buf++;
69                         zp->buf++;
70                         break;
71                 default:
72                         while (*(zp->buf) != '\n' && *(zp->buf) != '\t' && 
73                             *(zp->buf) != '\0') {
74                                 if (*(zp->buf) == '/' && zp->state == ZT_REGION) {
75                                         zp->tok[i] = '\0';
76                                         return;
77                                 }
78                                 zp->tok[i++] = *(zp->buf++);
79                         }
80                         zp->tok[i] = '\0';
81                         if (zp->state == ZT_LOCALE && *(zp->buf) == '\n')
82                                 zp->state = ZT_LOCALE_ENDL;
83                         else if (*(zp->buf) == '\0')
84                                 zp->state = ZT_NOTOK;
85                         break;
86                 }
87         }
88 }
89
90 void
91 zt_parse(struct zonetab *head) {
92         FILE *zone_tab;
93         struct stat sb;
94         struct zonetab *c;
95         struct zt_parse zp;
96         char *file;
97         int i;
98
99         zone_tab = fopen(ZONETAB_FILE, "r");
100         stat(ZONETAB_FILE, &sb);
101         file = malloc(sb.st_size + 1);
102         file = zt_readfile(file, sb.st_size, zone_tab);
103         zp.buf = file;
104         
105         zp.state = ZT_CC;
106         i = 0;
107         c = head;
108         
109         do {
110                 zt_get_token(&zp);
111                 switch (zp.state) {
112                 case ZT_CC:
113                         c->zt_cc = strdup(zp.tok);
114                         zp.state = ZT_COORDS;
115                         break;
116                 case ZT_COORDS:
117                         c->zt_coords = strdup(zp.tok);
118                         zp.state = ZT_REGION;
119                         break;
120                 case ZT_REGION:
121                         c->zt_region = strdup(zp.tok);
122                         zp.state = ZT_LOCALE;
123                         break;
124                 case ZT_LOCALE:
125                         c->zt_locale = strdup(zp.tok);
126                         zp.state = ZT_COMMENTS;
127                         break;
128                 case ZT_LOCALE_ENDL:
129                         c->zt_locale = strdup(zp.tok);
130                         c->zt_comments = NULL;
131                         c->next = malloc(sizeof(struct zonetab));
132                         c = c->next;
133                         zp.state = ZT_CC;
134                         break;
135                 case ZT_COMMENTS:
136                         c->zt_comments = strdup(zp.tok);
137                         c->next = malloc(sizeof(struct zonetab));
138                         c = c->next;
139                         zp.state = ZT_CC;
140                         break;
141                 case ZT_NOTOK:
142                         goto done;
143                 }
144                 zp.buf++;
145         } while (1);
146
147 done:
148         c->next = NULL;
149         free(file);
150 }
151
152 void
153 fn_zt_list_free(struct zonetab *zt) {
154         struct zonetab *p;
155         for (p = zt; p->next != NULL; p = p->next) {
156                 free(p->zt_cc);
157                 free(p->zt_coords);
158                 free(p->zt_region);
159                 free(p->zt_locale);
160                 if (p->zt_comments != NULL)
161                         free(p->zt_comments);
162         }
163 }
164
165 struct zonetab *
166 fn_zt_list(void) {
167         struct zonetab **zt;
168         
169         *zt = malloc(sizeof(struct zonetab));
170         zt_parse(*zt);
171         return(*zt);
172 }