/*
        m y t o u c h . c

        VERSION        :        1.10
        EDIT-DATE      :        12-May-88

         This program is similar to the UNIX and MS-DOS Touch functions,
    except that it will also allow you to specify the date and time, as
    well as use the current system date and time.

        m o d i f i c a t i o n   h i s t o r y

VERSION        EDIT-DATE        DESCRIPTION
-------        ---------        -----------
 1.10          12-May-88       This version allows the use of wildcards.
                               It is highly recommended that you use them
                               only while in the directory containing the
                               file(s).


*/

#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <direct.h>

main(argc, argv)
int argc;
char *argv[];
{
    register int i;
    int fh, split_val, done, f_count;
    char out_line[81];
    char cur_fname[MAXPATH];
    char drive[MAXDRIVE];
    char dir[MAXDIR];
    char file[MAXFILE];
    char ext[MAXEXT];
    struct ftime f_t;
    struct time now;
    struct date today;
    struct ffblk ffblk;

    if (argc == 1) {
        printf("USAGE:\n    mytouch <filename> [hh:mm] [mm/dd/yy]\n");
        printf("You must supply the filename, Wildcards are supported\n");
        printf("at this time.");
        exit(1);
        }
    split_val = fnsplit(argv[1], drive, dir, file, ext);
    done = 0;
    f_count = 0;
    do {
        if ((split_val & WILDCARDS) == 0) {
            sprintf(cur_fname, "%s", argv[1]);
            done = 1;
            }
        else {
            if (f_count == 0) {
                done = findfirst(argv[1], &ffblk, 0);
                if (done) {
                    printf("\nNo matching files found\n\n");
                    exit(1);
                    }
                }
            else {
                done = findnext(&ffblk);
                if (done) {
                    printf("\nAll files processed.\n\n");
                    exit(0);
                    }
                }
            f_count++;
            sprintf(cur_fname, "%s", ffblk.ff_name);
            }
        if (access(cur_fname, 0x00) == -1) {
            sprintf(out_line, "Error accessing %s  ",cur_fname);
            perror(out_line);
            exit(1);
            }
        if ((fh = open(cur_fname, O_RDONLY)) == -1) {
            sprintf(out_line, "Error opening %s  ", cur_fname);
            perror(out_line);
            exit(1);
            }
        getftime(fh, &f_t);
        if (argc > 2) {
/* printf("MYTOUCH:  argc > 2  (%d)\n", argc); */
            for (i=2;i<argc;i++) {
/* printf("MYTOUCH:  i = %d\n", i);
   printf("          strlen(argv[%d] = %d  (\"%s\")\n", i, strlen(argv[i]), argv[i]); */
                if (strlen(argv[i]) == 5 || argv[i][2] == ':') {
/* printf("MYTOUCH resetting time for %s (%s)\n", cur_fname, argv[i]); */
                    argv[i][2] = 0x00;
                    argv[i][5] = 0x00;
                    f_t.ft_hour = atoi(&argv[i][0]);
                    f_t.ft_min  = atoi(&argv[i][3]);
                    f_t.ft_tsec = 0;
                    argv[i][2] = ':';
                    }
                else if (strlen(argv[i]) == 8) {
/* printf("MYTOUCH resetting date for %s (%s)\n", cur_fname, argv[i]); */
                    argv[i][2] = 0x00;
                    argv[i][5] = 0x00;
                    argv[i][8] = 0x00;
                    f_t.ft_month = atoi(&argv[i][0]);
                    f_t.ft_day = atoi(&argv[i][3]);
                    f_t.ft_year = atoi(&argv[i][6]) - 80;
                    argv[i][2] = '/';
                    argv[i][5] = '/';
                    }
                }
            }
        else {
            gettime(&now);
            f_t.ft_tsec = 0;
            f_t.ft_min = now.ti_min;
            f_t.ft_hour = now.ti_hour;
            getdate(&today);
            f_t.ft_month = today.da_mon;
            f_t.ft_day = today.da_day;
            f_t.ft_year = today.da_year - 1980;
            }
        setftime(fh, &f_t);
        printf("%s  %02d:%02d:%02d.00  %02d/%02d/%02d\n",
               cur_fname,
               f_t.ft_hour, f_t.ft_min, f_t.ft_tsec,
               f_t.ft_month, f_t.ft_day, f_t.ft_year);
        close(fh);
        } while (!done);

}

