home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / pcmail / part06 / getwork.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  2.7 KB  |  104 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. /*    Mon Apr  4 23:40:43 MET 1988
  46. /* VERSION/RELEASE
  47. /*    1.3
  48. /*--*/
  49.  
  50. #include "defs.h"
  51. #include "logs.h"
  52. #include "status.h"
  53. #include "work.h"
  54. #include "params.h"
  55. #include "comm.h"
  56.  
  57. /* rmtwork - parse remote S request and open destination file */
  58.  
  59. public work *rmtwork(rqst)
  60. char *rqst;
  61. {
  62.     static work wrk;
  63.     char path[BUFSIZ];
  64.  
  65.     if (sscanf(rqst,"S %*s %s",path) != 1)    /* pick destination file name */
  66.     trap(E_LOST,"BAD REQUEST FORMAT");
  67.     debug(5)("rmtwork: path %s\n",path);
  68.     strcpy(wrk.path,locname(path));        /* convert to local name */
  69.     debug(5)("rmtwork: file %s\n",wrk.path);
  70.     if ((wrk.fp = fopen(wrk.path,"w")) == 0)    /* try to open that file */
  71.     trap(E_WRITERR,"CAN'T CREATE FILE (%s)",sys_errlist[errno]);
  72.     return(&wrk);
  73. }
  74.  
  75. /* getwork - receive file from remote host */
  76.  
  77. public void getwork(wrk)
  78. register work *wrk;
  79. {
  80.     char buf[BUFSIZ];
  81.     register int nread;
  82.     register int werror;
  83.  
  84.     while ((nread = CALL(Read)(ttfd,buf,BUFSIZ)) > 0 &&
  85.     fwrite(buf,sizeof(*buf),nread,wrk->fp) == nread)
  86.     /* void */ ;
  87.     werror = ferror(wrk->fp);            /* record error status */
  88.     fclose(wrk->fp);
  89.  
  90.     if (nread < 0) {                /* did the protocol fail? */
  91.     chmod(wrk->path,0666);            /* make file deletable */
  92.     unlink(wrk->path);            /* remove file */
  93.     trap(E_LOST,"FAILED (lost link)");    /* handle exception */
  94.     /* NOTREACHED */
  95.     } else if (werror) {            /* file write error? */
  96.     chmod(wrk->path,0666);            /* make file deletable */
  97.     unlink(wrk->path);            /* remove file */
  98.     trap(E_WRITERR,"FILE WRITE ERROR (%s)",sys_errlist[errno]);
  99.     /* NOTREACHED */
  100.     } else {
  101.     chmod(wrk->path,0444);            /* protect the file */
  102.     }
  103. }
  104.