home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / general / getwd.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  5KB  |  211 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Ray Burr
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Written and Copyright by Ray Burr. 
  21.  * Put under the GNU Library General Public License.
  22.  * Thanks Ray !
  23.  */
  24.  
  25. #define _KERNEL
  26. #include "ixemul.h"
  27. #include "kprintf.h"
  28. #include <string.h>
  29.  
  30. #ifndef MAXPATHLEN
  31. #define MAXPATHLEN 1024
  32. #endif
  33.  
  34. /* _GetPathFromLock - Given a pointer to an AmigaOS Lock structure, build
  35.    a rooted path string in a buffer of a given size.  The buffer should be
  36.    at least 'buffer_length' bytes.  Returns a pointer to the result
  37.    or NULL on an error.  'errno' will be set in the case of an error.  This
  38.    function returns the path string based at 'buffer[0]' but it can alter
  39.    any part of the buffer.  */
  40.  
  41. static char *
  42. _GetPathFromLock (BPTR lock, char *buffer, int buffer_length)
  43. {
  44.   char *p;
  45.   BPTR fl, next_fl;
  46.   int length;
  47.   struct FileInfoBlock *fib;
  48.   int omask;
  49.   char *result = 0;
  50.  
  51.   /* Allocate space on stack for fib. */
  52.  
  53.   BYTE fib_Block[sizeof (struct FileInfoBlock) + 2];
  54.  
  55.   /* Make sure fib is longword aligned. */
  56.  
  57.   fib = (struct FileInfoBlock *) fib_Block;
  58.   if (((ULONG) fib & 0x02) != 0)
  59.     fib = (struct FileInfoBlock *) ((ULONG) fib + 2);
  60.  
  61.   p = 0L;
  62.  
  63.   /* Duplicate the lock so that the directory structure can't change
  64.      while we're doing this. */
  65.  
  66.   omask = syscall (SYS_sigsetmask, ~0);
  67.   fl = DupLock (lock);
  68.  
  69.   /* Follow the chain of directories and build the name in 'buffer' */
  70.  
  71.   while (fl != 0L)
  72.     {
  73.       if (Examine (fl, fib) == DOSFALSE)
  74.     {
  75.       errno = __ioerr_to_errno(IoErr());
  76.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  77.       UnLock (fl);
  78.       goto ret;
  79.     }
  80.       next_fl = ParentDir (fl);
  81.       UnLock (fl);
  82.       if (p != 0L)
  83.     {
  84.       if (next_fl != 0L)
  85.         *--p = '/';
  86.       else
  87.         *--p = ':';
  88.     }
  89.       else
  90.     {
  91.       p = buffer + buffer_length - 1;    /* fix 20-jan-92 ## mw */
  92.       *p = '\0';
  93.       if (next_fl == 0L)
  94.         *--p = ':';
  95.     }
  96.       length = strlen (fib->fib_FileName);
  97.       p -= length;
  98.       if (p <= buffer)
  99.     {
  100.       if (next_fl != 0L)
  101.         UnLock (next_fl);
  102.       goto ret;
  103.     }
  104.       bcopy (fib->fib_FileName, p, length);
  105.       fl = next_fl;
  106.     }
  107.  
  108.   /* Move the pathname so that it starts at buffer[0]. */
  109.  
  110.   bcopy (p, buffer, strlen (p) + 1);
  111.  
  112.   result = buffer;
  113.  
  114. ret:
  115.   syscall (SYS_sigsetmask, omask);
  116.   return result;
  117. }
  118.  
  119.  
  120. static char *
  121. _get_pwd (char *buffer, int buffer_length)
  122. {
  123.   struct Process *proc;
  124.   char *result, *colon;
  125.   extern char *index (const char *, int);
  126.  
  127.   if (u.u_is_root)
  128.     {
  129.       strcpy(buffer, "/");
  130.       return buffer;
  131.     }
  132.  
  133.   proc = (struct Process *) FindTask (0L);
  134.  
  135.   /* Just return an empty string if this is not a process. */
  136.  
  137.   if (proc == 0L || proc->pr_Task.tc_Node.ln_Type != NT_PROCESS)
  138.     {
  139.       buffer[0] = '\0';
  140.       return buffer;
  141.     }
  142.  
  143.   /* make room for slash */
  144.   if (ix.ix_flags & ix_translate_slash)
  145.     buffer++;
  146.  
  147.   if (GetCurrentDirName (buffer, buffer_length) ||
  148.       NameFromLock (proc->pr_CurrentDir, buffer, buffer_length))
  149.     {
  150.       result = buffer;
  151.       goto returnit;
  152.     }
  153.   /* and as the last chance resort to the 1.3 algorithm */
  154.  
  155.   result = _GetPathFromLock (proc->pr_CurrentDir, buffer, buffer_length);
  156.  
  157. returnit:
  158.   if ((ix.ix_flags & ix_translate_slash) && result)
  159.     {
  160.       colon = index (result, ':');
  161.       if (colon)
  162.         {
  163.       *colon = '/';
  164.           result--;
  165.           result[0] = '/';
  166.       return result;
  167.         }
  168.  
  169.        bcopy (result, result - 1, strlen (result) + 1);
  170.        return result - 1;
  171.     }
  172.  
  173.   return result;
  174. }
  175.  
  176.  
  177. char *
  178. getwd (char *buffer)
  179. {
  180.   char *path;
  181.  
  182.   path = _get_pwd (buffer, MAXPATHLEN);
  183.   if (path == 0L)
  184.     {
  185.       strcpy (buffer, "getwd - ");
  186.       strcat (buffer, strerror (errno));
  187.       return 0L;
  188.     }
  189.  
  190.   return path;
  191. }
  192.  
  193.  
  194. char *
  195. getcwd (char *buffer, size_t buffer_length)
  196. {
  197.  
  198.   if (buffer == 0L)
  199.     {
  200.       buffer = (char *) syscall (SYS_malloc, buffer_length + 2);
  201.       if (buffer == 0L)
  202.     {
  203.       errno = ENOMEM;
  204.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  205.       return 0L;
  206.     }
  207.     }
  208.  
  209.   return _get_pwd (buffer, buffer_length);
  210. }
  211.