summaryrefslogtreecommitdiff
path: root/src/tet3/tcc/resdir.c
blob: 86232bd8eb95d1690b68c6fc832298c03de1e96c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 *	SCCS: @(#)resdir.c	1.4 (96/11/04)
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1996 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 * A copy of the end-user licence agreement is contained in the file
 * Licence which accompanies this distribution.
 * 
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/************************************************************************

SCCS:   	@(#)resdir.c	1.4 96/11/04 TETware release 3.3
NAME:		resdir.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	August 1996

DESCRIPTION:
	functions to create and administer the results directory

MODIFICATIONS:

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <time.h>
#include "dtmac.h"
#include "error.h"
#include "dtetlib.h"
#include "tetdir.h"
#include "tcc.h"

/* results directory name - gets overwritten by initresdir() */
static char *results_dir = "results";

/* static function declarations */
static char *ressubdir PROTOLIST((char *));


/*
**	initresdir() - create the results directory
**
**	iopt is the -i command-line option
**	cwd is tcc's initial working directory
*/

void initresdir(iopt, cwd)
char *iopt, *cwd;
{
	char resroot[MAXPATH], resdir[MAXPATH];
	int n, rc;

	/* Empty string means no results directory. */
	if (iopt && !*iopt) {
		results_dir = NULL;
		return;
	}

	/*
	** determine the name of the results directory
	**
	** if -i has not been specified, create a default directory
	** whose name is tet_tsroot/results/NNNN{bec}
	*/
	if (iopt && *iopt) {
		fullpath(cwd, iopt, resdir, sizeof resdir, 0);
		errno = 0;
		if (tet_mkalldirs(resdir) != 0 && errno != EEXIST)
			tcc_exit(1);
	}
	else {
		fullpath(tet_tsroot, results_dir, resroot, sizeof resroot, 0);
		for (n = 0; n < 5; n++) {
			fullpath(resroot, ressubdir(resroot), resdir,
				sizeof resdir, 0);
			errno = 0;
			if ((rc = tet_mkalldirs(resdir)) == 0 ||
				errno != EEXIST)
					break;
		}
		if (rc < 0)
			tcc_exit(1);
	}

	results_dir = rstrstore(resdir);
	TRACE2(tet_Ttcc, 1, "results directory = %s", results_dir);
}

/*
**	resdirname() - return the name of the results directory
*/

char *resdirname()
{
	if (results_dir) {
		ASSERT(isabspathloc(results_dir));
	}
	return(results_dir);
}

/*
**	ressubdir() - return the name of the results subdirectory to use
*/

static char *ressubdir(resroot)
char *resroot;
{
	DIR *dirp;
	struct dirent *dp;
	int thisval, maxval;
	static char buf[8];

	/* if the results directory exists, open it and find the
	** highest numbered subdirectory below it
	*/
	maxval = 0;
	if ((dirp = OPENDIR(resroot)) == (DIR *) 0) {
		if (errno != ENOENT)
			fatal(errno, "can't open", resroot);
	}
	else {
		while ((dp = READDIR(dirp)) != (struct dirent *) 0)
			if ((thisval = atoi(dp->d_name)) > maxval)
				maxval = thisval;
		(void) CLOSEDIR(dirp);
	}

	/*
	** check for overflow - this should not usually occur because
	** most systems limit the number of links (i.e., subdirectories)
	** to a much lower value
	*/
	if (++maxval > 9999)
		fatal(0, "too many results directories below", resroot);

	/* construct the name of the subdirectory for the selected
		modes of operation */
	(void) sprintf(buf, "%04d%s", maxval, resdirsuffix());

	return(buf);
}

/*
**	resdirsuffix() - return pointer to the suffix to use for the
**		results directory on the local system, or the saved files
**		directory on a remote system
*/

char *resdirsuffix()
{
	static char suffix[4];
	register char *p;

	if (!suffix[0]) {
		p = suffix;
		if (tcc_modes & TCC_BUILD)
			*p++ = 'b';
		if (tcc_modes & TCC_EXEC)
			*p++ = 'e';
		if (tcc_modes & TCC_CLEAN)
			*p++ = 'c';
	}

	return(suffix);
}