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