home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / f / frbts_20.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1991-04-28  |  9KB  |  356 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Misc.c      Version 2.0    By Craig Derouen                           */
  4. /*                Misc processsing module for Frobot.c                      */
  5. /*                                                                          */
  6. /*                                                                          */
  7. /****************************************************************************/
  8.  
  9. #include "frobot.h"
  10.  
  11. #include <time.h>
  12. #include "globals.h"
  13.  
  14. static char *dates[] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
  15.                        };
  16.  
  17. void showparms(void)
  18. {
  19.    showvers();
  20.    if (!ShowActivity)
  21.       return;
  22.    printf("-------------------------------------------\n");
  23.    printf("\tOrigin Node:                  %d:%d/%d\n",Zone,Net,Node);
  24.    printf("\tOutbound directory            %s\n",Outbdir);
  25.    printf("\tInbound directory             %s\n",Inbdir);
  26.    printf("\tNetMail directory             %s\n",Netmdir);
  27.    printf("\tScript control file:          %s\n",ScriptFile);
  28.    if (LogFile[0])
  29.       printf("\tLog File:                     %s\n",LogFile);
  30.    printf("\tActivity displayed:           ");
  31.       if (ShowActivity)
  32.          printf("Yes\n");
  33.       else printf("No\n");
  34.    printf("\tSend notify mail to host:     ");
  35.       if (SysopMail)
  36.          printf("Yes\n");
  37.       else printf("No\n");
  38.    printf("\tSend notify mail to yourself: ");
  39.       if (SysopMail)
  40.          printf("Yes\n");
  41.       else printf("No\n");
  42.    printf("\tBells and whistles:           ");
  43.       if (!Silent)
  44.          printf("Yes\n");
  45.       else printf("No\n");
  46.    printf("-------------------------------------------\n");
  47. }
  48.  
  49.  
  50. /* Parse out string containing Zone:Net/Node. Return values in ptrs,
  51.    return TRUE if error */
  52. int parsenet(char *str,unsigned int *zone,unsigned int *net,unsigned int *node)
  53. {
  54.    char *p1;
  55.    char tt[20];
  56.  
  57. /* This gets tricky, a Zone is optional, so.... */
  58.  
  59.    p1 = strchr(str,':');        /* Check for Zone sep */
  60.    if (p1 != NULL) {
  61.       strcpy(tt,str);
  62.       *zone = atoi(strtok(tt,":"));
  63.       str = strchr(str,':') +1;        /* bump past Zone sep */
  64.    }
  65.    else {
  66.       *zone = 1;
  67.       p1 = str;
  68.    }
  69.    p1 = strtok(str,"/");        /* Net/Node sep */
  70.    if (p1 != NULL) {
  71.       *net = atoi(p1);
  72.       *node = atoi(strtok(NULL," \t\n\r;"));
  73.       if (*net + *node)
  74.          return(FALSE);
  75.    }
  76.    return(TRUE);
  77. }
  78.  
  79. void aborterror(int code,char *str)
  80. {
  81.    switch (code) {
  82.  
  83.       case NOMEM:
  84.          if (str == NULL)
  85.             strcpy(temp,"Out of memory");
  86.          else strcpy(temp,str);
  87.  
  88.       case BADFILE:
  89.          if (str == NULL)
  90.             strcpy(temp,"File error");
  91.          else strcpy(temp,str);
  92.  
  93.    }
  94.    warble();
  95.    strcat(temp,"\n");
  96.    printf(temp);
  97.    logit(temp,'!');
  98.    exit(1);
  99. }
  100.  
  101. void warble()
  102. {
  103.    int x;
  104.  
  105.    for (x=0; x < 3; x++) {
  106.       utsound(500,2);
  107.       utsound(300,2);
  108.    }
  109. }
  110.  
  111.  
  112. void utsound(int freq,int time)
  113. {
  114.    if (Silent)
  115.       return;
  116.  
  117.    soundon(freq);
  118. /*  Delay specified time */
  119.    utsleep(time);
  120.    soundoff();
  121. }
  122.  
  123. unsigned utsleep(unsigned period)
  124. {
  125.     long     initclk;        /* Initial clock count.         */
  126.     long     nowclk;        /* Moving clock count.            */
  127.     unsigned elpticks;        /* Elapsed tick count.            */
  128.  
  129.     /* Find out the current clock count and wait until period        */
  130.     /* counts have passed.                        */
  131.  
  132.    _disable();
  133.  
  134.  
  135.     utgetclk(&initclk);
  136.  
  137.     for (elpticks = 0; elpticks < period; elpticks = (unsigned) (nowclk - initclk))  {
  138.        utgetclk(&nowclk);
  139.        if (nowclk < initclk)    /* Must have wrapped past midnight. */
  140.            nowclk += 0x1800b0L;
  141.     }
  142.  
  143.    _enable();
  144.  
  145.     return(elpticks);
  146. }
  147.  
  148.             /* Construct far void pointer.            */
  149. #define uttofaru(seg,off) ((void _far *)                  \
  150.                ((((unsigned long) (unsigned int)         \
  151.                  (seg)) << 16L) |                 \
  152.                ((unsigned long) (unsigned int) (off))))
  153.  
  154.         /* Address of PC BIOS clock tick count.         */
  155. #define UTCLOCKADDR (uttofaru (0x40, 0x6c))
  156.  
  157.         /* Address of PC BIOS clock rollover flag.        */
  158. #define UTROLLADDR (uttofaru (0x40, 0x70))
  159.  
  160.             /* Return byte pointed to by pointer.        */
  161. #define utpeekb(p) (*((const unsigned char _far *) (p)))
  162.  
  163. int utgetclk(long *pcount)
  164. {
  165.     unsigned char rollover; /* Copy of BIOS's rollover flag.        */
  166.  
  167.    _disable();
  168.  
  169.    _fmemcpy((void _far *) pcount,
  170.         (char _far *) UTCLOCKADDR,
  171.          sizeof (*pcount));
  172.  
  173.     rollover = utpeekb (UTROLLADDR);
  174.  
  175.    _enable();
  176.  
  177.     return (rollover != 0);
  178. }
  179.  
  180. void soundon(int freq)
  181. {
  182.    _asm {
  183.        mov cx,freq        ; get frequency
  184.  
  185.        mov dx,12        ; upper part of numerator
  186.        mov ax,343eh        ; lower part of numerator
  187.        div cx            ; Divide specified frequency into
  188.                    ; 1,193,182 hz clock freq used by timer
  189.        mov cx,ax        ; result in ax
  190.  
  191.        mov al,cl        ; lower byte out to timer
  192.        out 42h,al
  193.        mov al,ch        ; high byte out to timer
  194.        out 42h,al
  195.  
  196.        in al,61h        ; get contents of system port b
  197.        or al,3         ; turn speaker and timer on
  198.        out 61h,al        ; send out new values to port b
  199.    }
  200. }
  201.  
  202. void soundoff(void)
  203. {
  204.    _asm {
  205.        in al,61h        ; get contents of system port b
  206.        and al,11111100b    ; turn speaker and timer off
  207.        out 61h,al        ; send out new values to port b
  208.    }
  209. }
  210.  
  211. void strinsert(char *insstr,char *dest,int position)
  212. {
  213.    int i;
  214.    int len;
  215.  
  216.    len = strlen(insstr);
  217.    for(i = strlen(dest); i >= position; i--)        /* Shift the text */
  218.       *(dest + len + i) = *(dest + i);
  219.    for(i = 0; i < len; i++)        /* Now do an insert */
  220.       *(dest + position + i) = *(insstr + i);
  221. }
  222.  
  223.  
  224. /* Trim leading and trailing blanks on string */
  225. void blanktrim(char *string)
  226. {
  227.    int i;
  228.    char *p1,*p2;
  229.  
  230.    for(i = strlen(string) - 1; ('!' > string[i]) && i >= 0; i--)
  231.       ;
  232.    string[i+1] = '\x0';
  233.    if (*string) {
  234.       p1 = p2 = string;
  235.  
  236.       while('!' > *p1)
  237.          p1++;
  238.  
  239.       while(*p1 != '\x0')
  240.          *p2++ = *p1++;
  241.  
  242.       *p2 = '\x0';
  243.    }
  244. }
  245.  
  246. /* Compare passed date string (dd/mm/yy) with today's date
  247.    Return 0 if today, 1 if newer, -1 if older and -2 if bad date */
  248. int datecheck(char *inpstr)
  249. {
  250.    time_t curtime;
  251.    struct tm *ltime;
  252.    char timestr[38];
  253.    char *p,*dp;
  254.    int x,month,day,year;
  255.    int tmonth,tday,tyear;
  256.  
  257.  
  258.    time(&curtime);
  259.    ltime = localtime(&curtime);
  260.    p = asctime(ltime);
  261.    dp = strtok(p," ");        /* Fetch day string */
  262.  
  263.    if (strchr(inpstr,'/') == NULL) {        /* Special dates */
  264.       if (strcmpi(inpstr,"NOW") == 0)        /* Special token for today */
  265.          return(0);
  266.  
  267.       strcpy(timestr,inpstr);
  268.       timestr[3] = 0;        /* Use only the first 3 chars of day */
  269. /* Now check and see if its today */
  270.       if (strcmpi(dp,timestr) == 0)        /* We allow macros of Sun,Mon,etc. */
  271.          return(0);
  272.  
  273.       return(-2);        /* Invalid date then */
  274.     }
  275.  
  276.    p = ctime(&curtime);
  277.    strcpy(timestr,p);
  278.    if (*timestr) {        /* Valid time returned from computer */
  279. /* construct a date str exactly formatted like inpstr */
  280.       p = strtok(timestr," ");        /* Day */
  281.       p = strtok(NULL," ");        /* Month */
  282.       for (month=0; strcmpi(p,dates[month]) != 0 && x < 12; month++)
  283.          ;
  284.       month++;        /* Months start at 1 */
  285.       p = strtok(NULL," ");        /* day numeric string */
  286.       day = atoi(p);
  287.       p = strtok(NULL," ");        /* Time string */
  288.       p = strtok(NULL," \n");        /* year string */
  289.       year = atoi(p) - 1900;
  290.  
  291. /* Okay now start the compares */
  292.  
  293.       sscanf(inpstr,"%02d/%02d/%02d",&tmonth,&tday,&tyear);
  294.       if (tyear == 0 || tday == 0 || tmonth == 0)
  295.          return(-2);
  296.       if (tyear > year)
  297.          return(1);
  298.       if (tyear < year)
  299.          return(-1);
  300.       if (tmonth > month)
  301.          return(1);
  302.       if (tmonth < month)
  303.          return(-1);
  304.       if (tday > day)
  305.          return(1);
  306.       if (tday < day)
  307.          return(-1);
  308.       return(0);        /* Its today! */
  309.    }
  310.    else return(-2);
  311. }
  312.  
  313. /* Compare passed time string (hh:mm) with current time (in 24hour format)
  314.    Return 0 if now, 1 if newer, -1 if older and -2 if bad time */
  315. int timecheck(char *inpstr)
  316. {
  317.    time_t curtime;
  318.    char timestr[38];
  319.    char *p;
  320.    int hour,min;
  321.    int thour,tmin;
  322.  
  323.    if (strcmpi(inpstr,"NOW") == 0)        /* Special token for today */
  324.       return(0);
  325.  
  326.    if (strlen(inpstr) != 5)        /* Quick checking */
  327.       return(-2);
  328.  
  329.    time(&curtime);
  330.    p = ctime(&curtime);
  331.    strcpy(timestr,p);
  332.    if (*timestr) {        /* Valid time returned from computer */
  333. /* construct a time str exactly formatted like inpstr */
  334.       p = strtok(timestr," ");        /* Day */
  335.       p = strtok(NULL," ");        /* Month */
  336.       p = strtok(NULL," ");        /* day numeric string */
  337.       p = strtok(NULL," ");        /* Time string */
  338.  
  339. /* Okay now start the compares */
  340.       sscanf(p,"%02d:%02d",&hour,&min);
  341.       sscanf(inpstr,"%02d:%02d",&thour,&tmin);
  342.       if (thour < hour)
  343.          return(-1);
  344.       if (thour > hour)
  345.          return(1);
  346. /* Hour is the same */
  347.       if (tmin < min)
  348.          return(-1);
  349.       if (tmin > min)
  350.          return(1);
  351.       return(0);        /* Its now! */
  352.    }
  353.    else return(-2);
  354. }
  355.  
  356.