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 * tools.c - Supporting functions for nftw.c 26 */ 27 28#include "nftw.h" 29 30extern pathdata pathdat[]; 31extern struct list mnem[]; 32extern char ebuf[ERR_BUF_SIZ]; 33extern int npathdats, ngoods, nbads, nmnem; 34extern FILE *temp; 35void fail_exit(void); 36 37/* 38 * Function: void cleanup_function(void) 39 * 40 * Description: 41 * Cleans the residues$ 42 * 43 * Returns : 44 * Nothing 45 */ 46 47void cleanup_function(void) 48{ 49 chmod("./tmp/data/d333", (mode_t) S_IRWXU | S_IRWXG | S_IRWXO); 50 chmod("./tmp/data/d666", (mode_t) S_IRWXU | S_IRWXG | S_IRWXO); 51 chmod("./tmp/data/dirg/dir_right.1", (mode_t) S_IRWXU | S_IRWXG | 52 S_IRWXO); 53 system("rm -rf ./tmp"); 54 wait(NULL); 55} 56 57/* 58 * Function: void setup_path(void) 59 * 60 * Description: 61 * Setup the environment to run the nftw.c 62 * 63 * Returns : 64 * Nothing 65 */ 66 67void setup_path(void) 68{ 69 int i, fd; 70 FILE *temp = stderr; 71 72 if (mkdir("./tmp", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) { 73 sprintf(ebuf, "Can't mkdir ./tmp"); 74 perror(ebuf); 75 fprintf(temp, "ERROR: setup_path function failed\n"); 76 fail_exit(); 77 } 78 for (i = 0; i < npathdats; i++) { 79 if (pathdat[i].type == DIR) { 80 if (mkdir(pathdat[i].name, pathdat[i].mode) == -1) { 81 sprintf(ebuf, "Can't mkdir %s %d", 82 pathdat[i].name, i); 83 perror(ebuf); 84 fprintf(temp, 85 "ERROR: setup_path function failed\n"); 86 cleanup_function(); 87 fail_exit(); 88 } 89 } else if (pathdat[i].type == SYM) { 90 if (symlink(pathdat[i].contents, pathdat[i].name) == -1) { 91 sprintf(ebuf, "Can't symlink %s ", 92 pathdat[i].name); 93 perror(ebuf); 94 fprintf(temp, 95 "ERROR: setup_path function failed\n"); 96 cleanup_function(); 97 fail_exit(); 98 } 99 } else { 100 if ((fd = open(pathdat[i].name, O_WRONLY | O_CREAT, 101 pathdat[i].mode)) == -1) { 102 sprintf(ebuf, "Can't open %s", pathdat[i].name); 103 perror(ebuf); 104 fprintf(temp, 105 "ERROR: setup_path function failed\n"); 106 cleanup_function(); 107 fail_exit(); 108 } 109 if (write(fd, pathdat[i].contents, 110 strlen(pathdat[i].contents)) == -1) { 111 perror("Can't write"); 112 close(fd); 113 fprintf(temp, 114 "ERROR: setup_path function failed\n"); 115 cleanup_function(); 116 fail_exit(); 117 } 118 close(fd); 119 } 120 } 121 122 if (chmod("./tmp/data/d333", (mode_t) S_IWUSR | S_IXUSR | S_IWGRP | 123 S_IXGRP | S_IWOTH | S_IXOTH) == -1) { 124 sprintf(ebuf, "Can't chmod %s ", "./tmp/data/d333"); 125 perror(ebuf); 126 fprintf(temp, "ERROR: setup_path function failed\n"); 127 cleanup_function(); 128 fail_exit(); 129 } 130 if (chmod("./tmp/data/d666", (mode_t) S_IRUSR | S_IWUSR | S_IRGRP | 131 S_IWGRP | S_IROTH | S_IWOTH) == -1) { 132 sprintf(ebuf, "Can't chmod %s ", "./tmp/data/d666"); 133 perror(ebuf); 134 fprintf(temp, "ERROR: setup_path function failed\n"); 135 cleanup_function(); 136 fail_exit(); 137 } 138 if (chmod("./tmp/data/dirg/dir_right.1", (mode_t) S_IWUSR | S_IXUSR | 139 S_IWGRP | S_IXGRP | S_IWOTH | S_IXOTH) == -1) { 140 sprintf(ebuf, "Can't chmod %s ", "./tmp/data/dirg/dir_right.1"); 141 perror(ebuf); 142 fprintf(temp, "ERROR: setup_path function failed\n"); 143 cleanup_function(); 144 fail_exit(); 145 } 146} 147 148/* 149 * Function: int nftw_fn(const char, const struct stat, int, struct FTW ) 150 * 151 * Description: 152 * Dummy function for errno tests 153 * 154 * Returns : 155 * 0 156 */ 157 158int nftw_fn(const char *path, const struct stat *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 * nftw() 171 */ 172 173int callback(const char *path) 174{ 175 return nftw(path, nftw_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 253#ifdef DEBUG 254 fprintf(temp, "INFO: Call to fn() at %s\n", path_name); 255#endif 256 257} 258 259/** LTP Port **/ 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