Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / usr.sbin / installer / dfuibe_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 = 0;
51         char *cs;
52
53         /* XXX this is kinda ugly */
54         cs = buf;
55         while (--len > 0 && (i = getc(f)) != EOF)
56                 *(cs++) = i;
57         *cs = '\0';
58         return((i == EOF && buf == cs) ? NULL : buf);
59 }
60
61 void
62 zt_get_token(struct zt_parse *zp) {
63         int i = 0;
64
65         while (*(zp->buf) != '\0' && *(zp->buf) != '\n' && *(zp->buf) != '\t') {
66                 switch (*(zp->buf)) {
67                 case '#':
68                         while (*(zp->buf) != '\n')
69                                 zp->buf++;
70                         zp->buf++;
71                         break;
72                 default:
73                         while (*(zp->buf) != '\n' && *(zp->buf) != '\t' &&
74                             *(zp->buf) != '\0') {
75                                 if (*(zp->buf) == '/' && zp->state == ZT_REGION) {
76                                         zp->tok[i] = '\0';
77                                         return;
78                                 }
79                                 zp->tok[i++] = *(zp->buf++);
80                         }
81                         zp->tok[i] = '\0';
82                         if (zp->state == ZT_LOCALE && *(zp->buf) == '\n')
83                                 zp->state = ZT_LOCALE_ENDL;
84                         else if (*(zp->buf) == '\0')
85                                 zp->state = ZT_NOTOK;
86                         break;
87                 }
88         }
89 }
90
91 void
92 zt_parse(struct zonetab *head) {
93         FILE *zone_tab;
94         struct stat sb;
95         struct zonetab *c;
96         struct zt_parse zp;
97         char *file;
98         int i;
99
100         zone_tab = fopen(ZONETAB_FILE, "r");
101         stat(ZONETAB_FILE, &sb);
102         file = malloc(sb.st_size + 1);
103         file = zt_readfile(file, sb.st_size, zone_tab);
104         zp.buf = file;
105
106         zp.state = ZT_CC;
107         i = 0;
108         c = head;
109
110         do {
111                 zt_get_token(&zp);
112                 switch (zp.state) {
113                 case ZT_CC:
114                         c->zt_cc = strdup(zp.tok);
115                         zp.state = ZT_COORDS;
116                         break;
117                 case ZT_COORDS:
118                         c->zt_coords = strdup(zp.tok);
119                         zp.state = ZT_REGION;
120                         break;
121                 case ZT_REGION:
122                         c->zt_region = strdup(zp.tok);
123                         zp.state = ZT_LOCALE;
124                         break;
125                 case ZT_LOCALE:
126                         c->zt_locale = strdup(zp.tok);
127                         zp.state = ZT_COMMENTS;
128                         break;
129                 case ZT_LOCALE_ENDL:
130                         c->zt_locale = strdup(zp.tok);
131                         c->zt_comments = NULL;
132                         c->next = malloc(sizeof(struct zonetab));
133                         c = c->next;
134                         zp.state = ZT_CC;
135                         break;
136                 case ZT_COMMENTS:
137                         c->zt_comments = strdup(zp.tok);
138                         c->next = malloc(sizeof(struct zonetab));
139                         c = c->next;
140                         zp.state = ZT_CC;
141                         break;
142                 case ZT_NOTOK:
143                         goto done;
144                 }
145                 zp.buf++;
146         } while (1);
147
148 done:
149         c->next = NULL;
150         free(file);
151 }