home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uupoll068.lha / source / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  8.5 KB  |  370 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13. #include <dos/dostags.h>
  14. #include <utility/tagitem.h>
  15.  
  16. #include "cus.h"
  17. #include "defines.h"
  18. #include "proto.h"
  19.  
  20. int exists(char *cpPath)
  21. {
  22.     BPTR wpLock = NULL;
  23.     int rc;
  24.  
  25.     if ((wpLock = Lock(cpPath, SHARED_LOCK)) == NULL)
  26.         cu(FALSE);
  27.     else
  28.         cu(TRUE);
  29.  
  30.     cus:
  31.     if (wpLock)
  32.         UnLock(wpLock);
  33.     return rc;
  34. }
  35.  
  36. int SystemCmndSync(char *caCmnd)
  37. {
  38.     struct TagItem caTag[RANGE_5BIT];
  39.     int tagidx;
  40.     struct Process *pid;
  41.     ULONG ifh = NULL;
  42.     ULONG ofh = NULL;
  43.     int rc;
  44.  
  45.     pid = (struct Process *)FindTask(NULL);
  46.      tagidx = 0;
  47.  
  48.     if ((ifh = Open("NIL:", MODE_OLDFILE)) == NULL)
  49.         cu(-1);
  50.     caTag[tagidx]  .ti_Tag  = SYS_Input;
  51.     caTag[tagidx++].ti_Data = (ULONG)ifh;
  52.  
  53.     if ((ofh = Open("NIL:", MODE_NEWFILE)) == NULL)
  54.         cu(-1);
  55.     caTag[tagidx]  .ti_Tag  = SYS_Output;
  56.     caTag[tagidx++].ti_Data = (ULONG)ofh;
  57.  
  58.     caTag[tagidx]  .ti_Tag  = SYS_Asynch;
  59.     caTag[tagidx++].ti_Data = (ULONG)FALSE;
  60.  
  61.     caTag[tagidx]  .ti_Tag  = NP_StackSize;
  62.     caTag[tagidx++].ti_Data = (ULONG)(pid->pr_StackSize);
  63.     caTag[tagidx]  .ti_Tag  = NP_Cli;
  64.     caTag[tagidx++].ti_Data = (ULONG)TRUE;
  65.  
  66.     caTag[tagidx]  .ti_Tag  = TAG_DONE;
  67.     caTag[tagidx++].ti_Data = (ULONG)0;
  68.  
  69.     rc = SystemTagList(caCmnd, caTag);
  70.  
  71.     cus:
  72.     if (ifh)
  73.         Close(ifh);
  74.     if (ofh)
  75.         Close(ofh);
  76.     return rc;
  77. }
  78.  
  79. int SystemCmndAsync(char *caCmnd)
  80. {
  81.     struct TagItem caTag[RANGE_5BIT];
  82.     int tagidx;
  83.     struct Process *pid;
  84.     ULONG ifh = NULL;
  85.     ULONG ofh = NULL;
  86.     int rc;
  87.  
  88.     pid = (struct Process *)FindTask(NULL);
  89.      tagidx = 0;
  90.  
  91.     if ((ifh = Open("NIL:", MODE_OLDFILE)) == NULL)
  92.         cu(-1);
  93.     caTag[tagidx]  .ti_Tag  = SYS_Input;
  94.     caTag[tagidx++].ti_Data = (ULONG)ifh;
  95.  
  96.     if ((ofh = Open("NIL:", MODE_NEWFILE)) == NULL)
  97.         cu(-1);
  98.     caTag[tagidx]  .ti_Tag  = SYS_Output;
  99.     caTag[tagidx++].ti_Data = (ULONG)ofh;
  100.  
  101.     caTag[tagidx]  .ti_Tag  = SYS_Asynch;
  102.     caTag[tagidx++].ti_Data = (ULONG)TRUE;
  103.  
  104.     caTag[tagidx]  .ti_Tag  = NP_StackSize;
  105.     caTag[tagidx++].ti_Data = (ULONG)(pid->pr_StackSize);
  106.     caTag[tagidx]  .ti_Tag  = NP_Cli;
  107.     caTag[tagidx++].ti_Data = (ULONG)TRUE;
  108.  
  109.     caTag[tagidx]  .ti_Tag  = TAG_DONE;
  110.     caTag[tagidx++].ti_Data = (ULONG)0;
  111.  
  112.     if (SystemTagList(caCmnd, caTag) == 0) {
  113.         ifh = NULL;
  114.         ofh = NULL;
  115.         cu(0);
  116.     }
  117.     else
  118.         cu(-1);
  119.  
  120.     cus:
  121.     if (ifh)
  122.         Close(ifh);
  123.     if (ofh)
  124.         Close(ofh);
  125.     return rc;
  126. }
  127.  
  128. #define MAXLOCKS 128
  129. #define LOCKBUFSIZE (MAXLOCKS * sizeof(BPTR))
  130.  
  131. static unsigned short getdospath(BPTR wpLock, char *cpPath)
  132. {
  133.     BPTR *wpLockbuf = NULL;
  134.     register BPTR *wpLockbufCur;
  135.     register short wnLocks = NULL;
  136.     register char *cpPathCur;
  137.     struct FileInfoBlock *fibp = NULL;
  138.     register unsigned short rc;
  139.  
  140.     if (!(wpLockbuf = (BPTR *)AllocMem(LOCKBUFSIZE, MEMF_PUBLIC|MEMF_CLEAR)))
  141.         cu(NULL);
  142.     if (!(fibp = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)))
  143.         cu(NULL);
  144.  
  145.     /*    get all locks from current dir to root    */
  146.     wpLockbufCur = wpLockbuf;
  147.     for (    *wpLockbufCur = wpLock, wnLocks = 1;
  148.             *wpLockbufCur = ParentDir(*wpLockbufCur++);
  149.             wnLocks++    )
  150.         ;
  151.  
  152.     /*    get names associated with locks in reverse order through AmigaDOS(tm)
  153.         examinations and build up the target buffer    */
  154.  
  155.     /*    get name of root outside a loop because of last ':' char
  156.         and right unlocking sequence    */
  157.     cpPathCur = cpPath;
  158.     if (!(Examine(*(--wpLockbufCur), fibp)))
  159.         cu(NULL);
  160.     cpPathCur = stpcpy(cpPathCur, fibp->fib_FileName);
  161.     *cpPathCur++ = ':';
  162.  
  163.     /*    get names of subdirs within a loop    */
  164.     while (--wnLocks) {
  165.         UnLock(*wpLockbufCur--);    /* this will unlock the root lock at the first
  166.                                      * entry of this loop and will _not_ unlock
  167.                                      * the given 'wpLock' at the last loop! */
  168.         if (!(Examine(*wpLockbufCur, fibp)))
  169.             cu(NULL);
  170.         cpPathCur = stpcpy(cpPathCur, fibp->fib_FileName);
  171.         if (fibp->fib_DirEntryType)
  172.             *cpPathCur++ = '/';
  173.     }
  174.  
  175.     /*    set null terminator and calculate the total
  176.         length of actually written characters    */
  177.     *cpPathCur = NUL;
  178.     rc = (unsigned short)(cpPathCur - cpPath);
  179.  
  180.     cus:
  181.     /*    unlock all locks which are not already
  182.         unlocked because an error could have been occurred */
  183.     for ( ; wnLocks > 1; wnLocks--)
  184.         UnLock(*wpLockbufCur--);
  185.  
  186.     if (fibp)
  187.         FreeMem(fibp, sizeof(struct FileInfoBlock));
  188.     if (wpLockbuf)
  189.         FreeMem(wpLockbuf, LOCKBUFSIZE);
  190.  
  191.     return rc;
  192. }
  193.  
  194.  
  195. #define GETFNLRC_ERR -1
  196. #define GETFNLATTR_FILE 1
  197. #define GETFNLATTR_FILEANDDIR 2
  198. #define GETFNLATTR_DIR 4
  199.  
  200. static int getfnlst(char *cpPath, char *cpDest, unsigned int cnDest, int wAttr)
  201. {
  202.     char caPathTmp[RANGE_8BIT];
  203.     BPTR wpLock;
  204.     struct FileInfoBlock *fibp = NULL;
  205.     char *cpDestCur = cpDest;
  206.     int nFiles = 0;
  207.     int rc;
  208.  
  209.     wAttr = wAttr & (GETFNLATTR_FILE | GETFNLATTR_FILEANDDIR | GETFNLATTR_DIR);
  210.  
  211.     if ((fibp = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  212.         cu(NULL);
  213.  
  214.     if ((wpLock = Lock(cpPath, SHARED_LOCK)) == NULL)
  215.         cu(GETFNLRC_ERR);
  216.     if (getdospath(wpLock, caPathTmp) == 0) {
  217.         UnLock(wpLock);
  218.         cu(GETFNLRC_ERR);
  219.     }
  220.     if (strlen(caPathTmp) > cnDest-1)
  221.         cu(GETFNLRC_ERR);
  222.     cpDestCur = stpcpy(cpDest, caPathTmp);
  223.     *cpDestCur++ = NUL;
  224.  
  225.     if (Examine(wpLock, fibp) == NULL) {
  226.         UnLock(wpLock);
  227.         cu(GETFNLRC_ERR);
  228.     }
  229.     while (ExNext(wpLock, fibp)) {
  230.         if (    ( (fibp->fib_DirEntryType > 0 /*=DIR*/ ) && (wAttr == GETFNLATTR_FILE) )
  231.             ||    ( (fibp->fib_DirEntryType < 0 /*=FILE*/) && (wAttr == GETFNLATTR_DIR)  )    )
  232.             continue;
  233.         if ( (int)(cpDest + cnDest - cpDestCur) < strlen(fibp->fib_FileName) ) {
  234.             UnLock(wpLock);
  235.             cu(GETFNLRC_ERR);
  236.         }
  237.         cpDestCur = stpcpy(cpDestCur, fibp->fib_FileName);
  238.         *cpDestCur++ = NUL;
  239.         ++nFiles;
  240.     }
  241.     *cpDestCur++ = NUL;
  242.     UnLock(wpLock);
  243.     rc = nFiles;
  244.  
  245.     cus:
  246.     if (fibp)
  247.         FreeMem(fibp, sizeof(struct FileInfoBlock));
  248.     return rc;
  249. }
  250.  
  251. static char caSpoolfiles[RANGE_14BIT];
  252. static char *cpaSpoolfiles[RANGE_10BIT];
  253.  
  254. int PendingFiles(char *cpSpooldir, char *cpSystem, signed int nFiles)
  255. {
  256.     int rc;
  257.     int nSpoolfiles;
  258.     int i;
  259.     char caTmp[64];
  260.     int fcnt;
  261.  
  262.     if (!exists(cpSpooldir))
  263.         cu(FALSE);
  264.  
  265.     if ((nSpoolfiles = getfnlst(cpSpooldir, caSpoolfiles, sizeof(caSpoolfiles), GETFNLATTR_FILE)) == -1)
  266.         cu(DOSRC_FAIL);
  267.     nSpoolfiles = strbplst(cpaSpoolfiles, sizeof(cpaSpoolfiles), caSpoolfiles);
  268.  
  269.     fcnt = 0;
  270.     for (i = 1; i < nSpoolfiles; i++) {
  271.         if (cpaSpoolfiles[i][0] == 'C' && cpaSpoolfiles[i][1] == '.') {
  272.             strcpy(caTmp, cpSystem);
  273.             if (strlen(caTmp) > 7)
  274.                 caTmp[7] = NUL;
  275.             if (strnicmp(caTmp, &cpaSpoolfiles[i][2], strlen(caTmp)) == 0) {
  276.                 ++fcnt;
  277.             }
  278.         }
  279.     }
  280.  
  281.     rc = ((nFiles > 0) ? (fcnt >= nFiles) : (fcnt <= -(nFiles)));
  282.  
  283.     cus:
  284.     return rc;
  285. }
  286.  
  287. int PendingKBytes(char *cpSpooldir, char *cpSystem, signed int nKBytes)
  288. {
  289.     int rc;
  290.     int nSpoolfiles;
  291.     int i;
  292.     char caTmp[64];
  293.     int bcnt;
  294.     BPTR wpLock;
  295.     struct FileInfoBlock *fibp = NULL;
  296.  
  297.     if (!exists(cpSpooldir))
  298.         cu(FALSE);
  299.  
  300.     if ((fibp = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  301.         cu(NULL);
  302.  
  303.     if ((nSpoolfiles = getfnlst(cpSpooldir, caSpoolfiles, sizeof(caSpoolfiles), GETFNLATTR_FILE)) == -1)
  304.         cu(DOSRC_FAIL);
  305.     nSpoolfiles = strbplst(cpaSpoolfiles, sizeof(cpaSpoolfiles), caSpoolfiles);
  306.  
  307.     bcnt = 0;
  308.     for (i = 1; i < nSpoolfiles; i++) {
  309.         if ((cpaSpoolfiles[i][0] == 'C' || cpaSpoolfiles[i][0] == 'D') && cpaSpoolfiles[i][1] == '.') {
  310.             strcpy(caTmp, cpSystem);
  311.             if (strlen(caTmp) > 7)
  312.                 caTmp[7] = NUL;
  313.             if (strnicmp(caTmp, &cpaSpoolfiles[i][2], strlen(caTmp)) == 0) {
  314.                 sprintf(caTmp, "%s%s", cpaSpoolfiles[0], cpaSpoolfiles[i]);
  315.                 if ((wpLock = Lock(caTmp, SHARED_LOCK)) == NULL)
  316.                     continue;
  317.                 if (Examine(wpLock, fibp) == NULL) {
  318.                     UnLock(wpLock);
  319.                     continue;
  320.                 }
  321.                 bcnt += fibp->fib_Size;
  322.             }
  323.         }
  324.     }
  325.  
  326.     rc = ((nKBytes > 0) ? ((bcnt / 1000) >= nKBytes) : ((bcnt / 1000) <= -(nKBytes)));
  327.  
  328.     cus:
  329.     if (fibp)
  330.         FreeMem(fibp, sizeof(struct FileInfoBlock));
  331.     return rc;
  332. }
  333.  
  334. #define TMPFILE "t:uupoll_tmpfile"
  335.  
  336. void NotifyOnFailure(char *user, char *host, char *message)
  337. {
  338.     FILE *fp = NULL;
  339.     long t;
  340.     struct tm *tm;
  341.     char caTmp[64];
  342.     char caCmnd[256];
  343.  
  344.     (void)time(&t);
  345.     tm = localtime(&t);
  346.  
  347.     if ((fp = fopen(TMPFILE, "w")) != NULL) {
  348.         sprintf(caTmp, "%s", asctime(tm));
  349.         caTmp[strlen(caTmp)-1] = NUL;
  350.         fprintf(fp, "Subject: %s: %s\n"
  351.                     "\n"
  352.                     "[autogenerated message]\n"
  353.                     "\n"
  354.                     "Local Time  : %s\n"
  355.                     "Remote Host : %s\n"
  356.                     "Status      : %s\n"
  357.                     "\n"
  358.                     "--\n"
  359.                     "UUPOLL-DAEMON\n",
  360.                     host, message, caTmp, host, message);
  361.         fclose(fp);
  362.         sprintf(caCmnd, "sendmail <%s -t %s -f uucp -R \"UUCP Admin Subsystem\"", TMPFILE, user);
  363.         SystemCmndSync(caCmnd);
  364.         sprintf(caCmnd, "delete >nil: %s", TMPFILE);
  365.         SystemCmndSync(caCmnd);
  366.     }
  367.     return;
  368. }
  369.  
  370.