[dev] [sbase] [PATCH 3/4] tar: Implement -m flag

From: Michael Forney <mforney_AT_mforney.org>
Date: Sat, 1 Nov 2014 20:36:39 +0000

This changes the default behavior to adjust mtimes to what is present in
the file header.
---
 tar.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/tar.c b/tar.c
index 7bd709d..47d2334 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -2,9 +2,11 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <stdbool.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <limits.h>
 #include <grp.h>
 #include <pwd.h>
_AT_@ -49,13 +51,15 @@ static FILE *tarfile;
 static ino_t tarinode;
 static dev_t tardev;
 
+static bool mflag = false;
+
 static void
 usage(void)
 {
-	eprintf("usage: tar [-f tarfile] [-C dir] [-]x|t\n"
+	eprintf("usage: tar [-f tarfile] [-C dir] [-]x[m]|t\n"
 	        "       tar [-f tarfile] [-C dir] [-]c dir\n"
 	        "       tar [-C dir] cf tarfile dir\n"
-	        "       tar [-C dir] x|tf tarfile\n");
+	        "       tar [-C dir] x[m]|tf tarfile\n");
 }
 
 int
_AT_@ -79,6 +83,9 @@ main(int argc, char *argv[])
 	case 'f':
 		file = EARGF(usage());
 		break;
+	case 'm':
+		mflag = true;
+		break;
 	default:
 		usage();
 	} ARGEND;
_AT_@ -108,6 +115,9 @@ main(int argc, char *argv[])
 				argc--, argv++;
 				dir = argv[0];
 				break;
+			case 'm':
+				mflag = true;
+				break;
 			default:
 				usage();
 			}
_AT_@ -224,9 +234,12 @@ unarchive(char *fname, int l, char b[Blksiz])
 {
 	char lname[101];
 	FILE *f = NULL;
-	unsigned long  mode, major, minor, type;
+	unsigned long  mode, major, minor, type, mtime;
+	struct timeval times[2] = {0};
 	Header *h = (void*)b;
 
+	if(!mflag)
+		mtime = strtoul(h->mtime, 0, 8);
 	unlink(fname);
 	switch(h->type) {
 	case REG:
_AT_@ -276,6 +289,12 @@ unarchive(char *fname, int l, char b[Blksiz])
 	}
 	if(f)
 		fclose(f);
+
+	if(!mflag) {
+		times[0].tv_sec = times[1].tv_sec = mtime;
+		if(utimes(fname, times))
+			perror(fname);
+	}
 	return 0;
 }
 
-- 
2.1.3.1.g339ec9c
Received on Sat Nov 01 2014 - 21:36:39 CET

This archive was generated by hypermail 2.3.0 : Sat Nov 01 2014 - 21:48:19 CET