From olivas@thos.digiflux.org  Sun Jan  4 05:10:31 2004
Return-Path: <olivas@thos.digiflux.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 39E2916A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  4 Jan 2004 05:10:31 -0800 (PST)
Received: from thos.digiflux.org (43.Red-80-59-151.pooles.rima-tde.net [80.59.151.43])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 16BD843D2F
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  4 Jan 2004 05:10:28 -0800 (PST)
	(envelope-from olivas@thos.digiflux.org)
Received: from thos.digiflux.org (localhost [127.0.0.1])
	by thos.digiflux.org (8.12.6/8.12.6) with ESMTP id i04D9Ne1030300
	for <FreeBSD-gnats-submit@freebsd.org>; Sun, 4 Jan 2004 14:09:23 +0100 (CET)
	(envelope-from olivas@thos.digiflux.org)
Received: (from root@localhost)
	by thos.digiflux.org (8.12.6/8.12.6/Submit) id i04D9MX9030299;
	Sun, 4 Jan 2004 14:09:22 +0100 (CET)
Message-Id: <200401041309.i04D9MX9030299@thos.digiflux.org>
Date: Sun, 4 Jan 2004 14:09:22 +0100 (CET)
From: Stacy Olivas <olivas@digiflux.org>
Reply-To: Stacy Olivas <olivas@digiflux.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: Added -p option to kldxref to allow creation of hints file in alternate path
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         60892
>Category:       bin
>Synopsis:       [patch] added -p option to kldxref(8) to allow creation of hints file in alternate path
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 04 05:20:11 PST 2004
>Closed-Date:    
>Last-Modified:  Tue Oct 25 05:45:44 GMT 2005
>Originator:     Stacy Olivas
>Release:        FreeBSD 5.0-RELEASE-p18 i386
>Organization:
>Environment:
System: FreeBSD thos.digiflux.org 5.0-RELEASE-p18 FreeBSD 5.0-RELEASE-p18 #16: Mon Dec 22 14:22:42 CET 2003 olivas@thos.digiflux.org:/usr/src/sys/i386/compile/Thos i386


	
>Description:
	While working with the PicoBSD build scripts (for WarBSD), I found I needed to make a 
        linker.hints file in an alternate location (other than the /boot/kernel, etc).  The -f
        option only lets you change the filename, not the path.

        This patch adds the -p option to kldxref which allows you to specify a diffrent location to
        create the hints file.

        A side-effect of this patch is it allows you to use the -R option to parse sub dirs and
        store all the dependency info in a single file.
>How-To-Repeat:
	
>Fix:

        My patch:

--- /usr/src/usr.sbin/kldxref/kldxref.c	Mon Apr 22 15:44:44 2002
+++ kldxref.new.c	Sun Jan  4 14:00:07 2004
@@ -2,6 +2,9 @@
  * Copyright (c) 2000, Boris Popov
  * All rights reserved.
  *
+ * Modified by Stacy Olivas (olivas@digiflux.org)
+ * -Added the option "-p [path]" to allow creation of hints file at an alternate location
+ *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
@@ -50,9 +53,12 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <errno.h>
+#include <stdio.h>
 
 #include "ef.h"
 
+#define DEBUG		1	/* Set to 1 for debugging output */
+
 #define	MAXRECSIZE	1024
 #define check(val)	if ((error = (val)) != 0) break
 
@@ -81,6 +87,8 @@
 FILE *fxref;
 
 static const char *xref_file = "linker.hints";
+static const char *xref_path = "";		/* path for creating hints file */
+static int xref_flag = 0;  			/* 1 = filename specified */
 
 static char recbuf[MAXRECSIZE];
 static int recpos, reccnt;
@@ -261,6 +269,9 @@
 	strcpy(p, "lhint.XXXXXX");
 	if (mkstemp(dest) == -1)
 		err(1, "%s", dest);
+#if DEBUG
+        printf("--> Temp file: %s\n", dest);
+#endif
 }
 
 static char xrefname[MAXPATHLEN], tempname[MAXPATHLEN];
@@ -275,7 +286,7 @@
 	fts_options = FTS_PHYSICAL;
 /*	SLIST_INIT(&kldlist);*/
 
