home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MAXMAILP.ZIP / MAXLIB.C < prev    next >
C/C++ Source or Header  |  1991-01-02  |  6KB  |  283 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*    Maxlib.c      :Miscellaneous Maximus routines for MaxMail             */
  4. /*                                                                          */
  5. /****************************************************************************/
  6. #if defined OS2
  7.   #define INCL_DOS
  8.   #define INCL_DOSDATETIME
  9.   #include <os2.h>
  10.   extern int hangup;
  11. #endif
  12. #include "MaxMail.h"
  13.  
  14. /* Load in the MAXimus PARM file into our global structure */
  15. int read_prm(char *pname)
  16. {
  17.   int prmfile,x;
  18.  
  19.   offsets=NULL;
  20.  
  21.   if ((prmfile=sopen(pname,O_RDONLY | O_BINARY,SH_DENYNO,S_IREAD))==-1)
  22.    return(-1);
  23.  
  24.   x=read(prmfile,(char *)&prm,sizeof(struct m_pointers));
  25.    
  26.   if (prm.id != 'M' || prm.version != CTL_VER)
  27.   {
  28.     printf("\a\a");
  29.     aborterror(FILEOPEN,"Bad PARM file");
  30.   }
  31.  
  32.   x=(int)(fsize(pname)-((int)tell(prmfile)));
  33.   offsets=(char *)malloc(x+10);
  34.   x=read(prmfile,offsets,x);
  35.   close(prmfile);
  36.   return TRUE;
  37. }
  38.  
  39. /* Quick simple method to return size of file without
  40.    opening it */
  41. #if defined OS2
  42. long fsize(char *fname)
  43. {
  44.    FILEFINDBUF find;
  45.    USHORT HANDLE;
  46.    unsigned count;
  47.  
  48.    HANDLE = 0xffff;
  49.    count = 1;
  50.  
  51.    if (DosFindFirst((char far *) fname, (USHORT far *) &HANDLE,
  52.    0x0, (FILEFINDBUF far *) &find, sizeof(find),
  53.    (unsigned far *) &count,0L) ){
  54.       DosFindClose(HANDLE);
  55.       return(0L);        /* An error occured */
  56.    } else {
  57.       DosFindClose(HANDLE);
  58.       return(find.cbFile);
  59.    }
  60. }
  61. #else
  62. long fsize(char *fname)
  63. {
  64.    struct find_t find;
  65.  
  66.    if(_dos_findfirst(fname,0xffff,&find))
  67.       return(0L);        /* An error occured */
  68.    else return(find.size);
  69. }
  70. #endif
  71.  
  72. void get_currents()
  73. {
  74.    unsigned x;
  75.  
  76. /* dos_getdrive not available in OS/2 */
  77. #if !defined OS2
  78.    _dos_getdrive(&x);
  79.    sprintf(sdrive,"%c:",'A' + x - 1);
  80. #endif
  81.    getcwd(temp,_MAX_DIR - 1);
  82.    strcpy(sdir,temp+2);
  83.    strcat(sdir,"\\");
  84. }
  85.  
  86.  
  87. /* Switch to drive,dir specified */
  88. int switch_dir(char *dname)
  89. {
  90.    unsigned x,y;
  91.    char tt[65];
  92.  
  93.    strcpy(tt,dname);
  94.    x = strlen(tt);
  95.    tt[x-1] = 0;        /* Nullify trailing \ */
  96. /* dos_setdrive not available in OS/2 */
  97. #if !defined OS2
  98.    x = dname[0] - 'A' + 1;
  99.    _dos_setdrive(x,&y);        /*ignore 2nd parm */
  100. #endif
  101.    return(chdir(tt));
  102. }
  103.  
  104. #if !defined OS2
  105. /* Video stuff */
  106.  
  107. void strout(char *outmsg)
  108. {
  109.    if (!ReDirect)
  110.       printf(outmsg);
  111.    if (!IsLocal) {
  112.       check_carrier();
  113.       fos_txstrw(outmsg);
  114.       check_carrier();
  115.    }
  116. }
  117.  
  118. void strin(char *inmsg)
  119. {
  120.    unsigned ch;
  121.    int key,minutes;
  122.    char *p;
  123.  
  124.    p = inmsg;
  125.    cflush();
  126.    if (!IsLocal) {        /* Simulate GETS function for fossil */
  127.       minutes = timeon();        /* Establish a counter */
  128.       while (1) {
  129.          while ((ch = fos_inkeych()) == 0xFFFF) {
  130.             check_carrier();
  131.             if (timeon() > minutes + IDLETIME)        /* User is idle too long */
  132.                aborterror(USERIDLE,NULL);
  133.          }
  134.          key = ch & 0xff;
  135.          if (key == '\r') {        /* Carraige return */
  136.             break;
  137.          }
  138.          if (key == '\b') {        /* Backspace */
  139.             if (p > inmsg) {
  140.                fos_txcw((byte) key);
  141.                putchar(key);
  142.                fos_txcw((byte) ' ');
  143.                putchar(' ');
  144.                fos_txcw((byte) key);
  145.                putchar(key);
  146.                p--;
  147.             }
  148.             *p = 0;        /* Terminate */
  149.             continue;
  150.          }
  151.          else {
  152.             (*p) = key;
  153.             p++;
  154.          }
  155.          fos_txcw((byte) key);
  156.          putchar(key);
  157.       }
  158.       *p = 0;        /* Terminate */
  159.       *(p+1) = 0;
  160.       
  161.    }
  162.    else gets(inmsg);
  163.  
  164. }
  165.  
  166. int chrin(void)
  167. {
  168.    unsigned ch;
  169.    int key;
  170.    int minutes;
  171.  
  172.    cflush();
  173.    if (IsLocal)
  174.       return(getch());
  175.    else {
  176.       minutes = timeon();        /* Establish a counter */
  177.       while ((ch = fos_inkeych()) == 0xFFFF) {
  178.          check_carrier();
  179.          if (timeon() > minutes + IDLETIME) {        /* User is idle too long */
  180.             aborterror(USERIDLE,NULL);
  181.          }
  182.       }
  183.       return(ch & 0xff);
  184.    }
  185. }
  186. #endif
  187.  
  188. void cflush(void)
  189. {
  190.    if (IsLocal)
  191.       return;
  192. #if defined OS2
  193.    com_clear_in();
  194. #else
  195.    fos_purge_ibuf();
  196. #endif
  197. }
  198.  
  199. #if defined OS2
  200. int check_carrier(void)
  201. {
  202.    checkhangup();
  203.    if (hangup) {        /* Look at DCD(carrier bit) */
  204.       fclose(outfile);
  205.       aborterror(DROPCARRIER,NULL);
  206.    }
  207.    return(FALSE);
  208. }
  209. #else
  210. int check_carrier(void)
  211. {
  212.    unsigned status;
  213.  
  214.    status = fos_modem_stat();
  215.    if ((status & 0x80) == 0) {        /* Look at DCD(carrier bit) */
  216.       fclose(outfile);
  217.       aborterror(DROPCARRIER,NULL);
  218.    }
  219.    return(FALSE);
  220. }
  221. #endif
  222.  
  223.  
  224. void logit(char *strng,char flag)
  225. {
  226.    FILE *log;
  227.    char ttemp[40];
  228.    char *ttemp1;
  229.    char *month;
  230.    char *day;
  231.  
  232.    if (LogMode == FALSE)
  233.       return;
  234.  
  235.    ttemp1 = (char *) malloc(101);
  236.    if (ttemp1 == NULL) {
  237.       LogMode = FALSE;
  238.       aborterror(BADALLOC,NULL);
  239.    }
  240.  
  241.    strcpy(ttemp, timestring());
  242.    strtok(ttemp," ");               /* Strip Day of week */
  243.    month = strtok(NULL," ");
  244.    day = strtok(NULL," ");
  245.    tm = strtok(NULL," ");
  246.  
  247.    if (Task)
  248.       sprintf(ttemp1,"%c %s %s %s MaxMail{%d}  %s\n",flag,day,month,tm,Task,strng);
  249.    else sprintf(ttemp1,"%c %s %s %s MaxMail  %s\n",flag,day,month,tm,strng);
  250.    if ((log = _fsopen(LogFile,"a",SH_DENYWR)) == NULL) {
  251.       printf("\a\aError opening log file!\n");
  252.       LogMode = FALSE;
  253.       free(ttemp1);
  254.       return;
  255.    }
  256.    fputs(ttemp1,log);
  257.    free(ttemp1);
  258.    fclose(log);
  259. }
  260.  
  261. char *timestring(void)
  262. {
  263.    time_t     aclock;
  264.    struct tm  *newtime;
  265.  
  266.    time(&aclock);
  267.    newtime = localtime(&aclock);
  268.    return(asctime(newtime));
  269. }
  270.  
  271. void find_class(void)
  272. {
  273.   int x;
  274.  
  275.   for (x=0;x < MAXCLASS;x++)
  276.   {
  277.     if (LastUser.priv==prm.class[x].priv) {
  278.       UserClass=x;
  279.       break;
  280.     }
  281.   }
  282. }
  283.