xref: /onnv-gate/usr/src/cmd/abi/spectrans/spec2trace/io.c (revision 2775:892d346f56a9)
1*2775Sraf /*
2*2775Sraf  * CDDL HEADER START
3*2775Sraf  *
4*2775Sraf  * The contents of this file are subject to the terms of the
5*2775Sraf  * Common Development and Distribution License, Version 1.0 only
6*2775Sraf  * (the "License").  You may not use this file except in compliance
7*2775Sraf  * with the License.
8*2775Sraf  *
9*2775Sraf  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*2775Sraf  * or http://www.opensolaris.org/os/licensing.
11*2775Sraf  * See the License for the specific language governing permissions
12*2775Sraf  * and limitations under the License.
13*2775Sraf  *
14*2775Sraf  * When distributing Covered Code, include this CDDL HEADER in each
15*2775Sraf  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*2775Sraf  * If applicable, add the following below this CDDL HEADER, with the
17*2775Sraf  * fields enclosed by brackets "[]" replaced with your own identifying
18*2775Sraf  * information: Portions Copyright [yyyy] [name of copyright owner]
19*2775Sraf  *
20*2775Sraf  * CDDL HEADER END
21*2775Sraf  */
22*2775Sraf /*
23*2775Sraf  * Copyright (c) 1997-1999 by Sun Microsystems, Inc.
24*2775Sraf  * All rights reserved.
25*2775Sraf  */
26*2775Sraf 
27*2775Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*2775Sraf 
29*2775Sraf 
30*2775Sraf #include <stdio.h>
31*2775Sraf #include <string.h>
32*2775Sraf #include <errno.h>
33*2775Sraf #include <unistd.h>
34*2775Sraf #include <stdlib.h>
35*2775Sraf #include <sys/types.h>
36*2775Sraf #include <sys/param.h>
37*2775Sraf #include "parser.h"
38*2775Sraf #include "trace.h"
39*2775Sraf #include "db.h"
40*2775Sraf #include "util.h"
41*2775Sraf #include "errlog.h"
42*2775Sraf 
43*2775Sraf /* Types and Globals */
44*2775Sraf FILE	*Bodyfp = NULL;
45*2775Sraf FILE	*Headfp = NULL;
46*2775Sraf FILE	*Mapfp = NULL;
47*2775Sraf 
48*2775Sraf static char headfile_name[MAXLINE]; /* Saved for later. */
49*2775Sraf static char mapfile_name[MAXLINE]; /* Saved for later. */
50*2775Sraf 
51*2775Sraf /* File globals. */
52*2775Sraf static int alt_code_file(void);
53*2775Sraf static void abort_code_file(void);
54*2775Sraf 
55*2775Sraf /*
56*2775Sraf  * open_code_file - open the code file and the invisible temp file.
57*2775Sraf  */
58*2775Sraf int
open_code_file(void)59*2775Sraf open_code_file(void)
60*2775Sraf {
61*2775Sraf 	char	*dir = db_get_target_directory();
62*2775Sraf 	char	*body_file_name;
63*2775Sraf 	int	rc = YES;
64*2775Sraf 
65*2775Sraf 	errlog(BEGIN, "open_code_file() {");
66*2775Sraf 
67*2775Sraf 	/* Open the Head file, which gets the headers, includes and */
68*2775Sraf 	/* definitions, and eventually gets the body concatenated to it. */
69*2775Sraf 	(void) snprintf(headfile_name, sizeof (headfile_name), "%s.c",
70*2775Sraf 		db_get_output_file());
71*2775Sraf 	if ((Headfp = fopen(headfile_name, "w")) == NULL) {
72*2775Sraf 		errlog(FATAL, "%s: %s", headfile_name, strerror(errno));
73*2775Sraf 	}
74*2775Sraf 
75*2775Sraf 	(void) snprintf(mapfile_name, sizeof (mapfile_name), "%s-vers",
76*2775Sraf 	    db_get_output_file());
77*2775Sraf 
78*2775Sraf 	if ((Mapfp = fopen(mapfile_name, "w")) == NULL) {
79*2775Sraf 		errlog(FATAL, "%s: %s", mapfile_name, strerror(errno));
80*2775Sraf 	}
81*2775Sraf 	(void) fputs("SUNWabi_1.1 {\n    global:\n", Mapfp);
82*2775Sraf 
83*2775Sraf 	/* Now the Body file, which is an ephemeral temp-file. */
84*2775Sraf 	if ((body_file_name = tempnam(dir, NULL)) == NULL) {
85*2775Sraf 		errlog(FATAL, "out of memory creating a temp-file name");
86*2775Sraf 	}
87*2775Sraf 
88*2775Sraf 	if ((Bodyfp = fopen(body_file_name, "w+")) == NULL) {
89*2775Sraf 		errlog(FATAL, "%s: %s", body_file_name, strerror(errno));
90*2775Sraf 	}
91*2775Sraf 
92*2775Sraf 	if (unlink(body_file_name) != 0) {
93*2775Sraf 		errlog(FATAL, "unlink %s: %s", body_file_name, strerror(errno));
94*2775Sraf 	}
95*2775Sraf 
96*2775Sraf 	(void) free(body_file_name);
97*2775Sraf 	errlog(END, "}");
98*2775Sraf 	return (rc);
99*2775Sraf }
100*2775Sraf 
101*2775Sraf /*
102*2775Sraf  * abort_code_file -- close and discard files.
103*2775Sraf  * this function is also called from alt_code_file, so
104*2775Sraf  * it is not cool to unlink the code file or the mapfile
105*2775Sraf  */
106*2775Sraf static void
abort_code_file(void)107*2775Sraf abort_code_file(void)
108*2775Sraf {
109*2775Sraf 	errlog(BEGIN, "abort_code_file() {");
110*2775Sraf 	(void) fclose(Bodyfp);
111*2775Sraf 	(void) fclose(Headfp);
112*2775Sraf 	if (unlink(headfile_name) != 0) {
113*2775Sraf 		errlog(FATAL, "unlink %s: %s", headfile_name, strerror(errno));
114*2775Sraf 	}
115*2775Sraf 	errlog(END, "}");
116*2775Sraf }
117*2775Sraf 
118*2775Sraf int
alt_code_file(void)119*2775Sraf alt_code_file(void)
120*2775Sraf {
121*2775Sraf 	char hfn[MAXLINE];
122*2775Sraf 	FILE *hfp;
123*2775Sraf 
124*2775Sraf 	abort_code_file();
125*2775Sraf 	(void) snprintf(hfn, sizeof (hfn), "%s.c", db_get_output_file());
126*2775Sraf 	if ((hfp = fopen(hfn, "w")) == NULL) {
127*2775Sraf 		errlog(FATAL, "%s: %s", headfile_name, strerror(errno));
128*2775Sraf 	}
129*2775Sraf 
130*2775Sraf 	(void) fputs("static int __abi_place_holder;\n", hfp);
131*2775Sraf 	(void) fclose(hfp);
132*2775Sraf 
133*2775Sraf 	return (YES);
134*2775Sraf }
135*2775Sraf 
136*2775Sraf /*
137*2775Sraf  * commit_code_file -- close and commit files that have advanced
138*2775Sraf  *	beyond byte position 0.
139*2775Sraf  */
140*2775Sraf int
commit_code_file(void)141*2775Sraf commit_code_file(void)
142*2775Sraf {
143*2775Sraf 	char	copy_buffer[BUFSIZ*8];
144*2775Sraf 	size_t	n;
145*2775Sraf 
146*2775Sraf 	errlog(BEGIN, "commit_code_file() {");
147*2775Sraf 	/*
148*2775Sraf 	 * We unconditionally want a .pf and a -vers file
149*2775Sraf 	 */
150*2775Sraf 	(void) fputs("    local:\n\t*;\n};\n", Mapfp);
151*2775Sraf 	if (fclose(Mapfp) != 0) {
152*2775Sraf 		errlog(FATAL, "fclose %s: %s", mapfile_name, strerror(errno));
153*2775Sraf 	}
154*2775Sraf 	if (ftell(Bodyfp) == 0) {
155*2775Sraf 		/*
156*2775Sraf 		 * special case, redo C file with place holder
157*2775Sraf 		 * so that makefiles won't break...
158*2775Sraf 		 */
159*2775Sraf 		errlog(END, "}");
160*2775Sraf 		return (alt_code_file());
161*2775Sraf 	} else {
162*2775Sraf 		/* Concatenate body file to head file, close both. */
163*2775Sraf 		rewind(Bodyfp);
164*2775Sraf 		while ((n = fread(copy_buffer, 1,
165*2775Sraf 		    sizeof (copy_buffer), Bodyfp)) != 0) {
166*2775Sraf 			(void) fwrite(copy_buffer, 1, n, Headfp);
167*2775Sraf 		}
168*2775Sraf 		(void) fclose(Bodyfp);
169*2775Sraf 		if (fclose(Headfp) != 0) {
170*2775Sraf 			errlog(FATAL, "fclose <temp file>: %s",
171*2775Sraf 			    strerror(errno));
172*2775Sraf 		}
173*2775Sraf 	}
174*2775Sraf 
175*2775Sraf 	errlog(END, "}");
176*2775Sraf 	return (YES);
177*2775Sraf }
178