-	while ((opt = getopt(argc, argv, "Rdf:v")) != -1) {
+	while ((opt = getopt(argc, argv, "Rdf:p:v")) != -1) {
 		switch (opt) {
 		case 'd':
 			dflag = 1;
@@ -283,6 +294,10 @@
 		case 'f':
 			xref_file = optarg;
 			break;
+                case 'p':			/* alternate path specified */
+                        xref_path = optarg;
+                        xref_flag = 1;		
+			break;
 		case 'v':
 			verbose++;
 			break;
@@ -299,13 +314,19 @@
 	argc -= optind;
 	argv += optind;
 
+#if DEBUG
+/* print out variable info set from command line args */
+        printf("--> xref_file: %s\n" 
+               "--> xref_path: %s\n"
+               "--> xref_flag: %d\n", xref_file, xref_path, xref_flag);
+#endif
 	ftsp = fts_open(argv, fts_options, 0);
 	if (ftsp == NULL)
 		exit(1);
 
 	for (;;) {
 		p = fts_read(ftsp);
-		if ((p == NULL || p->fts_info == FTS_D) && !dflag && fxref) {
+		if ((p == NULL || p->fts_info == FTS_D) && !dflag && fxref && !xref_flag) {
 			fclose(fxref);
 			if (reccnt) {
 				rename(tempname, xrefname);
@@ -314,19 +335,49 @@
 				unlink(xrefname);
 			}
 		}
+
+/* if xref_flag is set, only cleanup after everything is finished. */
+                if (p == NULL && !dflag && xref_flag)
+                {
+                   fclose(fxref);
+                   if (reccnt) {
+                           rename(tempname, xrefname);
+                   } else {
+                           unlink(tempname);
+                           unlink(xrefname);
+                   }
+                }
 		if (p == NULL)
 			break;
-		if (p && p->fts_info == FTS_D && !dflag) {
+
+/* if xref_flag is set, create temp file in path specified */
+                if (p && p->fts_info == FTS_D && !dflag && xref_flag == 1) {
+                        snprintf(xrefname, sizeof(xrefname), "%s/%s", xref_path, xref_file);
+#if DEBUG
+                        printf("--> xrefname: %s\n", xrefname);
+#endif
+                        maketempfile(tempname, xref_path);
+                        xref_flag++;
+                        fxref = fopen(tempname, "w+t");
+                        if (fxref == NULL)
+                                err(1, "can't create %s", tempname);
+                        ival = 1;
+                        fwrite(&ival, sizeof(ival), 1, fxref);
+                        reccnt = 0;
+                }
+
+		if (p && p->fts_info == FTS_D && !dflag && !xref_flag) {
 			snprintf(xrefname, sizeof(xrefname), "%s/%s",
-			    ftsp->fts_path, xref_file);
+		           ftsp->fts_path, xref_file);
 			maketempfile(tempname, ftsp->fts_path);
-			fxref = fopen(tempname, "w+t");
-			if (fxref == NULL)
-				err(1, "can't create %s", tempname);
-			ival = 1;
-			fwrite(&ival, sizeof(ival), 1, fxref);
-			reccnt = 0;
-		}
+  		        fxref = fopen(tempname, "w+t");
+		        if (fxref == NULL)
+			        err(1, "can't create %s", tempname);
+		        ival = 1;
+		        fwrite(&ival, sizeof(ival), 1, fxref);
+		        reccnt = 0;
+               }
+             
 		if (p->fts_info != FTS_F)
 			continue;
 		read_kld(p->fts_path, p->fts_name);
@@ -340,7 +391,7 @@
 {
 
 	fprintf(stderr, "%s\n",
-	    "usage: kldxref [-Rdv] [-f hintfile] path [path..]"
+	    "usage: kldxref [-Rpdv] [[-f hintfile] || [-p hintfile]] path [path..]"
 	);
 	exit(1);
 }
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Sun Jan 4 11:05:31 PST 2004 
Responsible-Changed-Why:  
Rescue this from 'pending'; not sure if it should be kern or conf. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=60892 
>Unformatted:
