home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 239.lha / amiga / src / client / putfiles.c < prev    next >
C/C++ Source or Header  |  1989-05-02  |  4KB  |  187 lines

  1.  
  2. /*
  3.  *  PUTFILES.C
  4.  *
  5.  *  DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved.
  6.  *
  7.  *  Upload one or more files or directories to the remote host
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include "/server/servers.h"
  12. #include <local/typedefs.h>
  13.  
  14. int Enable_Abort;
  15. char Buf[1024];
  16.  
  17. main(ac,av)
  18. char *av[];
  19. {
  20.     long chan;
  21.     long n, len, orig;
  22.     long fh;
  23.     short i, j;
  24.     char fn = 0;
  25.     char *host = NULL;
  26.     char *dir = NULL;
  27.  
  28.     printf("PutFiles V%s%s\n", VERSION, PUTFILES_VERSION);
  29.     ac = DoOption(ac, av, "N%sd%s", &host, &dir);
  30.  
  31.     if (ac <= 1) {
  32.     puts("putfiles [-Nnetid] [-dremotedir] file/dir file/dir ....");
  33.     exit(1);
  34.     }
  35.     Enable_Abort = 0;
  36.     chan = DOpen(host, PORT_FILECOPY, -80, 126);
  37.     if (!chan) {
  38.     puts("Unable to connect");
  39.     exit(1);
  40.     }
  41.     DRead(chan, &fn, 1);
  42.     if (fn == 'S') {
  43.     puts("Remote Server Permission Denied");
  44.     DClose(chan);
  45.     exit(1);
  46.     }
  47.     if (fn != 'Y') {
  48.     puts("Remote Server Software Error");
  49.     DClose(chan);
  50.     exit(1);
  51.     }
  52.     if (dir) {
  53.     if (writehdr(chan, 'C', dir, 0) != 'Y') {
  54.         printf("Remote directory unavailable: %s\n", dir);
  55.         goto fail;
  56.     }
  57.     }
  58.     for (i = 1; i < ac; ++i) {
  59.     if (putname(chan, av[i]) < 0)
  60.         break;
  61.     }
  62. fail:
  63.     printf("\nclosing... ");
  64.     fflush(stdout);
  65.     DClose(chan);
  66.     puts("done");
  67. }
  68.  
  69.  
  70. putname(chan, file)
  71. char *file;
  72. {
  73.     long lock = Lock(file, SHARED_LOCK);
  74.     long dirl;
  75.     int ret = 1;
  76.     FIB *fib = (FIB *)malloc(sizeof(FIB));
  77.  
  78.     printf("%-20s ", file);
  79.     if (lock == NULL || !Examine(lock, fib)) {
  80.     if (lock)
  81.         UnLock(lock);
  82.     free(fib);
  83.     puts("NOT FOUND");
  84.     return(1);
  85.     }
  86.     if (fib->fib_DirEntryType > 0) {
  87.     char *dirname = (fib->fib_FileName[0]) ? fib->fib_FileName : "ram";
  88.     puts("DIR");
  89.     dirl = CurrentDir(lock);
  90.     if (writehdr(chan, 'X', dirname, 0) != 'Y') {
  91.         puts("Remote unable to make directory");
  92.         goto f1;
  93.     }
  94.     while (ExNext(lock, fib)) {
  95.         if (putname(chan, fib->fib_FileName) < 0) {
  96.         ret = -1;
  97.         goto f1;
  98.         }
  99.     }
  100.     writehdr(chan, 'Y', "?", 0);
  101. f1:
  102.     UnLock(CurrentDir(dirl));
  103.     } else {
  104.     UnLock(lock);
  105.     ret = putfile(chan, file, fib->fib_FileName);
  106.     }
  107.     free(fib);
  108.     return(ret);
  109. }
  110.  
  111. putfile(chan, file, stripedname)
  112. char *file;
  113. char *stripedname;
  114. {
  115.     long fh = Open(file, 1005);
  116.     long n, r, len;
  117.     long ttl = 0;
  118.     char co;
  119.  
  120.     fflush(stdout);
  121.     if (fh == NULL) {
  122.     puts("FILE NOT FOUND");
  123.     return(0);
  124.     }
  125.     Seek(fh, 0, 1);                 /*  get length */
  126.     len = ttl = Seek(fh, 0, -1);
  127.     if (writehdr(chan, 'W', stripedname, len) != 'Y') {
  128.     puts("REMOTE UNABLE TO ACCEPT FILE");
  129.     Close(fh);
  130.     return(0);
  131.     }
  132.     printf("%6ld/%-6ld", ttl - len, ttl);
  133.     while (len) {
  134.     fflush(stdout);
  135.     r = (len > sizeof(Buf)) ? sizeof(Buf) : len;
  136.     n = Read(fh, Buf, r);
  137.     if (n != r) {
  138.         puts("Local File error");
  139.         Close(fh);
  140.         return(-1);
  141.     }
  142.     if (DWrite(chan, Buf, n) != n) {
  143.         puts("Remote error");
  144.         Close(fh);
  145.         return(-1);
  146.     }
  147.     if (SetSignal(0,0) & (SIGBREAKF_CTRL_C|SIGBREAKF_CTRL_D)) {
  148.         puts("\nBreak");
  149.         Close(fh);
  150.         return(-1);
  151.     }
  152.     len -= n;
  153.     printf("\010\010\010\010\010\010\010\010\010\010\010\010\010");
  154.     printf("%6ld/%-6ld", ttl - len, ttl);
  155.     }
  156.     Close(fh);
  157.     printf("Queued, waiting...");
  158.     fflush(stdout);
  159.     DRead(chan, &co, 1);
  160.     if (co != 'Y') {
  161.     puts("Remote Server Software Error");
  162.     return(-1);
  163.     }
  164.     puts("  OK");
  165.     return(0);
  166. }
  167.  
  168. writehdr(chan, c, name, len)
  169. unsigned char c;
  170. char *name;
  171. long len;
  172. {
  173.     if (DWrite(chan, &c, 1) != 1)
  174.     return(-1);
  175.     c = strlen(name) + 1;
  176.     if (DWrite(chan, &c, 1) != 1)
  177.     return(-1);
  178.     if (DWrite(chan, name, c) != c)
  179.     return(-1);
  180.     if (DWrite(chan, &len, 4) != 4)
  181.     return(-1);
  182.     if (DRead(chan, &c, 1) == 1)
  183.     return(c);
  184.     return(-1);
  185. }
  186.  
  187.