xref: /freebsd/bin/sh/show.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Kenneth Almquist.
94b88c807SRodney W. Grimes  *
104b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
114b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
124b88c807SRodney W. Grimes  * are met:
134b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
154b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
164b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
174b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
194b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
204b88c807SRodney W. Grimes  *    without specific prior written permission.
214b88c807SRodney W. Grimes  *
224b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324b88c807SRodney W. Grimes  * SUCH DAMAGE.
334b88c807SRodney W. Grimes  */
344b88c807SRodney W. Grimes 
356da31df8STim J. Robbins #include <fcntl.h>
364b88c807SRodney W. Grimes #include <stdio.h>
376da31df8STim J. Robbins #include <stdlib.h>
38aa9caaf6SPeter Wemm #include <stdarg.h>
39d62ec71cSMartin Cracauer #include <errno.h>
40aa9caaf6SPeter Wemm 
414b88c807SRodney W. Grimes #include "shell.h"
424b88c807SRodney W. Grimes #include "parser.h"
434b88c807SRodney W. Grimes #include "nodes.h"
444b88c807SRodney W. Grimes #include "mystring.h"
45aa9caaf6SPeter Wemm #include "show.h"
464b88c807SRodney W. Grimes 
474b88c807SRodney W. Grimes 
484b88c807SRodney W. Grimes #ifdef DEBUG
4988328642SDavid E. O'Brien static void shtree(union node *, int, char *, FILE*);
5088328642SDavid E. O'Brien static void shcmd(union node *, FILE *);
5188328642SDavid E. O'Brien static void sharg(union node *, FILE *);
5288328642SDavid E. O'Brien static void indent(int, char *, FILE *);
5388328642SDavid E. O'Brien static void trstring(char *);
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes 
56aa9caaf6SPeter Wemm void
showtree(union node * n)575134c3f7SWarner Losh showtree(union node *n)
584b88c807SRodney W. Grimes {
594b88c807SRodney W. Grimes 	trputs("showtree called\n");
604b88c807SRodney W. Grimes 	shtree(n, 1, NULL, stdout);
614b88c807SRodney W. Grimes }
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes 
6488328642SDavid E. O'Brien static void
shtree(union node * n,int ind,char * pfx,FILE * fp)655134c3f7SWarner Losh shtree(union node *n, int ind, char *pfx, FILE *fp)
664b88c807SRodney W. Grimes {
674b88c807SRodney W. Grimes 	struct nodelist *lp;
68*1a4e959eSPiotr Pawel Stefaniak 	const char *s;
694b88c807SRodney W. Grimes 
70aa9caaf6SPeter Wemm 	if (n == NULL)
71aa9caaf6SPeter Wemm 		return;
72aa9caaf6SPeter Wemm 
734b88c807SRodney W. Grimes 	indent(ind, pfx, fp);
744b88c807SRodney W. Grimes 	switch(n->type) {
754b88c807SRodney W. Grimes 	case NSEMI:
764b88c807SRodney W. Grimes 		s = "; ";
774b88c807SRodney W. Grimes 		goto binop;
784b88c807SRodney W. Grimes 	case NAND:
794b88c807SRodney W. Grimes 		s = " && ";
804b88c807SRodney W. Grimes 		goto binop;
814b88c807SRodney W. Grimes 	case NOR:
824b88c807SRodney W. Grimes 		s = " || ";
834b88c807SRodney W. Grimes binop:
844b88c807SRodney W. Grimes 		shtree(n->nbinary.ch1, ind, NULL, fp);
854b88c807SRodney W. Grimes 	   /*    if (ind < 0) */
864b88c807SRodney W. Grimes 			fputs(s, fp);
874b88c807SRodney W. Grimes 		shtree(n->nbinary.ch2, ind, NULL, fp);
884b88c807SRodney W. Grimes 		break;
894b88c807SRodney W. Grimes 	case NCMD:
904b88c807SRodney W. Grimes 		shcmd(n, fp);
914b88c807SRodney W. Grimes 		if (ind >= 0)
924b88c807SRodney W. Grimes 			putc('\n', fp);
934b88c807SRodney W. Grimes 		break;
944b88c807SRodney W. Grimes 	case NPIPE:
954b88c807SRodney W. Grimes 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
964b88c807SRodney W. Grimes 			shcmd(lp->n, fp);
974b88c807SRodney W. Grimes 			if (lp->next)
984b88c807SRodney W. Grimes 				fputs(" | ", fp);
994b88c807SRodney W. Grimes 		}
1004b88c807SRodney W. Grimes 		if (n->npipe.backgnd)
1014b88c807SRodney W. Grimes 			fputs(" &", fp);
1024b88c807SRodney W. Grimes 		if (ind >= 0)
1034b88c807SRodney W. Grimes 			putc('\n', fp);
1044b88c807SRodney W. Grimes 		break;
1054b88c807SRodney W. Grimes 	default:
1064b88c807SRodney W. Grimes 		fprintf(fp, "<node type %d>", n->type);
1074b88c807SRodney W. Grimes 		if (ind >= 0)
1084b88c807SRodney W. Grimes 			putc('\n', fp);
1094b88c807SRodney W. Grimes 		break;
1104b88c807SRodney W. Grimes 	}
1114b88c807SRodney W. Grimes }
1124b88c807SRodney W. Grimes 
1134b88c807SRodney W. Grimes 
1144b88c807SRodney W. Grimes 
11588328642SDavid E. O'Brien static void
shcmd(union node * cmd,FILE * fp)1165134c3f7SWarner Losh shcmd(union node *cmd, FILE *fp)
1174b88c807SRodney W. Grimes {
1184b88c807SRodney W. Grimes 	union node *np;
1194b88c807SRodney W. Grimes 	int first;
120*1a4e959eSPiotr Pawel Stefaniak 	const char *s;
1214b88c807SRodney W. Grimes 	int dftfd;
1224b88c807SRodney W. Grimes 
1234b88c807SRodney W. Grimes 	first = 1;
1244b88c807SRodney W. Grimes 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
1254b88c807SRodney W. Grimes 		if (! first)
1264b88c807SRodney W. Grimes 			putchar(' ');
1274b88c807SRodney W. Grimes 		sharg(np, fp);
1284b88c807SRodney W. Grimes 		first = 0;
1294b88c807SRodney W. Grimes 	}
1304b88c807SRodney W. Grimes 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
1314b88c807SRodney W. Grimes 		if (! first)
1324b88c807SRodney W. Grimes 			putchar(' ');
1334b88c807SRodney W. Grimes 		switch (np->nfile.type) {
1344b88c807SRodney W. Grimes 			case NTO:	s = ">";  dftfd = 1; break;
1354b88c807SRodney W. Grimes 			case NAPPEND:	s = ">>"; dftfd = 1; break;
1364b88c807SRodney W. Grimes 			case NTOFD:	s = ">&"; dftfd = 1; break;
1371a958c66STim J. Robbins 			case NCLOBBER:	s = ">|"; dftfd = 1; break;
1384b88c807SRodney W. Grimes 			case NFROM:	s = "<";  dftfd = 0; break;
1394682f420SBrian Somers 			case NFROMTO:	s = "<>"; dftfd = 0; break;
1404b88c807SRodney W. Grimes 			case NFROMFD:	s = "<&"; dftfd = 0; break;
14162463f67SJens Schweikhardt 			case NHERE:	s = "<<"; dftfd = 0; break;
14262463f67SJens Schweikhardt 			case NXHERE:	s = "<<"; dftfd = 0; break;
143aa9caaf6SPeter Wemm 			default:  	s = "*error*"; dftfd = 0; break;
1444b88c807SRodney W. Grimes 		}
1454b88c807SRodney W. Grimes 		if (np->nfile.fd != dftfd)
1464b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->nfile.fd);
1474b88c807SRodney W. Grimes 		fputs(s, fp);
1484b88c807SRodney W. Grimes 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
14990735447STim J. Robbins 			if (np->ndup.dupfd >= 0)
1504b88c807SRodney W. Grimes 				fprintf(fp, "%d", np->ndup.dupfd);
15190735447STim J. Robbins 			else
15290735447STim J. Robbins 				fprintf(fp, "-");
15362463f67SJens Schweikhardt 		} else if (np->nfile.type == NHERE) {
15462463f67SJens Schweikhardt 				fprintf(fp, "HERE");
15562463f67SJens Schweikhardt 		} else if (np->nfile.type == NXHERE) {
15662463f67SJens Schweikhardt 				fprintf(fp, "XHERE");
1574b88c807SRodney W. Grimes 		} else {
1584b88c807SRodney W. Grimes 			sharg(np->nfile.fname, fp);
1594b88c807SRodney W. Grimes 		}
1604b88c807SRodney W. Grimes 		first = 0;
1614b88c807SRodney W. Grimes 	}
1624b88c807SRodney W. Grimes }
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 
1654b88c807SRodney W. Grimes 
16688328642SDavid E. O'Brien static void
sharg(union node * arg,FILE * fp)1675134c3f7SWarner Losh sharg(union node *arg, FILE *fp)
1684b88c807SRodney W. Grimes {
1694b88c807SRodney W. Grimes 	char *p;
1704b88c807SRodney W. Grimes 	struct nodelist *bqlist;
1714b88c807SRodney W. Grimes 	int subtype;
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 	if (arg->type != NARG) {
1744b88c807SRodney W. Grimes 		printf("<node type %d>\n", arg->type);
1754b88c807SRodney W. Grimes 		fflush(stdout);
1764b88c807SRodney W. Grimes 		abort();
1774b88c807SRodney W. Grimes 	}
1784b88c807SRodney W. Grimes 	bqlist = arg->narg.backquote;
1794b88c807SRodney W. Grimes 	for (p = arg->narg.text ; *p ; p++) {
1804b88c807SRodney W. Grimes 		switch (*p) {
1814b88c807SRodney W. Grimes 		case CTLESC:
1824b88c807SRodney W. Grimes 			putc(*++p, fp);
1834b88c807SRodney W. Grimes 			break;
1844b88c807SRodney W. Grimes 		case CTLVAR:
1854b88c807SRodney W. Grimes 			putc('$', fp);
1864b88c807SRodney W. Grimes 			putc('{', fp);
1874b88c807SRodney W. Grimes 			subtype = *++p;
188aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH)
189aa9caaf6SPeter Wemm 				putc('#', fp);
190aa9caaf6SPeter Wemm 
1914b88c807SRodney W. Grimes 			while (*p != '=')
1924b88c807SRodney W. Grimes 				putc(*p++, fp);
193aa9caaf6SPeter Wemm 
1944b88c807SRodney W. Grimes 			if (subtype & VSNUL)
1954b88c807SRodney W. Grimes 				putc(':', fp);
196aa9caaf6SPeter Wemm 
1974b88c807SRodney W. Grimes 			switch (subtype & VSTYPE) {
1984b88c807SRodney W. Grimes 			case VSNORMAL:
1994b88c807SRodney W. Grimes 				putc('}', fp);
2004b88c807SRodney W. Grimes 				break;
2014b88c807SRodney W. Grimes 			case VSMINUS:
2024b88c807SRodney W. Grimes 				putc('-', fp);
2034b88c807SRodney W. Grimes 				break;
2044b88c807SRodney W. Grimes 			case VSPLUS:
2054b88c807SRodney W. Grimes 				putc('+', fp);
2064b88c807SRodney W. Grimes 				break;
2074b88c807SRodney W. Grimes 			case VSQUESTION:
2084b88c807SRodney W. Grimes 				putc('?', fp);
2094b88c807SRodney W. Grimes 				break;
2104b88c807SRodney W. Grimes 			case VSASSIGN:
2114b88c807SRodney W. Grimes 				putc('=', fp);
2124b88c807SRodney W. Grimes 				break;
213aa9caaf6SPeter Wemm 			case VSTRIMLEFT:
214aa9caaf6SPeter Wemm 				putc('#', fp);
215aa9caaf6SPeter Wemm 				break;
216aa9caaf6SPeter Wemm 			case VSTRIMLEFTMAX:
217aa9caaf6SPeter Wemm 				putc('#', fp);
218aa9caaf6SPeter Wemm 				putc('#', fp);
219aa9caaf6SPeter Wemm 				break;
220aa9caaf6SPeter Wemm 			case VSTRIMRIGHT:
221aa9caaf6SPeter Wemm 				putc('%', fp);
222aa9caaf6SPeter Wemm 				break;
223aa9caaf6SPeter Wemm 			case VSTRIMRIGHTMAX:
224aa9caaf6SPeter Wemm 				putc('%', fp);
225aa9caaf6SPeter Wemm 				putc('%', fp);
226aa9caaf6SPeter Wemm 				break;
227aa9caaf6SPeter Wemm 			case VSLENGTH:
228aa9caaf6SPeter Wemm 				break;
2294b88c807SRodney W. Grimes 			default:
2304b88c807SRodney W. Grimes 				printf("<subtype %d>", subtype);
2314b88c807SRodney W. Grimes 			}
2324b88c807SRodney W. Grimes 			break;
2334b88c807SRodney W. Grimes 		case CTLENDVAR:
2344b88c807SRodney W. Grimes 		     putc('}', fp);
2354b88c807SRodney W. Grimes 		     break;
2364b88c807SRodney W. Grimes 		case CTLBACKQ:
2374b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
2384b88c807SRodney W. Grimes 			putc('$', fp);
2394b88c807SRodney W. Grimes 			putc('(', fp);
2404b88c807SRodney W. Grimes 			shtree(bqlist->n, -1, NULL, fp);
2414b88c807SRodney W. Grimes 			putc(')', fp);
2424b88c807SRodney W. Grimes 			break;
2434b88c807SRodney W. Grimes 		default:
2444b88c807SRodney W. Grimes 			putc(*p, fp);
2454b88c807SRodney W. Grimes 			break;
2464b88c807SRodney W. Grimes 		}
2474b88c807SRodney W. Grimes 	}
2484b88c807SRodney W. Grimes }
2494b88c807SRodney W. Grimes 
2504b88c807SRodney W. Grimes 
25188328642SDavid E. O'Brien static void
indent(int amount,char * pfx,FILE * fp)2525134c3f7SWarner Losh indent(int amount, char *pfx, FILE *fp)
2534b88c807SRodney W. Grimes {
2544b88c807SRodney W. Grimes 	int i;
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes 	for (i = 0 ; i < amount ; i++) {
2574b88c807SRodney W. Grimes 		if (pfx && i == amount - 1)
2584b88c807SRodney W. Grimes 			fputs(pfx, fp);
2594b88c807SRodney W. Grimes 		putc('\t', fp);
2604b88c807SRodney W. Grimes 	}
2614b88c807SRodney W. Grimes }
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes 
2644b88c807SRodney W. Grimes /*
2654b88c807SRodney W. Grimes  * Debugging stuff.
2664b88c807SRodney W. Grimes  */
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 
269*1a4e959eSPiotr Pawel Stefaniak static FILE *tracefile;
2709d22cd9bSDavid E. O'Brien #if DEBUG >= 2
2714b88c807SRodney W. Grimes int debug = 1;
2724b88c807SRodney W. Grimes #else
2734b88c807SRodney W. Grimes int debug = 0;
2744b88c807SRodney W. Grimes #endif
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes 
277aa9caaf6SPeter Wemm void
trputc(int c)2785134c3f7SWarner Losh trputc(int c)
279aa9caaf6SPeter Wemm {
2804b88c807SRodney W. Grimes 	if (tracefile == NULL)
2814b88c807SRodney W. Grimes 		return;
2824b88c807SRodney W. Grimes 	putc(c, tracefile);
2834b88c807SRodney W. Grimes 	if (c == '\n')
2844b88c807SRodney W. Grimes 		fflush(tracefile);
2854b88c807SRodney W. Grimes }
2864b88c807SRodney W. Grimes 
287ab0a2172SSteve Price 
288aa9caaf6SPeter Wemm void
sh_trace(const char * fmt,...)289d62ec71cSMartin Cracauer sh_trace(const char *fmt, ...)
2904b88c807SRodney W. Grimes {
291aa9caaf6SPeter Wemm 	va_list va;
292aa9caaf6SPeter Wemm 	va_start(va, fmt);
293aa9caaf6SPeter Wemm 	if (tracefile != NULL) {
294aa9caaf6SPeter Wemm 		(void) vfprintf(tracefile, fmt, va);
2954b88c807SRodney W. Grimes 		if (strchr(fmt, '\n'))
296aa9caaf6SPeter Wemm 			(void) fflush(tracefile);
297aa9caaf6SPeter Wemm 	}
298aa9caaf6SPeter Wemm 	va_end(va);
2994b88c807SRodney W. Grimes }
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes 
302aa9caaf6SPeter Wemm void
trputs(const char * s)3032cac6e36SJilles Tjoelker trputs(const char *s)
3044b88c807SRodney W. Grimes {
3054b88c807SRodney W. Grimes 	if (tracefile == NULL)
3064b88c807SRodney W. Grimes 		return;
3074b88c807SRodney W. Grimes 	fputs(s, tracefile);
3084b88c807SRodney W. Grimes 	if (strchr(s, '\n'))
3094b88c807SRodney W. Grimes 		fflush(tracefile);
3104b88c807SRodney W. Grimes }
3114b88c807SRodney W. Grimes 
3124b88c807SRodney W. Grimes 
31388328642SDavid E. O'Brien static void
trstring(char * s)3145134c3f7SWarner Losh trstring(char *s)
3154b88c807SRodney W. Grimes {
316afb033d5SSteve Price 	char *p;
3174b88c807SRodney W. Grimes 	char c;
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 	if (tracefile == NULL)
3204b88c807SRodney W. Grimes 		return;
3214b88c807SRodney W. Grimes 	putc('"', tracefile);
3224b88c807SRodney W. Grimes 	for (p = s ; *p ; p++) {
3234b88c807SRodney W. Grimes 		switch (*p) {
3244b88c807SRodney W. Grimes 		case '\n':  c = 'n';  goto backslash;
3254b88c807SRodney W. Grimes 		case '\t':  c = 't';  goto backslash;
3264b88c807SRodney W. Grimes 		case '\r':  c = 'r';  goto backslash;
3274b88c807SRodney W. Grimes 		case '"':  c = '"';  goto backslash;
3284b88c807SRodney W. Grimes 		case '\\':  c = '\\';  goto backslash;
3294b88c807SRodney W. Grimes 		case CTLESC:  c = 'e';  goto backslash;
3304b88c807SRodney W. Grimes 		case CTLVAR:  c = 'v';  goto backslash;
3314b88c807SRodney W. Grimes 		case CTLVAR+CTLQUOTE:  c = 'V';  goto backslash;
3324b88c807SRodney W. Grimes 		case CTLBACKQ:  c = 'q';  goto backslash;
3334b88c807SRodney W. Grimes 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
3344b88c807SRodney W. Grimes backslash:	  putc('\\', tracefile);
3354b88c807SRodney W. Grimes 			putc(c, tracefile);
3364b88c807SRodney W. Grimes 			break;
3374b88c807SRodney W. Grimes 		default:
3384b88c807SRodney W. Grimes 			if (*p >= ' ' && *p <= '~')
3394b88c807SRodney W. Grimes 				putc(*p, tracefile);
3404b88c807SRodney W. Grimes 			else {
3414b88c807SRodney W. Grimes 				putc('\\', tracefile);
3424b88c807SRodney W. Grimes 				putc(*p >> 6 & 03, tracefile);
3434b88c807SRodney W. Grimes 				putc(*p >> 3 & 07, tracefile);
3444b88c807SRodney W. Grimes 				putc(*p & 07, tracefile);
3454b88c807SRodney W. Grimes 			}
3464b88c807SRodney W. Grimes 			break;
3474b88c807SRodney W. Grimes 		}
3484b88c807SRodney W. Grimes 	}
3494b88c807SRodney W. Grimes 	putc('"', tracefile);
3504b88c807SRodney W. Grimes }
3514b88c807SRodney W. Grimes 
3524b88c807SRodney W. Grimes 
353aa9caaf6SPeter Wemm void
trargs(char ** ap)3545134c3f7SWarner Losh trargs(char **ap)
3554b88c807SRodney W. Grimes {
3564b88c807SRodney W. Grimes 	if (tracefile == NULL)
3574b88c807SRodney W. Grimes 		return;
3584b88c807SRodney W. Grimes 	while (*ap) {
3594b88c807SRodney W. Grimes 		trstring(*ap++);
3604b88c807SRodney W. Grimes 		if (*ap)
3614b88c807SRodney W. Grimes 			putc(' ', tracefile);
3624b88c807SRodney W. Grimes 		else
3634b88c807SRodney W. Grimes 			putc('\n', tracefile);
3644b88c807SRodney W. Grimes 	}
3654b88c807SRodney W. Grimes 	fflush(tracefile);
3664b88c807SRodney W. Grimes }
3674b88c807SRodney W. Grimes 
3684b88c807SRodney W. Grimes 
369aa9caaf6SPeter Wemm void
opentrace(void)3705134c3f7SWarner Losh opentrace(void)
3715134c3f7SWarner Losh {
3724b88c807SRodney W. Grimes 	char s[100];
3734b88c807SRodney W. Grimes 	int flags;
3744b88c807SRodney W. Grimes 
3754b88c807SRodney W. Grimes 	if (!debug)
3764b88c807SRodney W. Grimes 		return;
3774b88c807SRodney W. Grimes #ifdef not_this_way
378aa9caaf6SPeter Wemm 	{
379aa9caaf6SPeter Wemm 		char *p;
3804b88c807SRodney W. Grimes 		if ((p = getenv("HOME")) == NULL) {
3814b88c807SRodney W. Grimes 			if (geteuid() == 0)
3824b88c807SRodney W. Grimes 				p = "/";
3834b88c807SRodney W. Grimes 			else
3844b88c807SRodney W. Grimes 				p = "/tmp";
3854b88c807SRodney W. Grimes 		}
386670dd3f0SJilles Tjoelker 		strcpy(s, p);
3874b88c807SRodney W. Grimes 		strcat(s, "/trace");
388aa9caaf6SPeter Wemm 	}
3894b88c807SRodney W. Grimes #else
390670dd3f0SJilles Tjoelker 	strcpy(s, "./trace");
3914b88c807SRodney W. Grimes #endif /* not_this_way */
3924b88c807SRodney W. Grimes 	if ((tracefile = fopen(s, "a")) == NULL) {
3936c48b6cfSMartin Cracauer 		fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
3944b88c807SRodney W. Grimes 		return;
3954b88c807SRodney W. Grimes 	}
3964b88c807SRodney W. Grimes 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
3974b88c807SRodney W. Grimes 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
3984b88c807SRodney W. Grimes 	fputs("\nTracing started.\n", tracefile);
3994b88c807SRodney W. Grimes 	fflush(tracefile);
4004b88c807SRodney W. Grimes }
401ab0a2172SSteve Price #endif /* DEBUG */
402