4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
29 #include <sys/param.h>
34 #include <vroot/report.h>
35 #include <vroot/vroot.h>
36 #include <mk/defs.h> /* for tmpdir */
38 static FILE *report_file
;
39 static FILE *command_output_fp
;
40 static char *target_being_reported_for
;
41 static char *search_dir
;
42 static char command_output_tmpfile
[30];
43 static int is_path
= 0;
44 static char sfile
[MAXPATHLEN
];
46 static void (*warning_ptr
) (char *, ...) = (void (*) (char *, ...)) NULL
;
56 get_target_being_reported_for(void)
58 return(target_being_reported_for
);
63 close_report_file(void)
65 (void)fputs("\n", report_file
);
66 (void)fclose(report_file
);
71 clean_up(FILE *nse_depinfo_fp
, FILE *merge_fp
, char *nse_depinfo_file
, char *merge_file
, int unlinkf
)
73 fclose(nse_depinfo_fp
);
75 fclose(command_output_fp
);
76 unlink(command_output_tmpfile
);
80 rename(merge_file
, nse_depinfo_file
);
85 * Update the file, if necessary. We don't want to rewrite
86 * the file if we don't have to because we don't want the time of the file
87 * to change in that case.
94 char line
[MAXPATHLEN
+2];
95 char buf
[MAXPATHLEN
+2];
98 char nse_depinfo_file
[MAXPATHLEN
];
99 char merge_file
[MAXPATHLEN
];
100 char lock_file
[MAXPATHLEN
];
106 fprintf(command_output_fp
, "\n");
107 fclose(command_output_fp
);
108 if ((command_output_fp
= fopen(command_output_tmpfile
, "r")) == NULL
) {
111 sprintf(nse_depinfo_file
, "%s/%s", search_dir
, NSE_DEPINFO
);
112 sprintf(merge_file
, "%s/.tmp%s.%d", search_dir
, NSE_DEPINFO
, getpid());
113 sprintf(lock_file
, "%s/%s", search_dir
, NSE_DEPINFO_LOCK
);
114 err
= file_lock(nse_depinfo_file
, lock_file
, &file_locked
, 0);
116 if (warning_ptr
!= (void (*) (char *, ...)) NULL
) {
117 (*warning_ptr
)(gettext("Couldn't write to %s"), nse_depinfo_file
);
119 unlink(command_output_tmpfile
);
122 /* If .nse_depinfo file doesn't exist */
123 if ((nse_depinfo_fp
= fopen(nse_depinfo_file
, "r+")) == NULL
) {
125 if ((nse_depinfo_fp
=
126 fopen(nse_depinfo_file
, "w")) == NULL
) {
127 fprintf(stderr
, gettext("Cannot open `%s' for writing\n"),
129 unlink(command_output_tmpfile
);
134 while (fgets(line
, MAXPATHLEN
+2, command_output_fp
)
136 fprintf(nse_depinfo_fp
, "%s", line
);
138 fclose(command_output_fp
);
140 fclose(nse_depinfo_fp
);
144 unlink(command_output_tmpfile
);
147 if ((merge_fp
= fopen(merge_file
, "w")) == NULL
) {
148 fprintf(stderr
, gettext("Cannot open %s for writing\n"), merge_file
);
152 unlink(command_output_tmpfile
);
156 while (fgets(line
, MAXPATHLEN
+2, nse_depinfo_fp
) != NULL
) {
157 if (strncmp(line
, sfile
, len
) == 0 && line
[len
] == ':') {
158 while (fgets(buf
, MAXPATHLEN
+2, command_output_fp
)
161 fprintf(merge_fp
, "%s", buf
);
162 if (strcmp(line
, buf
)) {
167 if (buf
[strlen(buf
)-1] == '\n') {
171 if (changed
|| !is_path
) {
172 while (fgets(line
, MAXPATHLEN
, nse_depinfo_fp
)
174 fputs(line
, merge_fp
);
176 clean_up(nse_depinfo_fp
, merge_fp
,
177 nse_depinfo_file
, merge_file
, 0);
179 clean_up(nse_depinfo_fp
, merge_fp
,
180 nse_depinfo_file
, merge_file
, 1);
185 unlink(command_output_tmpfile
);
188 fputs(line
, merge_fp
);
190 /* Entry never found. Add it if there is a search path */
192 while (fgets(line
, MAXPATHLEN
+2, command_output_fp
) != NULL
) {
193 fprintf(nse_depinfo_fp
, "%s", line
);
196 clean_up(nse_depinfo_fp
, merge_fp
, nse_depinfo_file
, merge_file
, 1);
205 report_dep(char *iflag
, char *filename
)
208 if (command_output_fp
== NULL
) {
209 sprintf(command_output_tmpfile
,
210 "%s/%s.%d.XXXXXX", tmpdir
, NSE_DEPINFO
, getpid());
211 int fd
= mkstemp(command_output_tmpfile
);
212 if ((fd
< 0) || (command_output_fp
= fdopen(fd
, "w")) == NULL
) {
215 if ((search_dir
= getenv("NSE_DEP")) == NULL
) {
219 strcpy(sfile
, filename
);
220 if (iflag
== NULL
|| *iflag
== '\0') {
223 fprintf(command_output_fp
, "%s:", sfile
);
225 fprintf(command_output_fp
, " ");
226 fprintf(command_output_fp
, iflag
);
233 report_libdep(char *lib
, char *flag
)
236 char filename
[MAXPATHLEN
];
239 if ((p
= getenv(SUNPRO_DEPENDENCIES
)) == NULL
) {
242 ptr
= strchr(p
, ' ');
244 sprintf(filename
, "%s-%s", ptr
+1, flag
);
246 report_dep(lib
, filename
);
251 report_search_path(char *iflag
)
253 char curdir
[MAXPATHLEN
];
256 char filename
[MAXPATHLEN
];
259 if ((sdir
= getenv("NSE_DEP")) == NULL
) {
262 if ((p
= getenv(SUNPRO_DEPENDENCIES
)) == NULL
) {
265 ptr
= strchr(p
, ' ');
269 sprintf(filename
, "%s-CPP", ptr
+1);
270 getcwd(curdir
, sizeof(curdir
));
271 if (strcmp(curdir
, sdir
) != 0 && strlen(iflag
) > 2 &&
273 /* Makefile must have had an "cd xx; cc ..." */
274 /* Modify the -I path to be relative to the cd */
275 newiflag
= (char *)malloc(strlen(iflag
) + strlen(curdir
) + 2);
276 sprintf(newiflag
, "-%c%s/%s", iflag
[1], curdir
, &iflag
[2]);
277 report_dep(newiflag
, filename
);
279 report_dep(iflag
, filename
);
284 report_dependency(const char *name
)
286 register char *filename
;
287 char buffer
[MAXPATHLEN
+1];
290 char nse_depinfo_file
[MAXPATHLEN
];
292 if (report_file
== NULL
) {
293 if ((filename
= getenv(SUNPRO_DEPENDENCIES
)) == NULL
) {
294 report_file
= (FILE *)-1;
297 if (strlen(filename
) == 0) {
298 report_file
= (FILE *)-1;
301 (void)strcpy(buffer
, name
);
303 p
= strchr(filename
, ' ');
307 report_file
= (FILE *)-1;
310 if ((report_file
= fopen(filename
, "a")) == NULL
) {
311 if ((report_file
= fopen(filename
, "w")) == NULL
) {
312 report_file
= (FILE *)-1;
316 atexit(close_report_file
);
317 if ((p2
= strchr(p
+1, ' ')) != NULL
)
319 target_being_reported_for
= (char *)malloc((unsigned)(strlen(p
+1)+1));
320 (void)strcpy(target_being_reported_for
, p
+1);
321 (void)fputs(p
+1, report_file
);
322 (void)fputs(":", report_file
);
327 if (report_file
== (FILE *)-1)
329 (void)fputs(name
, report_file
);
330 (void)fputs(" ", report_file
);