[comp.sources.atari.st] v03i012: cal -- UNIXoid calendar program

koreth@panarthea.ebay.sun.com (Steven Grimm) (11/26/89)

Submitted-by: mlake@irscscm.ucb.com (Marshall Lake)
Posting-number: Volume 3, Issue 12
Archive-name: cal

The enclosed source code is a UNIX-like calendar program.
It was developed using MWC.  The documentation is self-contained.

-------CUT-------
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".
# Contents:  ./cal.c
if test -f './cal.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'./cal.c'\"
else
echo shar: Extracting \"'./cal.c'\" \(12751 characters\)
sed "s/^X//" >'./cal.c' <<'END_OF_FILE'
/*
    cal:  a UNIX-like calendar program
          format:  cal [-nihf filename]
                   where n = the number of days to look ahead including
                             today, max 99, default 2
                         filename = file to look for instead of cal.txt

                    i  return information regarding CAL
                    h  return help with regard to CAL
                    f  use the following specified file as input to CAL
                       instead of cal.txt

    RETURNS:
    0 - CAL terminated successfully
    1 - bad calling format
    2 - "count of days" greater than 99 or longer than 2 positions
    3 - "count of days" = 0
    4 - cal.txt file not found
    5 - record in cal.txt greater than 1000 characters
    6 - information only requested (i and/or h options)

    Marshall Lake, TEAM Software, 4/26/88
    POB 7332
    Washington, DC  20044
    Tel 703-533-2132
    Fax 703-538-4598
    GEnie:  MLAKE
    CIS:  73717,3174
    uucp:   ...{media,teemc,tcsc3b2,ki4pv}!ka3ovk!irscscm!mlake
*/

#include <time.h>
#include <stdio.h>
#include <osbind.h>
#include <ctype.h>

long elapsed_time;
char today[26],
     filename[100] = "cal.txt",
     blanks[14] = "             ",
     alpha_months[37] = "JanFebMarAprMayJunJulAugSepOctNovDec",
     full_months[109] = "january  february march    april    may      june\
     july     august   septemberoctober  november december ";
int  orig_day, day,
     orig_month, month;

