GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mg/dir.c Lines: 0 72 0.0 %
Date: 2016-12-06 Branches: 0 52 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: dir.c,v 1.28 2015/03/19 21:22:15 bcallah Exp $	*/
2
3
/* This file is in the public domain. */
4
5
/*
6
 * Name:	MG 2a
7
 *		Directory management functions
8
 * Created:	Ron Flax (ron@vsedev.vse.com)
9
 *		Modified for MG 2a by Mic Kaczmarczik 03-Aug-1987
10
 */
11
12
#include <sys/queue.h>
13
#include <sys/stat.h>
14
#include <signal.h>
15
#include <stdio.h>
16
#include <string.h>
17
#include <unistd.h>
18
19
#include "def.h"
20
21
static char	 mgcwd[NFILEN];
22
23
/*
24
 * Initialize anything the directory management routines need.
25
 */
26
void
27
dirinit(void)
28
{
29
	mgcwd[0] = '\0';
30
	if (getcwd(mgcwd, sizeof(mgcwd)) == NULL) {
31
		ewprintf("Can't get current directory!");
32
		chdir("/");
33
	}
34
	if (!(mgcwd[0] == '/' && mgcwd [1] == '\0'))
35
		(void)strlcat(mgcwd, "/", sizeof(mgcwd));
36
}
37
38
/*
39
 * Change current working directory.
40
 */
41
/* ARGSUSED */
42
int
43
changedir(int f, int n)
44
{
45
	char	bufc[NFILEN], *bufp;
46
47
	(void)strlcpy(bufc, mgcwd, sizeof(bufc));
48
	if ((bufp = eread("Change default directory: ", bufc, NFILEN,
49
	    EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
50
		return (ABORT);
51
	else if (bufp[0] == '\0')
52
		return (FALSE);
53
	/* Append trailing slash */
54
	if (chdir(bufc) == -1) {
55
		dobeep();
56
		ewprintf("Can't change dir to %s", bufc);
57
		return (FALSE);
58
	}
59
	if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL)
60
		panic("Can't get current directory!");
61
	if (mgcwd[strlen(mgcwd) - 1] != '/')
62
		(void)strlcat(mgcwd, "/", sizeof(mgcwd));
63
	ewprintf("Current directory is now %s", bufp);
64
	return (TRUE);
65
}
66
67
/*
68
 * Show current directory.
69
 */
70
/* ARGSUSED */
71
int
72
showcwdir(int f, int n)
73
{
74
	ewprintf("Current directory: %s", mgcwd);
75
	return (TRUE);
76
}
77
78
int
79
getcwdir(char *buf, size_t len)
80
{
81
	if (strlcpy(buf, mgcwd, len) >= len)
82
		return (FALSE);
83
84
	return (TRUE);
85
}
86
87
/* Create the directory and it's parents. */
88
/* ARGSUSED */
89
int
90
makedir(int f, int n)
91
{
92
	return (ask_makedir());
93
}
94
95
int
96
ask_makedir(void)
97
{
98
99
	char		 bufc[NFILEN];
100
	char		*path;
101
102
	if (getbufcwd(bufc, sizeof(bufc)) != TRUE)
103
		return (ABORT);
104
	if ((path = eread("Make directory: ", bufc, NFILEN,
105
	    EFDEF | EFNEW | EFCR | EFFILE)) == NULL)
106
		return (ABORT);
107
	else if (path[0] == '\0')
108
		return (FALSE);
109
110
	return (do_makedir(path));
111
}
112
113
int
114
do_makedir(char *path)
115
{
116
	struct stat	 sb;
117
	int		 finished, ishere;
118
	mode_t		 dir_mode, mode, oumask;
119
	char		*slash;
120
121
	if ((path = adjustname(path, TRUE)) == NULL)
122
		return (FALSE);
123
124
	/* Remove trailing slashes */
125
	slash = strrchr(path, '\0');
126
	while (--slash > path && *slash == '/')
127
		*slash = '\0';
128
129
	slash = path;
130
131
	oumask = umask(0);
132
	mode = 0777 & ~oumask;
133
	dir_mode = mode | S_IWUSR | S_IXUSR;
134
135
	for (;;) {
136
		slash += strspn(slash, "/");
137
		slash += strcspn(slash, "/");
138
139
		finished = (*slash == '\0');
140
		*slash = '\0';
141
142
		ishere = !stat(path, &sb);
143
		if (finished && ishere) {
144
			dobeep();
145
			ewprintf("Cannot create directory %s: file exists",
146
			     path);
147
			return(FALSE);
148
		} else if (!finished && ishere && S_ISDIR(sb.st_mode)) {
149
			*slash = '/';
150
			continue;
151
		}
152
153
		if (mkdir(path, finished ? mode : dir_mode) == 0) {
154
			if (mode > 0777 && chmod(path, mode) < 0) {
155
				umask(oumask);
156
				return (ABORT);
157
			}
158
		} else {
159
			if (!ishere || !S_ISDIR(sb.st_mode)) {
160
				if (!ishere) {
161
					dobeep();
162
					ewprintf("Creating directory: "
163
					    "permission denied, %s", path);
164
				} else
165
					eerase();
166
167
				umask(oumask);
168
				return (FALSE);
169
			}
170
		}
171
172
		if (finished)
173
			break;
174
175
		*slash = '/';
176
	}
177
178
	eerase();
179
	umask(oumask);
180
	return (TRUE);
181
}