touch: Use both atime and mtime of reference file
This commit is contained in:
		
							
								
								
									
										22
									
								
								touch.c
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								touch.c
									
									
									
									
									
								
							@@ -14,14 +14,13 @@
 | 
				
			|||||||
static int aflag;
 | 
					static int aflag;
 | 
				
			||||||
static int cflag;
 | 
					static int cflag;
 | 
				
			||||||
static int mflag;
 | 
					static int mflag;
 | 
				
			||||||
static struct timespec t;
 | 
					static struct timespec times[2];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void
 | 
					static void
 | 
				
			||||||
touch(const char *file)
 | 
					touch(const char *file)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int fd;
 | 
						int fd;
 | 
				
			||||||
	struct stat st;
 | 
						struct stat st;
 | 
				
			||||||
	struct timespec times[2];
 | 
					 | 
				
			||||||
	int r;
 | 
						int r;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ((r = stat(file, &st)) < 0) {
 | 
						if ((r = stat(file, &st)) < 0) {
 | 
				
			||||||
@@ -30,8 +29,10 @@ touch(const char *file)
 | 
				
			|||||||
		if (cflag)
 | 
							if (cflag)
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
	} else if (!r) {
 | 
						} else if (!r) {
 | 
				
			||||||
		times[0] = aflag ? t : st.st_atim;
 | 
							if (!aflag)
 | 
				
			||||||
		times[1] = mflag ? t : st.st_mtim;
 | 
								times[0] = st.st_atim;
 | 
				
			||||||
 | 
							if (!mflag)
 | 
				
			||||||
 | 
								times[1] = st.st_mtim;
 | 
				
			||||||
		if (utimensat(AT_FDCWD, file, times, 0) < 0)
 | 
							if (utimensat(AT_FDCWD, file, times, 0) < 0)
 | 
				
			||||||
			eprintf("utimensat %s:", file);
 | 
								eprintf("utimensat %s:", file);
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
@@ -117,8 +118,8 @@ int
 | 
				
			|||||||
main(int argc, char *argv[])
 | 
					main(int argc, char *argv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct stat st;
 | 
						struct stat st;
 | 
				
			||||||
	char *ref;
 | 
						char *ref = NULL;
 | 
				
			||||||
	clock_gettime(CLOCK_REALTIME, &t);
 | 
						clock_gettime(CLOCK_REALTIME, ×[0]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ARGBEGIN {
 | 
						ARGBEGIN {
 | 
				
			||||||
	case 'a':
 | 
						case 'a':
 | 
				
			||||||
@@ -129,7 +130,7 @@ main(int argc, char *argv[])
 | 
				
			|||||||
		break;
 | 
							break;
 | 
				
			||||||
	case 'd':
 | 
						case 'd':
 | 
				
			||||||
	case 't':
 | 
						case 't':
 | 
				
			||||||
		t.tv_sec = parsetime(EARGF(usage()), t.tv_sec);
 | 
							times[0].tv_sec = parsetime(EARGF(usage()), times[0].tv_sec);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case 'm':
 | 
						case 'm':
 | 
				
			||||||
		mflag = 1;
 | 
							mflag = 1;
 | 
				
			||||||
@@ -138,10 +139,11 @@ main(int argc, char *argv[])
 | 
				
			|||||||
		ref = EARGF(usage());
 | 
							ref = EARGF(usage());
 | 
				
			||||||
		if (stat(ref, &st) < 0)
 | 
							if (stat(ref, &st) < 0)
 | 
				
			||||||
			eprintf("stat '%s':", ref);
 | 
								eprintf("stat '%s':", ref);
 | 
				
			||||||
		t = st.st_mtim;
 | 
							times[0] = st.st_atim;
 | 
				
			||||||
 | 
							times[1] = st.st_mtim;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case 'T':
 | 
						case 'T':
 | 
				
			||||||
		t.tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
 | 
							times[0].tv_sec = estrtonum(EARGF(usage()), 0, LLONG_MAX);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		usage();
 | 
							usage();
 | 
				
			||||||
@@ -151,6 +153,8 @@ main(int argc, char *argv[])
 | 
				
			|||||||
		usage();
 | 
							usage();
 | 
				
			||||||
	if (!aflag && !mflag)
 | 
						if (!aflag && !mflag)
 | 
				
			||||||
		aflag = mflag = 1;
 | 
							aflag = mflag = 1;
 | 
				
			||||||
 | 
						if (!ref)
 | 
				
			||||||
 | 
							times[1] = times[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (; *argv; argc--, argv++)
 | 
						for (; *argv; argc--, argv++)
 | 
				
			||||||
		touch(*argv);
 | 
							touch(*argv);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user