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 #include <stdio.h>
28*753d2d2eSraf #include <string.h>
29*753d2d2eSraf #include <errno.h>
30*753d2d2eSraf #include <unistd.h>
31*753d2d2eSraf #include <stdlib.h>
32*753d2d2eSraf #include <sys/types.h>
33*753d2d2eSraf #include <sys/param.h>
34*753d2d2eSraf #include "parser.h"
35*753d2d2eSraf #include "trace.h"
36*753d2d2eSraf #include "db.h"
37*753d2d2eSraf #include "util.h"
38*753d2d2eSraf #include "errlog.h"
39*753d2d2eSraf 
40*753d2d2eSraf /* Types and Globals */
41*753d2d2eSraf FILE	*Bodyfp = NULL;
42*753d2d2eSraf FILE	*Headfp = NULL;
43*753d2d2eSraf FILE	*Mapfp = NULL;
44*753d2d2eSraf 
45*753d2d2eSraf static char headfile_name[MAXLINE]; /* Saved for later. */
46*753d2d2eSraf static char mapfile_name[MAXLINE]; /* Saved for later. */
47*753d2d2eSraf 
48*753d2d2eSraf /* File globals. */
49*753d2d2eSraf static int alt_code_file(void);
50*753d2d2eSraf static void abort_code_file(void);
51*753d2d2eSraf 
52*753d2d2eSraf /*
53*753d2d2eSraf  * open_code_file - open the code file and the invisible temp file.
54*753d2d2eSraf  */
55*753d2d2eSraf int
open_code_file(void)56*753d2d2eSraf open_code_file(void)
57*753d2d2eSraf {
58*753d2d2eSraf 	char	*dir = db_get_target_directory();
59*753d2d2eSraf 	char	*body_file_name;
60*753d2d2eSraf 	int	rc = YES;
61*753d2d2eSraf 
62*753d2d2eSraf 	errlog(BEGIN, "open_code_file() {");
63*753d2d2eSraf 
64*753d2d2eSraf 	/* Open the Head file, which gets the headers, includes and */
65*753d2d2eSraf 	/* definitions, and eventually gets the body concatenated to it. */
66*753d2d2eSraf 	(void) snprintf(headfile_name, sizeof (headfile_name), "%s.c",
67*753d2d2eSraf 		db_get_output_file());
68*753d2d2eSraf 	if ((Headfp = fopen(headfile_name, "w")) == NULL) {
69*753d2d2eSraf 		errlog(FATAL, "%s: %s", headfile_name, strerror(errno));
70*753d2d2eSraf 	}
71*753d2d2eSraf 
72*753d2d2eSraf 	(void) snprintf(mapfile_name, sizeof (mapfile_name), "%s-vers",
73*753d2d2eSraf 	    db_get_output_file());
74*753d2d2eSraf 
75*753d2d2eSraf 	if ((Mapfp = fopen(mapfile_name, "w")) == NULL) {
76*753d2d2eSraf 		errlog(FATAL, "%s: %s", mapfile_name, strerror(errno));
77*753d2d2eSraf 	}
78*753d2d2eSraf 	(void) fputs("SUNWabi_1.1 {\n    global:\n", Mapfp);
79*753d2d2eSraf 
80*753d2d2eSraf 	/* Now the Body file, which is an ephemeral temp-file. */
81*753d2d2eSraf 	if ((body_file_name = tempnam(dir, NULL)) == NULL) {
82*753d2d2eSraf 		errlog(FATAL, "out of memory creating a temp-file name");
83*753d2d2eSraf 	}
84*753d2d2eSraf 
85*753d2d2eSraf 	if ((Bodyfp = fopen(body_file_name, "w+")) == NULL) {
86*753d2d2eSraf 		errlog(FATAL, "%s: %s", body_file_name, strerror(errno));
87*753d2d2eSraf 	}
88*753d2d2eSraf 
89*753d2d2eSraf 	if (unlink(body_file_name) != 0) {
90*753d2d2eSraf 		errlog(FATAL, "unlink %s: %s", body_file_name, strerror(errno));
91*753d2d2eSraf 	}
92*753d2d2eSraf 
93*753d2d2eSraf 	(void) free(body_file_name);
94*753d2d2eSraf 	errlog(END, "}");
95*753d2d2eSraf 	return (rc);
96*753d2d2eSraf }
97*753d2d2eSraf 
98*753d2d2eSraf /*
99*753d2d2eSraf  * abort_code_file -- close and discard files.
100*753d2d2eSraf  * this function is also called from alt_code_file, so
101*753d2d2eSraf  * it is not cool to unlink the code file or the mapfile
102*753d2d2eSraf  */
103*753d2d2eSraf static void
abort_code_file(void)104*753d2d2eSraf abort_code_file(void)
105*753d2d2eSraf {
106*753d2d2eSraf 	errlog(BEGIN, "abort_code_file() {");
107*753d2d2eSraf 	(void) fclose(Bodyfp);
108*753d2d2eSraf 	(void) fclose(Headfp);
109*753d2d2eSraf 	if (unlink(headfile_name) != 0) {
110*753d2d2eSraf 		errlog(FATAL, "unlink %s: %s", headfile_name, strerror(errno));
111*753d2d2eSraf 	}
112*753d2d2eSraf 	errlog(END, "}");
113*753d2d2eSraf }
114*753d2d2eSraf 
115*753d2d2eSraf int
alt_code_file(void)116*753d2d2eSraf alt_code_file(void)
117*753d2d2eSraf {
118*753d2d2eSraf 	char hfn[MAXLINE];
119*753d2d2eSraf 	FILE *hfp;
120*753d2d2eSraf 
121*753d2d2eSraf 	abort_code_file();
122*753d2d2eSraf 	(void) snprintf(hfn, sizeof (hfn), "%s.c", db_get_output_file());
123*753d2d2eSraf 	if ((hfp = fopen(hfn, "w")) == NULL) {
124*753d2d2eSraf 		errlog(FATAL, "%s: %s", headfile_name, strerror(errno));
125*753d2d2eSraf 	}
126*753d2d2eSraf 
127*753d2d2eSraf 	(void) fputs("static int __abi_place_holder;\n", hfp);
128*753d2d2eSraf 	(void) fclose(hfp);
129*753d2d2eSraf 
130*753d2d2eSraf 	return (YES);
131*753d2d2eSraf }
132*753d2d2eSraf 
133*753d2d2eSraf /*
134*753d2d2eSraf  * commit_code_file -- close and commit files that have advanced
135*753d2d2eSraf  *	beyond byte position 0.
136*753d2d2eSraf  */
137*753d2d2eSraf int
commit_code_file(void)138*753d2d2eSraf commit_code_file(void)
139*753d2d2eSraf {
140*753d2d2eSraf 	char	copy_buffer[BUFSIZ*8];
141*753d2d2eSraf 	size_t	n;
142*753d2d2eSraf 
143*753d2d2eSraf 	errlog(BEGIN, "commit_code_file() {");
144*753d2d2eSraf 	/*
145*753d2d2eSraf 	 * We unconditionally want a .pf and a -vers file
146*753d2d2eSraf 	 */
147*753d2d2eSraf 	(void) fputs("    local:\n\t*;\n};\n", Mapfp);
148*753d2d2eSraf 	if (fclose(Mapfp) != 0) {
149*753d2d2eSraf 		errlog(FATAL, "fclose %s: %s", mapfile_name, strerror(errno));
150*753d2d2eSraf 	}
151*753d2d2eSraf 	if (ftell(Bodyfp) == 0) {
152*753d2d2eSraf 		/*
153*753d2d2eSraf 		 * special case, redo C file with place holder
154*753d2d2eSraf 		 * so that makefiles won't break...
155*753d2d2eSraf 		 */
156*753d2d2eSraf 		errlog(END, "}");
157*753d2d2eSraf 		return (alt_code_file());
158*753d2d2eSraf 	} else {
159*753d2d2eSraf 		/* Concatenate body file to head file, close both. */
160*753d2d2eSraf 		rewind(Bodyfp);
161*753d2d2eSraf 		while ((n = fread(copy_buffer, 1,
162*753d2d2eSraf 		    sizeof (copy_buffer), Bodyfp)) != 0) {
163*753d2d2eSraf 			(void) fwrite(copy_buffer, 1, n, Headfp);
164*753d2d2eSraf 		}
165*753d2d2eSraf 		(void) fclose(Bodyfp);
166*753d2d2eSraf 		if (fclose(Headfp) != 0) {
167*753d2d2eSraf 			errlog(FATAL, "fclose <temp file>: %s",
168*753d2d2eSraf 			    strerror(errno));
169*753d2d2eSraf 		}
170*753d2d2eSraf 	}
171*753d2d2eSraf 
172*753d2d2eSraf 	errlog(END, "}");
173*753d2d2eSraf 	return (YES);
174*753d2d2eSraf }
175