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