From 3183dbdb9defd1e8f3fc63f965f6035cfb69edc5 Mon Sep 17 00:00:00 2001
From: Shreenidhi Shedi <sshedi@vmware.com>
Date: Thu, 2 Sep 2021 23:45:59 +0530
Subject: [PATCH] Add CFLAGS in configure.ac & fix compiler warnings

Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
---
 configure.ac   |  2 ++
 src/copyin.c   | 10 +++++-----
 src/copyout.c  |  4 ++--
 src/extern.h   |  2 +-
 src/main.c     | 12 +++++++-----
 src/makepath.c | 16 ++++++++--------
 src/mt.c       |  7 ++++---
 src/tar.c      |  2 +-
 src/util.c     | 22 +++++++++++-----------
 9 files changed, 41 insertions(+), 36 deletions(-)

diff --git a/configure.ac b/configure.ac
index 0a35e4c..6b3c314 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,8 @@ AM_INIT_AUTOMAKE([1.15 gnits tar-ustar dist-bzip2 std-options silent-rules])
 # Enable silent rules by default:
 AM_SILENT_RULES([yes])
 
+CFLAGS="$CFLAGS -Wall -Wextra"
+
 dnl Check for programs
 AC_PROG_CC
 AC_PROG_CPP
diff --git a/src/copyin.c b/src/copyin.c
index 75f892e..9702b03 100644
--- a/src/copyin.c
+++ b/src/copyin.c
@@ -109,7 +109,7 @@ get_link_name (struct cpio_file_stat *file_hdr, int in_file_des)
 {
   char *link_name;
 
-  if (file_hdr->c_filesize < 0 || file_hdr->c_filesize > SIZE_MAX-1)
+  if (file_hdr->c_filesize < 0 || (long unsigned int) file_hdr->c_filesize > SIZE_MAX-1)
     {
       error (0, 0, _("%s: stored filename length is out of range"),
 	     file_hdr->c_name);
@@ -261,8 +261,8 @@ create_defered_links (struct cpio_file_stat *file_hdr)
   d_prev = NULL;
   while (d != NULL)
     {
-      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
-	  && (d->header.c_dev_min == min) )
+      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == (unsigned int) maj)
+	  && (d->header.c_dev_min == (unsigned int) min) )
 	{
 	  struct deferment *d_free;
 	  link_res = link_to_name (d->header.c_name, file_hdr->c_name);
@@ -314,8 +314,8 @@ create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
   d_prev = NULL;
   while (d != NULL)
     {
-      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
-	  && (d->header.c_dev_min == min) )
+      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == (unsigned int) maj)
+	  && (d->header.c_dev_min == (unsigned int) min) )
 	{
 	  if (d_prev != NULL)
 	    d_prev->next = d->next;
diff --git a/src/copyout.c b/src/copyout.c
index 32f6584..1caeea1 100644
--- a/src/copyout.c
+++ b/src/copyout.c
@@ -167,8 +167,8 @@ writeout_other_defers (struct cpio_file_stat *file_hdr, int out_des)
   d = deferouts;
   while (d != NULL)
     {
-      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
-	  && (d->header.c_dev_min == min) )
+      if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == (unsigned int) maj)
+	  && (d->header.c_dev_min == (unsigned int) min) )
 	{
 	  struct deferment *d_free;
 	  d->header.c_filesize = 0;
diff --git a/src/extern.h b/src/extern.h
index 91c34e7..07a8354 100644
--- a/src/extern.h
+++ b/src/extern.h
@@ -167,7 +167,7 @@ void disk_empty_output_buffer (int out_des, bool flush);
 void swahw_array (char *ptr, int count);
 void tape_buffered_write (char *in_buf, int out_des, off_t num_bytes);
 void tape_buffered_read (char *in_buf, int in_des, off_t num_bytes);
-int tape_buffered_peek (char *peek_buf, int in_des, int num_bytes);
+int tape_buffered_peek (char *peek_buf, int in_des, long num_bytes);
 void tape_toss_input (int in_des, off_t num_bytes);
 void copy_files_tape_to_disk (int in_des, int out_des, off_t num_bytes);
 void copy_files_disk_to_tape (int in_des, int out_des, off_t num_bytes, char *filename);
diff --git a/src/main.c b/src/main.c
index 52c30a9..9c0ddf5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -193,12 +193,12 @@ static struct argp_option options[] = {
   {NULL, 'O', N_("[[USER@]HOST:]FILE-NAME"), 0,
    N_("Archive filename to use instead of standard output. Optional USER and HOST specify the user and host names in case of a remote archive"), GRID+1 },
   {"renumber-inodes", RENUMBER_INODES_OPTION, NULL, 0,
-   N_("Renumber inodes") },
+   N_("Renumber inodes"), 0 },
   {"ignore-devno", IGNORE_DEVNO_OPTION, NULL, 0,
-   N_("Don't store device numbers") },
+   N_("Don't store device numbers"), 0 },
   {"device-independent", DEVICE_INDEPENDENT_OPTION, NULL, 0,
-   N_("Create device-independent (reproducible) archives") },
-  {"reproducible", 0, NULL, OPTION_ALIAS },
+   N_("Create device-independent (reproducible) archives"), 0 },
+  {"reproducible", 0, NULL, OPTION_ALIAS, NULL, 0 },
 #undef GRID
 
   /* ********** */
