aboutsummaryrefslogtreecommitdiffstats
path: root/alarm.c
blob: a0bfa356edbdb4ad0da7822ec88ba3c1082ced0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <signal.h>
#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>
#include <time.h>

volatile unsigned int variable = 0;
volatile unsigned int print_variable = 0;

void alarm_handler(int signum)
{
    alarm(60);
}

int main()
{        
    signal(SIGALRM, alarm_handler);
    alarm(1);

    for (;;)
    {
        select(0, NULL, NULL, NULL, NULL);
 time_t t = time(NULL);
 struct tm tm = *localtime(&t);
 printf("%d-%d-%d %d:%d:%d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);

    }
}
ht .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "os.h"
#include "vt.h"
#include "fdset.h"
#include "vbi.h"
#include "lang.h"
#include "misc.h"

char *fmt = "%a %b %d %H:%M:%S %Z %Y";
int max_diff = 2*60*60; // default: 2 hours
int set_time = 0;
char *outfile = "";
static char *channel;
u_int16_t sid;


static void chk_time(int t)
{
    struct tm *tm;
    time_t sys_t;
    int dt;
    char buf[256];

    if (t < 0 || t > 235959 || t%100 > 59 || t/100%100 > 59)
    return;

    sys_t = time(0);
    tm = localtime(&sys_t);

    // Now convert to UTC seconds
    t = t/100/100 * 60*60 + t/100%100 * 60 + t%100;
#ifdef BSD
    t -= tm->tm_gmtoff; // dst already included...
#else
    t += timezone;
    if (tm->tm_isdst)
	t -= 60*60;
#endif

    dt = t - sys_t % (24*60*60);
    if (dt <= -12*60*60)
	dt += 24*60*60;

    if (dt <= -max_diff || dt >= max_diff)
	fatal("time diff too big (%2d:%02d:%02d)", dt/60/60, abs(dt)/60%60, abs(dt)%60);

    sys_t += dt;

    if (set_time)
    {
	struct timeval tv[1];

	tv->tv_sec = sys_t;
	tv->tv_usec = 500000;
	if (settimeofday(tv, 0) == -1)
	    ioerror("settimeofday");
    }
    if (*fmt)
    {
	tm = localtime(&sys_t);
	if (strftime(buf, sizeof(buf), fmt, tm))
	    puts(buf);
    }
    exit(0);
}


static void event(void *_, struct vt_event *ev)
{
    switch (ev->type)
    {
	/* vbi may generate EV_PAGE, EV_HEADER, EV_XPACKET */
	/* for event arguments see vt.h */

	case EV_HEADER: // a new title line (for running headers)
	{
	    static int last_t = -1;
	    u8 *s = ev->p1;
	    int i, t = 1;

	    if (ev->i2 & PG_OUTOFSEQ)
		break;

	    for (i = 32; i < 40; ++i)
		if (s[i] >= '0' && s[i] <= '9')
		    t = t * 10+ s[i] - '0';
	    if (t >= 1000000 && t <= 1235959)
		if (t == last_t || t - last_t == 1)
		    chk_time(t - 1000000);
	    last_t = t;
	    break;
	}
    }
}


static void usage(FILE *fp, int exit_val)
{
    fprintf(fp, "usage: %s [options]\n", prgname);
    fprintf(fp,
	    "\n"
	    "  Valid options:\t\tDefault:\n"
	    "    -d -delta <max_secs>\t7200 (2 hours)\n"
	    "    -f -format <fmtstr>\t\t%%c\n"
	    "    -h -help\n"
	    "    -s -set\t\t\toff\n"
	    "    -to -timeout <seconds>\t(none)\n"
	    "    -v -vbi <vbidev>\t\t/dev/vbi\n"
		"                 \t\t/dev/vbi0\n"
		"                 \t\t/dev/video0\n"
		"                 \t\t/dev/dvb/adapter0/demux0\n"
	    );
    exit(exit_val);
}


static int option(int argc, char **argv, int *ind, char **arg)
{
    static struct { char *nam, *altnam; int arg; } opts[] = {
	{ "-delta", "-d", 1 },
	{ "-format", "-f", 1 },
	{ "-help", "-h", 0 },
	{ "-set", "-s", 0 },
	{ "-timeout", "-to", 1 },
	{ "-vbi", "-v", 1 },
    };
    int i;

    if (*ind >= argc)
	return 0;

    *arg = argv[(*ind)++];
    for (i = 0; i < NELEM(opts); ++i)
	if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
	{
	    if (opts[i].arg)
		if (*ind < argc)
		    *arg = argv[(*ind)++];
		else
		    fatal("option %s requires an argument", *arg);
	    return i+1;
	}

    if (**arg == '-')
    {
	fatal("%s: invalid option", *arg);
	usage(stderr, 1);
    }

    return -1;
}


int main(int argc, char **argv)
{
    char *vbi_name = NULL;
    int timeout = 0;
    struct vbi *vbi;
    int opt, ind;
    char *arg;
    int ttpid = -1;

    setprgname(argv[0]);
    ind = 1;
    while (opt = option(argc, argv, &ind, &arg))
	switch (opt)
	{
	    case 1: // -delta
		max_diff = atoi(arg);
		if (max_diff < 1)
		    fatal("-delta: illegal value '%s'", arg);
		if (max_diff > 12*60*60)
		{
		    max_diff = 12*60*60;
		    error("-delta: %d too big. Assuming %d", arg, max_diff);
		}
		break;
	    case 2: // -format
		fmt = arg;
		break;
	    case 3: // help
		usage(stdout, 0);
		break;
	    case 4: // -set
		set_time = 1;
		break;
	    case 5: // -timeout
		timeout = atoi(arg);
		if (timeout < 1 || timeout > 60*60)
		    fatal("-timeout: illegal value '%s'", arg);
		break;
	    case 6: // -vbi
		vbi_name = arg;
		break;
	    case -1:
		usage(stderr, 1);
		break;
	}

    fdset_init(fds);

    if (timeout)
    {
	signal(SIGALRM, SIG_DFL); // kill me
	alarm(timeout);
    }
    vbi = vbi_open(vbi_name, 0, channel, outfile, sid, ttpid); // open device
    if (not vbi)
	fatal_ioerror(vbi_name);
    vbi_add_handler(vbi, event, 0); // register event handler

    for (;;)
	fdset_select(fds, -1); // call scheduler

    /* never reached */
    vbi_del_handler(vbi, event, 0);
    vbi_close(vbi);
    exit(0);
}