DISPLIB Reposting part 6 of 6
Gary K. Sloane
sloane at noscvax.UUCP
Sat Aug 9 10:24:24 AEST 1986
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
# part4
# This archive created: Fri Aug 8 17:14:13 1986
# By: Gary K. Sloane (Computer Sciences Corporation, San Diego)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'part4'
then
echo shar: "will not over-write existing file 'part4'"
else
cat << \SHAR_EOF > 'part4'
static cntdate(edate,sptr)
char *edate, *sptr;
{
char *tptr;
int chcnt;
/* find out how many characters in previous field */
tptr = sptr;
chcnt = 0;
while (--tptr != (edate-1))
{
if(*tptr == '/')
break;
else
chcnt++;
}
return(chcnt);
}
static chkdate(edate, date, oldate)
char *edate, *date;
int oldate;
{
char *malloc();
char *testdate = malloc(10);
char *emonth = malloc(3);
char *eday = malloc(3);
char *eyear = malloc(3);
int i, eimonth, eiday;
/* make a copy of edate to play around with */
(void)strcpy(testdate,edate);
/* replace each '/' with ':' in testdate */
for(i = 0; i < strlen(testdate); i++)
if (*(edate + i) == '/')
*(testdate + i) = ':';
/* add a ":" on testdate for extract */
(void)strcat(testdate,":");
/* get month, day, and year into fields */
extract(emonth,testdate,1,':');
extract(eday,testdate,2,':');
extract(eyear,testdate,3,':');
/* make month, day, and year integers */
eimonth = stoi(emonth);
eiday = stoi(eday);
/* check for valid month entered */
if ( (eimonth > 12) || eimonth == 0)
return(2);
/* check for valid day entered */
if (eiday == 0)
return(3);
/* is the day valid for the entered month? */
switch(eimonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (eiday > 31)
return(3);
else
break;
case 4:
case 6:
case 9:
case 11:
if (eiday > 30)
return(3);
else
break;
case 2:
if (eiday > 29)
return(3);
else
break;
}
/* make sure that the entered date is after today's date */
if((oldate!= 0) && (strcmp(date2ymd(edate),date2ymd(date)) <= 0))
return(4);
/* it's a good date */
return(1);
}
/********************************************/
/* convert a MM/DD/YY date to a YYMMDD date */
/********************************************/
char *date2ymd(date)
char *date;
{
char tdate[10], mo[3], dy[3], yr[3];
static char ymddate[7];
/* extract the yr, mo, and dy from the date */
sprintf(tdate, "%s/", date);
extract(yr, tdate, 3, '/');
extract(mo, tdate, 1, '/');
extract(dy, tdate, 2, '/');
/* build the YYMMDD date */
if(strlen(yr) == 2)
(void)strcpy(ymddate, yr);
else
sprintf(ymddate, "0%s", yr);
if(strlen(mo) == 1)
(void)strcat(ymddate, "0");
(void)strcat(ymddate, mo);
if(strlen(dy) == 1)
(void)strcat(ymddate, "0");
(void)strcat(ymddate, dy);
/* return the YYMMDD date */
return(ymddate);
}
/***********************************************************************/
/* Given a String With delimiter Separated Fields, and Given the */
/* Position of the Field You Want, Extract that Field */
/***********************************************************************/
extract(fieldnam,bufr,fieldnum, delim)
char delim, *bufr, *fieldnam;
int fieldnum;
{
int fieldcnt = 1;
char *tptr, *fptr;
tptr = bufr;
/* move pointer to first address of desired field */
while (fieldcnt < fieldnum)
{
if( (*tptr == '\0') || (*tptr == delim) )
++fieldcnt;
++tptr;
}
/* put a null at end of desired field and return field */
fptr = fieldnam;
for(;;)
{
if( (*tptr == '\0') || (*tptr == delim) )
break;
*fptr = *tptr;
++fptr;
++tptr;
}
*fptr = '\0';
}
/*************************************************/
/* reload the program on an abort (DEL entered) */
/* called by edits and editdate */
/*************************************************/
/*VARARGS1*/
reload(prog, a1, a2, a3, a4, a5)
char *prog;
{
int s;
/* close all descriptors except stdin, stdout, stderr */
for(s = 3; s < NOFILE; s++)
(void) close(s);
/* overlay the caller with a 'new' program */
execl(prog, prog, a1, a2, a3, a4, a5);
/* if the execl fails... */
perror("execl failed:");
exit(-1);
}
/*****************************************************************/
/* This function returns a positive integer from a string */
/* If the string contains invalid character(s) it returns a -1. */
/* If the string converts to a negative number it returns a -2. */
/*****************************************************************/
int stoi(string)
char *string;
{
register int index;
int exp, numchars, newint, placeint[64]; /* declare the integers */
int neg = 0; /* 1 if number is negative */
/*count number of characters and convert each to integer value*/
for(index=0, numchars=0; *(string+index)!='\0'; ++index,++numchars)
{
/* it must be a numeric */
if (*(string+index) >= '0' && *(string+index) <= '9')
placeint[numchars] = *(string+index) - '0';
else if ( (index == 0) && (*(string+index) == '-') )
neg = 1;
else
return(-1);
}
/* return -2 if number is negative */
if (neg == 1)
return(-2);
/* make sure there's some characters to convert */
if(numchars==0)return(-1);
/* convert integers in placeint to an integer by multiplying them */
/* by their corresponding place values */
newint = 0;
for(exp = 0,index=numchars-1; index > -1;exp++,index--)
newint += power(10, exp) * placeint[index];
return(newint);
}
/***********************/
/* raise x to the n-th */
/***********************/
int power(x,n)
int x, n;
{
int p;
for(p=1; n>0; --n)
p *= x;
return(p);
}
SHAR_EOF
fi
exit 0
# End of shell archive
More information about the Comp.sources.unix
mailing list