Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | * Mini tar implementation for busybox based on code taken from sash. |
| 3 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | * Copyright (c) 1999 by David I. Bell |
| 5 | * Permission is granted to use, distribute, or modify this source, |
| 6 | * provided that this copyright notice remains intact. |
| 7 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | * Permission to distribute this code under the GPL has been granted. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | * |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 10 | * Modified for busybox by Erik Andersen <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 11 | * Adjusted to grok stdin/stdout options. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | |
| 30 | #include "internal.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <dirent.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <signal.h> |
| 36 | #include <time.h> |
| 37 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 38 | /* Note that tar.c expects TRUE and FALSE to be defined |
| 39 | * exactly the opposite of how they are used everywhere else. |
| 40 | * Some time this should be integrated a bit better, but this |
| 41 | * does the job for now. |
| 42 | */ |
| 43 | #undef FALSE |
| 44 | #undef TRUE |
| 45 | #define FALSE ((int) 0) |
| 46 | #define TRUE ((int) 1) |
| 47 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 48 | |
| 49 | static const char tar_usage[] = |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 50 | "tar -[cxtvOf] [tarFileName] [FILE] ...\n" |
| 51 | "Create, extract, or list files from a tar file\n\n" |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 52 | "\tc=create, x=extract, t=list contents, v=verbose,\n" |
| 53 | "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n"; |
| 54 | |
| 55 | |
| 56 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Tar file constants. |
| 59 | */ |
| 60 | #define TAR_BLOCK_SIZE 512 |
| 61 | #define TAR_NAME_SIZE 100 |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | * The POSIX (and basic GNU) tar header format. |
| 66 | * This structure is always embedded in a TAR_BLOCK_SIZE sized block |
| 67 | * with zero padding. We only process this information minimally. |
| 68 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 69 | typedef struct { |
| 70 | char name[TAR_NAME_SIZE]; |
| 71 | char mode[8]; |
| 72 | char uid[8]; |
| 73 | char gid[8]; |
| 74 | char size[12]; |
| 75 | char mtime[12]; |
| 76 | char checkSum[8]; |
| 77 | char typeFlag; |
| 78 | char linkName[TAR_NAME_SIZE]; |
| 79 | char magic[6]; |
| 80 | char version[2]; |
| 81 | char uname[32]; |
| 82 | char gname[32]; |
| 83 | char devMajor[8]; |
| 84 | char devMinor[8]; |
| 85 | char prefix[155]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | } TarHeader; |
| 87 | |
| 88 | #define TAR_MAGIC "ustar" |
| 89 | #define TAR_VERSION "00" |
| 90 | |
| 91 | #define TAR_TYPE_REGULAR '0' |
| 92 | #define TAR_TYPE_HARD_LINK '1' |
| 93 | #define TAR_TYPE_SOFT_LINK '2' |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | * Static data. |
| 98 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 99 | static int listFlag; |
| 100 | static int extractFlag; |
| 101 | static int createFlag; |
| 102 | static int verboseFlag; |
| 103 | static int tostdoutFlag; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 104 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 105 | static int inHeader; |
| 106 | static int badHeader; |
| 107 | static int errorFlag; |
| 108 | static int skipFileFlag; |
| 109 | static int warnedRoot; |
| 110 | static int eofFlag; |
| 111 | static long dataCc; |
| 112 | static int outFd; |
| 113 | static char outName[TAR_NAME_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | /* |
| 117 | * Static data associated with the tar file. |
| 118 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 119 | static const char *tarName; |
| 120 | static int tarFd; |
| 121 | static dev_t tarDev; |
| 122 | static ino_t tarInode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | /* |
| 126 | * Local procedures to restore files from a tar file. |
| 127 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 128 | static void readTarFile (int fileCount, char **fileTable); |
| 129 | static void readData (const char *cp, int count); |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 130 | static long getOctal (const char *cp, int len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 131 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 132 | static void readHeader (const TarHeader * hp, |
| 133 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 134 | |
| 135 | |
| 136 | /* |
| 137 | * Local procedures to save files into a tar file. |
| 138 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 139 | static void saveFile (const char *fileName, int seeLinks); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 140 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 141 | static void saveRegularFile (const char *fileName, |
| 142 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 143 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 144 | static void saveDirectory (const char *fileName, |
| 145 | const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 146 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 147 | static int wantFileName (const char *fileName, |
| 148 | int fileCount, char **fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 150 | static void writeHeader (const char *fileName, const struct stat *statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 151 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 152 | static void writeTarFile (int fileCount, char **fileTable); |
| 153 | static void writeTarBlock (const char *buf, int len); |
| 154 | static int putOctal (char *cp, int len, long value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 155 | |
| 156 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 157 | extern int tar_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 158 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 159 | const char *options; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 161 | argc--; |
| 162 | argv++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 163 | |
Eric Andersen | be971d6 | 1999-11-03 16:52:50 +0000 | [diff] [blame] | 164 | if (argc < 1) |
| 165 | usage( tar_usage); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 166 | |
| 167 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 168 | errorFlag = FALSE; |
| 169 | extractFlag = FALSE; |
| 170 | createFlag = FALSE; |
| 171 | listFlag = FALSE; |
| 172 | verboseFlag = FALSE; |
| 173 | tostdoutFlag = FALSE; |
| 174 | tarName = NULL; |
| 175 | tarDev = 0; |
| 176 | tarInode = 0; |
| 177 | tarFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 178 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 179 | /* |
| 180 | * Parse the options. |
| 181 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 182 | if (**argv == '-') { |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 183 | options = (*argv++) + 1; |
| 184 | argc--; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 185 | for (; *options; options++) { |
| 186 | switch (*options) { |
| 187 | case 'f': |
| 188 | if (tarName != NULL) { |
| 189 | fprintf (stderr, "Only one 'f' option allowed\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 190 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 191 | exit (FALSE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 192 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 193 | |
| 194 | tarName = *argv++; |
| 195 | argc--; |
| 196 | |
| 197 | break; |
| 198 | |
| 199 | case 't': |
| 200 | listFlag = TRUE; |
| 201 | break; |
| 202 | |
| 203 | case 'x': |
| 204 | extractFlag = TRUE; |
| 205 | break; |
| 206 | |
| 207 | case 'c': |
| 208 | createFlag = TRUE; |
| 209 | break; |
| 210 | |
| 211 | case 'v': |
| 212 | verboseFlag = TRUE; |
| 213 | break; |
| 214 | |
| 215 | case 'O': |
| 216 | tostdoutFlag = TRUE; |
| 217 | break; |
| 218 | |
| 219 | case '-': |
| 220 | break; |
| 221 | |
| 222 | default: |
| 223 | fprintf (stderr, "Unknown tar flag '%c'\n", *options); |
| 224 | |
| 225 | exit (FALSE); |
| 226 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 227 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 228 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 229 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 230 | /* |
| 231 | * Validate the options. |
| 232 | */ |
| 233 | if (extractFlag + listFlag + createFlag != 1) { |
| 234 | fprintf (stderr, |
| 235 | "Exactly one of 'c', 'x' or 't' must be specified\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 236 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 237 | exit (FALSE); |
| 238 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 239 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 240 | /* |
| 241 | * Do the correct type of action supplying the rest of the |
| 242 | * command line arguments as the list of files to process. |
| 243 | */ |
| 244 | if (createFlag) |
| 245 | writeTarFile (argc, argv); |
| 246 | else |
| 247 | readTarFile (argc, argv); |
| 248 | if (errorFlag) |
| 249 | fprintf (stderr, "\n"); |
| 250 | exit (errorFlag); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
| 254 | /* |
| 255 | * Read a tar file and extract or list the specified files within it. |
| 256 | * If the list is empty than all files are extracted or listed. |
| 257 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 258 | static void readTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 259 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 260 | const char *cp; |
| 261 | int cc; |
| 262 | int inCc; |
| 263 | int blockSize; |
| 264 | char buf[BUF_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 265 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 266 | skipFileFlag = FALSE; |
| 267 | badHeader = FALSE; |
| 268 | warnedRoot = FALSE; |
| 269 | eofFlag = FALSE; |
| 270 | inHeader = TRUE; |
| 271 | inCc = 0; |
| 272 | dataCc = 0; |
| 273 | outFd = -1; |
| 274 | blockSize = sizeof (buf); |
| 275 | cp = buf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 276 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 277 | /* |
| 278 | * Open the tar file for reading. |
| 279 | */ |
| 280 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
| 281 | tarFd = STDIN; |
| 282 | } else |
| 283 | tarFd = open (tarName, O_RDONLY); |
| 284 | |
| 285 | if (tarFd < 0) { |
| 286 | perror (tarName); |
| 287 | errorFlag = TRUE; |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Read blocks from the file until an end of file header block |
| 293 | * has been seen. (A real end of file from a read is an error.) |
| 294 | */ |
| 295 | while (!eofFlag) { |
| 296 | /* |
| 297 | * Read the next block of data if necessary. |
| 298 | * This will be a large block if possible, which we will |
| 299 | * then process in the small tar blocks. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 300 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 301 | if (inCc <= 0) { |
| 302 | cp = buf; |
| 303 | inCc = fullRead (tarFd, buf, blockSize); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 304 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 305 | if (inCc < 0) { |
| 306 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 307 | errorFlag = TRUE; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 308 | goto done; |
| 309 | } |
| 310 | |
| 311 | if (inCc == 0) { |
| 312 | fprintf (stderr, |
| 313 | "Unexpected end of file from \"%s\"", tarName); |
| 314 | errorFlag = TRUE; |
| 315 | goto done; |
| 316 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 319 | /* |
| 320 | * If we are expecting a header block then examine it. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 321 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 322 | if (inHeader) { |
| 323 | readHeader ((const TarHeader *) cp, fileCount, fileTable); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 324 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 325 | cp += TAR_BLOCK_SIZE; |
| 326 | inCc -= TAR_BLOCK_SIZE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 327 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 328 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 331 | /* |
| 332 | * We are currently handling the data for a file. |
| 333 | * Process the minimum of the amount of data we have available |
| 334 | * and the amount left to be processed for the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 335 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 336 | cc = inCc; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 337 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 338 | if (cc > dataCc) |
| 339 | cc = dataCc; |
| 340 | |
| 341 | readData (cp, cc); |
| 342 | |
| 343 | /* |
| 344 | * If the amount left isn't an exact multiple of the tar block |
| 345 | * size then round it up to the next block boundary since there |
| 346 | * is padding at the end of the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 347 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 348 | if (cc % TAR_BLOCK_SIZE) |
| 349 | cc += TAR_BLOCK_SIZE - (cc % TAR_BLOCK_SIZE); |
| 350 | |
| 351 | cp += cc; |
| 352 | inCc -= cc; |
| 353 | } |
| 354 | |
| 355 | done: |
| 356 | /* |
| 357 | * Close the tar file if needed. |
| 358 | */ |
| 359 | if ((tarFd >= 0) && (close (tarFd) < 0)) |
| 360 | perror (tarName); |
| 361 | |
| 362 | /* |
| 363 | * Close the output file if needed. |
| 364 | * This is only done here on a previous error and so no |
| 365 | * message is required on errors. |
| 366 | */ |
| 367 | if (tostdoutFlag == FALSE) { |
| 368 | if (outFd >= 0) |
| 369 | (void) close (outFd); |
| 370 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
| 374 | /* |
| 375 | * Examine the header block that was just read. |
| 376 | * This can specify the information for another file, or it can mark |
| 377 | * the end of the tar file. |
| 378 | */ |
| 379 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 380 | readHeader (const TarHeader * hp, int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 381 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 382 | int mode; |
| 383 | int uid; |
| 384 | int gid; |
| 385 | int checkSum; |
| 386 | long size; |
| 387 | time_t mtime; |
| 388 | const char *name; |
| 389 | int cc; |
| 390 | int hardLink; |
| 391 | int softLink; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 392 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 393 | /* |
| 394 | * If the block is completely empty, then this is the end of the |
| 395 | * archive file. If the name is null, then just skip this header. |
| 396 | */ |
| 397 | name = hp->name; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 398 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 399 | if (*name == '\0') { |
| 400 | for (cc = TAR_BLOCK_SIZE; cc > 0; cc--) { |
| 401 | if (*name++) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 402 | return; |
| 403 | } |
| 404 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 405 | eofFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 406 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 407 | return; |
| 408 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 409 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 410 | /* |
| 411 | * There is another file in the archive to examine. |
| 412 | * Extract the encoded information and check it. |
| 413 | */ |
| 414 | mode = getOctal (hp->mode, sizeof (hp->mode)); |
| 415 | uid = getOctal (hp->uid, sizeof (hp->uid)); |
| 416 | gid = getOctal (hp->gid, sizeof (hp->gid)); |
| 417 | size = getOctal (hp->size, sizeof (hp->size)); |
| 418 | mtime = getOctal (hp->mtime, sizeof (hp->mtime)); |
| 419 | checkSum = getOctal (hp->checkSum, sizeof (hp->checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 420 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 421 | if ((mode < 0) || (uid < 0) || (gid < 0) || (size < 0)) { |
| 422 | if (!badHeader) |
| 423 | fprintf (stderr, "Bad tar header, skipping\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 424 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 425 | badHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 426 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 427 | return; |
| 428 | } |
| 429 | |
| 430 | badHeader = FALSE; |
| 431 | skipFileFlag = FALSE; |
| 432 | |
| 433 | /* |
| 434 | * Check for the file modes. |
| 435 | */ |
| 436 | hardLink = ((hp->typeFlag == TAR_TYPE_HARD_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 437 | (hp->typeFlag == TAR_TYPE_HARD_LINK - '0')); |
| 438 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 439 | softLink = ((hp->typeFlag == TAR_TYPE_SOFT_LINK) || |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 440 | (hp->typeFlag == TAR_TYPE_SOFT_LINK - '0')); |
| 441 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 442 | /* |
| 443 | * Check for a directory or a regular file. |
| 444 | */ |
| 445 | if (name[strlen (name) - 1] == '/') |
| 446 | mode |= S_IFDIR; |
| 447 | else if ((mode & S_IFMT) == 0) |
| 448 | mode |= S_IFREG; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 449 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 450 | /* |
| 451 | * Check for absolute paths in the file. |
| 452 | * If we find any, then warn the user and make them relative. |
| 453 | */ |
| 454 | if (*name == '/') { |
| 455 | while (*name == '/') |
| 456 | name++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 457 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 458 | if (!warnedRoot) { |
| 459 | fprintf (stderr, |
| 460 | "Absolute path detected, removing leading slashes\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 463 | warnedRoot = TRUE; |
| 464 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 465 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 466 | /* |
| 467 | * See if we want this file to be restored. |
| 468 | * If not, then set up to skip it. |
| 469 | */ |
| 470 | if (!wantFileName (name, fileCount, fileTable)) { |
| 471 | if (!hardLink && !softLink && S_ISREG (mode)) { |
| 472 | inHeader = (size == 0); |
| 473 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 476 | skipFileFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 477 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 478 | return; |
| 479 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 480 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 481 | /* |
| 482 | * This file is to be handled. |
| 483 | * If we aren't extracting then just list information about the file. |
| 484 | */ |
| 485 | if (!extractFlag) { |
| 486 | if (verboseFlag) { |
| 487 | printf ("%s %3d/%-d %9ld %s %s", modeString (mode), |
| 488 | uid, gid, size, timeString (mtime), name); |
| 489 | } else |
| 490 | printf ("%s", name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 491 | |
| 492 | if (hardLink) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 493 | printf (" (link to \"%s\")", hp->linkName); |
| 494 | else if (softLink) |
| 495 | printf (" (symlink to \"%s\")", hp->linkName); |
| 496 | else if (S_ISREG (mode)) { |
| 497 | inHeader = (size == 0); |
| 498 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 501 | printf ("\n"); |
| 502 | |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * We really want to extract the file. |
| 508 | */ |
| 509 | if (verboseFlag) |
| 510 | printf ("x %s\n", name); |
| 511 | |
| 512 | if (hardLink) { |
| 513 | if (link (hp->linkName, name) < 0) |
| 514 | perror (name); |
| 515 | |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | if (softLink) { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 520 | #ifdef S_ISLNK |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 521 | if (symlink (hp->linkName, name) < 0) |
| 522 | perror (name); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 523 | #else |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 524 | fprintf (stderr, "Cannot create symbolic links\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 525 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 528 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 529 | /* |
| 530 | * If the file is a directory, then just create the path. |
| 531 | */ |
| 532 | if (S_ISDIR (mode)) { |
| 533 | createPath (name, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 534 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 535 | return; |
| 536 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 537 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 538 | /* |
| 539 | * There is a file to write. |
| 540 | * First create the path to it if necessary with a default permission. |
| 541 | */ |
| 542 | createPath (name, 0777); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 543 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 544 | inHeader = (size == 0); |
| 545 | dataCc = size; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 546 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 547 | /* |
| 548 | * Start the output file. |
| 549 | */ |
| 550 | if (tostdoutFlag == TRUE) |
| 551 | outFd = STDOUT; |
| 552 | else |
| 553 | outFd = open (name, O_WRONLY | O_CREAT | O_TRUNC, mode); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 554 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 555 | if (outFd < 0) { |
| 556 | perror (name); |
| 557 | skipFileFlag = TRUE; |
| 558 | return; |
| 559 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 560 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 561 | /* |
| 562 | * If the file is empty, then that's all we need to do. |
| 563 | */ |
| 564 | if (size == 0 && tostdoutFlag == FALSE) { |
| 565 | (void) close (outFd); |
| 566 | outFd = -1; |
| 567 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
| 571 | /* |
| 572 | * Handle a data block of some specified size that was read. |
| 573 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 574 | static void readData (const char *cp, int count) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 575 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 576 | /* |
| 577 | * Reduce the amount of data left in this file. |
| 578 | * If there is no more data left, then we need to read |
| 579 | * the header again. |
| 580 | */ |
| 581 | dataCc -= count; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 582 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 583 | if (dataCc <= 0) |
| 584 | inHeader = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 585 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 586 | /* |
| 587 | * If we aren't extracting files or this file is being |
| 588 | * skipped then do nothing more. |
| 589 | */ |
| 590 | if (!extractFlag || skipFileFlag) |
| 591 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 592 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 593 | /* |
| 594 | * Write the data to the output file. |
| 595 | */ |
| 596 | if (fullWrite (outFd, cp, count) < 0) { |
| 597 | perror (outName); |
| 598 | if (tostdoutFlag == FALSE) { |
| 599 | (void) close (outFd); |
| 600 | outFd = -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 601 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 602 | skipFileFlag = TRUE; |
| 603 | return; |
| 604 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 605 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 606 | /* |
| 607 | * If the write failed, close the file and disable further |
| 608 | * writes to this file. |
| 609 | */ |
| 610 | if (dataCc <= 0 && tostdoutFlag == FALSE) { |
| 611 | if (close (outFd)) |
| 612 | perror (outName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 613 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 614 | outFd = -1; |
| 615 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | |
| 619 | /* |
| 620 | * Write a tar file containing the specified files. |
| 621 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 622 | static void writeTarFile (int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 623 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 624 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 625 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 626 | /* |
| 627 | * Make sure there is at least one file specified. |
| 628 | */ |
| 629 | if (fileCount <= 0) { |
| 630 | fprintf (stderr, "No files specified to be saved\n"); |
| 631 | errorFlag = TRUE; |
| 632 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 633 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 634 | /* |
| 635 | * Create the tar file for writing. |
| 636 | */ |
| 637 | if ((tarName == NULL) || !strcmp (tarName, "-")) { |
| 638 | tostdoutFlag = TRUE; |
| 639 | tarFd = STDOUT; |
| 640 | } else |
| 641 | tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0666); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 642 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 643 | if (tarFd < 0) { |
| 644 | perror (tarName); |
| 645 | errorFlag = TRUE; |
| 646 | return; |
| 647 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 648 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 649 | /* |
| 650 | * Get the device and inode of the tar file for checking later. |
| 651 | */ |
| 652 | if (fstat (tarFd, &statbuf) < 0) { |
| 653 | perror (tarName); |
| 654 | errorFlag = TRUE; |
| 655 | goto done; |
| 656 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 657 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 658 | tarDev = statbuf.st_dev; |
| 659 | tarInode = statbuf.st_ino; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 660 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 661 | /* |
| 662 | * Append each file name into the archive file. |
| 663 | * Follow symbolic links for these top level file names. |
| 664 | */ |
| 665 | while (!errorFlag && (fileCount-- > 0)) { |
| 666 | saveFile (*fileTable++, FALSE); |
| 667 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 668 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 669 | /* |
| 670 | * Now write an empty block of zeroes to end the archive. |
| 671 | */ |
| 672 | writeTarBlock ("", 1); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 673 | |
| 674 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 675 | done: |
| 676 | /* |
| 677 | * Close the tar file and check for errors if it was opened. |
| 678 | */ |
| 679 | if ((tostdoutFlag == FALSE) && (tarFd >= 0) && (close (tarFd) < 0)) |
| 680 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | |
| 684 | /* |
| 685 | * Save one file into the tar file. |
| 686 | * If the file is a directory, then this will recursively save all of |
| 687 | * the files and directories within the directory. The seeLinks |
| 688 | * flag indicates whether or not we want to see symbolic links as |
| 689 | * they really are, instead of blindly following them. |
| 690 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 691 | static void saveFile (const char *fileName, int seeLinks) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 692 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 693 | int status; |
| 694 | int mode; |
| 695 | struct stat statbuf; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 696 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 697 | if (verboseFlag) |
| 698 | printf ("a %s\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 699 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 700 | /* |
| 701 | * Check that the file name will fit in the header. |
| 702 | */ |
| 703 | if (strlen (fileName) >= TAR_NAME_SIZE) { |
| 704 | fprintf (stderr, "%s: File name is too long\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 705 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 706 | return; |
| 707 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 708 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 709 | /* |
| 710 | * Find out about the file. |
| 711 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 712 | #ifdef S_ISLNK |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 713 | if (seeLinks) |
| 714 | status = lstat (fileName, &statbuf); |
| 715 | else |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 716 | #endif |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 717 | status = stat (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 718 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 719 | if (status < 0) { |
| 720 | perror (fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 721 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 722 | return; |
| 723 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 724 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 725 | /* |
| 726 | * Make sure we aren't trying to save our file into itself. |
| 727 | */ |
| 728 | if ((statbuf.st_dev == tarDev) && (statbuf.st_ino == tarInode)) { |
| 729 | fprintf (stderr, "Skipping saving of archive file itself\n"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 730 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 731 | return; |
| 732 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 733 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 734 | /* |
| 735 | * Check the type of file. |
| 736 | */ |
| 737 | mode = statbuf.st_mode; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 738 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 739 | if (S_ISDIR (mode)) { |
| 740 | saveDirectory (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 741 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 742 | return; |
| 743 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 744 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 745 | if (S_ISREG (mode)) { |
| 746 | saveRegularFile (fileName, &statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 747 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 748 | return; |
| 749 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 750 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 751 | /* |
| 752 | * The file is a strange type of file, ignore it. |
| 753 | */ |
| 754 | fprintf (stderr, "%s: not a directory or regular file\n", fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | |
| 758 | /* |
| 759 | * Save a regular file to the tar file. |
| 760 | */ |
| 761 | static void |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 762 | saveRegularFile (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 763 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 764 | int sawEof; |
| 765 | int fileFd; |
| 766 | int cc; |
| 767 | int dataCount; |
| 768 | long fullDataCount; |
| 769 | char data[TAR_BLOCK_SIZE * 16]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 770 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 771 | /* |
| 772 | * Open the file for reading. |
| 773 | */ |
| 774 | fileFd = open (fileName, O_RDONLY); |
| 775 | |
| 776 | if (fileFd < 0) { |
| 777 | perror (fileName); |
| 778 | |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | /* |
| 783 | * Write out the header for the file. |
| 784 | */ |
| 785 | writeHeader (fileName, statbuf); |
| 786 | |
| 787 | /* |
| 788 | * Write the data blocks of the file. |
| 789 | * We must be careful to write the amount of data that the stat |
| 790 | * buffer indicated, even if the file has changed size. Otherwise |
| 791 | * the tar file will be incorrect. |
| 792 | */ |
| 793 | fullDataCount = statbuf->st_size; |
| 794 | sawEof = FALSE; |
| 795 | |
| 796 | while (fullDataCount > 0) { |
| 797 | /* |
| 798 | * Get the amount to write this iteration which is |
| 799 | * the minumum of the amount left to write and the |
| 800 | * buffer size. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 801 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 802 | dataCount = sizeof (data); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 803 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 804 | if (dataCount > fullDataCount) |
| 805 | dataCount = (int) fullDataCount; |
| 806 | |
| 807 | /* |
| 808 | * Read the data from the file if we haven't seen the |
| 809 | * end of file yet. |
| 810 | */ |
| 811 | cc = 0; |
| 812 | |
| 813 | if (!sawEof) { |
| 814 | cc = fullRead (fileFd, data, dataCount); |
| 815 | |
| 816 | if (cc < 0) { |
| 817 | perror (fileName); |
| 818 | |
| 819 | (void) close (fileFd); |
| 820 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 821 | |
| 822 | return; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | /* |
| 826 | * If the file ended too soon, complain and set |
| 827 | * a flag so we will zero fill the rest of it. |
| 828 | */ |
| 829 | if (cc < dataCount) { |
| 830 | fprintf (stderr, |
| 831 | "%s: Short read - zero filling", fileName); |
| 832 | |
| 833 | sawEof = TRUE; |
| 834 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 837 | /* |
| 838 | * Zero fill the rest of the data if necessary. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 839 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 840 | if (cc < dataCount) |
| 841 | memset (data + cc, 0, dataCount - cc); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 842 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 843 | /* |
| 844 | * Write the buffer to the TAR file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 845 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 846 | writeTarBlock (data, dataCount); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 847 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 848 | fullDataCount -= dataCount; |
| 849 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 850 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 851 | /* |
| 852 | * Close the file. |
| 853 | */ |
| 854 | if ((tostdoutFlag == FALSE) && close (fileFd) < 0) |
| 855 | fprintf (stderr, "%s: close: %s\n", fileName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | |
| 859 | /* |
| 860 | * Save a directory and all of its files to the tar file. |
| 861 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 862 | static void saveDirectory (const char *dirName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 863 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 864 | DIR *dir; |
| 865 | struct dirent *entry; |
| 866 | int needSlash; |
Eric Andersen | 1b61f41 | 1999-10-13 18:56:42 +0000 | [diff] [blame] | 867 | char fullName[NAME_MAX]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 868 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 869 | /* |
| 870 | * Construct the directory name as used in the tar file by appending |
| 871 | * a slash character to it. |
| 872 | */ |
| 873 | strcpy (fullName, dirName); |
| 874 | strcat (fullName, "/"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 875 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 876 | /* |
| 877 | * Write out the header for the directory entry. |
| 878 | */ |
| 879 | writeHeader (fullName, statbuf); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 880 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 881 | /* |
| 882 | * Open the directory. |
| 883 | */ |
| 884 | dir = opendir (dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 885 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 886 | if (dir == NULL) { |
| 887 | fprintf (stderr, "Cannot read directory \"%s\": %s\n", |
| 888 | dirName, strerror (errno)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 889 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 890 | return; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * See if a slash is needed. |
| 895 | */ |
| 896 | needSlash = (*dirName && (dirName[strlen (dirName) - 1] != '/')); |
| 897 | |
| 898 | /* |
| 899 | * Read all of the directory entries and check them, |
| 900 | * except for the current and parent directory entries. |
| 901 | */ |
| 902 | while (!errorFlag && ((entry = readdir (dir)) != NULL)) { |
| 903 | if ((strcmp (entry->d_name, ".") == 0) || |
| 904 | (strcmp (entry->d_name, "..") == 0)) { |
| 905 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 908 | /* |
| 909 | * Build the full path name to the file. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 910 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 911 | strcpy (fullName, dirName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 912 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 913 | if (needSlash) |
| 914 | strcat (fullName, "/"); |
| 915 | |
| 916 | strcat (fullName, entry->d_name); |
| 917 | |
| 918 | /* |
| 919 | * Write this file to the tar file, noticing whether or not |
| 920 | * the file is a symbolic link. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 921 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 922 | saveFile (fullName, TRUE); |
| 923 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 924 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 925 | /* |
| 926 | * All done, close the directory. |
| 927 | */ |
| 928 | closedir (dir); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | |
| 932 | /* |
| 933 | * Write a tar header for the specified file name and status. |
| 934 | * It is assumed that the file name fits. |
| 935 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 936 | static void writeHeader (const char *fileName, const struct stat *statbuf) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 937 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 938 | long checkSum; |
| 939 | const unsigned char *cp; |
| 940 | int len; |
| 941 | TarHeader header; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 942 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 943 | /* |
| 944 | * Zero the header block in preparation for filling it in. |
| 945 | */ |
| 946 | memset ((char *) &header, 0, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 947 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 948 | /* |
| 949 | * Fill in the header. |
| 950 | */ |
| 951 | strcpy (header.name, fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 952 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 953 | strncpy (header.magic, TAR_MAGIC, sizeof (header.magic)); |
| 954 | strncpy (header.version, TAR_VERSION, sizeof (header.version)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 955 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 956 | putOctal (header.mode, sizeof (header.mode), statbuf->st_mode & 0777); |
| 957 | putOctal (header.uid, sizeof (header.uid), statbuf->st_uid); |
| 958 | putOctal (header.gid, sizeof (header.gid), statbuf->st_gid); |
| 959 | putOctal (header.size, sizeof (header.size), statbuf->st_size); |
| 960 | putOctal (header.mtime, sizeof (header.mtime), statbuf->st_mtime); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 961 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 962 | header.typeFlag = TAR_TYPE_REGULAR; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 963 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 964 | /* |
| 965 | * Calculate and store the checksum. |
| 966 | * This is the sum of all of the bytes of the header, |
| 967 | * with the checksum field itself treated as blanks. |
| 968 | */ |
| 969 | memset (header.checkSum, ' ', sizeof (header.checkSum)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 970 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 971 | cp = (const unsigned char *) &header; |
| 972 | len = sizeof (header); |
| 973 | checkSum = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 974 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 975 | while (len-- > 0) |
| 976 | checkSum += *cp++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 977 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 978 | putOctal (header.checkSum, sizeof (header.checkSum), checkSum); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 979 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 980 | /* |
| 981 | * Write the tar header. |
| 982 | */ |
| 983 | writeTarBlock ((const char *) &header, sizeof (header)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
| 987 | /* |
| 988 | * Write data to one or more blocks of the tar file. |
| 989 | * The data is always padded out to a multiple of TAR_BLOCK_SIZE. |
| 990 | * The errorFlag static variable is set on an error. |
| 991 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 992 | static void writeTarBlock (const char *buf, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 993 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 994 | int partialLength; |
| 995 | int completeLength; |
| 996 | char fullBlock[TAR_BLOCK_SIZE]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 997 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 998 | /* |
| 999 | * If we had a write error before, then do nothing more. |
| 1000 | */ |
| 1001 | if (errorFlag) |
| 1002 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1003 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1004 | /* |
| 1005 | * Get the amount of complete and partial blocks. |
| 1006 | */ |
| 1007 | partialLength = len % TAR_BLOCK_SIZE; |
| 1008 | completeLength = len - partialLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1009 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1010 | /* |
| 1011 | * Write all of the complete blocks. |
| 1012 | */ |
| 1013 | if ((completeLength > 0) && !fullWrite (tarFd, buf, completeLength)) { |
| 1014 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1015 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1016 | errorFlag = TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1017 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1018 | return; |
| 1019 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1020 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1021 | /* |
| 1022 | * If there are no partial blocks left, we are done. |
| 1023 | */ |
| 1024 | if (partialLength == 0) |
| 1025 | return; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1026 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1027 | /* |
| 1028 | * Copy the partial data into a complete block, and pad the rest |
| 1029 | * of it with zeroes. |
| 1030 | */ |
| 1031 | memcpy (fullBlock, buf + completeLength, partialLength); |
| 1032 | memset (fullBlock + partialLength, 0, TAR_BLOCK_SIZE - partialLength); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1033 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1034 | /* |
| 1035 | * Write the last complete block. |
| 1036 | */ |
| 1037 | if (!fullWrite (tarFd, fullBlock, TAR_BLOCK_SIZE)) { |
| 1038 | perror (tarName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1039 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1040 | errorFlag = TRUE; |
| 1041 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | |
| 1045 | /* |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1046 | * Read an octal value in a field of the specified width, with optional |
| 1047 | * spaces on both sides of the number and with an optional null character |
| 1048 | * at the end. Returns -1 on an illegal format. |
| 1049 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1050 | static long getOctal (const char *cp, int len) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1051 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1052 | long val; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1053 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1054 | while ((len > 0) && (*cp == ' ')) { |
| 1055 | cp++; |
| 1056 | len--; |
| 1057 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1058 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1059 | if ((len == 0) || !isOctal (*cp)) |
| 1060 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1061 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1062 | val = 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1063 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1064 | while ((len > 0) && isOctal (*cp)) { |
| 1065 | val = val * 8 + *cp++ - '0'; |
| 1066 | len--; |
| 1067 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1068 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1069 | while ((len > 0) && (*cp == ' ')) { |
| 1070 | cp++; |
| 1071 | len--; |
| 1072 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1073 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1074 | if ((len > 0) && *cp) |
| 1075 | return -1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1076 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1077 | return val; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | |
| 1081 | /* |
| 1082 | * Put an octal string into the specified buffer. |
| 1083 | * The number is zero and space padded and possibly null padded. |
| 1084 | * Returns TRUE if successful. |
| 1085 | */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1086 | static int putOctal (char *cp, int len, long value) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1087 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1088 | int tempLength; |
| 1089 | char *tempString; |
| 1090 | char tempBuffer[32]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1091 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1092 | /* |
| 1093 | * Create a string of the specified length with an initial space, |
| 1094 | * leading zeroes and the octal number, and a trailing null. |
| 1095 | */ |
| 1096 | tempString = tempBuffer; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1097 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1098 | sprintf (tempString, " %0*lo", len - 2, value); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1099 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1100 | tempLength = strlen (tempString) + 1; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1101 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1102 | /* |
| 1103 | * If the string is too large, suppress the leading space. |
| 1104 | */ |
| 1105 | if (tempLength > len) { |
| 1106 | tempLength--; |
| 1107 | tempString++; |
| 1108 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1109 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1110 | /* |
| 1111 | * If the string is still too large, suppress the trailing null. |
| 1112 | */ |
| 1113 | if (tempLength > len) |
| 1114 | tempLength--; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1115 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1116 | /* |
| 1117 | * If the string is still too large, fail. |
| 1118 | */ |
| 1119 | if (tempLength > len) |
| 1120 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1121 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1122 | /* |
| 1123 | * Copy the string to the field. |
| 1124 | */ |
| 1125 | memcpy (cp, tempString, len); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1126 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1127 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | |
| 1131 | /* |
| 1132 | * See if the specified file name belongs to one of the specified list |
| 1133 | * of path prefixes. An empty list implies that all files are wanted. |
| 1134 | * Returns TRUE if the file is selected. |
| 1135 | */ |
Eric Andersen | f811e07 | 1999-10-09 00:25:00 +0000 | [diff] [blame] | 1136 | static int |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1137 | wantFileName (const char *fileName, int fileCount, char **fileTable) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1138 | { |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1139 | const char *pathName; |
| 1140 | int fileLength; |
| 1141 | int pathLength; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1142 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1143 | /* |
| 1144 | * If there are no files in the list, then the file is wanted. |
| 1145 | */ |
| 1146 | if (fileCount == 0) |
| 1147 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1148 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1149 | fileLength = strlen (fileName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1150 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1151 | /* |
| 1152 | * Check each of the test paths. |
| 1153 | */ |
| 1154 | while (fileCount-- > 0) { |
| 1155 | pathName = *fileTable++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1156 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1157 | pathLength = strlen (pathName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1158 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1159 | if (fileLength < pathLength) |
| 1160 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1161 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1162 | if (memcmp (fileName, pathName, pathLength) != 0) |
| 1163 | continue; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1164 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1165 | if ((fileLength == pathLength) || (fileName[pathLength] == '/')) { |
| 1166 | return TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1167 | } |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1168 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1169 | |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 1170 | return FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1175 | /* END CODE */ |