/*
	File:     changetime.c
	Credit:   Designed and Programmed bu Eric Shalov <eric@freehack.com>.
	Platform: POSIX
	Created:  Dec. 1997
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>

char *program_name = NULL;
const char program_version[] = "ChangeTime: v1.0\n"
	"Designed and Programmed by Eric Shalov <eric@freehack.com>.";

/* prototypes */
int usage();

extern int optind;

/* modes */
int verbose = 0;

int main(int argc, char *argv[]) {
	char *sourcefile = NULL;
	char optch;

	struct utimbuf ut;
	struct stat st;
	
	int success = 1;

	program_name = argv[0];

	/* process program arguments */
	while( (optch = getopt(argc, argv, "vV")) != EOF ) {
		switch(optch) {
			case 'v':
				verbose = 1;
				break;
			case 'V':
				puts(program_version);
				exit(0);
				break;
			default:
				usage();
				exit(1);
		}
	}
	
	argv += optind;
	argc -= optind;
	
	if(argc<2) {
		fprintf(stderr,"Must specify source and destination files.\n");
		usage();
		exit(1);
	}
	
	sourcefile = argv[0];
	if( stat(sourcefile,&st) ) {	
		perror("changetime: unable to stat source file");
		exit(1);
	}

	/* scan through the files, setting mtime and atime with utime() call */
	for(--argc,argv++;argc--;argv++) {	
		ut.actime = st.st_atime;
		ut.modtime = st.st_mtime;
		if( utime(*argv, &ut) ) {
			fprintf(stderr,"Unable to utime `%s': ", *argv);
			perror("");
			success = 0;
		} else if(verbose)
			printf("File times duplicated: `%s' => `%s'.\n",
				sourcefile, *argv);
	}

	return !success;
}

int usage() {
	fprintf(stderr,"Usage: %s <sourcefile> [ files... ]\n"
		"\tSet files' mtime and atime to that of the sourcefile.\n",
		program_name);
	return 0;
}

