COUNTDN.C ·
C ·
3.3 KB ·
1990-03-15 ·
from PersonalComputing_May-1990
/*************************************
C O U N T D O W N
by Ian C. Sharpe
(c) Personal Computing 1990
Use: COUNTDN c r
c=counter number 0-99
r=value to reset when run down 0-255
If countdown=0 then ERRORLEVEL=1
else ERRORLEVEL=0
ERRORLEVEL=-1 if error occurs
Compiled with Zortech C v3:
ztc countdn -a -w -msi -o
Refer to magazine text for other compilers
Creates hidden data file \COUNTDAT.###
*************************************/
#define MAXCOUNTERS 100
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dos.h>
int checkisnum(char *);
void hidefile(char *);
main(int argc, char *argv[])
{
int counter,resetval,i,c,blastoff;
char counters[MAXCOUNTERS];
FILE *fp;
puts("\nCOUNTDOWN (c) March 1990 Ian Sharpe\n");
/*
Obtain parameters & check their validity
*/
if(argc != 3)
{
puts("Wrong parameter count");
puts("Use COUNTDN [counter] [reset val]");
exit(-1);
}
if( checkisnum(argv[1])==0 || checkisnum(argv[2])==0 )
{
printf("%s %s is not a valid command line\n",argv[1], argv[2]);
exit(-1);
}
if( (counter=atoi(argv[1])) >= MAXCOUNTERS || counter < 0 )
{
printf("Counter number must be 0-%d. You put %d\n",MAXCOUNTERS-1, counter);
exit(-1);
}
if( (resetval=atoi(argv[2])) > 255 || resetval<0 )
{
printf("Reset values must be 0-255. You put %d\n",resetval);
exit(-1);
}
/*
Try to find data file.
If not found, create it if the user permits
*/
if( ! findfirst("\\COUNTDAT.###", 0x02 ) )
{
puts("Can't find count down data");
puts("file \\COUNTDAT.### in root");
puts("directory of default drive");
puts("\nCreate it? (Y/N)");
for(c=0; c != 'Y' && c != 'N'; )
c=toupper(getch());
if( c == 'N' )
{
puts("Aborting");
exit(-1);
}
fp=fopen("\\COUNTDAT.###", "wb");
memset(counters, 0, MAXCOUNTERS);
if( ! fwrite(counters, MAXCOUNTERS, 1, fp) )
{
puts("Error creating \\COUNTDAT.###\nAborting");
fclose(fp);
unlink("\\COUNTDAT.###");
exit(-1);
}
fclose(fp);
hidefile("\\COUNTDAT.###");
}
/*
Open data file, decrement counter, rewrite data
*/
fp=fopen("\\COUNTDAT.###","r+");
if( ! fread(counters, MAXCOUNTERS, 1, fp) )
{
puts("Can't read from \\COUNTDAT.###\nAborting");
fclose(fp);
exit(-1);
}
if( counters[counter] == 0 )
{
putchar(7);
counters[counter]=(char)resetval;
blastoff=1;
}
else
{
counters[counter]--;
blastoff=0;
}
rewind(fp);
fwrite(counters, MAXCOUNTERS, 1, fp);
fclose(fp);
exit(blastoff);
}
int checkisnum( char *string )
/*
Checks if every character in *string is a digit 0...9
If not, return 0. If OK, return 1
*/
{
while( *string )
if( ! isdigit(*string++) )
return 0;
return 1;
}
void hidefile(char *filename)
/*
Hide file
*/
{
union REGS regs;
regs.h.ah=0x43;
regs.h.al=1;
regs.x.cx=2; /* 2=hidden attribute */
regs.x.dx=(int)filename;
intdos(®s, ®s);
}