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