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