Notes on portability, and on when #include <linux/blah> is appropriate.
[oweals/busybox.git] / archival / tar.c
index a4b13b5def5afd291d04a481b93c35f2970f0e7e..e4008b0c9c5a141216bd8fb759edf9b0141d896d 100644 (file)
@@ -19,9 +19,8 @@
  *
  * Based in part on the tar implementation from busybox-0.28
  *  Copyright (C) 1995 Bruce Perens
- *  This is free software under the GNU General Public License.
  *
- * Licensed under GPL v2 (or later), see file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 #include <fcntl.h>
@@ -44,7 +43,7 @@
 
 /* Tar file constants  */
 # define TAR_MAGIC          "ustar"    /* ustar and a null */
-# define TAR_VERSION        "  "       /* Be compatable with GNU tar format */
+# define TAR_VERSION        "  "       /* Be compatible with GNU tar format */
 
 #define TAR_BLOCK_SIZE         512
 #define TAR_MAGIC_LEN          6
@@ -91,7 +90,7 @@ struct HardLinkInfo {
 struct TarBallInfo {
        char *fileName;                 /* File name of the tarball */
        int tarFd;                              /* Open-for-write file descriptor
-                                                          for the tarball */
+                                                          for the tarball */
        struct stat statBuf;    /* Stat info for the tarball, letting
                                                           us know the inode and device that the
                                                           tarball lives, so we can avoid trying
@@ -569,7 +568,7 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
 
        /* do the decompression, and cleanup */
        if (bb_xread_char(archive_handle->src_fd) != 0x1f ||
-               bb_xread_char(archive_handle->src_fd) != 0x9d)
+               bb_xread_char(archive_handle->src_fd) != 0x9d)
        {
                bb_error_msg_and_die("Invalid magic");
        }
@@ -597,7 +596,7 @@ static char get_header_tar_Z(archive_handle_t *archive_handle)
 #define TAR_OPT_AFTER_START               8
 
 #define CTX_CREATE                        (1 << (TAR_OPT_AFTER_START))
-#define TAR_OPT_DEREFERNCE                (1 << (TAR_OPT_AFTER_START + 1))
+#define TAR_OPT_DEREFERENCE               (1 << (TAR_OPT_AFTER_START + 1))
 #ifdef CONFIG_FEATURE_TAR_CREATE
 # define TAR_OPT_STR_CREATE               "ch"
 # define TAR_OPT_AFTER_CREATE             TAR_OPT_AFTER_START + 2
@@ -699,7 +698,7 @@ static const struct option tar_long_options[] = {
 # ifdef CONFIG_FEATURE_TAR_COMPRESS
        { "compress",                   0,      NULL,   'Z' },
 # endif
-       { 0,                    0, 0, 0 }
+       { 0,                                    0, 0, 0 }
 };
 #else
 #define tar_long_options       0
@@ -713,7 +712,7 @@ int tar_main(int argc, char **argv)
        const char *tar_filename = "-";
        unsigned long opt;
        llist_t *excludes = NULL;
-       
+
        /* Initialise default values */
        tar_handle = init_handle();
        tar_handle->flags = ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
@@ -743,7 +742,7 @@ int tar_main(int argc, char **argv)
        }
        if((opt & CTX_EXTRACT) && tar_handle->action_data != data_extract_to_stdout)
                tar_handle->action_data = data_extract_all;
-               
+
        if (opt & TAR_OPT_2STDOUT)
                tar_handle->action_data = data_extract_to_stdout;
 
@@ -834,8 +833,8 @@ int tar_main(int argc, char **argv)
                }
        }
 
-       if ((base_dir) && (chdir(base_dir)))
-               bb_perror_msg_and_die("Couldnt chdir to %s", base_dir);
+       if (base_dir)
+               bb_xchdir(base_dir);
 
        /* create an archive */
        if (ENABLE_FEATURE_TAR_CREATE && (opt & CTX_CREATE)) {
@@ -843,16 +842,17 @@ int tar_main(int argc, char **argv)
                int zipMode = 0;
 
                if (ENABLE_FEATURE_TAR_GZIP && get_header_ptr == get_header_tar_gz)
-                       zipMode = 1;
+                       zipMode = 1;
                if (ENABLE_FEATURE_TAR_BZIP2 && get_header_ptr == get_header_tar_bz2)
-                       zipMode = 2;
+                       zipMode = 2;
 
                if ((tar_handle->action_header == header_list) ||
                                (tar_handle->action_header == header_verbose_list))
                {
                        verboseFlag = TRUE;
                }
-               writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERNCE, tar_handle->accept,
+               writeTarFile(tar_handle->src_fd, verboseFlag, opt & TAR_OPT_DEREFERENCE,
+                               tar_handle->accept,
                        tar_handle->reject, zipMode);
        } else {
                while (get_header_ptr(tar_handle) == EXIT_SUCCESS);