home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff294.lzh / DNet / unix / client / putfiles.c < prev   
C/C++ Source or Header  |  1989-12-11  |  4KB  |  199 lines

  1.  
  2. /*
  3.  *  PUTFILES.C
  4.  *
  5.  *    DNET (c)Copyright 1988, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  Download one or more files from the remote computer
  8.  *
  9.  *  PUTFILES file/dir1 file/dir2 ... file/dirN
  10.  *
  11.  *  placed in directory server ran from on remote host.
  12.  */
  13.  
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <sys/dir.h>
  17. #include <stdio.h>
  18. #include <sys/file.h>
  19. #include "../server/servers.h"
  20.  
  21. char Buf[1024];
  22.  
  23. typedef struct stat STAT;
  24.  
  25. main(ac,av)
  26. char *av[];
  27. {
  28.     long chan;
  29.     long n, len, orig;
  30.     long fh;
  31.     short i, j;
  32.     char fn;
  33.  
  34.     if (ac == 1) {
  35.     puts("putfiles V1.00 (c)Copyright 1987, Matthew Dillon, All Rights Reserved");
  36.     puts("putfiles file/dir file/dir .....");
  37.     exit(1);
  38.     }
  39.  
  40.     chan = DOpen(NULL, PORT_FILECOPY, -80, 126);
  41.     if (chan < 0) {
  42.     puts("Unable to connect");
  43.     exit(1);
  44.     }
  45.     ggread(chan, &fn, 1);
  46.     if (fn != 'Y') {
  47.     puts("Remote Server Software Error");
  48.     close(chan);
  49.     }
  50.     for (i = 1; i < ac; ++i) {
  51.     if (strncmp(av[i], "-d", 2) == 0) {/*-ddir or -d dir*/
  52.         char *dir = av[i]+2;
  53.         if (*dir == 0 && i+1 < ac) {
  54.         ++i;
  55.         dir = av[i];
  56.         }
  57.         if (writehdr_nc(chan, 'C', dir, 0) != 'Y') {
  58.         puts ("unable to go to specified remote directory");
  59.         break;
  60.         }
  61.     } else {
  62.         if (putname(chan, av[i]) < 0)
  63.             break;
  64.     }
  65.     }
  66.     printf("\nclosing... ");
  67.     fflush(stdout);
  68.     close(chan);
  69.     puts("done");
  70. }
  71.  
  72. putname(chan, file)
  73. char *file;
  74. {
  75.     STAT sstat;
  76.     char svdir[256];
  77.     int ret;
  78.  
  79.     printf("%-20s ", file);
  80.     if (stat(file, &sstat) < 0) {
  81.     puts("NOT FOUND");
  82.     return(1);
  83.     }
  84.     if (sstat.st_mode & S_IFDIR) {
  85.     DIR *dir;
  86.     struct direct *de;
  87.  
  88.     getwd(svdir);
  89.     puts("DIR");
  90.     chdir(file);
  91.     if (writehdr(chan, 'X', file, 0) != 'Y') {
  92.         puts("Remote unable to make directory");
  93.         goto f1;
  94.     }
  95.     if (dir = opendir(".")) {
  96.         while (de = readdir(dir))  {
  97.         if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name,"..")==0)
  98.             continue;
  99.         if (putname(chan, de->d_name) < 0) {
  100.             ret = -1;
  101.             break;
  102.         }
  103.         }
  104.     }
  105.     writehdr(chan, 'Y', "?", 0);
  106. f1:
  107.     chdir(svdir);
  108.     } else {
  109.     ret = putfile(chan, file);
  110.     }
  111.     return(ret);
  112. }
  113.  
  114. putfile(chan, file)
  115. char *file;
  116. {
  117.     int fd = open(file, O_RDONLY);
  118.     long n, r, len;
  119.     long ttl = 0;
  120.     char co;
  121.  
  122.     fflush(stdout);
  123.     if (fd < 0) {
  124.     puts("FILE NOT FOUND");
  125.     return(0);
  126.     }
  127.     len = ttl = lseek(fd, 0, 2);
  128.     lseek(fd, 0, 0);
  129.     if (writehdr(chan, 'W', file, len) != 'Y') {
  130.     puts("REMOTE UNABLE TO ACCEPT FILE");
  131.     close(fd);
  132.     return(0);
  133.     }
  134.     printf("%6ld/%-6ld", ttl - len, ttl);
  135.     while (len) {
  136.     fflush(stdout);
  137.     r = (len > sizeof(Buf)) ? sizeof(Buf) : len;
  138.     n = read(fd, Buf, r);
  139.     if (n != r) {
  140.         puts("Local File error");
  141.         close(fd);
  142.         return(-1);
  143.     }
  144.     if (gwrite(chan, Buf, n) != n) {
  145.         puts("Remote error");
  146.         close(fd);
  147.         return(-1);
  148.     }
  149.     len -= n;
  150.     printf("\010\010\010\010\010\010\010\010\010\010\010\010\010");
  151.     printf("%6ld/%-6ld", ttl - len, ttl);
  152.     }
  153.     close(fd);
  154.     if (len) {
  155.     puts("REMOTE ERROR");
  156.     return(-1);
  157.     }
  158.     printf("  Queued, waiting... ");
  159.     fflush(stdout);
  160.     ggread(chan, &co, 1);
  161.     if (co != 'Y') {
  162.     puts("Remote Server Software Error");
  163.     return(-1);
  164.     }
  165.     puts("OK");
  166.     return(0);
  167. }
  168.  
  169. writehdr(chan, c, name, len)
  170. unsigned char c;
  171. char *name;
  172. long len;
  173. {
  174.     short i;
  175.     for (i = strlen(name) - 1; i >= 0; --i) {
  176.     if (name[i] == '/' || name[i] == ':')
  177.         break;
  178.     }
  179.     name += i + 1;
  180.     return(writehdr_nc(chan, c, name, len));
  181. }
  182.  
  183. writehdr_nc(chan, c, name, len)
  184. unsigned char c;
  185. char *name;
  186. long len;
  187. {
  188.     gwrite(chan, &c, 1);
  189.     c = strlen(name) + 1;
  190.     gwrite(chan, &c, 1);
  191.     gwrite(chan, name, c);
  192.     len = htonl68(len);
  193.     gwrite(chan, &len, 4);
  194.     if (ggread(chan, &c, 1) == 1)
  195.     return(c);
  196.     return(-1);
  197. }
  198.  
  199.