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 
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 	c = head;
107 
108 	do {
109 		zt_get_token(&zp);
110 		switch (zp.state) {
111 		case ZT_CC:
112 			c->zt_cc = strdup(zp.tok);
113 			zp.state = ZT_COORDS;
114 			break;
115 		case ZT_COORDS:
116 			c->zt_coords = strdup(zp.tok);
117 			zp.state = ZT_REGION;
118 			break;
119 		case ZT_REGION:
120 			c->zt_region = strdup(zp.tok);
121 			zp.state = ZT_LOCALE;
122 			break;
123 		case ZT_LOCALE:
124 			c->zt_locale = strdup(zp.tok);
125 			zp.state = ZT_COMMENTS;
126 			break;
127 		case ZT_LOCALE_ENDL:
128 			c->zt_locale = strdup(zp.tok);
129 			c->zt_comments = NULL;
130 			c->next = malloc(sizeof(struct zonetab));
131 			c = c->next;
132 			zp.state = ZT_CC;
133 			break;
134 		case ZT_COMMENTS:
135 			c->zt_comments = strdup(zp.tok);
136 			c->next = malloc(sizeof(struct zonetab));
137 			c = c->next;
138 			zp.state = ZT_CC;
139 			break;
140 		case ZT_NOTOK:
141 			goto done;
142 		}
143 		zp.buf++;
144 	} while (1);
145 
146 done:
147 	c->next = NULL;
148 	free(file);
149 }
150