1*10d63b7dSRichard Lowe /* 2*10d63b7dSRichard Lowe * CDDL HEADER START 3*10d63b7dSRichard Lowe * 4*10d63b7dSRichard Lowe * The contents of this file are subject to the terms of the 5*10d63b7dSRichard Lowe * Common Development and Distribution License (the "License"). 6*10d63b7dSRichard Lowe * You may not use this file except in compliance with the License. 7*10d63b7dSRichard Lowe * 8*10d63b7dSRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*10d63b7dSRichard Lowe * or http://www.opensolaris.org/os/licensing. 10*10d63b7dSRichard Lowe * See the License for the specific language governing permissions 11*10d63b7dSRichard Lowe * and limitations under the License. 12*10d63b7dSRichard Lowe * 13*10d63b7dSRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each 14*10d63b7dSRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*10d63b7dSRichard Lowe * If applicable, add the following below this CDDL HEADER, with the 16*10d63b7dSRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying 17*10d63b7dSRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner] 18*10d63b7dSRichard Lowe * 19*10d63b7dSRichard Lowe * CDDL HEADER END 20*10d63b7dSRichard Lowe */ 21*10d63b7dSRichard Lowe /* 22*10d63b7dSRichard Lowe * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 23*10d63b7dSRichard Lowe * Use is subject to license terms. 24*10d63b7dSRichard Lowe */ 25*10d63b7dSRichard Lowe 26*10d63b7dSRichard Lowe #include <stdio.h> 27*10d63b7dSRichard Lowe #include <stdlib.h> 28*10d63b7dSRichard Lowe #include <string.h> 29*10d63b7dSRichard Lowe #include <sys/param.h> 30*10d63b7dSRichard Lowe #include <sys/wait.h> 31*10d63b7dSRichard Lowe #include <unistd.h> 32*10d63b7dSRichard Lowe #include <libintl.h> 33*10d63b7dSRichard Lowe 34*10d63b7dSRichard Lowe #include <vroot/report.h> 35*10d63b7dSRichard Lowe #include <vroot/vroot.h> 36*10d63b7dSRichard Lowe #include <mk/defs.h> /* for tmpdir */ 37*10d63b7dSRichard Lowe 38*10d63b7dSRichard Lowe static FILE *report_file; 39*10d63b7dSRichard Lowe static FILE *command_output_fp; 40*10d63b7dSRichard Lowe static char *target_being_reported_for; 41*10d63b7dSRichard Lowe static char *search_dir; 42*10d63b7dSRichard Lowe static char command_output_tmpfile[30]; 43*10d63b7dSRichard Lowe static int is_path = 0; 44*10d63b7dSRichard Lowe static char sfile[MAXPATHLEN]; 45*10d63b7dSRichard Lowe extern "C" { 46*10d63b7dSRichard Lowe static void (*warning_ptr) (char *, ...) = (void (*) (char *, ...)) NULL; 47*10d63b7dSRichard Lowe } 48*10d63b7dSRichard Lowe 49*10d63b7dSRichard Lowe FILE * 50*10d63b7dSRichard Lowe get_report_file(void) 51*10d63b7dSRichard Lowe { 52*10d63b7dSRichard Lowe return(report_file); 53*10d63b7dSRichard Lowe } 54*10d63b7dSRichard Lowe 55*10d63b7dSRichard Lowe char * 56*10d63b7dSRichard Lowe get_target_being_reported_for(void) 57*10d63b7dSRichard Lowe { 58*10d63b7dSRichard Lowe return(target_being_reported_for); 59*10d63b7dSRichard Lowe } 60*10d63b7dSRichard Lowe 61*10d63b7dSRichard Lowe extern "C" { 62*10d63b7dSRichard Lowe static void 63*10d63b7dSRichard Lowe close_report_file(void) 64*10d63b7dSRichard Lowe { 65*10d63b7dSRichard Lowe (void)fputs("\n", report_file); 66*10d63b7dSRichard Lowe (void)fclose(report_file); 67*10d63b7dSRichard Lowe } 68*10d63b7dSRichard Lowe } // extern "C" 69*10d63b7dSRichard Lowe 70*10d63b7dSRichard Lowe static void 71*10d63b7dSRichard Lowe clean_up(FILE *nse_depinfo_fp, FILE *merge_fp, char *nse_depinfo_file, char *merge_file, int unlinkf) 72*10d63b7dSRichard Lowe { 73*10d63b7dSRichard Lowe fclose(nse_depinfo_fp); 74*10d63b7dSRichard Lowe fclose(merge_fp); 75*10d63b7dSRichard Lowe fclose(command_output_fp); 76*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 77*10d63b7dSRichard Lowe if (unlinkf) 78*10d63b7dSRichard Lowe unlink(merge_file); 79*10d63b7dSRichard Lowe else 80*10d63b7dSRichard Lowe rename(merge_file, nse_depinfo_file); 81*10d63b7dSRichard Lowe } 82*10d63b7dSRichard Lowe 83*10d63b7dSRichard Lowe 84*10d63b7dSRichard Lowe /* 85*10d63b7dSRichard Lowe * Update the file, if necessary. We don't want to rewrite 86*10d63b7dSRichard Lowe * the file if we don't have to because we don't want the time of the file 87*10d63b7dSRichard Lowe * to change in that case. 88*10d63b7dSRichard Lowe */ 89*10d63b7dSRichard Lowe 90*10d63b7dSRichard Lowe extern "C" { 91*10d63b7dSRichard Lowe static void 92*10d63b7dSRichard Lowe close_file(void) 93*10d63b7dSRichard Lowe { 94*10d63b7dSRichard Lowe char line[MAXPATHLEN+2]; 95*10d63b7dSRichard Lowe char buf[MAXPATHLEN+2]; 96*10d63b7dSRichard Lowe FILE *nse_depinfo_fp; 97*10d63b7dSRichard Lowe FILE *merge_fp; 98*10d63b7dSRichard Lowe char nse_depinfo_file[MAXPATHLEN]; 99*10d63b7dSRichard Lowe char merge_file[MAXPATHLEN]; 100*10d63b7dSRichard Lowe char lock_file[MAXPATHLEN]; 101*10d63b7dSRichard Lowe int err; 102*10d63b7dSRichard Lowe int len; 103*10d63b7dSRichard Lowe int changed = 0; 104*10d63b7dSRichard Lowe int file_locked; 105*10d63b7dSRichard Lowe 106*10d63b7dSRichard Lowe fprintf(command_output_fp, "\n"); 107*10d63b7dSRichard Lowe fclose(command_output_fp); 108*10d63b7dSRichard Lowe if ((command_output_fp = fopen(command_output_tmpfile, "r")) == NULL) { 109*10d63b7dSRichard Lowe return; 110*10d63b7dSRichard Lowe } 111*10d63b7dSRichard Lowe sprintf(nse_depinfo_file, "%s/%s", search_dir, NSE_DEPINFO); 112*10d63b7dSRichard Lowe sprintf(merge_file, "%s/.tmp%s.%d", search_dir, NSE_DEPINFO, getpid()); 113*10d63b7dSRichard Lowe sprintf(lock_file, "%s/%s", search_dir, NSE_DEPINFO_LOCK); 114*10d63b7dSRichard Lowe err = file_lock(nse_depinfo_file, lock_file, &file_locked, 0); 115*10d63b7dSRichard Lowe if (err) { 116*10d63b7dSRichard Lowe if (warning_ptr != (void (*) (char *, ...)) NULL) { 117*10d63b7dSRichard Lowe (*warning_ptr)(gettext("Couldn't write to %s"), nse_depinfo_file); 118*10d63b7dSRichard Lowe } 119*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 120*10d63b7dSRichard Lowe return; 121*10d63b7dSRichard Lowe } 122*10d63b7dSRichard Lowe /* If .nse_depinfo file doesn't exist */ 123*10d63b7dSRichard Lowe if ((nse_depinfo_fp = fopen(nse_depinfo_file, "r+")) == NULL) { 124*10d63b7dSRichard Lowe if (is_path) { 125*10d63b7dSRichard Lowe if ((nse_depinfo_fp = 126*10d63b7dSRichard Lowe fopen(nse_depinfo_file, "w")) == NULL) { 127*10d63b7dSRichard Lowe fprintf(stderr, gettext("Cannot open `%s' for writing\n"), 128*10d63b7dSRichard Lowe nse_depinfo_file); 129*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 130*10d63b7dSRichard Lowe 131*10d63b7dSRichard Lowe unlink(lock_file); 132*10d63b7dSRichard Lowe return; 133*10d63b7dSRichard Lowe } 134*10d63b7dSRichard Lowe while (fgets(line, MAXPATHLEN+2, command_output_fp) 135*10d63b7dSRichard Lowe != NULL) { 136*10d63b7dSRichard Lowe fprintf(nse_depinfo_fp, "%s", line); 137*10d63b7dSRichard Lowe } 138*10d63b7dSRichard Lowe fclose(command_output_fp); 139*10d63b7dSRichard Lowe } 140*10d63b7dSRichard Lowe fclose(nse_depinfo_fp); 141*10d63b7dSRichard Lowe if (file_locked) { 142*10d63b7dSRichard Lowe unlink(lock_file); 143*10d63b7dSRichard Lowe } 144*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 145*10d63b7dSRichard Lowe return; 146*10d63b7dSRichard Lowe } 147*10d63b7dSRichard Lowe if ((merge_fp = fopen(merge_file, "w")) == NULL) { 148*10d63b7dSRichard Lowe fprintf(stderr, gettext("Cannot open %s for writing\n"), merge_file); 149*10d63b7dSRichard Lowe if (file_locked) { 150*10d63b7dSRichard Lowe unlink(lock_file); 151*10d63b7dSRichard Lowe } 152*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 153*10d63b7dSRichard Lowe return; 154*10d63b7dSRichard Lowe } 155*10d63b7dSRichard Lowe len = strlen(sfile); 156*10d63b7dSRichard Lowe while (fgets(line, MAXPATHLEN+2, nse_depinfo_fp) != NULL) { 157*10d63b7dSRichard Lowe if (strncmp(line, sfile, len) == 0 && line[len] == ':') { 158*10d63b7dSRichard Lowe while (fgets(buf, MAXPATHLEN+2, command_output_fp) 159*10d63b7dSRichard Lowe != NULL) { 160*10d63b7dSRichard Lowe if (is_path) { 161*10d63b7dSRichard Lowe fprintf(merge_fp, "%s", buf); 162*10d63b7dSRichard Lowe if (strcmp(line, buf)) { 163*10d63b7dSRichard Lowe /* changed */ 164*10d63b7dSRichard Lowe changed = 1; 165*10d63b7dSRichard Lowe } 166*10d63b7dSRichard Lowe } 167*10d63b7dSRichard Lowe if (buf[strlen(buf)-1] == '\n') { 168*10d63b7dSRichard Lowe break; 169*10d63b7dSRichard Lowe } 170*10d63b7dSRichard Lowe } 171*10d63b7dSRichard Lowe if (changed || !is_path) { 172*10d63b7dSRichard Lowe while (fgets(line, MAXPATHLEN, nse_depinfo_fp) 173*10d63b7dSRichard Lowe != NULL) { 174*10d63b7dSRichard Lowe fputs(line, merge_fp); 175*10d63b7dSRichard Lowe } 176*10d63b7dSRichard Lowe clean_up(nse_depinfo_fp, merge_fp, 177*10d63b7dSRichard Lowe nse_depinfo_file, merge_file, 0); 178*10d63b7dSRichard Lowe } else { 179*10d63b7dSRichard Lowe clean_up(nse_depinfo_fp, merge_fp, 180*10d63b7dSRichard Lowe nse_depinfo_file, merge_file, 1); 181*10d63b7dSRichard Lowe } 182*10d63b7dSRichard Lowe if (file_locked) { 183*10d63b7dSRichard Lowe unlink(lock_file); 184*10d63b7dSRichard Lowe } 185*10d63b7dSRichard Lowe unlink(command_output_tmpfile); 186*10d63b7dSRichard Lowe return; 187*10d63b7dSRichard Lowe } /* entry found */ 188*10d63b7dSRichard Lowe fputs(line, merge_fp); 189*10d63b7dSRichard Lowe } 190*10d63b7dSRichard Lowe /* Entry never found. Add it if there is a search path */ 191*10d63b7dSRichard Lowe if (is_path) { 192*10d63b7dSRichard Lowe while (fgets(line, MAXPATHLEN+2, command_output_fp) != NULL) { 193*10d63b7dSRichard Lowe fprintf(nse_depinfo_fp, "%s", line); 194*10d63b7dSRichard Lowe } 195*10d63b7dSRichard Lowe } 196*10d63b7dSRichard Lowe clean_up(nse_depinfo_fp, merge_fp, nse_depinfo_file, merge_file, 1); 197*10d63b7dSRichard Lowe if (file_locked) { 198*10d63b7dSRichard Lowe unlink(lock_file); 199*10d63b7dSRichard Lowe } 200*10d63b7dSRichard Lowe } 201*10d63b7dSRichard Lowe 202*10d63b7dSRichard Lowe } // extern "C" 203*10d63b7dSRichard Lowe 204*10d63b7dSRichard Lowe static void 205*10d63b7dSRichard Lowe report_dep(char *iflag, char *filename) 206*10d63b7dSRichard Lowe { 207*10d63b7dSRichard Lowe 208*10d63b7dSRichard Lowe if (command_output_fp == NULL) { 209*10d63b7dSRichard Lowe sprintf(command_output_tmpfile, 210*10d63b7dSRichard Lowe "%s/%s.%d.XXXXXX", tmpdir, NSE_DEPINFO, getpid()); 211*10d63b7dSRichard Lowe int fd = mkstemp(command_output_tmpfile); 212*10d63b7dSRichard Lowe if ((fd < 0) || (command_output_fp = fdopen(fd, "w")) == NULL) { 213*10d63b7dSRichard Lowe return; 214*10d63b7dSRichard Lowe } 215*10d63b7dSRichard Lowe if ((search_dir = getenv("NSE_DEP")) == NULL) { 216*10d63b7dSRichard Lowe return; 217*10d63b7dSRichard Lowe } 218*10d63b7dSRichard Lowe atexit(close_file); 219*10d63b7dSRichard Lowe strcpy(sfile, filename); 220*10d63b7dSRichard Lowe if (iflag == NULL || *iflag == '\0') { 221*10d63b7dSRichard Lowe return; 222*10d63b7dSRichard Lowe } 223*10d63b7dSRichard Lowe fprintf(command_output_fp, "%s:", sfile); 224*10d63b7dSRichard Lowe } 225*10d63b7dSRichard Lowe fprintf(command_output_fp, " "); 226*10d63b7dSRichard Lowe fprintf(command_output_fp, iflag); 227*10d63b7dSRichard Lowe if (iflag != NULL) { 228*10d63b7dSRichard Lowe is_path = 1; 229*10d63b7dSRichard Lowe } 230*10d63b7dSRichard Lowe } 231*10d63b7dSRichard Lowe 232*10d63b7dSRichard Lowe void 233*10d63b7dSRichard Lowe report_libdep(char *lib, char *flag) 234*10d63b7dSRichard Lowe { 235*10d63b7dSRichard Lowe char *ptr; 236*10d63b7dSRichard Lowe char filename[MAXPATHLEN]; 237*10d63b7dSRichard Lowe char *p; 238*10d63b7dSRichard Lowe 239*10d63b7dSRichard Lowe if ((p= getenv(SUNPRO_DEPENDENCIES)) == NULL) { 240*10d63b7dSRichard Lowe return; 241*10d63b7dSRichard Lowe } 242*10d63b7dSRichard Lowe ptr = strchr(p, ' '); 243*10d63b7dSRichard Lowe if(ptr) { 244*10d63b7dSRichard Lowe sprintf(filename, "%s-%s", ptr+1, flag); 245*10d63b7dSRichard Lowe is_path = 1; 246*10d63b7dSRichard Lowe report_dep(lib, filename); 247*10d63b7dSRichard Lowe } 248*10d63b7dSRichard Lowe } 249*10d63b7dSRichard Lowe 250*10d63b7dSRichard Lowe void 251*10d63b7dSRichard Lowe report_search_path(char *iflag) 252*10d63b7dSRichard Lowe { 253*10d63b7dSRichard Lowe char curdir[MAXPATHLEN]; 254*10d63b7dSRichard Lowe char *sdir; 255*10d63b7dSRichard Lowe char *newiflag; 256*10d63b7dSRichard Lowe char filename[MAXPATHLEN]; 257*10d63b7dSRichard Lowe char *p, *ptr; 258*10d63b7dSRichard Lowe 259*10d63b7dSRichard Lowe if ((sdir = getenv("NSE_DEP")) == NULL) { 260*10d63b7dSRichard Lowe return; 261*10d63b7dSRichard Lowe } 262*10d63b7dSRichard Lowe if ((p= getenv(SUNPRO_DEPENDENCIES)) == NULL) { 263*10d63b7dSRichard Lowe return; 264*10d63b7dSRichard Lowe } 265*10d63b7dSRichard Lowe ptr = strchr(p, ' '); 266*10d63b7dSRichard Lowe if( ! ptr ) { 267*10d63b7dSRichard Lowe return; 268*10d63b7dSRichard Lowe } 269*10d63b7dSRichard Lowe sprintf(filename, "%s-CPP", ptr+1); 270*10d63b7dSRichard Lowe getcwd(curdir, sizeof(curdir)); 271*10d63b7dSRichard Lowe if (strcmp(curdir, sdir) != 0 && strlen(iflag) > 2 && 272*10d63b7dSRichard Lowe iflag[2] != '/') { 273*10d63b7dSRichard Lowe /* Makefile must have had an "cd xx; cc ..." */ 274*10d63b7dSRichard Lowe /* Modify the -I path to be relative to the cd */ 275*10d63b7dSRichard Lowe newiflag = (char *)malloc(strlen(iflag) + strlen(curdir) + 2); 276*10d63b7dSRichard Lowe sprintf(newiflag, "-%c%s/%s", iflag[1], curdir, &iflag[2]); 277*10d63b7dSRichard Lowe report_dep(newiflag, filename); 278*10d63b7dSRichard Lowe } else { 279*10d63b7dSRichard Lowe report_dep(iflag, filename); 280*10d63b7dSRichard Lowe } 281*10d63b7dSRichard Lowe } 282*10d63b7dSRichard Lowe 283*10d63b7dSRichard Lowe void 284*10d63b7dSRichard Lowe report_dependency(const char *name) 285*10d63b7dSRichard Lowe { 286*10d63b7dSRichard Lowe register char *filename; 287*10d63b7dSRichard Lowe char buffer[MAXPATHLEN+1]; 288*10d63b7dSRichard Lowe register char *p; 289*10d63b7dSRichard Lowe register char *p2; 290*10d63b7dSRichard Lowe char nse_depinfo_file[MAXPATHLEN]; 291*10d63b7dSRichard Lowe 292*10d63b7dSRichard Lowe if (report_file == NULL) { 293*10d63b7dSRichard Lowe if ((filename= getenv(SUNPRO_DEPENDENCIES)) == NULL) { 294*10d63b7dSRichard Lowe report_file = (FILE *)-1; 295*10d63b7dSRichard Lowe return; 296*10d63b7dSRichard Lowe } 297*10d63b7dSRichard Lowe if (strlen(filename) == 0) { 298*10d63b7dSRichard Lowe report_file = (FILE *)-1; 299*10d63b7dSRichard Lowe return; 300*10d63b7dSRichard Lowe } 301*10d63b7dSRichard Lowe (void)strcpy(buffer, name); 302*10d63b7dSRichard Lowe name = buffer; 303*10d63b7dSRichard Lowe p = strchr(filename, ' '); 304*10d63b7dSRichard Lowe if(p) { 305*10d63b7dSRichard Lowe *p= 0; 306*10d63b7dSRichard Lowe } else { 307*10d63b7dSRichard Lowe report_file = (FILE *)-1; 308*10d63b7dSRichard Lowe return; 309*10d63b7dSRichard Lowe } 310*10d63b7dSRichard Lowe if ((report_file= fopen(filename, "a")) == NULL) { 311*10d63b7dSRichard Lowe if ((report_file= fopen(filename, "w")) == NULL) { 312*10d63b7dSRichard Lowe report_file= (FILE *)-1; 313*10d63b7dSRichard Lowe return; 314*10d63b7dSRichard Lowe } 315*10d63b7dSRichard Lowe } 316*10d63b7dSRichard Lowe atexit(close_report_file); 317*10d63b7dSRichard Lowe if ((p2= strchr(p+1, ' ')) != NULL) 318*10d63b7dSRichard Lowe *p2= 0; 319*10d63b7dSRichard Lowe target_being_reported_for= (char *)malloc((unsigned)(strlen(p+1)+1)); 320*10d63b7dSRichard Lowe (void)strcpy(target_being_reported_for, p+1); 321*10d63b7dSRichard Lowe (void)fputs(p+1, report_file); 322*10d63b7dSRichard Lowe (void)fputs(":", report_file); 323*10d63b7dSRichard Lowe *p= ' '; 324*10d63b7dSRichard Lowe if (p2 != NULL) 325*10d63b7dSRichard Lowe *p2= ' '; 326*10d63b7dSRichard Lowe } 327*10d63b7dSRichard Lowe if (report_file == (FILE *)-1) 328*10d63b7dSRichard Lowe return; 329*10d63b7dSRichard Lowe (void)fputs(name, report_file); 330*10d63b7dSRichard Lowe (void)fputs(" ", report_file); 331*10d63b7dSRichard Lowe } 332*10d63b7dSRichard Lowe 333*10d63b7dSRichard Lowe 334