File: | src/usr.bin/mg/dir.c |
Warning: | line 57, column 7 Although the value stored to 'bufp' is used in the enclosing expression, the value is never actually read from 'bufp' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* $OpenBSD: dir.c,v 1.31 2019/06/28 13:35:02 deraadt 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[NFILEN1024]; |
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((void *)0)) |
31 | ewprintf("Can't get current directory!"); |
32 | if (mgcwd[0] != '\0' && !(mgcwd[0] == '/' && mgcwd[1] == '\0')) |
33 | (void)strlcat(mgcwd, "/", sizeof(mgcwd)); |
34 | } |
35 | |
36 | /* |
37 | * Change current working directory. |
38 | */ |
39 | /* ARGSUSED */ |
40 | int |
41 | changedir(int f, int n) |
42 | { |
43 | char bufc[NFILEN1024], *bufp; |
44 | |
45 | (void)strlcpy(bufc, mgcwd, sizeof(bufc)); |
46 | if ((bufp = eread("Change default directory: ", bufc, NFILEN1024, |
47 | EFDEF0x0020 | EFNEW0x0008 | EFCR0x0010 | EFFILE0x0004)) == NULL((void *)0)) |
48 | return (ABORT2); |
49 | else if (bufp[0] == '\0') |
50 | return (FALSE0); |
51 | /* Append trailing slash */ |
52 | if (chdir(bufc) == -1) { |
53 | dobeep(); |
54 | ewprintf("Can't change dir to %s", bufc); |
55 | return (FALSE0); |
56 | } |
57 | if ((bufp = getcwd(mgcwd, sizeof(mgcwd))) == NULL((void *)0)) { |
Although the value stored to 'bufp' is used in the enclosing expression, the value is never actually read from 'bufp' | |
58 | if (bufc[0] == '/') |
59 | (void)strlcpy(mgcwd, bufc, sizeof(mgcwd)); |
60 | else |
61 | (void)strlcat(mgcwd, bufc, sizeof(mgcwd)); |
62 | } |
63 | if (mgcwd[strlen(mgcwd) - 1] != '/') |
64 | (void)strlcat(mgcwd, "/", sizeof(mgcwd)); |
65 | ewprintf("Current directory is now %s", mgcwd); |
66 | return (TRUE1); |
67 | } |
68 | |
69 | /* |
70 | * Show current directory. |
71 | */ |
72 | /* ARGSUSED */ |
73 | int |
74 | showcwdir(int f, int n) |
75 | { |
76 | ewprintf("Current directory: %s", mgcwd); |
77 | return (TRUE1); |
78 | } |
79 | |
80 | int |
81 | getcwdir(char *buf, size_t len) |
82 | { |
83 | if (strlcpy(buf, mgcwd, len) >= len) |
84 | return (FALSE0); |
85 | |
86 | return (TRUE1); |
87 | } |
88 | |
89 | /* Create the directory and it's parents. */ |
90 | /* ARGSUSED */ |
91 | int |
92 | makedir(int f, int n) |
93 | { |
94 | return (ask_makedir()); |
95 | } |
96 | |
97 | int |
98 | ask_makedir(void) |
99 | { |
100 | |
101 | char bufc[NFILEN1024]; |
102 | char *path; |
103 | |
104 | if (getbufcwd(bufc, sizeof(bufc)) != TRUE1) |
105 | return (ABORT2); |
106 | if ((path = eread("Make directory: ", bufc, NFILEN1024, |
107 | EFDEF0x0020 | EFNEW0x0008 | EFCR0x0010 | EFFILE0x0004)) == NULL((void *)0)) |
108 | return (ABORT2); |
109 | else if (path[0] == '\0') |
110 | return (FALSE0); |
111 | |
112 | return (do_makedir(path)); |
113 | } |
114 | |
115 | int |
116 | do_makedir(char *path) |
117 | { |
118 | struct stat sb; |
119 | int finished, ishere; |
120 | mode_t dir_mode, f_mode, oumask; |
121 | char *slash; |
122 | |
123 | if ((path = adjustname(path, TRUE1)) == NULL((void *)0)) |
124 | return (FALSE0); |
125 | |
126 | /* Remove trailing slashes */ |
127 | slash = strrchr(path, '\0'); |
128 | while (--slash > path && *slash == '/') |
129 | *slash = '\0'; |
130 | |
131 | slash = path; |
132 | |
133 | oumask = umask(0); |
134 | f_mode = 0777 & ~oumask; |
135 | dir_mode = f_mode | S_IWUSR0000200 | S_IXUSR0000100; |
136 | |
137 | for (;;) { |
138 | slash += strspn(slash, "/"); |
139 | slash += strcspn(slash, "/"); |
140 | |
141 | finished = (*slash == '\0'); |
142 | *slash = '\0'; |
143 | |
144 | ishere = !stat(path, &sb); |
145 | if (finished && ishere) { |
146 | dobeep(); |
147 | ewprintf("Cannot create directory %s: file exists", |
148 | path); |
149 | return(FALSE0); |
150 | } else if (!finished && ishere && S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) { |
151 | *slash = '/'; |
152 | continue; |
153 | } |
154 | |
155 | if (mkdir(path, finished ? f_mode : dir_mode) == 0) { |
156 | if (f_mode > 0777 && chmod(path, f_mode) == -1) { |
157 | umask(oumask); |
158 | return (ABORT2); |
159 | } |
160 | } else { |
161 | if (!ishere || !S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) { |
162 | if (!ishere) { |
163 | dobeep(); |
164 | ewprintf("Creating directory: " |
165 | "permission denied, %s", path); |
166 | } else |
167 | eerase(); |
168 | |
169 | umask(oumask); |
170 | return (FALSE0); |
171 | } |
172 | } |
173 | |
174 | if (finished) |
175 | break; |
176 | |
177 | *slash = '/'; |
178 | } |
179 | |
180 | eerase(); |
181 | umask(oumask); |
182 | return (TRUE1); |
183 | } |