xref: /freebsd/bin/sh/show.c (revision 1a958c66539ebf0e7f6083ba71b466b11dc6499f)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes #include <stdio.h>
46aa9caaf6SPeter Wemm #include <stdarg.h>
47d62ec71cSMartin Cracauer #include <errno.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes #include "shell.h"
504b88c807SRodney W. Grimes #include "parser.h"
514b88c807SRodney W. Grimes #include "nodes.h"
524b88c807SRodney W. Grimes #include "mystring.h"
53aa9caaf6SPeter Wemm #include "show.h"
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes 
564b88c807SRodney W. Grimes #ifdef DEBUG
575134c3f7SWarner Losh static void shtree(union node *, int, char *, FILE*);
585134c3f7SWarner Losh static void shcmd(union node *, FILE *);
595134c3f7SWarner Losh static void sharg(union node *, FILE *);
605134c3f7SWarner Losh static void indent(int, char *, FILE *);
615134c3f7SWarner Losh static void trstring(char *);
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes 
64aa9caaf6SPeter Wemm void
655134c3f7SWarner Losh showtree(union node *n)
664b88c807SRodney W. Grimes {
674b88c807SRodney W. Grimes 	trputs("showtree called\n");
684b88c807SRodney W. Grimes 	shtree(n, 1, NULL, stdout);
694b88c807SRodney W. Grimes }
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes 
72aa9caaf6SPeter Wemm static void
735134c3f7SWarner Losh shtree(union node *n, int ind, char *pfx, FILE *fp)
744b88c807SRodney W. Grimes {
754b88c807SRodney W. Grimes 	struct nodelist *lp;
764b88c807SRodney W. Grimes 	char *s;
774b88c807SRodney W. Grimes 
78aa9caaf6SPeter Wemm 	if (n == NULL)
79aa9caaf6SPeter Wemm 		return;
80aa9caaf6SPeter Wemm 
814b88c807SRodney W. Grimes 	indent(ind, pfx, fp);
824b88c807SRodney W. Grimes 	switch(n->type) {
834b88c807SRodney W. Grimes 	case NSEMI:
844b88c807SRodney W. Grimes 		s = "; ";
854b88c807SRodney W. Grimes 		goto binop;
864b88c807SRodney W. Grimes 	case NAND:
874b88c807SRodney W. Grimes 		s = " && ";
884b88c807SRodney W. Grimes 		goto binop;
894b88c807SRodney W. Grimes 	case NOR:
904b88c807SRodney W. Grimes 		s = " || ";
914b88c807SRodney W. Grimes binop:
924b88c807SRodney W. Grimes 		shtree(n->nbinary.ch1, ind, NULL, fp);
934b88c807SRodney W. Grimes 	   /*    if (ind < 0) */
944b88c807SRodney W. Grimes 			fputs(s, fp);
954b88c807SRodney W. Grimes 		shtree(n->nbinary.ch2, ind, NULL, fp);
964b88c807SRodney W. Grimes 		break;
974b88c807SRodney W. Grimes 	case NCMD:
984b88c807SRodney W. Grimes 		shcmd(n, fp);
994b88c807SRodney W. Grimes 		if (ind >= 0)
1004b88c807SRodney W. Grimes 			putc('\n', fp);
1014b88c807SRodney W. Grimes 		break;
1024b88c807SRodney W. Grimes 	case NPIPE:
1034b88c807SRodney W. Grimes 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
1044b88c807SRodney W. Grimes 			shcmd(lp->n, fp);
1054b88c807SRodney W. Grimes 			if (lp->next)
1064b88c807SRodney W. Grimes 				fputs(" | ", fp);
1074b88c807SRodney W. Grimes 		}
1084b88c807SRodney W. Grimes 		if (n->npipe.backgnd)
1094b88c807SRodney W. Grimes 			fputs(" &", fp);
1104b88c807SRodney W. Grimes 		if (ind >= 0)
1114b88c807SRodney W. Grimes 			putc('\n', fp);
1124b88c807SRodney W. Grimes 		break;
1134b88c807SRodney W. Grimes 	default:
1144b88c807SRodney W. Grimes 		fprintf(fp, "<node type %d>", n->type);
1154b88c807SRodney W. Grimes 		if (ind >= 0)
1164b88c807SRodney W. Grimes 			putc('\n', fp);
1174b88c807SRodney W. Grimes 		break;
1184b88c807SRodney W. Grimes 	}
1194b88c807SRodney W. Grimes }
1204b88c807SRodney W. Grimes 
1214b88c807SRodney W. Grimes 
1224b88c807SRodney W. Grimes 
123aa9caaf6SPeter Wemm static void
1245134c3f7SWarner Losh shcmd(union node *cmd, FILE *fp)
1254b88c807SRodney W. Grimes {
1264b88c807SRodney W. Grimes 	union node *np;
1274b88c807SRodney W. Grimes 	int first;
1284b88c807SRodney W. Grimes 	char *s;
1294b88c807SRodney W. Grimes 	int dftfd;
1304b88c807SRodney W. Grimes 
1314b88c807SRodney W. Grimes 	first = 1;
1324b88c807SRodney W. Grimes 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
1334b88c807SRodney W. Grimes 		if (! first)
1344b88c807SRodney W. Grimes 			putchar(' ');
1354b88c807SRodney W. Grimes 		sharg(np, fp);
1364b88c807SRodney W. Grimes 		first = 0;
1374b88c807SRodney W. Grimes 	}
1384b88c807SRodney W. Grimes 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
1394b88c807SRodney W. Grimes 		if (! first)
1404b88c807SRodney W. Grimes 			putchar(' ');
1414b88c807SRodney W. Grimes 		switch (np->nfile.type) {
1424b88c807SRodney W. Grimes 			case NTO:	s = ">";  dftfd = 1; break;
1434b88c807SRodney W. Grimes 			case NAPPEND:	s = ">>"; dftfd = 1; break;
1444b88c807SRodney W. Grimes 			case NTOFD:	s = ">&"; dftfd = 1; break;
1451a958c66STim J. Robbins 			case NCLOBBER:	s = ">|"; dftfd = 1; break;
1464b88c807SRodney W. Grimes 			case NFROM:	s = "<";  dftfd = 0; break;
1474682f420SBrian Somers 			case NFROMTO:	s = "<>"; dftfd = 0; break;
1484b88c807SRodney W. Grimes 			case NFROMFD:	s = "<&"; dftfd = 0; break;
149aa9caaf6SPeter Wemm 			default:  	s = "*error*"; dftfd = 0; break;
1504b88c807SRodney W. Grimes 		}
1514b88c807SRodney W. Grimes 		if (np->nfile.fd != dftfd)
1524b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->nfile.fd);
1534b88c807SRodney W. Grimes 		fputs(s, fp);
1544b88c807SRodney W. Grimes 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
1554b88c807SRodney W. Grimes 			fprintf(fp, "%d", np->ndup.dupfd);
1564b88c807SRodney W. Grimes 		} else {
1574b88c807SRodney W. Grimes 			sharg(np->nfile.fname, fp);
1584b88c807SRodney W. Grimes 		}
1594b88c807SRodney W. Grimes 		first = 0;
1604b88c807SRodney W. Grimes 	}
1614b88c807SRodney W. Grimes }
1624b88c807SRodney W. Grimes 
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 
165aa9caaf6SPeter Wemm static void
1665134c3f7SWarner Losh sharg(union node *arg, FILE *fp)
1674b88c807SRodney W. Grimes {
1684b88c807SRodney W. Grimes 	char *p;
1694b88c807SRodney W. Grimes 	struct nodelist *bqlist;
1704b88c807SRodney W. Grimes 	int subtype;
1714b88c807SRodney W. Grimes 
1724b88c807SRodney W. Grimes 	if (arg->type != NARG) {
1734b88c807SRodney W. Grimes 		printf("<node type %d>\n", arg->type);
1744b88c807SRodney W. Grimes 		fflush(stdout);
1754b88c807SRodney W. Grimes 		abort();
1764b88c807SRodney W. Grimes 	}
1774b88c807SRodney W. Grimes 	bqlist = arg->narg.backquote;
1784b88c807SRodney W. Grimes 	for (p = arg->narg.text ; *p ; p++) {
1794b88c807SRodney W. Grimes 		switch (*p) {
1804b88c807SRodney W. Grimes 		case CTLESC:
1814b88c807SRodney W. Grimes 			putc(*++p, fp);
1824b88c807SRodney W. Grimes 			break;
1834b88c807SRodney W. Grimes 		case CTLVAR:
1844b88c807SRodney W. Grimes 			putc('$', fp);
1854b88c807SRodney W. Grimes 			putc('{', fp);
1864b88c807SRodney W. Grimes 			subtype = *++p;
187aa9caaf6SPeter Wemm 			if (subtype == VSLENGTH)
188aa9caaf6SPeter Wemm 				putc('#', fp);
189aa9caaf6SPeter Wemm 
1904b88c807SRodney W. Grimes 			while (*p != '=')
1914b88c807SRodney W. Grimes 				putc(*p++, fp);
192aa9caaf6SPeter Wemm 
1934b88c807SRodney W. Grimes 			if (subtype & VSNUL)
1944b88c807SRodney W. Grimes 				putc(':', fp);
195aa9caaf6SPeter Wemm 
1964b88c807SRodney W. Grimes 			switch (subtype & VSTYPE) {
1974b88c807SRodney W. Grimes 			case VSNORMAL:
1984b88c807SRodney W. Grimes 				putc('}', fp);
1994b88c807SRodney W. Grimes 				break;
2004b88c807SRodney W. Grimes 			case VSMINUS:
2014b88c807SRodney W. Grimes 				putc('-', fp);
2024b88c807SRodney W. Grimes 				break;
2034b88c807SRodney W. Grimes 			case VSPLUS:
2044b88c807SRodney W. Grimes 				putc('+', fp);
2054b88c807SRodney W. Grimes 				break;
2064b88c807SRodney W. Grimes 			case VSQUESTION:
2074b88c807SRodney W. Grimes 				putc('?', fp);
2084b88c807SRodney W. Grimes 				break;
2094b88c807SRodney W. Grimes 			case VSASSIGN:
2104b88c807SRodney W. Grimes 				putc('=', fp);
2114b88c807SRodney W. Grimes 				break;
212aa9caaf6SPeter Wemm 			case VSTRIMLEFT:
213aa9caaf6SPeter Wemm 				putc('#', fp);
214aa9caaf6SPeter Wemm 				break;
215aa9caaf6SPeter Wemm 			case VSTRIMLEFTMAX:
216aa9caaf6SPeter Wemm 				putc('#', fp);
217aa9caaf6SPeter Wemm 				putc('#', fp);
218aa9caaf6SPeter Wemm 				break;
219aa9caaf6SPeter Wemm 			case VSTRIMRIGHT:
220aa9caaf6SPeter Wemm 				putc('%', fp);
221aa9caaf6SPeter Wemm 				break;
222aa9caaf6SPeter Wemm 			case VSTRIMRIGHTMAX:
223aa9caaf6SPeter Wemm 				putc('%', fp);
224aa9caaf6SPeter Wemm 				putc('%', fp);
225aa9caaf6SPeter Wemm 				break;
226aa9caaf6SPeter Wemm 			case VSLENGTH:
227aa9caaf6SPeter Wemm 				break;
2284b88c807SRodney W. Grimes 			default:
2294b88c807SRodney W. Grimes 				printf("<subtype %d>", subtype);
2304b88c807SRodney W. Grimes 			}
2314b88c807SRodney W. Grimes 			break;
2324b88c807SRodney W. Grimes 		case CTLENDVAR:
2334b88c807SRodney W. Grimes 		     putc('}', fp);
2344b88c807SRodney W. Grimes 		     break;
2354b88c807SRodney W. Grimes 		case CTLBACKQ:
2364b88c807SRodney W. Grimes 		case CTLBACKQ|CTLQUOTE:
2374b88c807SRodney W. Grimes 			putc('$', fp);
2384b88c807SRodney W. Grimes 			putc('(', fp);
2394b88c807SRodney W. Grimes 			shtree(bqlist->n, -1, NULL, fp);
2404b88c807SRodney W. Grimes 			putc(')', fp);
2414b88c807SRodney W. Grimes 			break;
2424b88c807SRodney W. Grimes 		default:
2434b88c807SRodney W. Grimes 			putc(*p, fp);
2444b88c807SRodney W. Grimes 			break;
2454b88c807SRodney W. Grimes 		}
2464b88c807SRodney W. Grimes 	}
2474b88c807SRodney W. Grimes }
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes 
250aa9caaf6SPeter Wemm static void
2515134c3f7SWarner Losh indent(int amount, char *pfx, FILE *fp)
2524b88c807SRodney W. Grimes {
2534b88c807SRodney W. Grimes 	int i;
2544b88c807SRodney W. Grimes 
2554b88c807SRodney W. Grimes 	for (i = 0 ; i < amount ; i++) {
2564b88c807SRodney W. Grimes 		if (pfx && i == amount - 1)
2574b88c807SRodney W. Grimes 			fputs(pfx, fp);
2584b88c807SRodney W. Grimes 		putc('\t', fp);
2594b88c807SRodney W. Grimes 	}
2604b88c807SRodney W. Grimes }
2614b88c807SRodney W. Grimes 
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes /*
2644b88c807SRodney W. Grimes  * Debugging stuff.
2654b88c807SRodney W. Grimes  */
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes FILE *tracefile;
2694b88c807SRodney W. Grimes 
2704b88c807SRodney W. Grimes #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
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
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
3035134c3f7SWarner Losh trputs(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 
313aa9caaf6SPeter Wemm static void
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
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
3705134c3f7SWarner Losh opentrace(void)
3715134c3f7SWarner Losh {
3724b88c807SRodney W. Grimes 	char s[100];
3734b88c807SRodney W. Grimes 	char *getenv();
374aa9caaf6SPeter Wemm #ifdef O_APPEND
3754b88c807SRodney W. Grimes 	int flags;
376aa9caaf6SPeter Wemm #endif
3774b88c807SRodney W. Grimes 
3784b88c807SRodney W. Grimes 	if (!debug)
3794b88c807SRodney W. Grimes 		return;
3804b88c807SRodney W. Grimes #ifdef not_this_way
381aa9caaf6SPeter Wemm 	{
382aa9caaf6SPeter Wemm 		char *p;
3834b88c807SRodney W. Grimes 		if ((p = getenv("HOME")) == NULL) {
3844b88c807SRodney W. Grimes 			if (geteuid() == 0)
3854b88c807SRodney W. Grimes 				p = "/";
3864b88c807SRodney W. Grimes 			else
3874b88c807SRodney W. Grimes 				p = "/tmp";
3884b88c807SRodney W. Grimes 		}
3894b88c807SRodney W. Grimes 		scopy(p, s);
3904b88c807SRodney W. Grimes 		strcat(s, "/trace");
391aa9caaf6SPeter Wemm 	}
3924b88c807SRodney W. Grimes #else
3934b88c807SRodney W. Grimes 	scopy("./trace", s);
3944b88c807SRodney W. Grimes #endif /* not_this_way */
3954b88c807SRodney W. Grimes 	if ((tracefile = fopen(s, "a")) == NULL) {
3966c48b6cfSMartin Cracauer 		fprintf(stderr, "Can't open %s: %s\n", s, strerror(errno));
3974b88c807SRodney W. Grimes 		return;
3984b88c807SRodney W. Grimes 	}
3994b88c807SRodney W. Grimes #ifdef O_APPEND
4004b88c807SRodney W. Grimes 	if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
4014b88c807SRodney W. Grimes 		fcntl(fileno(tracefile), F_SETFL, flags | O_APPEND);
4024b88c807SRodney W. Grimes #endif
4034b88c807SRodney W. Grimes 	fputs("\nTracing started.\n", tracefile);
4044b88c807SRodney W. Grimes 	fflush(tracefile);
4054b88c807SRodney W. Grimes }
406ab0a2172SSteve Price #endif /* DEBUG */
407