DataMuseum.dk

Presents historical artifacts from the history of:

Commodore CBM-900

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about Commodore CBM-900

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download

⟦d8e3639d7⟧ TextFile

    Length: 3859 (0xf13)
    Types: TextFile
    Notes: UNIX file
    Names: »date.c«

Derivation

└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
    └─⟦2d53db1df⟧ UNIX V7 Filesystem
        └─ ⟦this⟧ »frankh/src/junk/date.c« 
└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code
    └─⟦f4b8d8c84⟧ UNIX V7 Filesystem
        └─ ⟦this⟧ »cmd/date.c« 

TextFile

/*
 * Print and set the date.
 */

#include <stdio.h>
#include <ctype.h>
#include <types.h>
#include <time.h>
#include <timeb.h>
#include <utmp.h>
#include <stat.h>

#define TFCFLAG	0		/* Truly French GMT is UTC */
int	gmtflag;		/* Do things in Greenwich Mean Time */

struct	tm	*gtime();

struct	timeb	tb;
struct	utmp	utmps[2];
char	utmpf[] = "/usr/adm/wtmp";
struct	stat	sbuf;
time_t	timep[2];
char	btimf[] = "/etc/boottime";
#define	MIN	(60L)
#define	HOUR	(60L*60L)
#define	DAY	(60L*60L*24L)
#define	APR	3
#define	OCT	9

/*
 * Table of days per month.
 * This will be adjusted for leap years.
 */
char	dpm[] = {
	31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

main(argc, argv)
char *argv[];
{
	register char *s;
	register struct tm *tmp;
	long ltime;
	char *tzone;

	if (argc>1 && *argv[1]=='-') {
		if (argv[1][1]=='u' && argv[1][2]=='\0')
			gmtflag = 1;
		else
			usage();
		argc--;
		argv++;
	}
	if (argc > 2)
		usage();
	if (argc > 1)
		sdate(argv[1]);
	ftime(&tb);
	ltime = tb.time;
	s = asctime(tmp = gtime(&ltime));
	s[24] = '\0';
	if (!gmtflag)
		tzone = tzname[tmp->tm_isdst];
	else
		tzone = TFCFLAG ? "UTC" : "GMT";
	printf("%s %s\n", s, tzone);
}

/*
 * Set the date/time based on
 * string `s'.
 */
sdate(s)
char *s;
{
	register char *sp;
	register int year;
	register int month;
	int status;
	int ufd;
	long ltime;
	struct tm *tmp;

	ftime(&tb);
	ltime = tb.time;
	utmps[0].ut_time = ltime;
	tmp = gtime(&ltime);
	for (sp = s; *sp != '\0'; sp++)
		;
	if ((sp -= 2) < s)
		usage();
	if (sp>s && sp[-1]=='.') {
		tmp->tm_sec = convert(sp, 60, s);
		sp--;
	} else {
		sp += 2;
		tmp->tm_sec = 0;
	}
	tmp->tm_min = convert(sp-=2, 60, s);
	tmp->tm_hour = convert(sp-=2, 24, s);
	if ((sp-=2) >= s) {
		tmp->tm_mday = convert(sp, 31+1, s);
		if ((sp-=2) >= s) {
			tmp->tm_mon = convert(sp, 12+1, s) - 1;
			if ((sp-=2) >= s)
				tmp->tm_year = convert(sp, 100, s);
		}
	}
	if (tmp->tm_mday > dpm[tmp->tm_mon])
		usage();
	/*
	 * Convert using the year, month, day, hour, minute,
	 * and second values in the `tm' structure.
	 * When all is completed, take care of timezone.
	 */
	ltime = tmp->tm_sec + MIN*tmp->tm_min + HOUR*tmp->tm_hour;
	ltime += (tmp->tm_mday-1) * DAY;
	year = 1900 + tmp->tm_year;
	/*
	 * Adjust length of February in leap years.
	 */
	if (!isleap(year))
		dpm[1] = 28;
	month = tmp->tm_mon;
	while (--month >= 0)
		ltime += dpm[month] * DAY;
	while (--year >= 1970)
		ltime += (long)DAY * (isleap(year) ? 366 : 365);
	if (!gmtflag) {
		ltime += timezone;
		tmp = localtime(&ltime);
		if (tmp->tm_isdst)
			ltime -= HOUR;
	}
	status = stime(&ltime);
	if (status < 0) {
		error("No permission\n");
		return;
	}

	/* Correct the modtime of /etc/boottime */
	stat(btimf, &sbuf);
	timep[0] = ltime - utmps[0].ut_time + sbuf.st_mtime;
	timep[1] = timep[0];
	utime(btimf, timep);

	/* Note the change of time in /usr/adm/wtmp */
	utmps[1].ut_time = ltime;
	utmps[0].ut_line[0] = '|';
	utmps[1].ut_line[0] = '}';
	if ((ufd = open(utmpf, 1)) >= 0) {
		lseek(ufd, 0L, 2);
		write(ufd, utmps, sizeof (utmps));
		close(ufd);
	}
}

/*
 * Return 1 on leap years.
 */
isleap(yr)
register yr;
{
	if (yr%4000 == 0)
		return (0);
	if (yr%400==0 || (yr%100!=0 && yr%4==0))
		return (1);
	return (0);
}

/*
 * Convert a piece of the time.
 * The pointer cp should never fall before
 * beg and the two digits parsed should be
 * less than max.
 */
convert(cp, max, beg)
register char *cp;
char *beg;
{
	register val;

	if (cp<beg || !isdigit(cp[0]) || !isdigit(cp[1]))
		usage();
	val = (cp[0]-'0')*10 + cp[1]-'0';
	if (val >= max)
		usage();
	return (val);
}

/*
 * Return either local time or Greenwich
 * Mean `tm' in a `tm' structure.
 */
struct tm *
gtime(tp)
register time_t *tp;
{
	return (gmtflag ? gmtime(tp) : localtime(tp));
}

usage()
{
	error("Usage: date [-u] [yymmddhhmm[.ss]]\n");
}

error(x)
{
	printf("%r", &x);
	exit(1);
}