main (argc, argv)
int argc;
char *argv[];
{
    int  x,
         y,
         l,
         set_i, set_f, set_h, set_n,
         count_of_days,
         loop;
    char cal_rec[1001],
         mat1[5],            /* mm/dd */
         mat11[4],           /* m/dd */
         mat12[4],           /* mm/d */
         mat13[3],           /* m/d */
         mat2[5],            /* mm-dd */
         mat21[4],           /* m-dd */
         mat22[4],           /* mm-d */
         mat23[3],           /* m-d */
         mat3[6],            /* aaa dd */
         mat31[5],           /* aaa d */
         mat4[6],            /* dd aaa */
         mat41[5],           /* d aaa */
         mat5[13],           /* aaaaaaaaa dd */
         mat51[12],          /* aaaaaaaaa d */
         mat6[13],           /* dd aaaaaaaaa */
         mat61[12];          /* d aaaaaaaaa */
    FILE *stream;

    if (argc > 3)
        bad_format ();

    set_i = set_h = set_f = set_n = 0;
    if (argc > 1) {
        if (strlen (argv[1]) > 6 || argv[1][0] != '-')
            bad_format ();
        for (x = 1; x < strlen (argv[1]); x++)
            if (argv[1][x] >= '0' && argv[1][x] <= '9') {
                if (set_n == 1)
                    bad_format ();
                set_n = 1;
                if (argv[1][x + 1] >= '0' && argv[1][x + 1] <= '9') {
                    if (argv[1][x + 2] >= '0' && argv[1][x + 2] <= '9') {
                        fprintf (stderr, " max count 2 digits\n\n");
                        exit (2);
                    }
                    count_of_days = (argv[1][x] - '0') * 10 +
                                     (argv[1][x + 1] - '0');
                    x++;
                }
                else
                    count_of_days = argv[1][x] - '0';
            }
            else
                if (argv[1][x] == 'f') {
                    if (set_f == 1 || argc != 3)
                        bad_format ();
                    set_f = 1;
                }
                else
                    if (argv[1][x] == 'i') {
                        if (set_i == 1)
                            bad_format ();
                        set_i = 1;
                    }
                    else
                        if (argv[1][x] == 'h') {
                            if (set_h == 1)
                                bad_format ();
                            set_h = 1;
                        }
                        else
                            bad_format ();
    }
    if (set_n == 0)
        count_of_days = 2;
    if (set_f == 1)
        strcpy (&filename[0], argv[2]);
    if (set_i == 1) {
        printf ("\n\n CAL: a UNIX-like calendar program\n");
        printf (" written by M. Lake\n");
        printf (" v1.3  7/28/88\n\n");
    }
    if (set_h == 1)
        print_info ();

    if (count_of_days == 0)
        exit (3);
    if (set_i == 1 || set_h == 1 && set_n == 0 && set_f == 0)
        exit (6);

    time (&elapsed_time);
    strncpy (&today[0], ctime (&elapsed_time), 26);
    orig_day = (today[8] - '0') * 10 + (today[9] - '0');
    for (x = 0; x < 34; x += 3)
        if (strncmp (&today[4], &alpha_months[x], 3) == 0)
            orig_month = x / 3 + 1;

    if ((stream = fopen (filename, "rb")) == NULL) {
        fprintf (stderr,
                 "%s file does not exist where it is expected!\n\n", filename);
        exit (4);
    }

    mat1[2] = mat11[1] = mat12[2] = mat13[1] = '/';
    mat2[2] = mat21[1] = mat22[2] = mat23[1] = '-';
    mat3[3] = mat4[2] = mat31[3] = mat41[1] = ' ';

    cal_rec[1000] = '\0';

    while (1) {
next_record:
        month = orig_month;
        day = orig_day;
        if ((fgets (&cal_rec[0], 1001, stream)) == 0) {
            fclose (stream);
            exit (0);
        }
        if (strlen (&cal_rec[0]) == 1000) {
            fprintf (stderr, "\n\nA record in %s is too big!!\n", filename);
            fprintf (stderr, "1000 characters is the largest allowed.\n");
            fclose (stream);
            exit (5);
        }

        l = strlen (&cal_rec[0]) - 1;
        for (loop = 0; loop < count_of_days; loop++) {
            strncpy (&mat5[0], &blanks[0], 13);
            strncpy (&mat6[0], &blanks[0], 13);
            strncpy (&mat51[0], &blanks[0], 12);
            strncpy (&mat61[0], &blanks[0], 12);
            mat1[0] = mat2[0] = mat12[0] = mat22[0] = month / 10 + '0';
            mat1[1] = mat2[1] = mat11[0] = mat21[0] = mat12[1] =
              mat22[1] = mat13[0] = mat23[0] = month % 10 + '0';
            mat1[3] = mat2[3] = mat3[4] = mat4[0] = mat6[0] = mat11[2] =
              mat21[2] = day / 10 + '0';
            mat1[4] = mat2[4] = mat3[5] = mat4[1] = mat6[1] = mat11[3] =
              mat21[3] = mat12[3] = mat22[3] = mat13[2] = mat23[2] =
              mat31[4] = mat41[0] = mat61[0] = day % 10 + '0';
            for (x = (month - 1) * 3, y = 0; y < 3; x++, y++)
                mat3[y] = mat4[y + 3] = mat31[y] = mat41[y + 2] =
                  alpha_months[x];
            for (x = (month - 1) * 9, y = 0; y < 9; x++, y++)
                mat5[y] = mat6[y + 3] = mat51[y] = mat61[y + 2] =
                   full_months[x];
            for (x = 3; x < 12; x++)
                if (mat6[x] == ' ') {
                    mat6[x] = mat5[x] = mat61[x - 1] = mat51[x - 1] = '\0';
                    break;
                }
            mat5[x - 2] = day / 10 + '0';
            mat5[x - 1] = mat51[x - 2] = day % 10 + '0';

            for (x = 0; (x + 2) < l; x++) {
                if (strncmp (&cal_rec[x], &mat1[0], 5) == 0 ||
                    strncmp (&cal_rec[x], &mat2[0], 5) == 0 ||
                    strnicmp (&cal_rec[x], &mat3[0], 6) == 0 ||
                    strnicmp (&cal_rec[x], &mat5[0], strlen (&mat5[0])) == 0 ||
                    strnicmp (&cal_rec[x], &mat6[0], strlen (&mat6[0])) == 0 ||
                    strnicmp (&cal_rec[x], &mat4[0], 6) == 0) {
                    printf ("%s", cal_rec);
                    goto next_record;
                }
                if (month < 10 && day < 10)
                    if ((strncmp (&cal_rec[x], &mat13[0], 3) == 0 ||
                         strncmp (&cal_rec[x], &mat23[0], 3) == 0) &&
                        (cal_rec[x - 1] < '0' || cal_rec[x - 1] > '9') &&
                        (cal_rec[x + 3] < '0' || cal_rec[x + 3] > '9')) {
                        printf ("%s", cal_rec);
                        goto next_record;
                    }
                if (month < 10)
                    if ((strncmp (&cal_rec[x], &mat11[0], 4) == 0 ||
                         strncmp (&cal_rec[x], &mat21[0], 4) == 0) &&
                        (cal_rec[x - 1] < '0' || cal_rec[x - 1] > '9')) {
                        printf ("%s", cal_rec);
                        goto next_record;
                    }
                if (day < 10) {
                    if (((strncmp (&cal_rec[x], &mat12[0], 4) == 0 ||
                          strncmp (&cal_rec[x], &mat22[0], 4) == 0) &&
                          (cal_rec[x + 4] < '0' || cal_rec[x + 4] > '9')) ||
                          (strnicmp (&cal_rec[x], &mat31[0], 5) == 0 &&
                          (cal_rec[x + 5] < '0' || cal_rec[x + 5] > '9')) ||
                          (strnicmp (&cal_rec[x], &mat41[0], 5) == 0 &&
                          (cal_rec[x - 1] < '0' || cal_rec[x - 1] > '9'))) {
                        printf ("%s", cal_rec);
                        goto next_record;
                     }
                     if ((strnicmp (&cal_rec[x], &mat51[0],
                             strlen (&mat51[0])) == 0 &&
                          (cal_rec[x + strlen (&mat51[0])] < '0' ||
                             cal_rec[x + strlen (&mat51[0])] > '9')) ||
                          (strnicmp (&cal_rec[x], &mat61[0],
                             strlen (&mat61[0])) == 0 &&
                          (cal_rec[x - 1] < '0' || cal_rec[x - 1] > '9'))) {
                        printf ("%s", cal_rec);
                        goto next_record;
                    }
                }
            }
            add_a_day ();
        }
    }
}