@@ -248,7 +248,7 @@ static struct argp_option options[] = {
    N_("Write files with large blocks of zeros as sparse files"), GRID+1 },
 #undef GRID
 
-  {0, 0, 0, 0}
+  {0, 0, 0, 0, NULL, 0}
 };
 
 static char *input_archive_name = 0;
@@ -295,6 +295,8 @@ warn_control (char *arg)
 static error_t
 parse_opt (int key, char *arg, struct argp_state *state)
 {
+  (void)state; //unused
+
   switch (key)
     {
     case '0':		/* Read null-terminated filenames.  */
diff --git a/src/makepath.c b/src/makepath.c
index c9e372d..d34db54 100644
--- a/src/makepath.c
+++ b/src/makepath.c
@@ -91,11 +91,11 @@ make_path (char const *argpath,
 		    stat_error (dirpath);
 		  else
 		    {
-		      if (owner != -1)
+		      if ((int) owner != -1)
 			stats.st_uid = owner;
-		      if (group != -1)
+		      if ((int) group != -1)
 			stats.st_gid = group;
-		      
+
 		      delay_set_stat (dirpath, &stats, invert_permissions);
 		    }
 		}
@@ -119,7 +119,7 @@ make_path (char const *argpath,
 
       if (mkdir (dirpath, tmpmode ^ invert_permissions))
 	{
-	  /* In some cases, if the final component in dirpath was `.' then we 
+	  /* In some cases, if the final component in dirpath was `.' then we
 	     just got an EEXIST error from that last mkdir().  If that's
 	     the case, ignore it.  */
 	  if ( (errno != EEXIST) ||
@@ -134,14 +134,14 @@ make_path (char const *argpath,
 	stat_error (dirpath);
       else
 	{
-	  if (owner != -1)
+	  if ((int) owner != -1)
 	    stats.st_uid = owner;
-	  if (group != -1)
+	  if ((int) group != -1)
 	    stats.st_gid = group;
-	  
+
 	  delay_set_stat (dirpath, &stats, invert_permissions);
 	}
-	
+
       if (verbose_fmt_string != NULL)
 	error (0, 0, verbose_fmt_string, dirpath);
 
diff --git a/src/mt.c b/src/mt.c
index b89a5fc..247356a 100644
--- a/src/mt.c
+++ b/src/mt.c
@@ -178,9 +178,9 @@ enum
 
 static struct argp_option options[] = {
   { "file", 'f', N_("DEVICE"), 0,
-    N_("use device as the file name of the tape drive to operate on") },
+    N_("use device as the file name of the tape drive to operate on"), 0 },
   { "rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
-    N_("use remote COMMAND instead of rsh") },
+    N_("use remote COMMAND instead of rsh"), 0 },
   { NULL }
 };
 
@@ -208,7 +208,8 @@ parse_opt (int key, char *arg, struct argp_state *state)
 	  {
 	    char *p;
 	    long val = strtol (arg, &p, 0);
-	    if (*p || (count = val) != count)
+        count = val;
+	    if (*p || (val != count))
 	      error (MT_EXIT_INVOP, 0, _("invalid count value"));
 	  }
 	  break;
diff --git a/src/tar.c b/src/tar.c
index 280b5a9..349331a 100644
--- a/src/tar.c
+++ b/src/tar.c
@@ -209,7 +209,7 @@ write_out_tar_header (struct cpio_file_stat *file_hdr, int out_des)
       char *name;
 
       strncpy (tar_hdr->magic, TMAGIC, TMAGLEN);
-      strncpy (tar_hdr->version, TVERSION, TVERSLEN);
+      memcpy (tar_hdr->version, TVERSION, TVERSLEN);
 
       name = getuser (file_hdr->c_uid);
       if (name)
diff --git a/src/util.c b/src/util.c
index edeeb9a..cbe87c0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -72,7 +72,7 @@ tape_empty_output_buffer (int out_des)
 #endif
 
   bytes_written = rmtwrite (out_des, output_buffer, output_size);
-  if (bytes_written != output_size)
+  if ((size_t) bytes_written != output_size)
     {
       int rest_bytes_written;
       int rest_output_size;
@@ -139,7 +139,7 @@ disk_empty_output_buffer (int out_des, bool flush)
   else
     bytes_written = write (out_des, output_buffer, output_size);
 
-  if (bytes_written != output_size)
+  if ((size_t) bytes_written != output_size)
     {
       if (bytes_written == -1)
 	error (PAXEXIT_FAILURE, errno, _("write error"));
@@ -302,7 +302,7 @@ tape_buffered_read (char *in_buf, int in_des, off_t num_bytes)
     {
       if (input_size == 0)
 	tape_fill_input_buffer (in_des, io_block_size);
-      if (bytes_left < input_size)
+      if ((size_t)bytes_left < input_size)
 	space_left = bytes_left;
       else
 	space_left = input_size;
@@ -323,7 +323,7 @@ tape_buffered_read (char *in_buf, int in_des, off_t num_bytes)
    then EOF has been reached.  */
 
 int
-tape_buffered_peek (char *peek_buf, int in_des, int num_bytes)
+tape_buffered_peek (char *peek_buf, int in_des, long num_bytes)
 {
   long tmp_input_size;
   long got_bytes;
@@ -342,10 +342,10 @@ tape_buffered_peek (char *peek_buf, int in_des, int num_bytes)
     }
 #endif
 
-  while (input_size < num_bytes)
+  while (input_size < (size_t) num_bytes)
     {
       append_buf = in_buff + input_size;
-      if ( (append_buf - input_buffer) >= input_buffer_size)
+      if ( (append_buf - input_buffer) >= (long int)input_buffer_size)
 	{
 	  /* We can keep up to 2 "blocks" (either the physical block size
 	     or 512 bytes(the size of a tar record), which ever is
@@ -376,7 +376,7 @@ tape_buffered_peek (char *peek_buf, int in_des, int num_bytes)
       input_bytes += tmp_input_size;
       input_size += tmp_input_size;
     }
-  if (num_bytes <= input_size)
+  if ((size_t)num_bytes <= input_size)
     got_bytes = num_bytes;
   else
     got_bytes = input_size;
@@ -396,7 +396,7 @@ tape_toss_input (int in_des, off_t num_bytes)
     {
       if (input_size == 0)
 	tape_fill_input_buffer (in_des, io_block_size);
-      if (bytes_left < input_size)
+      if ((size_t) bytes_left < input_size)
 	space_left = bytes_left;
       else
 	space_left = input_size;
@@ -449,7 +449,7 @@ copy_files_tape_to_disk (int in_des, int out_des, off_t num_bytes)
     {
       if (input_size == 0)
 	tape_fill_input_buffer (in_des, io_block_size);
-      size = (input_size < num_bytes) ? input_size : num_bytes;
+      size = (input_size < (size_t) num_bytes) ? (off_t) input_size : num_bytes;
       if (crc_i_flag)
 	{
 	  for (k = 0; k < size; ++k)
@@ -503,7 +503,7 @@ copy_files_disk_to_tape (int in_des, int out_des, off_t num_bytes,
 	    write_nuls_to_file (num_bytes, out_des, tape_buffered_write);
 	    break;
 	  }
-      size = (input_size < num_bytes) ? input_size : num_bytes;
+      size = (input_size < (size_t) num_bytes) ? (off_t) input_size : num_bytes;
       if (crc_i_flag)
 	{
 	  for (k = 0; k < size; ++k)
@@ -554,7 +554,7 @@ copy_files_disk_to_disk (int in_des, int out_des, off_t num_bytes,
 	    write_nuls_to_file (num_bytes, out_des, disk_buffered_write);
 	    break;
 	  }
-      size = (input_size < num_bytes) ? input_size : num_bytes;
+      size = (input_size < (size_t) num_bytes) ? (off_t) input_size : num_bytes;
       if (crc_i_flag)
 	{
 	  for (k = 0; k < size; ++k)
-- 
2.17.1

