Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines. |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * Copyright (C) tons of folks. Tracking down who wrote what |
| 5 | * isn't something I'm going to worry about... If you wrote something |
| 6 | * here, please feel free to acknowledge your work. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 23 | * Permission has been granted to redistribute this code under the GPL. |
| 24 | * |
| 25 | */ |
| 26 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 27 | #include "internal.h" |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 32 | #include <dirent.h> |
| 33 | #include <time.h> |
| 34 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 37 | #include <ctype.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 39 | /* volatile so gcc knows this is the enod of the line */ |
| 40 | volatile void usage(const char *usage) |
| 41 | { |
Eric Andersen | a07f0b0 | 1999-10-22 19:49:09 +0000 | [diff] [blame] | 42 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT); |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 43 | fprintf(stderr, "Usage: %s\n", usage); |
| 44 | exit(FALSE); |
| 45 | } |
| 46 | |
| 47 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 48 | #if defined (BB_INIT) || defined (BB_PS) |
| 49 | |
| 50 | /* Returns kernel version encoded as major*65536 + minor*256 + patch, |
| 51 | * so, for example, to check if the kernel is greater than 2.2.11: |
| 52 | * if (get_kernel_revision() <= 2*65536+2*256+11) { <stuff> } |
| 53 | */ |
| 54 | int |
| 55 | get_kernel_revision() |
| 56 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 57 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 58 | int major=0, minor=0, patch=0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 59 | char* filename="/proc/sys/kernel/osrelease"; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 60 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 61 | file = fopen(filename,"r"); |
| 62 | if (file == NULL) { |
Eric Andersen | 2f6c04f | 1999-11-01 23:59:44 +0000 | [diff] [blame^] | 63 | /* bummer, /proc must not be mounted... */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 64 | return( 0); |
| 65 | } |
| 66 | fscanf(file,"%d.%d.%d",&major,&minor,&patch); |
| 67 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 68 | return major*65536 + minor*256 + patch; |
| 69 | } |
| 70 | |
| 71 | #endif |
| 72 | |
| 73 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 74 | |
| 75 | #if defined (BB_CP) || defined (BB_MV) |
| 76 | /* |
| 77 | * Return TRUE if a fileName is a directory. |
| 78 | * Nonexistant files return FALSE. |
| 79 | */ |
| 80 | int isDirectory(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 81 | { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 82 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 83 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 84 | if (stat(name, &statBuf) < 0) |
| 85 | return FALSE; |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 86 | if (S_ISDIR(statBuf.st_mode)) |
| 87 | return TRUE; |
| 88 | return(FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Copy one file to another, while possibly preserving its modes, times, |
| 94 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
| 95 | * error message output. (Failure is not indicted if the attributes cannot |
| 96 | * be set.) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 97 | * -Erik Andersen |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 98 | */ |
| 99 | int |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 100 | copyFile( const char *srcName, const char *destName, |
| 101 | int setModes, int followLinks) |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 102 | { |
| 103 | int rfd; |
| 104 | int wfd; |
| 105 | int rcc; |
| 106 | int result; |
| 107 | char buf[BUF_SIZE]; |
| 108 | struct stat srcStatBuf; |
| 109 | struct stat dstStatBuf; |
| 110 | struct utimbuf times; |
| 111 | |
| 112 | if (followLinks == FALSE) |
| 113 | result = stat(srcName, &srcStatBuf); |
| 114 | else |
| 115 | result = lstat(srcName, &srcStatBuf); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 116 | if (result < 0) { |
| 117 | perror(srcName); |
| 118 | return FALSE; |
| 119 | } |
| 120 | |
| 121 | if (followLinks == FALSE) |
| 122 | result = stat(destName, &dstStatBuf); |
| 123 | else |
| 124 | result = lstat(destName, &dstStatBuf); |
| 125 | if (result < 0) { |
| 126 | dstStatBuf.st_ino = -1; |
| 127 | dstStatBuf.st_dev = -1; |
| 128 | } |
| 129 | |
| 130 | if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && |
| 131 | (srcStatBuf.st_ino == dstStatBuf.st_ino)) { |
| 132 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 133 | return FALSE; |
| 134 | } |
| 135 | |
| 136 | if (S_ISDIR(srcStatBuf.st_mode)) { |
| 137 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
| 138 | /* Make sure the directory is writable */ |
| 139 | if (mkdir(destName, 0777777 ^ umask(0))) { |
| 140 | perror(destName); |
| 141 | return (FALSE); |
| 142 | } |
| 143 | } else if (S_ISLNK(srcStatBuf.st_mode)) { |
| 144 | char *link_val; |
| 145 | int link_size; |
| 146 | |
| 147 | //fprintf(stderr, "copying link %s to %s\n", srcName, destName); |
| 148 | link_val = (char *) alloca(PATH_MAX + 2); |
| 149 | link_size = readlink(srcName, link_val, PATH_MAX + 1); |
| 150 | if (link_size < 0) { |
| 151 | perror(srcName); |
| 152 | return (FALSE); |
| 153 | } |
| 154 | link_val[link_size] = '\0'; |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 155 | link_size = symlink(link_val, destName); |
| 156 | if (link_size != 0) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 157 | perror(destName); |
| 158 | return (FALSE); |
| 159 | } |
| 160 | } else if (S_ISFIFO(srcStatBuf.st_mode)) { |
| 161 | //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); |
| 162 | if (mkfifo(destName, 644)) { |
| 163 | perror(destName); |
| 164 | return (FALSE); |
| 165 | } |
| 166 | } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) |
| 167 | || S_ISSOCK (srcStatBuf.st_mode)) { |
| 168 | //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); |
| 169 | if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev)) { |
| 170 | perror(destName); |
| 171 | return (FALSE); |
| 172 | } |
| 173 | } else if (S_ISREG(srcStatBuf.st_mode)) { |
| 174 | //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); |
| 175 | rfd = open(srcName, O_RDONLY); |
| 176 | if (rfd < 0) { |
| 177 | perror(srcName); |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
| 181 | wfd = creat(destName, srcStatBuf.st_mode); |
| 182 | if (wfd < 0) { |
| 183 | perror(destName); |
| 184 | close(rfd); |
| 185 | return FALSE; |
| 186 | } |
| 187 | |
| 188 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 189 | if (fullWrite(wfd, buf, rcc) < 0) |
| 190 | goto error_exit; |
| 191 | } |
| 192 | if (rcc < 0) { |
| 193 | goto error_exit; |
| 194 | } |
| 195 | |
| 196 | close(rfd); |
| 197 | if (close(wfd) < 0) { |
| 198 | return FALSE; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (setModes == TRUE) { |
| 203 | //fprintf(stderr, "Setting permissions for %s\n", destName); |
| 204 | chmod(destName, srcStatBuf.st_mode); |
| 205 | if (followLinks == TRUE) |
| 206 | chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 207 | else |
| 208 | lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); |
| 209 | |
| 210 | times.actime = srcStatBuf.st_atime; |
| 211 | times.modtime = srcStatBuf.st_mtime; |
| 212 | |
| 213 | utime(destName, ×); |
| 214 | } |
| 215 | |
| 216 | return TRUE; |
| 217 | |
| 218 | |
| 219 | error_exit: |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 220 | perror(destName); |
| 221 | close(rfd); |
| 222 | close(wfd); |
| 223 | |
| 224 | return FALSE; |
| 225 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 226 | #endif |
| 227 | |
| 228 | |
| 229 | |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 230 | #ifdef BB_TAR |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 231 | /* |
| 232 | * Return the standard ls-like mode string from a file mode. |
| 233 | * This is static and so is overwritten on each call. |
| 234 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 235 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 237 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 238 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 239 | strcpy(buf, "----------"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 241 | /* |
| 242 | * Fill in the file type. |
| 243 | */ |
| 244 | if (S_ISDIR(mode)) |
| 245 | buf[0] = 'd'; |
| 246 | if (S_ISCHR(mode)) |
| 247 | buf[0] = 'c'; |
| 248 | if (S_ISBLK(mode)) |
| 249 | buf[0] = 'b'; |
| 250 | if (S_ISFIFO(mode)) |
| 251 | buf[0] = 'p'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 252 | if (S_ISLNK(mode)) |
| 253 | buf[0] = 'l'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 254 | if (S_ISSOCK(mode)) |
| 255 | buf[0] = 's'; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 256 | /* |
| 257 | * Now fill in the normal file permissions. |
| 258 | */ |
| 259 | if (mode & S_IRUSR) |
| 260 | buf[1] = 'r'; |
| 261 | if (mode & S_IWUSR) |
| 262 | buf[2] = 'w'; |
| 263 | if (mode & S_IXUSR) |
| 264 | buf[3] = 'x'; |
| 265 | if (mode & S_IRGRP) |
| 266 | buf[4] = 'r'; |
| 267 | if (mode & S_IWGRP) |
| 268 | buf[5] = 'w'; |
| 269 | if (mode & S_IXGRP) |
| 270 | buf[6] = 'x'; |
| 271 | if (mode & S_IROTH) |
| 272 | buf[7] = 'r'; |
| 273 | if (mode & S_IWOTH) |
| 274 | buf[8] = 'w'; |
| 275 | if (mode & S_IXOTH) |
| 276 | buf[9] = 'x'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 277 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 278 | /* |
| 279 | * Finally fill in magic stuff like suid and sticky text. |
| 280 | */ |
| 281 | if (mode & S_ISUID) |
| 282 | buf[3] = ((mode & S_IXUSR) ? 's' : 'S'); |
| 283 | if (mode & S_ISGID) |
| 284 | buf[6] = ((mode & S_IXGRP) ? 's' : 'S'); |
| 285 | if (mode & S_ISVTX) |
| 286 | buf[9] = ((mode & S_IXOTH) ? 't' : 'T'); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 287 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 288 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 292 | /* |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 293 | * Get the time string to be used for a file. |
| 294 | * This is down to the minute for new files, but only the date for old files. |
| 295 | * The string is returned from a static buffer, and so is overwritten for |
| 296 | * each call. |
| 297 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 298 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 299 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 300 | time_t now; |
| 301 | char *str; |
| 302 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 303 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 304 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 305 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 306 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 307 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 308 | strcpy(buf, &str[4]); |
| 309 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 310 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 311 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 312 | strcpy(&buf[7], &str[20]); |
| 313 | buf[11] = '\0'; |
| 314 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 315 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 316 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | |
| 320 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 321 | * Write all of the supplied buffer out to a file. |
| 322 | * This does multiple writes as necessary. |
| 323 | * Returns the amount written, or -1 on an error. |
| 324 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 325 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 326 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 327 | int cc; |
| 328 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 329 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 330 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 331 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 332 | while (len > 0) { |
| 333 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 334 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 335 | if (cc < 0) |
| 336 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 337 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 338 | buf += cc; |
| 339 | total += cc; |
| 340 | len -= cc; |
| 341 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 342 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 343 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
| 348 | * Read all of the supplied buffer from a file. |
| 349 | * This does multiple reads as necessary. |
| 350 | * Returns the amount read, or -1 on an error. |
| 351 | * A short read is returned on an end of file. |
| 352 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 353 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 354 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 355 | int cc; |
| 356 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 357 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 358 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 359 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 360 | while (len > 0) { |
| 361 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 362 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 363 | if (cc < 0) |
| 364 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 365 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 366 | if (cc == 0) |
| 367 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 368 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 369 | buf += cc; |
| 370 | total += cc; |
| 371 | len -= cc; |
| 372 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 373 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 374 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 375 | } |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 376 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 377 | |
| 378 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 379 | #if defined (BB_CHOWN) || defined (BB_CP) || defined (BB_FIND) || defined (BB_LS) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 380 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 381 | * Walk down all the directories under the specified |
| 382 | * location, and do something (something specified |
| 383 | * by the fileAction and dirAction function pointers). |
Eric Andersen | b7a1a75 | 1999-10-19 23:37:14 +0000 | [diff] [blame] | 384 | * |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 385 | * Unfortunatly, while nftw(3) could replace this and reduce |
| 386 | * code size a bit, nftw() wasn't supported before GNU libc 2.1, |
| 387 | * and so isn't sufficiently portable to take over... |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 388 | */ |
| 389 | int |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 390 | recursiveAction(const char *fileName, int recurse, int followLinks, int depthFirst, |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 391 | int (*fileAction) (const char *fileName, struct stat* statbuf), |
| 392 | int (*dirAction) (const char *fileName, struct stat* statbuf)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 393 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 394 | int status; |
| 395 | struct stat statbuf; |
| 396 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 397 | |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 398 | if (followLinks == FALSE) |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 399 | status = stat(fileName, &statbuf); |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 400 | else |
| 401 | status = lstat(fileName, &statbuf); |
| 402 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 403 | if (status < 0) { |
| 404 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 405 | return (FALSE); |
| 406 | } |
| 407 | |
| 408 | if (recurse == FALSE) { |
| 409 | if (S_ISDIR(statbuf.st_mode)) { |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 410 | if (dirAction != NULL) |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 411 | return (dirAction(fileName, &statbuf)); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 412 | else |
Eric Andersen | 3c16382 | 1999-10-14 22:16:57 +0000 | [diff] [blame] | 413 | return (TRUE); |
| 414 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 417 | if (S_ISDIR(statbuf.st_mode)) { |
| 418 | DIR *dir; |
| 419 | dir = opendir(fileName); |
| 420 | if (!dir) { |
| 421 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 422 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 423 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 424 | if (dirAction != NULL && depthFirst == FALSE) { |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 425 | status = dirAction(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 426 | if (status == FALSE) { |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 427 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 428 | return (FALSE); |
| 429 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 430 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 431 | while ((next = readdir(dir)) != NULL) { |
| 432 | char nextFile[NAME_MAX]; |
| 433 | if ((strcmp(next->d_name, "..") == 0) |
| 434 | || (strcmp(next->d_name, ".") == 0)) { |
| 435 | continue; |
| 436 | } |
| 437 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 438 | status = |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 439 | recursiveAction(nextFile, TRUE, followLinks, depthFirst, |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 440 | fileAction, dirAction); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 441 | if (status < 0) { |
| 442 | closedir(dir); |
| 443 | return (FALSE); |
| 444 | } |
| 445 | } |
| 446 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 447 | if (status < 0) { |
| 448 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 449 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 450 | } |
Eric Andersen | 63a0e53 | 1999-10-22 05:12:14 +0000 | [diff] [blame] | 451 | if (dirAction != NULL && depthFirst == TRUE) { |
Eric Andersen | bed30e9 | 1999-10-18 19:02:32 +0000 | [diff] [blame] | 452 | status = dirAction(fileName, &statbuf); |
| 453 | if (status == FALSE) { |
| 454 | perror(fileName); |
| 455 | return (FALSE); |
| 456 | } |
| 457 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 458 | } else { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 459 | if (fileAction == NULL) |
| 460 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 461 | else |
Eric Andersen | 9b58718 | 1999-10-17 05:43:39 +0000 | [diff] [blame] | 462 | return (fileAction(fileName, &statbuf)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 463 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 464 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 467 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 468 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 469 | |
| 470 | |
| 471 | #if defined (BB_TAR) || defined (BB_MKDIR) |
| 472 | /* |
| 473 | * Attempt to create the directories along the specified path, except for |
| 474 | * the final component. The mode is given for the final directory only, |
| 475 | * while all previous ones get default protections. Errors are not reported |
| 476 | * here, as failures to restore files can be reported later. |
| 477 | */ |
| 478 | extern void createPath (const char *name, int mode) |
| 479 | { |
| 480 | char *cp; |
| 481 | char *cpOld; |
| 482 | char buf[NAME_MAX]; |
| 483 | |
| 484 | strcpy (buf, name); |
| 485 | |
| 486 | cp = strchr (buf, '/'); |
| 487 | |
| 488 | while (cp) { |
| 489 | cpOld = cp; |
| 490 | cp = strchr (cp + 1, '/'); |
| 491 | |
| 492 | *cpOld = '\0'; |
| 493 | |
| 494 | if (mkdir (buf, cp ? 0777 : mode) == 0) |
| 495 | printf ("Directory \"%s\" created\n", buf); |
| 496 | |
| 497 | *cpOld = '/'; |
| 498 | } |
| 499 | } |
| 500 | #endif |
| 501 | |
| 502 | |
| 503 | |
| 504 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_MKDIR) |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 505 | /* [ugoa]{+|-|=}[rwxst] */ |
| 506 | |
| 507 | |
| 508 | |
| 509 | extern int |
| 510 | parse_mode( const char* s, mode_t* theMode) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 511 | { |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 512 | mode_t andMode = S_ISVTX|S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 513 | mode_t orMode = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 514 | mode_t mode = 0; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 515 | mode_t groups = 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 516 | char type; |
| 517 | char c; |
| 518 | |
| 519 | do { |
| 520 | for ( ; ; ) { |
| 521 | switch ( c = *s++ ) { |
| 522 | case '\0': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 523 | return -1; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 524 | case 'u': |
| 525 | groups |= S_ISUID|S_IRWXU; |
| 526 | continue; |
| 527 | case 'g': |
| 528 | groups |= S_ISGID|S_IRWXG; |
| 529 | continue; |
| 530 | case 'o': |
| 531 | groups |= S_IRWXO; |
| 532 | continue; |
| 533 | case 'a': |
| 534 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 535 | continue; |
| 536 | case '+': |
| 537 | case '=': |
| 538 | case '-': |
| 539 | type = c; |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 540 | if ( groups == 0 ) /* The default is "all" */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 541 | groups |= S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO; |
| 542 | break; |
| 543 | default: |
Eric Andersen | fa0540f | 1999-10-22 18:18:31 +0000 | [diff] [blame] | 544 | if ( isdigit(c) && c >= '0' && c <= '7' && |
| 545 | mode == 0 && groups == 0 ) { |
| 546 | *theMode = strtol(--s, NULL, 8); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 547 | return (TRUE); |
| 548 | } |
| 549 | else |
| 550 | return (FALSE); |
| 551 | } |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | while ( (c = *s++) != '\0' ) { |
| 556 | switch ( c ) { |
| 557 | case ',': |
| 558 | break; |
| 559 | case 'r': |
| 560 | mode |= S_IRUSR|S_IRGRP|S_IROTH; |
| 561 | continue; |
| 562 | case 'w': |
| 563 | mode |= S_IWUSR|S_IWGRP|S_IWOTH; |
| 564 | continue; |
| 565 | case 'x': |
| 566 | mode |= S_IXUSR|S_IXGRP|S_IXOTH; |
| 567 | continue; |
| 568 | case 's': |
| 569 | mode |= S_IXGRP|S_ISUID|S_ISGID; |
| 570 | continue; |
| 571 | case 't': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 572 | mode |= 0; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 573 | continue; |
| 574 | default: |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 575 | *theMode &= andMode; |
| 576 | *theMode |= orMode; |
| 577 | return( TRUE); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 578 | } |
| 579 | break; |
| 580 | } |
| 581 | switch ( type ) { |
| 582 | case '=': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 583 | andMode &= ~(groups); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 584 | /* fall through */ |
| 585 | case '+': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 586 | orMode |= mode & groups; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 587 | break; |
| 588 | case '-': |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 589 | andMode &= ~(mode & groups); |
| 590 | orMode &= andMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } while ( c == ',' ); |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 594 | *theMode &= andMode; |
| 595 | *theMode |= orMode; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 596 | return (TRUE); |
| 597 | } |
Eric Andersen | ce8f3b9 | 1999-10-20 07:03:36 +0000 | [diff] [blame] | 598 | |
| 599 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 600 | #endif |
| 601 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 602 | |
| 603 | |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | #if defined (BB_CHMOD_CHOWN_CHGRP) || defined (BB_PS) |
| 609 | |
| 610 | /* Use this to avoid needing the glibc NSS stuff |
| 611 | * This uses storage buf to hold things. |
| 612 | * */ |
| 613 | uid_t |
| 614 | my_getid(const char *filename, char *name, uid_t id) |
| 615 | { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 616 | FILE *file; |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 617 | char *rname, *start, *end, buf[128]; |
| 618 | uid_t rid; |
| 619 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 620 | file=fopen(filename,"r"); |
| 621 | if (file == NULL) { |
| 622 | perror(filename); |
| 623 | return (-1); |
| 624 | } |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 625 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 626 | while (fgets (buf, 128, file) != NULL) { |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 627 | if (buf[0] == '#') |
| 628 | continue; |
| 629 | |
| 630 | start = buf; |
| 631 | end = strchr (start, ':'); |
| 632 | if (end == NULL) |
| 633 | continue; |
| 634 | *end = '\0'; |
| 635 | rname = start; |
| 636 | |
| 637 | start = end + 1; |
| 638 | end = strchr (start, ':'); |
| 639 | if (end == NULL) |
| 640 | continue; |
| 641 | |
| 642 | start = end + 1; |
| 643 | rid = (uid_t) strtol (start, &end, 10); |
| 644 | if (end == start) |
| 645 | continue; |
| 646 | |
| 647 | if (name) { |
| 648 | if (0 == strcmp(rname, name)) |
| 649 | return( rid); |
| 650 | } |
| 651 | if ( id != -1 && id == rid ) { |
| 652 | strncpy(name, rname, 8); |
| 653 | return( TRUE); |
| 654 | } |
| 655 | } |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 656 | fclose(file); |
Eric Andersen | d23f9ba | 1999-10-20 19:18:15 +0000 | [diff] [blame] | 657 | return (-1); |
| 658 | } |
| 659 | |
| 660 | uid_t |
| 661 | my_getpwnam(char *name) |
| 662 | { |
| 663 | return my_getid("/etc/passwd", name, -1); |
| 664 | } |
| 665 | |
| 666 | gid_t |
| 667 | my_getgrnam(char *name) |
| 668 | { |
| 669 | return my_getid("/etc/group", name, -1); |
| 670 | } |
| 671 | |
| 672 | void |
| 673 | my_getpwuid(char* name, uid_t uid) |
| 674 | { |
| 675 | my_getid("/etc/passwd", name, uid); |
| 676 | } |
| 677 | |
| 678 | void |
| 679 | my_getgrgid(char* group, gid_t gid) |
| 680 | { |
| 681 | my_getid("/etc/group", group, gid); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | #endif |
| 686 | |
| 687 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 688 | |
Eric Andersen | 0460ff2 | 1999-10-25 23:32:44 +0000 | [diff] [blame] | 689 | |
| 690 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
| 691 | |
| 692 | |
| 693 | #include <linux/kd.h> |
| 694 | #include <sys/ioctl.h> |
| 695 | |
| 696 | int is_a_console(int fd) |
| 697 | { |
| 698 | char arg; |
| 699 | |
| 700 | arg = 0; |
| 701 | return (ioctl(fd, KDGKBTYPE, &arg) == 0 |
| 702 | && ((arg == KB_101) || (arg == KB_84))); |
| 703 | } |
| 704 | |
| 705 | static int open_a_console(char *fnam) |
| 706 | { |
| 707 | int fd; |
| 708 | |
| 709 | /* try read-only */ |
| 710 | fd = open(fnam, O_RDWR); |
| 711 | |
| 712 | /* if failed, try read-only */ |
| 713 | if (fd < 0 && errno == EACCES) |
| 714 | fd = open(fnam, O_RDONLY); |
| 715 | |
| 716 | /* if failed, try write-only */ |
| 717 | if (fd < 0 && errno == EACCES) |
| 718 | fd = open(fnam, O_WRONLY); |
| 719 | |
| 720 | /* if failed, fail */ |
| 721 | if (fd < 0) |
| 722 | return -1; |
| 723 | |
| 724 | /* if not a console, fail */ |
| 725 | if (! is_a_console(fd)) |
| 726 | { |
| 727 | close(fd); |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | /* success */ |
| 732 | return fd; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Get an fd for use with kbd/console ioctls. |
| 737 | * We try several things because opening /dev/console will fail |
| 738 | * if someone else used X (which does a chown on /dev/console). |
| 739 | * |
| 740 | * if tty_name is non-NULL, try this one instead. |
| 741 | */ |
| 742 | |
| 743 | int get_console_fd(char* tty_name) |
| 744 | { |
| 745 | int fd; |
| 746 | |
| 747 | if (tty_name) |
| 748 | { |
| 749 | if (-1 == (fd = open_a_console(tty_name))) |
| 750 | return -1; |
| 751 | else |
| 752 | return fd; |
| 753 | } |
| 754 | |
| 755 | fd = open_a_console("/dev/tty"); |
| 756 | if (fd >= 0) |
| 757 | return fd; |
| 758 | |
| 759 | fd = open_a_console("/dev/tty0"); |
| 760 | if (fd >= 0) |
| 761 | return fd; |
| 762 | |
| 763 | fd = open_a_console("/dev/console"); |
| 764 | if (fd >= 0) |
| 765 | return fd; |
| 766 | |
| 767 | for (fd = 0; fd < 3; fd++) |
| 768 | if (is_a_console(fd)) |
| 769 | return fd; |
| 770 | |
| 771 | fprintf(stderr, |
| 772 | "Couldnt get a file descriptor referring to the console\n"); |
| 773 | return -1; /* total failure */ |
| 774 | } |
| 775 | |
| 776 | |
| 777 | #endif |
| 778 | |
| 779 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 780 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND || defined BB_SED) |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 781 | |
| 782 | /* Do a case insensitive strstr() */ |
| 783 | char* stristr(char *haystack, const char *needle) |
| 784 | { |
| 785 | int len = strlen( needle ); |
| 786 | while( *haystack ) { |
| 787 | if( !strncasecmp( haystack, needle, len ) ) |
| 788 | break; |
| 789 | haystack++; |
| 790 | } |
| 791 | |
| 792 | if( !(*haystack) ) |
| 793 | haystack = NULL; |
| 794 | |
| 795 | return haystack; |
| 796 | } |
| 797 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 798 | /* This tries to find a needle in a haystack, but does so by |
| 799 | * only trying to match literal strings (look 'ma, no regexps!) |
| 800 | * This is short, sweet, and carries _very_ little baggage, |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 801 | * unlike its beefier cousin in regexp.c |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 802 | * -Erik Andersen |
| 803 | */ |
| 804 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 805 | { |
| 806 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 807 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 808 | haystack = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 809 | else |
| 810 | haystack = stristr (haystack, needle); |
| 811 | if (haystack == NULL) |
| 812 | return FALSE; |
| 813 | return TRUE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 817 | /* This performs substitutions after a string match has been found. */ |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 818 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
| 819 | { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 820 | int foundOne=0; |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 821 | char *where, *slider, *slider1, *oldhayStack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 822 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 823 | if (ignoreCase == FALSE) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 824 | where = strstr (haystack, needle); |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 825 | else |
| 826 | where = stristr (haystack, needle); |
| 827 | |
| 828 | if (strcmp(needle, newNeedle)==0) |
| 829 | return FALSE; |
| 830 | |
| 831 | oldhayStack = (char*)malloc((unsigned)(strlen(haystack))); |
| 832 | while(where!=NULL) { |
| 833 | foundOne++; |
| 834 | strcpy(oldhayStack, haystack); |
| 835 | #if 0 |
| 836 | if ( strlen(newNeedle) > strlen(needle)) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 837 | haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - |
| 838 | strlen(needle) + strlen(newNeedle))); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 839 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 840 | #endif |
| 841 | for(slider=haystack,slider1=oldhayStack;slider!=where;slider++,slider1++); |
| 842 | *slider=0; |
| 843 | haystack=strcat(haystack, newNeedle); |
| 844 | slider1+=strlen(needle); |
| 845 | haystack = strcat(haystack, slider1); |
| 846 | where = strstr (slider, needle); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 847 | } |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 848 | free( oldhayStack); |
| 849 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 850 | if (foundOne > 0) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 851 | return TRUE; |
| 852 | else |
| 853 | return FALSE; |
| 854 | } |
| 855 | |
Eric Andersen | 24d8e7d | 1999-10-29 06:50:17 +0000 | [diff] [blame] | 856 | |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 857 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 858 | /* END CODE */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 859 | |