home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / communic / pcmail / main / getwork.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  3.0 KB  |  111 lines

  1. /*++
  2. /* NAME
  3. /*    getwork 3
  4. /* SUMMARY
  5. /*    receive work from remote system
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    cico
  10. /* SYNOPSIS
  11. /*    #include "work.h"
  12. /*
  13. /*      work *rmtwork(rqst)
  14. /*      char *rqst;
  15. /*
  16. /*    void getwork(wrk)
  17. /*    work *wrk;
  18. /* DESCRIPTION
  19. /*      rmtwork() parses a remote Send request. A suitable destination
  20. /*    file is opened. The resulting work structure is for use by
  21. /*    getwork().
  22. /*
  23. /*    getwork() receives a file from the remote system, after the
  24. /*    necessary preparations have been done by rmtwork().
  25. /*    The file is deleted in case of transmission failure.
  26. /* FUNCTIONS AND MACROS
  27. /*    trap(), locname()
  28. /* SEE ALSO
  29. /*    sendwork(), scanwork()
  30. /* DIAGNOSTICS
  31. /*    Exceptions are handled with longjmp(systrap,errorcode).
  32. /*
  33. /*    rmtwork() traps in case of invalid requests or if the destination
  34. /*    file could not be opened.
  35. /*
  36. /*    getwork() traps in case of read/write errors.
  37. /* AUTHOR(S)
  38. /*    W.Z. Venema
  39. /*    Eindhoven University of Technology
  40. /*    Department of Mathematics and Computer Science
  41. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  42. /* CREATION DATE
  43. /*    Sat Mar 28 16:57:57 GMT+1:00 1987
  44. /* LAST MODIFICATION
  45. /*    90/01/22 13:01:39
  46. /* VERSION/RELEASE
  47. /*    2.1
  48. /*--*/
  49.  
  50. #include <stdio.h>
  51.  
  52. #include "defs.h"
  53. #include "logs.h"
  54. #include "status.h"
  55. #include "work.h"
  56. #include "params.h"
  57. #include "comm.h"
  58.  
  59. /* rmtwork - parse remote S request and open destination file */
  60.  
  61. public work *rmtwork(rqst)
  62. char   *rqst;
  63. {
  64.     static work wrk;
  65.     char    path[BUFSIZ];
  66.  
  67.     if (sscanf(rqst, "S %*s %s", path) != 1)    /* pick destination file name */
  68.     trap(E_LOST, "BAD REQUEST FORMAT");
  69.     debug(5) ("rmtwork: path %s\n", path);
  70.     strcpy(wrk.path, locname(path));        /* convert to local name */
  71.     debug(5) ("rmtwork: file %s\n", wrk.path);
  72.     if ((wrk.fp = fopen(wrk.path, "w")) == 0)    /* try to open that file */
  73.     trap(E_WRITERR, "CAN'T CREATE FILE (%s)", sys_errlist[errno]);
  74.     return (&wrk);
  75. }
  76.  
  77. /* getwork - receive file from remote host */
  78.  
  79. public void getwork(wrk)
  80. register work *wrk;
  81. {
  82.     char    buf[BUFSIZ];
  83.     register int nread;
  84.     register int werror;
  85.  
  86.     while ((nread = CALL(Read) (ttfd, buf, BUFSIZ)) > 0 &&
  87.        fwrite(buf, sizeof(*buf), nread, wrk->fp) == nread)
  88.      /* void */ ;
  89.     werror = ferror(wrk->fp);            /* record error status */
  90.     fclose(wrk->fp);
  91.  
  92.     /*
  93.      * In case of any errors we force a protocol shutdown. The oher side will
  94.      * send the same file again next time we make a connection.
  95.      */
  96.  
  97.     if (nread < 0) {                /* did the protocol fail? */
  98.     chmod(wrk->path, 0666);            /* make file deletable */
  99.     unlink(wrk->path);            /* remove file */
  100.     trap(E_LOST, "FAILED (lost link)");    /* handle exception */
  101.     /* NOTREACHED */
  102.     } else if (werror) {            /* file write error? */
  103.     chmod(wrk->path, 0666);            /* make file deletable */
  104.     unlink(wrk->path);            /* remove file */
  105.     trap(E_WRITERR, "FILE WRITE ERROR (%s)", sys_errlist[errno]);
  106.     /* NOTREACHED */
  107.     } else {
  108.     chmod(wrk->path, 0444);            /* protect the file */
  109.     }
  110. }
  111.