Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Utility routines. |
| 3 | * |
| 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | * |
| 20 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell |
| 21 | * Permission has been granted to redistribute this code under the GPL. |
| 22 | * |
| 23 | */ |
| 24 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 25 | #include "internal.h" |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | #include <errno.h> |
| 29 | #include <fcntl.h> |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 30 | #include <dirent.h> |
| 31 | #include <time.h> |
| 32 | #include <utime.h> |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
| 36 | #if 0 |
| 37 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 38 | extern char *join_paths(char *buffer, const char *a, const char *b) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 40 | int length = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 42 | if (a && *a) { |
| 43 | length = strlen(a); |
| 44 | memcpy(buffer, a, length); |
| 45 | } |
| 46 | if (b && *b) { |
| 47 | if (length > 0 && buffer[length - 1] != '/') |
| 48 | buffer[length++] = '/'; |
| 49 | if (*b == '/') |
| 50 | b++; |
| 51 | strcpy(&buffer[length], b); |
| 52 | } |
| 53 | return buffer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #endif |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 63 | static CHUNK *chunkList; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 64 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Return the standard ls-like mode string from a file mode. |
| 68 | * This is static and so is overwritten on each call. |
| 69 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 70 | const char *modeString(int mode) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 72 | static char buf[12]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 74 | strcpy(buf, "----------"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Fill in the file type. |
| 78 | */ |
| 79 | if (S_ISDIR(mode)) |
| 80 | buf[0] = 'd'; |
| 81 | if (S_ISCHR(mode)) |
| 82 | buf[0] = 'c'; |
| 83 | if (S_ISBLK(mode)) |
| 84 | buf[0] = 'b'; |
| 85 | if (S_ISFIFO(mode)) |
| 86 | buf[0] = 'p'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 87 | #ifdef S_ISLNK |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 88 | if (S_ISLNK(mode)) |
| 89 | buf[0] = 'l'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 90 | #endif |
| 91 | #ifdef S_ISSOCK |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 92 | if (S_ISSOCK(mode)) |
| 93 | buf[0] = 's'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 96 | /* |
| 97 | * Now fill in the normal file permissions. |
| 98 | */ |
| 99 | if (mode & S_IRUSR) |
| 100 | buf[1] = 'r'; |
| 101 | if (mode & S_IWUSR) |
| 102 | buf[2] = 'w'; |
| 103 | if (mode & S_IXUSR) |
| 104 | buf[3] = 'x'; |
| 105 | if (mode & S_IRGRP) |
| 106 | buf[4] = 'r'; |
| 107 | if (mode & S_IWGRP) |
| 108 | buf[5] = 'w'; |
| 109 | if (mode & S_IXGRP) |
| 110 | buf[6] = 'x'; |
| 111 | if (mode & S_IROTH) |
| 112 | buf[7] = 'r'; |
| 113 | if (mode & S_IWOTH) |
| 114 | buf[8] = 'w'; |
| 115 | if (mode & S_IXOTH) |
| 116 | buf[9] = 'x'; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 117 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 118 | /* |
| 119 | * Finally fill in magic stuff like suid and sticky text. |
| 120 | */ |
| 121 | if (mode & S_ISUID) |
| 122 | buf[3] = ((mode & S_IXUSR) ? 's' : 'S'); |
| 123 | if (mode & S_ISGID) |
| 124 | buf[6] = ((mode & S_IXGRP) ? 's' : 'S'); |
| 125 | if (mode & S_ISVTX) |
| 126 | buf[9] = ((mode & S_IXOTH) ? 't' : 'T'); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 127 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 128 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 133 | * Return TRUE if a fileName is a directory. |
| 134 | * Nonexistant files return FALSE. |
| 135 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 136 | int isDirectory(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 137 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 138 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 139 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 140 | if (stat(name, &statBuf) < 0) |
| 141 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 142 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 143 | return S_ISDIR(statBuf.st_mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * Return TRUE if a filename is a block or character device. |
| 149 | * Nonexistant files return FALSE. |
| 150 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 151 | int isDevice(const char *name) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 152 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 153 | struct stat statBuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 154 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 155 | if (stat(name, &statBuf) < 0) |
| 156 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 158 | return S_ISBLK(statBuf.st_mode) || S_ISCHR(statBuf.st_mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
| 162 | /* |
| 163 | * Copy one file to another, while possibly preserving its modes, times, |
| 164 | * and modes. Returns TRUE if successful, or FALSE on a failure with an |
| 165 | * error message output. (Failure is not indicted if the attributes cannot |
| 166 | * be set.) |
| 167 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 168 | int |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 169 | copyFile( |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 170 | const char *srcName, |
| 171 | const char *destName, int setModes, int followLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 173 | int rfd; |
| 174 | int wfd; |
| 175 | int rcc; |
| 176 | char buf[BUF_SIZE]; |
| 177 | struct stat statBuf1; |
| 178 | struct stat statBuf2; |
| 179 | struct utimbuf times; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 180 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 181 | if (stat(srcName, &statBuf1) < 0) { |
| 182 | perror(srcName); |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | if (stat(destName, &statBuf2) < 0) { |
| 187 | statBuf2.st_ino = -1; |
| 188 | statBuf2.st_dev = -1; |
| 189 | } |
| 190 | |
| 191 | if ((statBuf1.st_dev == statBuf2.st_dev) && |
| 192 | (statBuf1.st_ino == statBuf2.st_ino)) { |
| 193 | fprintf(stderr, "Copying file \"%s\" to itself\n", srcName); |
| 194 | return FALSE; |
| 195 | } |
| 196 | |
| 197 | if (S_ISDIR(statBuf1.st_mode)) { |
| 198 | /* Make sure the directory is writable */ |
| 199 | if (mkdir(destName, 0777777 ^ umask(0))) { |
| 200 | perror(destName); |
| 201 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 202 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 203 | } else if (S_ISFIFO(statBuf1.st_mode)) { |
| 204 | if (mkfifo(destName, 644)) { |
| 205 | perror(destName); |
| 206 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 207 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 208 | } else if (S_ISBLK(statBuf1.st_mode) || S_ISCHR(statBuf1.st_mode)) { |
| 209 | if (mknod(destName, 644, statBuf1.st_rdev)) { |
| 210 | perror(destName); |
| 211 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 212 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 213 | } else if (S_ISLNK(statBuf1.st_mode)) { |
| 214 | char *link_val; |
| 215 | int link_size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 216 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 217 | link_val = (char *) alloca(PATH_MAX + 2); |
| 218 | link_size = readlink(srcName, link_val, PATH_MAX + 1); |
| 219 | if (link_size < 0) { |
| 220 | perror(srcName); |
| 221 | return (FALSE); |
| 222 | } |
| 223 | link_val[link_size] = '\0'; |
| 224 | if (symlink(link_val, destName)) { |
| 225 | perror(srcName); |
| 226 | return (FALSE); |
| 227 | } |
| 228 | } else { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | rfd = open(srcName, O_RDONLY); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 230 | if (rfd < 0) { |
| 231 | perror(srcName); |
| 232 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | wfd = creat(destName, statBuf1.st_mode); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 236 | if (wfd < 0) { |
| 237 | perror(destName); |
| 238 | close(rfd); |
| 239 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 242 | while ((rcc = read(rfd, buf, sizeof(buf))) > 0) { |
| 243 | if (fullWrite(wfd, buf, rcc) < 0) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 244 | goto error_exit; |
| 245 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 246 | if (rcc < 0) { |
| 247 | perror(srcName); |
| 248 | goto error_exit; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 251 | close(rfd); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 252 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 253 | if (close(wfd) < 0) { |
| 254 | perror(destName); |
| 255 | return FALSE; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (setModes == TRUE) { |
| 260 | chmod(destName, statBuf1.st_mode); |
| 261 | if (followLinks == TRUE) |
| 262 | chown(destName, statBuf1.st_uid, statBuf1.st_gid); |
| 263 | else |
| 264 | lchown(destName, statBuf1.st_uid, statBuf1.st_gid); |
| 265 | |
| 266 | times.actime = statBuf1.st_atime; |
| 267 | times.modtime = statBuf1.st_mtime; |
| 268 | |
| 269 | utime(destName, ×); |
| 270 | } |
| 271 | |
| 272 | return TRUE; |
| 273 | |
| 274 | |
| 275 | error_exit: |
| 276 | close(rfd); |
| 277 | close(wfd); |
| 278 | |
| 279 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | /* |
| 284 | * Build a path name from the specified directory name and file name. |
| 285 | * If the directory name is NULL, then the original fileName is returned. |
| 286 | * The built path is in a static area, and is overwritten for each call. |
| 287 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 288 | char *buildName(const char *dirName, const char *fileName) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 289 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 290 | const char *cp; |
| 291 | static char buf[PATH_LEN]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 292 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 293 | if ((dirName == NULL) || (*dirName == '\0')) { |
| 294 | strcpy(buf, fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 295 | return buf; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | cp = strrchr(fileName, '/'); |
| 299 | |
| 300 | if (cp) |
| 301 | fileName = cp + 1; |
| 302 | |
| 303 | strcpy(buf, dirName); |
| 304 | strcat(buf, "/"); |
| 305 | |
| 306 | return buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 312 | * Make a NULL-terminated string out of an argc, argv pair. |
| 313 | * Returns TRUE if successful, or FALSE if the string is too long, |
| 314 | * with an error message given. This does not handle spaces within |
| 315 | * arguments correctly. |
| 316 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 317 | int makeString( int argc, const char **argv, char *buf, int bufLen) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 318 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 319 | int len; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 320 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 321 | while (argc-- > 0) { |
| 322 | len = strlen(*argv); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 323 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 324 | if (len >= bufLen) { |
| 325 | fprintf(stderr, "Argument string too long\n"); |
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 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 330 | strcpy(buf, *argv++); |
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 | buf += len; |
| 333 | bufLen -= len; |
| 334 | |
| 335 | if (argc) |
| 336 | *buf++ = ' '; |
| 337 | |
| 338 | bufLen--; |
| 339 | } |
| 340 | |
| 341 | *buf = '\0'; |
| 342 | |
| 343 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
| 348 | * Allocate a chunk of memory (like malloc). |
| 349 | * The difference, though, is that the memory allocated is put on a |
| 350 | * list of chunks which can be freed all at one time. You CAN NOT free |
| 351 | * an individual chunk. |
| 352 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 353 | char *getChunk(int size) |
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 | CHUNK *chunk; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 356 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 357 | if (size < CHUNK_INIT_SIZE) |
| 358 | size = CHUNK_INIT_SIZE; |
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 | chunk = (CHUNK *) malloc(size + sizeof(CHUNK) - CHUNK_INIT_SIZE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 361 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 362 | if (chunk == NULL) |
| 363 | return NULL; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 364 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 365 | chunk->next = chunkList; |
| 366 | chunkList = chunk; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 367 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 368 | return chunk->data; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
| 372 | /* |
| 373 | * Duplicate a string value using the chunk allocator. |
| 374 | * The returned string cannot be individually freed, but can only be freed |
| 375 | * with other strings when freeChunks is called. Returns NULL on failure. |
| 376 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 377 | char *chunkstrdup(const char *str) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 378 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 379 | int len; |
| 380 | char *newStr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 381 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 382 | len = strlen(str) + 1; |
| 383 | newStr = getChunk(len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 384 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 385 | if (newStr) |
| 386 | memcpy(newStr, str, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 387 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 388 | return newStr; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | |
| 392 | /* |
| 393 | * Free all chunks of memory that had been allocated since the last |
| 394 | * call to this routine. |
| 395 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 396 | void freeChunks(void) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 397 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 398 | CHUNK *chunk; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 399 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 400 | while (chunkList) { |
| 401 | chunk = chunkList; |
| 402 | chunkList = chunk->next; |
| 403 | free((char *) chunk); |
| 404 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
| 408 | /* |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 409 | * Get the time string to be used for a file. |
| 410 | * This is down to the minute for new files, but only the date for old files. |
| 411 | * The string is returned from a static buffer, and so is overwritten for |
| 412 | * each call. |
| 413 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 414 | const char *timeString(time_t timeVal) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 415 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 416 | time_t now; |
| 417 | char *str; |
| 418 | static char buf[26]; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 419 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 420 | time(&now); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 421 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 422 | str = ctime(&timeVal); |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 423 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 424 | strcpy(buf, &str[4]); |
| 425 | buf[12] = '\0'; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 426 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 427 | if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) { |
| 428 | strcpy(&buf[7], &str[20]); |
| 429 | buf[11] = '\0'; |
| 430 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 431 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 432 | return buf; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
| 436 | /* |
| 437 | * Routine to see if a text string is matched by a wildcard pattern. |
| 438 | * Returns TRUE if the text is matched, or FALSE if it is not matched |
| 439 | * or if the pattern is invalid. |
| 440 | * * matches zero or more characters |
| 441 | * ? matches a single character |
| 442 | * [abc] matches 'a', 'b' or 'c' |
| 443 | * \c quotes character c |
| 444 | * Adapted from code written by Ingo Wilken. |
| 445 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 446 | int match(const char *text, const char *pattern) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 447 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 448 | const char *retryPat; |
| 449 | const char *retryText; |
| 450 | int ch; |
| 451 | int found; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 452 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 453 | retryPat = NULL; |
| 454 | retryText = NULL; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 455 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 456 | while (*text || *pattern) { |
| 457 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 458 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 459 | switch (ch) { |
| 460 | case '*': |
| 461 | retryPat = pattern; |
| 462 | retryText = text; |
| 463 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 464 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 465 | case '[': |
| 466 | found = FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 467 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 468 | while ((ch = *pattern++) != ']') { |
| 469 | if (ch == '\\') |
| 470 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 471 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 472 | if (ch == '\0') |
| 473 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 474 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 475 | if (*text == ch) |
| 476 | found = TRUE; |
| 477 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 478 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 479 | if (!found) { |
| 480 | pattern = retryPat; |
| 481 | text = ++retryText; |
| 482 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 483 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 484 | /* fall into next case */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 485 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 486 | case '?': |
| 487 | if (*text++ == '\0') |
| 488 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 489 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 490 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 491 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 492 | case '\\': |
| 493 | ch = *pattern++; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 494 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 495 | if (ch == '\0') |
| 496 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 497 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 498 | /* fall into next case */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 499 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 500 | default: |
| 501 | if (*text == ch) { |
| 502 | if (*text) |
| 503 | text++; |
| 504 | break; |
| 505 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 506 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 507 | if (*text) { |
| 508 | pattern = retryPat; |
| 509 | text = ++retryText; |
| 510 | break; |
| 511 | } |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 512 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 513 | return FALSE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 516 | if (pattern == NULL) |
| 517 | return FALSE; |
| 518 | } |
| 519 | |
| 520 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
| 524 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 525 | * Write all of the supplied buffer out to a file. |
| 526 | * This does multiple writes as necessary. |
| 527 | * Returns the amount written, or -1 on an error. |
| 528 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 529 | int fullWrite(int fd, const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 530 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 531 | int cc; |
| 532 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 533 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 534 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 535 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 536 | while (len > 0) { |
| 537 | cc = write(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 538 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 539 | if (cc < 0) |
| 540 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 541 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 542 | buf += cc; |
| 543 | total += cc; |
| 544 | len -= cc; |
| 545 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 546 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 547 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | /* |
| 552 | * Read all of the supplied buffer from a file. |
| 553 | * This does multiple reads as necessary. |
| 554 | * Returns the amount read, or -1 on an error. |
| 555 | * A short read is returned on an end of file. |
| 556 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 557 | int fullRead(int fd, char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 558 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 559 | int cc; |
| 560 | int total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 561 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 562 | total = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 563 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 564 | while (len > 0) { |
| 565 | cc = read(fd, buf, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 566 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 567 | if (cc < 0) |
| 568 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 569 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 570 | if (cc == 0) |
| 571 | break; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 572 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 573 | buf += cc; |
| 574 | total += cc; |
| 575 | len -= cc; |
| 576 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 577 | |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 578 | return total; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | |
| 582 | /* |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 583 | * Walk down all the directories under the specified |
| 584 | * location, and do something (something specified |
| 585 | * by the fileAction and dirAction function pointers). |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 586 | */ |
| 587 | int |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 588 | recursiveAction(const char *fileName, int recurse, int followLinks, |
| 589 | int (*fileAction) (const char *fileName), |
| 590 | int (*dirAction) (const char *fileName)) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 591 | { |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 592 | int status; |
| 593 | struct stat statbuf; |
| 594 | struct dirent *next; |
Eric Andersen | 9d3aba7 | 1999-10-06 09:04:55 +0000 | [diff] [blame] | 595 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 596 | if (followLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 597 | status = lstat(fileName, &statbuf); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 598 | else |
| 599 | status = stat(fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 600 | if (status < 0) { |
| 601 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 602 | return (FALSE); |
| 603 | } |
| 604 | |
| 605 | if (recurse == FALSE) { |
| 606 | if (S_ISDIR(statbuf.st_mode)) { |
| 607 | if (dirAction == NULL) |
| 608 | return (TRUE); |
| 609 | else |
| 610 | return (dirAction(fileName)); |
| 611 | } else { |
| 612 | if (fileAction == NULL) |
| 613 | return (TRUE); |
| 614 | else |
| 615 | return (fileAction(fileName)); |
| 616 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 619 | if (S_ISDIR(statbuf.st_mode)) { |
| 620 | DIR *dir; |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 621 | fprintf(stderr, "Dir: %s\n", fileName); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 622 | dir = opendir(fileName); |
| 623 | if (!dir) { |
| 624 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 625 | return (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 626 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 627 | if (dirAction != NULL) { |
| 628 | status = dirAction(fileName); |
| 629 | if (status == FALSE) { |
| 630 | perror("cp"); |
| 631 | return (FALSE); |
| 632 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 633 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 634 | while ((next = readdir(dir)) != NULL) { |
| 635 | char nextFile[NAME_MAX]; |
| 636 | if ((strcmp(next->d_name, "..") == 0) |
| 637 | || (strcmp(next->d_name, ".") == 0)) { |
| 638 | continue; |
| 639 | } |
| 640 | sprintf(nextFile, "%s/%s", fileName, next->d_name); |
| 641 | status = |
| 642 | recursiveAction(nextFile, TRUE, followLinks, fileAction, |
| 643 | dirAction); |
| 644 | if (status < 0) { |
| 645 | closedir(dir); |
| 646 | return (FALSE); |
| 647 | } |
| 648 | } |
| 649 | status = closedir(dir); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 650 | if (status < 0) { |
| 651 | perror(fileName); |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 652 | return (FALSE); |
Eric Andersen | 2b69c40 | 1999-10-05 22:58:32 +0000 | [diff] [blame] | 653 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 654 | } else { |
| 655 | fprintf(stderr, "File: %s\n", fileName); |
| 656 | if (fileAction == NULL) |
| 657 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 658 | else |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 659 | return (fileAction(fileName)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 660 | } |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 661 | return (TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | |
| 665 | |
| 666 | /* END CODE */ |