/* DECIMAL COUNTER by Eugene Vassiltsov
EXAMPLE:
This page has been visited: <!--#exec cmd="count counter.dat" --> times. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
int main (int argc, char * argv[])
{
FILE * fi; char list[10];
int hits = 0, fd;
if (argc < 2)
{
printf ("<!-- Usage: count filename -->\n");
exit (0);
}
fd = open (argv[1], O_RDONLY);
flock (fd, LOCK_EX);
if ( (fi = fdopen(fd, "r")) == NULL )
{
printf ("<!-- count: Can't open \"%s\" counter data file! -->\n", argv[1]);
exit (0);
}
fscanf ( fi, "%s", list ); fclose (fi);
hits = atoi (list);
hits += 1;
fd = open (argv[1], O_WRONLY);
flock (fd, LOCK_EX);
if ( (fi = fdopen(fd, "w")) == NULL )
{
printf ("<!-- count: Can't open \"%s\" counter data file! -->\n", argv[1]);
exit (0);
}
fprintf (fi, "%d\n", hits);
fclose (fi);
switch (strlen(list))
{
case 4:
printf ("%c,%c%c%c\n", list[0],list[1],list[2],list[3]); break;
case 5:
printf ("%c,%c%c%c,%c\n", list[0],list[1],list[2],list[3],
list[4]); break;
case 6:
printf ("%c,%c%c%c,%c%c\n", list[0],list[1],list[2],list[3],
list[4],list[5]); break;
case 7:
printf ("%c,%c%c%c,%c%c%c\n", list[0],list[1],list[2],list[3],
list[4],list[5],list[6]); break;
case 8:
printf ("%c%c,%c%c%c,%c%c%c\n", list[0],list[1],list[2],list[3],
list[4],list[5],list[6],list[7]); break;
default:
printf ("%s\n", list); break;
}
exit (0);
}