home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / gen_library / rcs / getwd.c,v < prev    next >
Encoding:
Text File  |  1992-08-09  |  5.7 KB  |  288 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     version39-41:1.1;
  5. locks;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    92.08.09.21.05.24;    author amiga;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    92.06.08.18.31.20;    author mwild;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @initial checkin
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @change to 2.x header files
  28. @
  29. text
  30. @/*
  31.  *  This file is part of ixemul.library for the Amiga.
  32.  *  Copyright (C) 1991, 1992  Ray Burr
  33.  *
  34.  *  This library is free software; you can redistribute it and/or
  35.  *  modify it under the terms of the GNU Library General Public
  36.  *  License as published by the Free Software Foundation; either
  37.  *  version 2 of the License, or (at your option) any later version.
  38.  *
  39.  *  This library is distributed in the hope that it will be useful,
  40.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  42.  *  Library General Public License for more details.
  43.  *
  44.  *  You should have received a copy of the GNU Library General Public
  45.  *  License along with this library; if not, write to the Free
  46.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  47.  */
  48.  
  49. /* Written and Copyright by Ray Burr. 
  50.  * Put under the GNU Library General Public License.
  51.  * Thanks Ray !
  52.  */
  53.  
  54. #define KERNEL
  55. #include "ixemul.h"
  56.  
  57. extern int _dos20;
  58.  
  59. #ifndef MAXPATHLEN
  60. #define MAXPATHLEN 1024
  61. #endif
  62.  
  63. /* _GetPathFromLock - Given a pointer to an AmigaDOS Lock structure, build
  64.    a rooted path string in a buffer of a given size.  The buffer should be
  65.    at least 'buffer_length' bytes.  Returns a pointer to the result
  66.    or NULL on an error.  'errno' will be set in the case of an error.  This
  67.    function returns the path string based at 'buffer[0]' but it can alter
  68.    any part of the buffer.  */
  69.  
  70. static char *
  71. _GetPathFromLock (BPTR lock, char *buffer, int buffer_length)
  72. {
  73.   char *p;
  74.   BPTR fl, next_fl;
  75.   int length;
  76.   struct FileInfoBlock *fib;
  77.   int omask;
  78.   char *result = 0;
  79.  
  80.   /* Allocate space on stack for fib. */
  81.  
  82.   BYTE fib_Block[sizeof (struct FileInfoBlock) + 2];
  83.  
  84.   /* Make sure fib is longword aligned. */
  85.  
  86.   fib = (struct FileInfoBlock *) fib_Block;
  87.   if (((ULONG) fib & 0x02) != 0)
  88.     fib = (struct FileInfoBlock *) ((ULONG) fib + 2);
  89.  
  90.   p = 0L;
  91.  
  92.   /* Duplicate the lock so that the directory structure can't change
  93.      while we're doing this. */
  94.  
  95.   omask = syscall (SYS_sigsetmask, ~0);
  96.   fl = DupLock (lock);
  97.  
  98.   /* Follow the chain of directories and build the name in 'buffer' */
  99.  
  100.   while (fl != 0L)
  101.     {
  102.       if (Examine (fl, fib) == DOSFALSE)
  103.     {
  104.       errno = __ioerr_to_errno(IoErr());
  105.       UnLock (fl);
  106.       goto ret;
  107.     }
  108.       next_fl = ParentDir (fl);
  109.       UnLock (fl);
  110.       if (p != 0L)
  111.     {
  112.       if (next_fl != 0L)
  113.         *--p = '/';
  114.       else
  115.         *--p = ':';
  116.     }
  117.       else
  118.     {
  119.       p = buffer + buffer_length - 1;    /* fix 20-jan-92 ## mw */
  120.       *p = '\0';
  121.       if (next_fl == 0L)
  122.         *--p = ':';
  123.     }
  124.       length = strlen (fib->fib_FileName);
  125.       p -= length;
  126.       if (p <= buffer)
  127.     {
  128.       if (next_fl != 0L)
  129.         UnLock (next_fl);
  130.       goto ret;
  131.     }
  132.       bcopy (fib->fib_FileName, p, length);
  133.       fl = next_fl;
  134.     }
  135.  
  136.   /* Move the pathname so that it starts at buffer[0]. */
  137.  
  138.   bcopy (p, buffer, strlen (p) + 1);
  139.  
  140.   result = buffer;
  141.  
  142. ret:
  143.   syscall (SYS_sigsetmask, omask);
  144.   return result;
  145. }
  146.  
  147.  
  148. static char *
  149. _get_pwd (char *buffer, int buffer_length)
  150. {
  151.   struct Process *proc;
  152.   char *result, *colon;
  153.  
  154.   proc = (struct Process *) FindTask (0L);
  155.  
  156.   /* Just return an empty string if this is not a process. */
  157.  
  158.   if (proc == 0L || proc->pr_Task.tc_Node.ln_Type != NT_PROCESS)
  159.     {
  160.       buffer[0] = '\0';
  161.       return buffer;
  162.     }
  163.  
  164.   /* make room for slash */
  165.   if (ix.ix_translate_slash)
  166.     buffer++;
  167.  
  168.   if (_dos20)
  169.     {
  170.       if (GetCurrentDirName (buffer, buffer_length) ||
  171.           NameFromLock (proc->pr_CurrentDir, buffer, buffer_length))
  172.         {
  173.           errno = 0;
  174.           result = buffer;
  175.           goto returnit;
  176.         }
  177.       /* and as the last chance resort to the 1.3 algorithm */
  178.     }
  179.  
  180.   result = _GetPathFromLock (proc->pr_CurrentDir, buffer, buffer_length);
  181.  
  182. returnit:
  183.   if (ix.ix_translate_slash && result)
  184.     {
  185.       if (colon = index (result, ':'))
  186.         {
  187.       *colon = '/';
  188.           result--;
  189.           result[0] = '/';
  190.       return result;
  191.         }
  192.  
  193.        bcopy (result, result - 1, strlen (result) + 1);
  194.        return result - 1;
  195.     }
  196.  
  197.   return result;
  198. }
  199.  
  200.  
  201. char *
  202. getwd (char *buffer)
  203. {
  204.   char *path;
  205.  
  206.   path = _get_pwd (buffer, MAXPATHLEN);
  207.   if (path == 0L)
  208.     {
  209.       strcpy (buffer, "getwd - ");
  210.       strcat (buffer, strerror (errno));
  211.       return 0L;
  212.     }
  213.  
  214.   return path;
  215. }
  216.  
  217.  
  218. char *
  219. getcwd (char *buffer, int buffer_length)
  220. {
  221.  
  222.   if (buffer == 0L)
  223.     {
  224.       buffer = syscall (SYS_malloc, buffer_length + 2);
  225.       if (buffer == 0L)
  226.     {
  227.       errno = ENOMEM;
  228.       return 0L;
  229.     }
  230.     }
  231.  
  232.   return _get_pwd (buffer, buffer_length);
  233. }
  234. @
  235.  
  236.  
  237. 1.1
  238. log
  239. @Initial revision
  240. @
  241. text
  242. @a33 44
  243. #define BASE_EXT_DECL
  244. #define BASE_PAR_DECL    
  245. #define BASE_PAR_DECL0    
  246. #define BASE_NAME    ix.ix_dos_base
  247.  
  248. __inline static LONG NameFromLock(BASE_PAR_DECL BPTR lock, UBYTE* buffer, long int len)
  249. {
  250.     BASE_EXT_DECL
  251.     register LONG res __asm("d0");
  252.     register void *a6 __asm ("a6");
  253.     register BPTR d1 __asm("d1");
  254.     register UBYTE* d2 __asm("d2");
  255.     register long int d3 __asm("d3");
  256.  
  257.     a6 = BASE_NAME;
  258.     d1 = lock;
  259.     d2 = buffer;
  260.     d3 = len;
  261.     __asm volatile ("
  262.     jsr a6@@(-0x192)"
  263.     : "=r" (res)
  264.     : "r" (a6), "r" (d1), "r" (d2), "r" (d3)
  265.     : "d0", "d1", "a0", "a1", "d2", "d3");
  266.     return res;
  267. }
  268. __inline static BOOL GetCurrentDirName(BASE_PAR_DECL UBYTE* buf, long int len)
  269. {
  270.     BASE_EXT_DECL
  271.     register BOOL res __asm("d0");
  272.     register void *a6 __asm ("a6");
  273.     register UBYTE* d1 __asm("d1");
  274.     register long int d2 __asm("d2");
  275.  
  276.     a6 = BASE_NAME;
  277.     d1 = buf;
  278.     d2 = len;
  279.     __asm volatile ("
  280.     jsr a6@@(-0x234)"
  281.     : "=r" (res)
  282.     : "r" (a6), "r" (d1), "r" (d2)
  283.     : "d0", "d1", "a0", "a1", "d2");
  284.     return res;
  285. }
  286.  
  287. @
  288.