| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | /* See LICENSE file for copyright and license details. */ | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <fcntl.h>
 | 
					
						
							| 
									
										
										
										
											2015-01-30 16:52:44 +01:00
										 |  |  | #include <limits.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-13 19:54:28 +01:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | #include <time.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <utime.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-13 18:29:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | #include "util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 11:15:18 +00:00
										 |  |  | static int aflag; | 
					
						
							|  |  |  | static int cflag; | 
					
						
							|  |  |  | static int mflag; | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | static time_t t; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 11:24:37 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | touch(const char *file) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int fd; | 
					
						
							|  |  |  | 	struct stat st; | 
					
						
							|  |  |  | 	struct utimbuf ut; | 
					
						
							|  |  |  | 	int r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((r = stat(file, &st)) < 0) { | 
					
						
							|  |  |  | 		if (errno != ENOENT) | 
					
						
							|  |  |  | 			eprintf("stat %s:", file); | 
					
						
							|  |  |  | 		if (cflag) | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 	} else if (r == 0) { | 
					
						
							|  |  |  | 		ut.actime = aflag ? t : st.st_atime; | 
					
						
							|  |  |  | 		ut.modtime = mflag ? t : st.st_mtime; | 
					
						
							|  |  |  | 		if (utime(file, &ut) < 0) | 
					
						
							|  |  |  | 			eprintf("utime %s:", file); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((fd = open(file, O_CREAT | O_EXCL, 0644)) < 0) | 
					
						
							|  |  |  | 		eprintf("open %s:", file); | 
					
						
							|  |  |  | 	close(fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	touch(file); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | usage(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-09 00:41:57 +01:00
										 |  |  | 	eprintf("usage: %s [-acm] [-r ref_file | -t timestamp] file ...\n", argv0); | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | int | 
					
						
							|  |  |  | main(int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-09 00:41:57 +01:00
										 |  |  | 	struct stat st; | 
					
						
							|  |  |  | 	char *ref; | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | 	t = time(NULL); | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ARGBEGIN { | 
					
						
							| 
									
										
										
										
											2015-01-20 11:15:18 +00:00
										 |  |  | 	case 'a': | 
					
						
							|  |  |  | 		aflag = 1; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 	case 'c': | 
					
						
							| 
									
										
										
										
											2014-11-13 21:24:47 +01:00
										 |  |  | 		cflag = 1; | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-01-20 11:15:18 +00:00
										 |  |  | 	case 'm': | 
					
						
							|  |  |  | 		mflag = 1; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2015-02-09 00:41:57 +01:00
										 |  |  | 	case 'r': | 
					
						
							|  |  |  | 		ref = EARGF(usage()); | 
					
						
							|  |  |  | 		if (stat(ref, &st) < 0) | 
					
						
							|  |  |  | 			eprintf("stat '%s':", ref); | 
					
						
							|  |  |  | 		t = st.st_mtime; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 	case 't': | 
					
						
							| 
									
										
										
										
											2015-02-08 23:45:03 +00:00
										 |  |  | 		t = estrtonum(EARGF(usage()), 0, LLONG_MAX); | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		usage(); | 
					
						
							|  |  |  | 	} ARGEND; | 
					
						
							| 
									
										
										
										
											2013-08-31 23:04:49 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (argc < 1) | 
					
						
							|  |  |  | 		usage(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-13 18:29:30 +01:00
										 |  |  | 	for (; argc > 0; argc--, argv++) | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 		touch(argv[0]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 23:46:04 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2011-05-23 02:36:34 +01:00
										 |  |  | } |