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