1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2002
4 *
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/* 10/31/2002   Port to LTP     robbiew@us.ibm.com */
21/* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
22
23/*
24 * NAME
25 *	tools64.c - Supporting functions for nftw64.c
26 */
27
28#include "nftw64.h"
29
30extern pathdata pathdat[];
31extern struct list mnem[];
32extern char ebuf[ERR_BUF_SIZ];
33extern int npathdats, ngoods, nbads, nmnem;
34void fail_exit(void);
35extern FILE *temp;
36/*
37 * Function: void cleanup_function(void)
38 *
39 * Description:
40 *	Cleans the residues$
41 *
42 * Returns :
43 *	Nothing
44 */
45
46void cleanup_function(void)
47{
48	chmod("./tmp/data/d333", (mode_t) S_IRWXU | S_IRWXG | S_IRWXO);
49	chmod("./tmp/data/d666", (mode_t) S_IRWXU | S_IRWXG | S_IRWXO);
50	chmod("./tmp/data/dirg/dir_right.1", (mode_t) S_IRWXU | S_IRWXG |
51	      S_IRWXO);
52	system("rm -rf ./tmp");
53	wait(NULL);
54}
55
56/*
57 * Function: void setup_path(void)
58 *
59 * Description:
60 *	Setup the environment to run the nftw64.c
61 *
62 * Returns :
63 *	Nothing
64 */
65
66void setup_path(void)
67{
68	int i, fd;
69	temp = stderr;
70
71	if (mkdir("./tmp", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
72		sprintf(ebuf, "Can't mkdir ./tmp");
73		perror(ebuf);
74		fprintf(temp, "ERROR: setup_path function failed\n");
75		fail_exit();
76	}
77	for (i = 0; i < npathdats; i++) {
78		if (pathdat[i].type == DIR) {
79			if (mkdir(pathdat[i].name, pathdat[i].mode) == -1) {
80				sprintf(ebuf, "Can't mkdir %s %d",
81					pathdat[i].name, i);
82				perror(ebuf);
83				fprintf(temp,
84					"ERROR: setup_path function failed\n");
85				cleanup_function();
86				fail_exit();
87			}
88		} else if (pathdat[i].type == SYM) {
89			if (symlink(pathdat[i].contents, pathdat[i].name) == -1) {
90				sprintf(ebuf, "Can't symlink %s ",
91					pathdat[i].name);
92				perror(ebuf);
93				fprintf(temp,
94					"ERROR: setup_path function failed\n");
95				cleanup_function();
96				fail_exit();
97			}
98		} else {
99			if ((fd = open(pathdat[i].name, O_WRONLY | O_CREAT,
100				       pathdat[i].mode)) == -1) {
101				sprintf(ebuf, "Can't open %s", pathdat[i].name);
102				perror(ebuf);
103				fprintf(temp,
104					"ERROR: setup_path function failed\n");
105				cleanup_function();
106				fail_exit();
107			}
108			if (write(fd, pathdat[i].contents,
109				  strlen(pathdat[i].contents)) == -1) {
110				perror("Can't write");
111				close(fd);
112				fprintf(temp,
113					"ERROR: setup_path function failed\n");
114				cleanup_function();
115				fail_exit();
116			}
117			close(fd);
118		}
119	}
120
121	if (chmod("./tmp/data/d333", (mode_t) S_IWUSR | S_IXUSR | S_IWGRP |
122		  S_IXGRP | S_IWOTH | S_IXOTH) == -1) {
123		sprintf(ebuf, "Can't chmod %s ", "./tmp/data/d333");
124		perror(ebuf);
125		fprintf(temp, "ERROR: setup_path function failed\n");
126		cleanup_function();
127		fail_exit();
128	}
129	if (chmod("./tmp/data/d666", (mode_t) S_IRUSR | S_IWUSR | S_IRGRP |
130		  S_IWGRP | S_IROTH | S_IWOTH) == -1) {
131		sprintf(ebuf, "Can't chmod %s ", "./tmp/data/d666");
132		perror(ebuf);
133		fprintf(temp, "ERROR: setup_path function failed\n");
134		cleanup_function();
135		fail_exit();
136	}
137	if (chmod("./tmp/data/dirg/dir_right.1", (mode_t) S_IWUSR | S_IXUSR |
138		  S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) == -1) {
139		sprintf(ebuf, "Can't chmod %s ", "./tmp/data/dirg/dir_right.1");
140		perror(ebuf);
141		fprintf(temp, "ERROR: setup_path function failed\n");
142		cleanup_function();
143		fail_exit();
144	}
145}
146
147/*
148 * Function: int nftw64_fn(const char, const struct stat64, int, struct FTW )
149 *
150 * Description:
151 *      Dummy function for errno tests
152 *
153 * Returns :
154 *	0
155 */
156
157int
158nftw64_fn(const char *path, const struct stat64 *st, int ival, struct FTW *FTWS)
159{
160	return (0);
161}
162
163/*
164 * Function: int callback(char *)
165 *
166 * Description:
167 *      Dummy function for errno tests
168 *
169 * Returns :
170 *	nftw64()
171 */
172
173int callback(const char *path)
174{
175	return nftw64(path, nftw64_fn, 10, FTW_MOUNT);
176}
177
178/*
179 * Function: char * ftw_mnemonic(int)
180 *
181 * Description:
182 *	Conversion function for printing
183 *
184 * Returns:
185 *	String for printing
186 */
187
188char *ftw_mnemonic(int x)
189{
190	static char s[STRLEN];
191	int i;
192
193	for (i = 0; i < nmnem; i++)
194		if (x == mnem[i].i)
195			return (mnem[i].s);
196
197	sprintf(s, "Unknown value for third argument to fn(): %d\n", x);
198	return (s);
199}
200
201/*
202 * Function: int getbase(char *)
203 *
204 * Description:
205 *	Find basename
206 *
207 * Returns:
208 *	Position of filename in path
209 */
210
211int getbase(const char *s)
212{
213	int i, last = 0;
214
215	for (i = 0; *s != '\0'; s++, i++)
216		if (*s == '/')
217			last = i;
218	return (last ? last + 1 : 0);
219}
220
221/*
222 * Function: int getlev( char *);
223 *
224 * Description:
225 *	Find level
226 *
227 * Returns:
228 *	Number of /'s in path
229 */
230
231int getlev(const char *s)
232{
233	int i;
234	for (i = 0; *s != '\0'; s++)
235		if (*s == '/')
236			i++;
237	return (i);
238}
239
240/*
241 * Function: void doinfo(char *);
242 *
243 * Description:
244 *	print the file being visited
245 *
246 * Returns:
247 *	Nothing
248 */
249
250void do_info(const char *path_name)
251{
252#ifdef DEBUG
253	temp = stderr;
254	fprintf(temp, "INFO: Call to fn() at %s\n", path_name);
255#endif
256}
257
258/** LTP Port **/
259
260/*
261 * fail_exit()
262 *
263 * Exit on failure
264 */
265void fail_exit(void)
266{
267	tst_brkm(TFAIL, NULL, "Test failed");
268}
269
270/**************/
271