dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fm / eversholt / common / eftread.c
blob16c5bc91c506ecaa847808ec12fd3b77d74b8614
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * eftread.c -- routines for reading .eft files
29 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <stdio.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <alloca.h>
39 #include "out.h"
40 #include "stable.h"
41 #include "lut.h"
42 #include "tree.h"
43 #include "eft.h"
44 #include "eftread.h"
45 #include "esclex.h"
46 #include "version.h"
47 #include "ptree.h"
49 /* for uintX_t, htonl(), etc */
50 #include <sys/types.h>
51 #include <netinet/in.h>
52 #include <inttypes.h>
54 #ifndef MIN
55 #define MIN(x, y) ((x) <= (y) ? (x) : (y))
56 #endif
58 static int Showheader;
61 * eftread_showheader -- set showheader flag
64 void
65 eftread_showheader(int newval)
67 Showheader = newval;
71 * eftread_fopen -- fopen an EFT file for reading
75 FILE *
76 eftread_fopen(const char *fname, char *idbuf, size_t idbufsz)
78 FILE *fp;
79 FILE *tfp;
80 struct eftheader hdr;
81 #define BUFLEN 8192
82 char buf[BUFLEN];
83 int cc;
84 uint32_t csum = 0;
85 char *ptr;
87 if ((ptr = strrchr(fname, '.')) == NULL || strcmp(ptr, ".eft") != 0) {
88 out(O_ERR, "%s: not a valid EFT (bad extension)", fname);
89 return (NULL);
92 if ((fp = fopen(fname, "r")) == NULL) {
93 out(O_ERR|O_SYS, "%s", fname);
94 return (NULL);
97 if (fread(&hdr, 1, sizeof (hdr), fp) < sizeof (hdr)) {
98 (void) fclose(fp);
99 out(O_ERR, "%s: not a valid EFT (too short)", fname);
100 return (NULL);
102 hdr.magic = ntohl(hdr.magic);
103 hdr.major = ntohs(hdr.major);
104 hdr.minor = ntohs(hdr.minor);
105 hdr.cmajor = ntohs(hdr.cmajor);
106 hdr.cminor = ntohs(hdr.cminor);
107 hdr.identlen = ntohl(hdr.identlen);
108 hdr.dictlen = ntohl(hdr.dictlen);
109 hdr.csum = ntohl(hdr.csum);
111 if (Showheader)
112 out(O_VERB, "%s: magic %x EFT version %d.%d esc version %d.%d",
113 fname, hdr.magic, hdr.major, hdr.minor,
114 hdr.cmajor, hdr.cminor);
116 if (hdr.magic != EFT_HDR_MAGIC) {
117 (void) fclose(fp);
118 out(O_ERR, "%s: not a valid EFT (bad magic)", fname);
119 return (NULL);
122 if (hdr.major != EFT_HDR_MAJOR || hdr.minor > EFT_HDR_MINOR) {
123 (void) fclose(fp);
124 out(O_ERR, "%s is version %d.%d, "
125 "this program supports up to %d.%d", fname,
126 hdr.major, hdr.minor, EFT_HDR_MAJOR, EFT_HDR_MINOR);
127 return (NULL);
130 bzero(idbuf, idbufsz);
131 if (hdr.identlen != 0) {
132 long npos = ftell(fp) + (long)hdr.identlen; /* after ident */
133 size_t rsz = MIN(hdr.identlen, idbufsz - 1);
135 if (fread(idbuf, 1, rsz, fp) != rsz)
136 out(O_DIE|O_SYS, "%s: fread", fname);
137 if (fseek(fp, npos, SEEK_SET) == -1)
138 out(O_DIE|O_SYS, "%s: fseek", fname);
141 if (hdr.dictlen && (hdr.dictlen < 2 || hdr.dictlen > 1000)) {
142 (void) fclose(fp);
143 out(O_ERR, "%s: bad dictlen: %d", fname, hdr.dictlen);
144 return (NULL);
147 /* read in dict strings */
148 if (hdr.dictlen) {
149 char *dbuf = alloca(hdr.dictlen);
150 char *dptr;
152 if ((cc = fread(dbuf, 1, hdr.dictlen, fp)) != hdr.dictlen)
153 out(O_DIE|O_SYS, "short fread on %s (dictlen %d)",
154 fname, hdr.dictlen);
156 /* work from end of string array backwards, finding names */
157 for (dptr = &dbuf[hdr.dictlen - 2]; dptr > dbuf; dptr--)
158 if (*dptr == '\0') {
159 /* found separator, record string */
160 Dicts = lut_add(Dicts,
161 (void *)stable(dptr + 1), NULL, NULL);
163 /* record the first string */
164 Dicts = lut_add(Dicts,
165 (void *)stable(dptr), NULL, NULL);
168 if ((tfp = tmpfile()) == NULL)
169 out(O_DIE|O_SYS, "cannot create temporary file");
171 while ((cc = fread(buf, 1, BUFLEN, fp)) > 0) {
172 char *ptr;
174 for (ptr = buf; ptr < &buf[cc]; ptr++) {
175 *ptr = ~((unsigned char)*ptr);
176 csum += (uint32_t)*ptr;
178 if (cc != fwrite(buf, 1, cc, tfp) || ferror(tfp))
179 out(O_DIE|O_SYS, "fwrite on tmpfile");
181 if (ferror(fp))
182 out(O_DIE|O_SYS, "fread on %s", fname);
183 (void) fclose(fp);
185 if (hdr.csum != csum) {
186 out(O_ERR, "%s: bad checksum (%x != %x)", fname,
187 hdr.csum, csum);
188 (void) fclose(tfp);
189 return (NULL);
192 if (Showheader) {
193 int len = strlen(hdr.comment);
194 if (len > 0 && hdr.comment[len - 1] == '\n')
195 hdr.comment[len - 1] = '\0';
196 out(O_OK, "%s:\n\t%s", fname, hdr.comment);
199 rewind(tfp);
201 return (tfp);