File: fnm2path.c

package info (click to toggle)
dcc 1.2.74-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,552 kB
  • ctags: 4,041
  • sloc: ansic: 41,034; perl: 2,310; sh: 2,186; makefile: 224
file content (153 lines) | stat: -rw-r--r-- 3,669 bytes parent folder | download | duplicates (2)
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
/* Distributed Checksum Clearinghouse
 *
 * Copyright (c) 2005 by Rhyolite Software
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE
 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 *
 * Rhyolite Software DCC 1.2.74-1.15 $Revision$
 */

#include "dcc_defs.h"
#ifdef DCC_WIN32
#include <direct.h>
#endif

DCC_PATH dcc_homedir;

DCC_PATH dcc_path_buf;

/* make an obvious pathname from a file name for printing */
const char *
dcc_fnm2path(DCC_PATH path, const char *nm)
{
#ifdef DCC_WIN32
	char *p;
	DWORD lasterror;
#else
	int dir_len, j;
	const char *dir, *p;
#endif

	/* the answer is the input pathname if it is null or absolute */
	if (!nm)
		return "(null)";
	if (*nm == '\0')
		return "";
	if (dcc_homedir[0] == '\0') {
		STRLIMCPY(path, sizeof(DCC_PATH), nm);
		return path;
	}

#ifdef DCC_WIN32
	lasterror = GetLastError();
	GetFullPathName(nm, sizeof(DCC_PATH), path, &p);
	SetLastError(lasterror);
#else
	if (nm[0] == '/') {
		STRLIMCPY(path, sizeof(DCC_PATH), nm);
		return path;
	}

	/* trim trailing slash from input file or pathname */
	dir = dcc_homedir;
	dir_len = strlen(dir);
	while (dir_len > 1 && dir[dir_len-1] == '/')
		--dir_len;

	/* remove extra "../" and "./" */
	for (;;) {
		if (dir_len == 0
		    || (dir_len == 1 && dir[0] == '.'
			&& strcmp(nm, ".")
			&& strcmp(nm, "..")
			&& !strchr(nm, '/'))) {
			STRLIMCPY(path, sizeof(DCC_PATH), nm);
			return path;
		}
		if (!strncmp(nm, "./", 2)
		    || !strcmp(nm, ".")) {
			if (*++nm  != '\0')
				++nm;
		} else if (!strncmp(nm,  "../", 3)
			   || !strcmp(nm,  "..")) {
			p = strrchr(dir, '/');
			if (!p || p >= &dir[dir_len]) {
				if (!strncmp(dir, "..", dir_len))
					break;
				p = dir;
			}
			j = p-dir;
			if (!strncmp(p, "/..", dir_len-j))
				break;
			dir_len = j;
			nm += 2;
			if (*nm != '\0')
				++nm;
		} else {
			break;
		}
		if (*nm == '\0') {
			if (dir_len == 0) {
				dir = ".";
				dir_len = 1;
			}
			snprintf(path, sizeof(DCC_PATH), "%.*s", dir_len, dir);
			return path;
		}
	}

	snprintf(path, sizeof(DCC_PATH), "%.*s/%s", dir_len, dir, nm);
#endif
	return path;
}



/* change to the DCC home directory */
u_char					/* 0=failed 1=ok */
dcc_cdhome(DCC_EMSG emsg, const char *newdir)
{
	if (!newdir)
		newdir = DCC_HOMEDIR;

	if (*newdir == '\0') {
		dcc_pemsg(EX_NOINPUT, emsg,
			  "invalid null DCC home directory");
		return 0;
	}
	if (strlen(newdir) >= ISZ(dcc_homedir)) {
		dcc_pemsg(EX_NOINPUT, emsg,
			  "DCC home directory \"%s\" too long", newdir);
		return 0;
	}
#ifdef DCC_WIN32
	if (!SetCurrentDirectory(newdir)) {
		dcc_pemsg(EX_NOINPUT, emsg, "SetCurrentDirectory(%s): %s" ,
			  DCC_NM2PATH(newdir), ERROR_STR());
		return 0;
	}
	if (!getcwd(dcc_homedir, sizeof(dcc_homedir)))
		BUFCPY(dcc_homedir, DCC_NM2PATH(newdir));
#else
	if (0 > chdir(newdir)) {
		dcc_pemsg(EX_NOINPUT, emsg, "chdir(%s): %s",
			  DCC_NM2PATH(newdir), ERROR_STR());
		return 0;
	}
	if (*newdir == '/'
	    || !getcwd(dcc_homedir, sizeof(dcc_homedir)))
		BUFCPY(dcc_homedir, DCC_NM2PATH(newdir));
#endif
	return 1;
}