home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / FTPMOUNT.LZX / FTPMount-0.7 / Source / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-06  |  4.5 KB  |  264 lines

  1. /*
  2.  * This source file is Copyright 1995 by Evan Scott.
  3.  * All rights reserved.
  4.  * Permission is granted to distribute this file provided no
  5.  * fees beyond distribution costs are levied.
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <exec/alerts.h>
  11.  
  12. #include <dos/dos.h>
  13. #include <dos/dosextens.h>
  14. #include <dos/dostags.h>
  15.  
  16. #include <proto/exec.h>
  17. #include <proto/dos.h>
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "evtypes.h"
  24. #include "verify.h"
  25. #include "tcp.h"
  26.  
  27. #include "site.h"
  28. #include "ftp.h"
  29. #include "split.h"
  30.  
  31. #include "globals.h"
  32.  
  33. boolean collapse(b8 *s)
  34. {
  35.     b8 *t, *u;
  36.     
  37.     t = s;
  38.     u = s;
  39.     
  40.     /* . & .. are illegal */
  41.     if (u[0] == '.') {
  42.         if (u[1] == 0) return false;
  43.         if (u[1] == '/') return false;
  44.         if (u[1] == '.') {
  45.             if (u[2] == 0) return false;
  46.             if (u[2] == '/') return false;
  47.         }
  48.     }
  49.     
  50.     while (*u) {
  51.         if (t == s && u[0] == '/') return false;
  52.         
  53.         if (u[0] == '/' && u[1] == '.') {
  54.             if (u[2] == 0) return false;
  55.             if (u[2] == '/') return false;
  56.             if (u[2] == '.') {
  57.                 if (u[3] == 0) return false;
  58.                 if (u[3] == '/') return false;
  59.             }
  60.         }
  61.         
  62.         if (u[0] == '/' && u[1] == '/') {
  63.             while (--t > s) {
  64.                 if (*t == '/') break;
  65.             }
  66.             u++;
  67.             if (t == s) u++;
  68.         } else {
  69.             *t++ = *u++;
  70.         }
  71.     }
  72.     
  73.     if (t > s && t[-1] == '/') t[-1] = 0;    /* cull the trailing '/' */
  74.     
  75.     *t = 0;
  76.     
  77.     return true;
  78. }
  79.  
  80. boolean split_data(lock *l, b8 *z, split *sd)
  81. {
  82.     b8 *s, *t;
  83.     int len1, len2, len3;
  84.     /* 
  85.      * sigh ... all that work to separate the libs to processes ... just
  86.      * given up and used the global DosBase here
  87.      */
  88.     struct DevProc *dp;
  89.     
  90.     truth(z != nil);
  91.     
  92.     if (!l && z[0] == 0) {
  93.         
  94.         sd->port = local_port;
  95.         sd->path = nil;
  96.         
  97.         sd->work = nil;
  98.  
  99.         return true;
  100.     }
  101.     
  102.     s = (b8 *)allocate(z[0] + 1, V_cstr);
  103.     if (!s) {
  104.         sd->work = nil;
  105.         return false;
  106.     }
  107.     
  108.     if (z[0])
  109.         memcpy(s, &z[1], z[0]);
  110.  
  111.     s[z[0]] = 0;
  112.     
  113.     sd->work = s;
  114.  
  115.     for (; ; s++) {    
  116.         if (!*s) {
  117.             z = sd->work;
  118.             break;
  119.         }
  120.         
  121.         if (*s == ':') {
  122.             s++;
  123.             dp = GetDeviceProc(sd->work, nil);
  124.             if (!dp) {
  125.                 deallocate(sd->work, V_cstr);
  126.                 sd->work = nil;
  127.                 return false;
  128.             }
  129.             
  130.             l = (lock *)(dp->dvp_Lock << 2);
  131.             
  132.             FreeDeviceProc(dp);
  133.             
  134.             z = s;
  135.             
  136.             break;
  137.         }
  138.     }
  139.     
  140.     /* have to look at lock to see where we are relative to */
  141.     
  142.     if (!l || (l->port == local_port && l->rfsl == ftphosts_lock)) {
  143.         s = z;
  144.         goto resolve;
  145.     }
  146.     
  147.     verify(l, V_lock);
  148.     
  149.     if (l->port == local_port) {    /* most handlers might allow this, but there's no point */
  150.         show_string("Split Fail B");
  151.         deallocate(sd->work, V_cstr);
  152.         sd->work = nil;
  153.         return false;
  154.     }
  155.     
  156.     /* construct a full path name so we can collapse it sensibly */
  157.     
  158.     if (l->fname[0] == 0) {
  159.         len1 = strlen(l->port->mp_Node.ln_Name);
  160.         len2 = strlen(z);
  161.         
  162.         s = (b8 *)allocate(len1 + len2 + 2, V_cstr);
  163.         if (!s) {
  164.             deallocate(sd->work, V_cstr);
  165.             sd->work = nil;
  166.             return false;
  167.         }
  168.         
  169.         strcpy(s, l->port->mp_Node.ln_Name);
  170.         
  171.         if (sd->work[0]) {
  172.             s[len1] = '/';
  173.             strcpy(&s[len1 + 1], z);
  174.         } else {
  175.             s[len1] = 0;
  176.         }
  177.         
  178.         deallocate(sd->work, V_cstr);
  179.         sd->work = s;
  180.     } else {
  181.         len1 = strlen(l->port->mp_Node.ln_Name);
  182.         len2 = strlen(l->fname);
  183.         len3 = strlen(z);
  184.         
  185.         s = (b8 *)allocate(len1 + len2 + len3 + 3, V_cstr);
  186.         if (!s) {
  187.             deallocate(sd->work, V_cstr);
  188.             sd->work = nil;
  189.             return false;
  190.         }
  191.         
  192.         strcpy(s, l->port->mp_Node.ln_Name);
  193.         s[len1] = '/';
  194.         strcpy(&s[len1 + 1], l->fname);
  195.         if (sd->work[0]) {
  196.             s[len1 + len2 + 1] = '/';
  197.             strcpy(&s[len1 + len2 + 2], z);
  198.         } else {
  199.             s[len1 + len2 + 1] = 0;
  200.         }
  201.         deallocate(sd->work, V_cstr);
  202.         sd->work = s;
  203.     }
  204.     
  205. resolve:
  206.     if (!collapse(s)) {
  207.         deallocate(sd->work, V_cstr);
  208.         sd->work = nil;
  209.         return false;
  210.     }
  211.     
  212.     for (t = s; *t; t++) {
  213.         if (*t == '/') {
  214.             *t++ = 0;
  215.             sd->port = get_site(s);
  216.             if (!sd->port) {
  217.                 deallocate(sd->work, V_cstr);
  218.                 sd->work = nil;
  219.                 return false;
  220.             }
  221.             if (t[0])
  222.                 sd->path = t;
  223.             else
  224.                 sd->path = nil;
  225.             return true;
  226.         }
  227.     }
  228.     
  229.     if (s[0] == 0) {
  230.         deallocate(sd->work, V_cstr);
  231.         sd->work = nil;
  232.         sd->path = nil;
  233.         sd->port = local_port;
  234.         return true;
  235.     }
  236.     
  237.     /* ok, we don't have a '/' so check for .info or Unnamed or
  238.        Disk ... they are "special" */
  239.     if (stricmp(s, "Disk") == 0 ||
  240.             strnicmp(s, "Unnamed", 7) == 0 ||
  241.             stricmp(s, ".backdrop") == 0 ||
  242.             stricmp(&s[strlen(s) - 5], ".info") == 0) {
  243.         sd->port = local_port;
  244.         sd->path = s;
  245.         return true;
  246.     }
  247.     
  248.     sd->port = get_site(s);
  249.     deallocate(sd->work, V_cstr);
  250.     sd->work = nil;
  251.     sd->path = nil;
  252.     
  253.     if (!sd->port) return false;
  254.     return true;
  255. }
  256.  
  257. void end_split(split *sd)
  258. {
  259.     if (sd->work) {
  260.         deallocate(sd->work, V_cstr);
  261.         sd->work = nil;
  262.     }
  263. }
  264.