add_a_day () {
    day++;
    if (day == 29 && month == 2) {
        day = 1;
        month++;
    }
    if (day == 31 && (month == 9 || month == 4 ||
                      month == 6 || month == 11)) {
        day = 1;
        month++;
    }
    if (day == 32)
        if (month == 12)
            day = month = 1;
        else {
            day = 1;
            month++;
        }
}

bad_format () {
    fprintf (stderr, "\n format: 'cal [-nihf filename]'\n");
    fprintf (stderr, " where n = count of days to check\n");
    fprintf (stderr, "\n enter 'cal -h' and press <RETURN> for help\n");
    exit (1);
}

/* check for a match with no regard to case */

strnicmp (a, b, length)
char *a, *b;
int length;
{
    char a_fld[100], b_fld[100];
    int x;

    strncpy (&a_fld[0], a, length);
    strncpy (&b_fld[0], b, length);

    for (x = 0; x < length; x++) {
        if (isupper (a_fld[x]))
            a_fld[x] = tolower (a_fld[x]);
        if (isupper (b_fld[x]))
            b_fld[x] = tolower (b_fld[x]);
    }

    return (strncmp (&a_fld[0], &b_fld[0], length));
}

print_info () {
    printf ("\n\ncal:  a UNIX-like calendar program\n\n");
    printf ("format:  cal [-nihf filename]\n");
    printf ("  where n = the number of days to look ahead including\n");
    printf ("        today, max 99, default 2\n");
    printf ("    filename = file to look for instead of cal.txt\n\n");
    printf ("   i  return information regarding CAL\n");
    printf ("   h  return help with regard to CAL\n");
    printf ("   f  use the following specified file as input to CAL\n");
    printf ("      instead of cal.txt\n\n");
    printf ("CAL will return records from a 'cal.txt' file residing in\n");
    printf ("the same directory as where 'cal.prg' is called from.\n");
    printf ("CAL will seek out and return records containing dates that\n");
    printf ("are being requested.  A record is a string of characters\n");
    printf ("ending with a carriage return.\n\n");
    printf (" ** PRESS ANY KEY **\n");
    Crawcin ();
    printf ("\n\nThe format of the date may be any of the following:\n");
    printf ("mm/dd, m/dd, mm/d, m/d\n");
    printf ("mm-dd, m-dd, mm-d, m-d\n");
    printf ("aaa dd, aaa d\n");
    printf ("dd aaa, d aaa\n");
    printf ("aaaaaaaaa dd, aaaaaaaaa d\n");
    printf ("dd aaaaaaaaa, d aaaaaaaaa\n\n");
    printf ("where mm or m = numeric month (1 - 12), dd or d = numeric\n");
    printf ("day (1 - 31), aaa = the first three characters of the\n");
    printf ("alpha month (jan - dec), aaaaaaaaa = the entire alpha\n");
    printf ("month (january - december) and is variable length.\n\n");
    printf ("CAL is NOT case sensitive with regard to alpha months.\n");
    printf ("'cal.txt' may be any size up to the physical limitations\n");
    printf ("of your machine.\n");
    printf ("The largest record allowed is 1000 characters.\n\n");
    printf (" ** PRESS ANY KEY **\n");
    Crawcin ();
    printf ("\n\nRESERVATIONS:\n");
    printf ("CAL will not recognize years.\n");
    printf ("The internal date in your machine must be correct to\n");
    printf ("enable CAL to return the correct information.\n\n");
}
END_OF_FILE
if test 12751 -ne `wc -c <'./cal.c'`; then
    echo shar: \"'./cal.c'\" unpacked with wrong size!
fi
# end of './cal.c'
fi