home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / shell / execute_upd.lha / Execute / wedge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-28  |  3.4 KB  |  181 lines

  1. /*
  2.  * Routine to convert a UNIX-style path to an AmigaDOS path.
  3.  * A UNIX-style path is understood here as follows:
  4.  * 
  5.  *      leading . means current dir; like "", but filled in explicitly
  6.  *      ../ means /
  7.  *      ..  means /
  8.  * 
  9.  * Written as part of UnixDirs2, a (to be written) system patch which allows
  10.  * use of UNIX-style paths everywhere.
  11.  * 
  12.  * Martin W. Scott,  8 January 1993
  13.  */
  14. #include <exec/types.h>
  15. #include <exec/execbase.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <pragmas/dos_lib.h>
  21. #include <pragmas/exec_lib.h>
  22.  
  23. #include <string.h>
  24. #include <stdio.h>
  25.  
  26. extern struct ExecBase *SysBase;
  27.  
  28. /* insert $cwd into s, return pointer to next free char */
  29. static char *
  30. insertcwd (char *s, LONG len)
  31. {
  32.   /* geta4 (); */
  33.  
  34.   if (NameFromLock (((struct Process *) (SysBase->ThisTask))->pr_CurrentDir, s, len))
  35.     {
  36.       while (*s)
  37.     s++;
  38.     }
  39.   return s;
  40. }
  41.  
  42. /* adjust path from UNIX-style to Amiga-style */
  43. /* TO DO: length checking when building new path */
  44. static BOOL
  45. adjustpath (char *path, char *newpath, LONG len)
  46. {
  47.   char tmp_buf[512] = "", *s = tmp_buf, *t;
  48.  
  49.   if (path == NULL)        /* bypass */
  50.     return FALSE;
  51.  
  52.   if (t = strchr (path, ':'))    /* check for ':' in path */
  53.     {
  54.       t++;            /* copy device component */
  55.       while (path < t)
  56.     *s++ = *path++;
  57.     }
  58.   else if (path[0] == '/')
  59.     {
  60.       path++;
  61.  
  62.       if (t = strchr (path, '/'))
  63.     {
  64.       while (path < t)
  65.         *s++ = *path++;
  66.  
  67.       *s++ = ':', path++;
  68.     }
  69.       else
  70.     {
  71.       while (*path)
  72.         *s++ = *path++;
  73.  
  74.       *s++ = ':';
  75.     }
  76.     }
  77.   else if (path[0] == '.')
  78.     {
  79.       if (!path[1])        /* only "." */
  80.     {
  81.       s = insertcwd (s, len);
  82.       path++;        /* path[0] == '\0' - STOP */
  83.     }
  84.       else if (path[1] == '/')    /* initial component is $cwd */
  85.     {
  86.       s = insertcwd (s, len);
  87.       if (*(s - 1) != ':')
  88.         *s++ = '/';        /* copy '/', increment pointers */
  89.       path += 2;
  90.     }
  91.     }
  92.  
  93.   while (path[0])
  94.     {
  95.       if (path[0] == '.' && path[1] == '.')
  96.     {
  97.       if (path[2] == '/')    /* just skip "..", copying '/' */
  98.         {
  99.           path += 2;
  100.           *s++ = *path++;
  101.         }
  102.       else if (!path[2])    /* append '/' and stop */
  103.         {
  104.           path += 2;
  105.           *s++ = '/';
  106.         }
  107.       else
  108.         *s++ = *path++;
  109.     }
  110.       else
  111.     *s++ = *path++;
  112.     }
  113.   *s = '\0';
  114.  
  115.   strcpy (newpath, tmp_buf);
  116.  
  117.   return TRUE;
  118. }
  119.  
  120. int
  121. wedge ()
  122. {
  123.   int retval = 0;
  124.  
  125.   char *copy_of_argstring;
  126.  
  127.   if (copy_of_argstring = strdup (GetArgStr ()))
  128.     {
  129.       BPTR lock;
  130.  
  131.       /*
  132.        * buffers should be big enouth to handle all but the most 
  133.        * extreme of extreme cases
  134.        */
  135.       char tmp_buf1[512] = "", tmp_buf2[512] = "", *ptr = copy_of_argstring;
  136.  
  137.       /*
  138.        * argstring is terminated with a newline character for the 
  139.        * benifit of ReadArgs().
  140.        */
  141.       if (strlen (copy_of_argstring) == 1)
  142.     {
  143.       free (copy_of_argstring);
  144.       return (retval);
  145.     }
  146.  
  147.       if (lock = Lock (strcpy (tmp_buf1, strsep (&ptr, " \t\n")), SHARED_LOCK))
  148.     {
  149.       if (NameFromLock (lock, tmp_buf1, 512))
  150.         {
  151.           FILE *fd;
  152.  
  153.           if (fd = fopen (tmp_buf1, "r"))
  154.         {
  155.           fgets (tmp_buf2, 512, fd);
  156.  
  157.           if ((tmp_buf2[0] == '#') && (tmp_buf2[1] == '!'))
  158.             {
  159.               adjustpath (strtok (&tmp_buf2[2], " \t\n"), tmp_buf2, 512);
  160.  
  161.               {
  162.             char command[512];
  163.  
  164.             sprintf (command, "\"%s\" \"%s\" %s", tmp_buf2, tmp_buf1, ptr);
  165.  
  166.             SystemTags (command, 0L);
  167.               }
  168.  
  169.               retval = 1;
  170.             }
  171.  
  172.           fclose (fd);
  173.         }
  174.         }
  175.       UnLock (lock);
  176.     }
  177.       free (copy_of_argstring);
  178.     }
  179.   return (retval);
  180. }
  181.