home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uupoll068.lha / misc / uupoll067.lha / src / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-16  |  5.3 KB  |  230 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.  
  14. #include "cus.h"
  15. #include "defines.h"
  16. #include "proto.h"
  17.  
  18. int exists(char *cpPath)
  19. {
  20.     BPTR wpLock = NULL;
  21.     int rc;
  22.  
  23.     if ((wpLock = Lock(cpPath, SHARED_LOCK)) == NULL)
  24.         CU(FALSE);
  25.     rc = TRUE;
  26.  
  27.     CUS:
  28.     if (wpLock)
  29.         UnLock(wpLock);
  30.     return (rc);
  31. }
  32.  
  33.  
  34. #define MAXLOCKS 128
  35. #define LOCKBUFSIZE (MAXLOCKS * sizeof(BPTR))
  36.  
  37. static unsigned short getdospath(BPTR wpLock, char *cpPath)
  38. {
  39.     BPTR *wpLockbuf = NULL;
  40.     register BPTR *wpLockbufCur;
  41.     register short wnLocks = NULL;
  42.     register char *cpPathCur;
  43.     struct FileInfoBlock *fibp = NULL;
  44.     register unsigned short rc;
  45.  
  46.     if (!(wpLockbuf = (BPTR *)AllocMem(LOCKBUFSIZE, MEMF_PUBLIC|MEMF_CLEAR)))
  47.         CU(NULL);
  48.     if (!(fibp = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)))
  49.         CU(NULL);
  50.  
  51.     /*    get all locks from current dir to root    */
  52.     wpLockbufCur = wpLockbuf;
  53.     for (    *wpLockbufCur = wpLock, wnLocks = 1;
  54.             *wpLockbufCur = ParentDir(*wpLockbufCur++);
  55.             wnLocks++    )
  56.         ;
  57.  
  58.     /*    get names associated with locks in reverse order through AmigaDOS(tm)
  59.         examinations and build up the target buffer    */
  60.  
  61.     /*    get name of root outside a loop because of last ':' char
  62.         and right unlocking sequence    */
  63.     cpPathCur = cpPath;
  64.     if (!(Examine(*(--wpLockbufCur), fibp)))
  65.         CU(NULL);
  66.     cpPathCur = stpcpy(cpPathCur, fibp->fib_FileName);
  67.     *cpPathCur++ = ':';
  68.  
  69.     /*    get names of subdirs within a loop    */
  70.     while (--wnLocks) {
  71.         UnLock(*wpLockbufCur--);    /* this will unlock the root lock at the first
  72.                                      * entry of this loop and will _not_ unlock
  73.                                      * the given 'wpLock' at the last loop! */
  74.         if (!(Examine(*wpLockbufCur, fibp)))
  75.             CU(NULL);
  76.         cpPathCur = stpcpy(cpPathCur, fibp->fib_FileName);
  77.         if (fibp->fib_DirEntryType)
  78.             *cpPathCur++ = '/';
  79.     }
  80.  
  81.     /*    set null terminator and calculate the total
  82.         length of actually written characters    */
  83.     *cpPathCur = NUL;
  84.     rc = (unsigned short)(cpPathCur - cpPath);
  85.  
  86.     CUS:
  87.     /*    unlock all locks which are not already
  88.         unlocked because an error could have been occurred */
  89.     for ( ; wnLocks > 1; wnLocks--)
  90.         UnLock(*wpLockbufCur--);
  91.  
  92.     if (fibp)
  93.         FreeMem(fibp, sizeof(struct FileInfoBlock));
  94.     if (wpLockbuf)
  95.         FreeMem(wpLockbuf, LOCKBUFSIZE);
  96.  
  97.     return rc;
  98. }
  99.  
  100.  
  101. #define GETFNLRC_ERR -1
  102. #define GETFNLATTR_FILE 1
  103. #define GETFNLATTR_FILEANDDIR 2
  104. #define GETFNLATTR_DIR 4
  105.  
  106. static int getfnlst(char *cpPath, char *cpDest, unsigned int cnDest, int wAttr)
  107. {
  108.     char caPathTmp[RANGE_8BIT];
  109.     BPTR wpLock;
  110.     struct FileInfoBlock *fibp = NULL;
  111.     char *cpDestCur = cpDest;
  112.     int nFiles = 0;
  113.     int rc;
  114.  
  115.     wAttr = wAttr & (GETFNLATTR_FILE | GETFNLATTR_FILEANDDIR | GETFNLATTR_DIR);
  116.  
  117.     if ((fibp = (struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  118.         EE(NULL);
  119.  
  120.     if ((wpLock = Lock(cpPath, SHARED_LOCK)) == NULL)
  121.         EE(GETFNLRC_ERR);
  122.     if (getdospath(wpLock, caPathTmp) == 0) {
  123.         UnLock(wpLock);
  124.         EE(GETFNLRC_ERR);
  125.     }
  126.     if (strlen(caPathTmp) > cnDest-1)
  127.         EE(GETFNLRC_ERR);
  128.     cpDestCur = stpcpy(cpDest, caPathTmp);
  129.     *cpDestCur++ = NUL;
  130.  
  131.     if (Examine(wpLock, fibp) == NULL) {
  132.         UnLock(wpLock);
  133.         EE(GETFNLRC_ERR);
  134.     }
  135.     while (ExNext(wpLock, fibp)) {
  136.         if (    ( (fibp->fib_DirEntryType > 0 /*=DIR*/ ) && (wAttr == GETFNLATTR_FILE) )
  137.             ||    ( (fibp->fib_DirEntryType < 0 /*=FILE*/) && (wAttr == GETFNLATTR_DIR)  )    )
  138.             continue;
  139.         if ( (int)(cpDest + cnDest - cpDestCur) < strlen(fibp->fib_FileName) ) {
  140.             UnLock(wpLock);
  141.             EE(GETFNLRC_ERR);
  142.         }
  143.         cpDestCur = stpcpy(cpDestCur, fibp->fib_FileName);
  144.         *cpDestCur++ = NUL;
  145.         ++nFiles;
  146.     }
  147.     *cpDestCur++ = NUL;
  148.     UnLock(wpLock);
  149.     rc = nFiles;
  150.  
  151.     EE:
  152.     if (fibp)
  153.         FreeMem(fibp, sizeof(struct FileInfoBlock));
  154.     return rc;
  155. }
  156.  
  157. static char caSpoolfiles[RANGE_14BIT];
  158. static char *cpaSpoolfiles[RANGE_10BIT];
  159.  
  160. int PendingWork(char *cpSpooldir, char *cpSystem, int nFiles)
  161. {
  162.     int rc;
  163.     int nSpoolfiles;
  164.     int i;
  165.     char caTmp[64];
  166.     int fcnt;
  167.  
  168.     if (!exists(cpSpooldir))
  169.         CU(FALSE);
  170.  
  171.     if ((nSpoolfiles = getfnlst(cpSpooldir, caSpoolfiles, sizeof(caSpoolfiles), 1)) == -1)
  172.         CU(DOSRC_FAIL);
  173.     nSpoolfiles = strbplst(cpaSpoolfiles, sizeof(cpaSpoolfiles), caSpoolfiles);
  174.  
  175.     fcnt = 0;
  176.     for (i = 1; i < nSpoolfiles; i++) {
  177.         if (cpaSpoolfiles[i][0] == 'C' && cpaSpoolfiles[i][1] == '.') {
  178.             strcpy(caTmp, cpSystem);
  179.             if (strlen(caTmp) > 7)
  180.                 caTmp[7] = NUL;
  181.             if (strnicmp(caTmp, &cpaSpoolfiles[i][2], strlen(caTmp)) == 0) {
  182.                 ++fcnt;
  183.             }
  184.         }
  185.     }
  186.     if (fcnt < nFiles)
  187.         CU(FALSE);
  188.     else
  189.         CU(TRUE);
  190.  
  191.     CUS:
  192.     return rc;
  193. }
  194.  
  195. #define TMPFILE "t:uupoll_tmpfile"
  196.  
  197. void NotifyOnFailure(char *user, char *host, char *message)
  198. {
  199.     FILE *fp = NULL;
  200.     long t;
  201.     struct tm *tm;
  202.     char caTmp[64];
  203.     char caCmnd[256];
  204.  
  205.     (void)time(&t);
  206.     tm = localtime(&t);
  207.  
  208.     if ((fp = fopen(TMPFILE, "w")) != NULL) {
  209.         sprintf(caTmp, "%s", asctime(tm));
  210.         caTmp[strlen(caTmp)-1] = NUL;
  211.         fprintf(fp, "To: %s\n"
  212.                     "Subject: %s: %s\n"
  213.                     "\n"
  214.                     "[autogenerated message]\n"
  215.                     "\n"
  216.                     "Local Time  : %s\n"
  217.                     "Remote Host : %s\n"
  218.                     "Status      : %s\n"
  219.                     "\n"
  220.                     "--\n"
  221.                     "UUPOLL-DAEMON\n",
  222.                     user, host, message, caTmp, host, message);
  223.         fclose(fp);
  224.         sprintf(caCmnd, "run <nil: >nil: sendmail <%s -f UUPOLL-DAEMON -R \"UUCP Admin Subsystem\"", TMPFILE);
  225.         system(caCmnd);
  226.     }
  227.     return;
  228. }
  229.  
  230.