home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38C.ZIP / MAC / DIRECTRY.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  8KB  |  271 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/mac/directry.c,v 1.1 1992/01/24 03:29:45 dvadura Exp $
  2. -- SYNOPSIS -- Fake directory and file functions for the Mac
  3. --
  4. -- DESCRIPTION
  5. --  This file contains implementations for some ANSI standard routines dmake
  6. --  uses which are not otherwise available for the mac.
  7. --
  8. --  Assume we are using at least 128K ROMS.
  9. --
  10. -- AUTHOR
  11. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  12. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  13. --
  14. --
  15. -- COPYRIGHT
  16. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  17. -- 
  18. --      This program is free software; you can redistribute it and/or
  19. --      modify it under the terms of the GNU General Public License
  20. --      (version 1), as published by the Free Software Foundation, and
  21. --      found in the file 'LICENSE' included with this distribution.
  22. -- 
  23. --      This program is distributed in the hope that it will be useful,
  24. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  25. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. --      GNU General Public License for more details.
  27. -- 
  28. --      You should have received a copy of the GNU General Public License
  29. --      along with this program;  if not, write to the Free Software
  30. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. --
  32. -- LOG
  33. --     $Log: directry.c,v $
  34.  * Revision 1.1  1992/01/24  03:29:45  dvadura
  35.  * dmake Version 3.8, Initial revision
  36.  *
  37. */
  38.  
  39. #include <Errors.h>
  40. #include <Files.h>
  41. #include <OSUtils.h>
  42. #include <StdLib.h>
  43. #include <Strings.h>
  44. #include <SysEqu.h>
  45. #include "extern.h"
  46.  
  47.  
  48.  
  49. /*
  50.  * Implementation of stat function for dmake on the mac.
  51.  *
  52.  * Many fields aren't filled in, and the times are seconds from 1/1//1904,
  53.  * but it should be enough for dmake (I think we only need st_mtime and
  54.  * st_mode's S_IFDIR set correctly).
  55.  */
  56. PUBLIC int stat(pPath, pStat)
  57. char *pPath;
  58. struct stat *pStat;
  59. {
  60.     CInfoPBRec infoPB;
  61.     OSErr err;
  62.     int retVal;
  63.  
  64.     pPath = Unix2MacFName (pPath);
  65.  
  66.     infoPB.hFileInfo.ioCompletion = NULL;
  67.     infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
  68.     infoPB.hFileInfo.ioVRefNum = 0;
  69.     infoPB.hFileInfo.ioFDirIndex = 0;
  70.     infoPB.hFileInfo.ioDirID = 0;
  71.     err = PBGetCatInfo(&infoPB, FALSE);
  72.     p2cstr ((StringPtr) pPath);
  73.  
  74.     if (err == noErr) {
  75.         pStat->st_mtime = (time_t) infoPB.hFileInfo.ioFlMdDat;
  76.         pStat->st_ctime = (time_t) infoPB.hFileInfo.ioFlCrDat;
  77.         pStat->st_mode = S_IREAD | S_IEXEC;
  78.  
  79.         /* If it is a directory ... */
  80.         if (infoPB.hFileInfo.ioFlAttrib & 0x10) {
  81.             pStat->st_size = infoPB.dirInfo.ioDrNmFls;
  82.             pStat->st_mode |= S_IFDIR;
  83.         } else {
  84.             pStat->st_size = infoPB.hFileInfo.ioFlLgLen;
  85.             pStat->st_mode |= S_IFREG;
  86.         } /* if ... else */
  87.  
  88.         /* If it is writeable */
  89.         if ((infoPB.hFileInfo.ioFlAttrib & 0x1) == 0) {
  90.             pStat->st_mode |= S_IWRITE;
  91.         } /* if */
  92.         
  93.         retVal = 0;
  94.  
  95.     } else {
  96.         retVal = -1;
  97.     } /* if ... else */
  98.  
  99.     return (retVal);
  100. } /* PUBLIC int stat () */
  101.  
  102.  
  103.  
  104. /*
  105.  * Return the current working directory, or NULL if there is an error.
  106.  */
  107. PUBLIC char *getcwd (char *pPath, size_t pathSize) {
  108.     DirInfo dirInfo;
  109.     OSErr err;
  110.     Str255 dirName;
  111.     char *pBeginName;
  112.     char *pC;
  113.     size_t len;
  114.     size_t spaceForColon;
  115.  
  116.     pPath = Unix2MacFName (pPath);
  117.  
  118.     /* Set up the info for the PBGetCatInfo() calls */
  119.     dirInfo.ioCompletion = NULL;
  120.     dirInfo.ioNamePtr = dirName;
  121.     dirInfo.ioVRefNum = 0;
  122.     dirInfo.ioFDirIndex = -1;
  123.     dirInfo.ioDrDirID = 0;
  124.     pBeginName = pPath + pathSize - 1;
  125.     spaceForColon = 0;  /* Make sure we don't have an end colon on the name */
  126.  
  127.     /*
  128.      * Keep going up the directory path until the end is reached or an error
  129.      * occurs.  Ideally, we would check for errors at every level and stop
  130.      * when we received an fnfErr (File Not Found), but it appears that there
  131.      * are some problems with network volumes.  (During testing, I received
  132.      * a paramErr (No Default Volume) beyond the top level.)  Thus, to keep it
  133.      * simple, I assume any error past the first directory indicates we have
  134.      * seen all directories.
  135.      */
  136.     while (TRUE) {
  137.         err = PBGetCatInfo ((CInfoPBPtr) &dirInfo, FALSE);
  138.         len = ((size_t)(unsigned char) dirName[0]);
  139.         if ((err == noErr) && (len < pBeginName - pPath)) {
  140.             p2cstr (dirName);
  141.             pBeginName -= len + spaceForColon;
  142.             strcpy (pBeginName, dirName);
  143.             /* Note that strcpy() adds the '\0' at the end of
  144.                the first directory for us */
  145.             if (spaceForColon == 1) {
  146.                 pBeginName[len] = ':';
  147.             } else {
  148.                 /* The end of the string shouldn't have a ':' */
  149.                 spaceForColon = 1;
  150.             } /* if */
  151.  
  152.             /* Set up for the next call to PBGetCatInfo() with
  153.                the parent's directory ID */
  154.             dirInfo.ioDrDirID = dirInfo.ioDrParID;
  155.  
  156.         } else if (spaceForColon == 1) {
  157.             /* We got past the top-level directory */
  158.             break;
  159.  
  160.         } else {
  161.             /* We either have an error when looking at the first directory
  162.                or have run out of room. */
  163.             return (NULL);
  164.         } /* if ... elses */
  165.     } /* while */
  166.  
  167.     /* Now copy the directory string to the beginning of the path string.
  168.        (It's possible the directory already starts at the beginning of the
  169.        string, but this is unlikely and doesn't hurt anything if it does,
  170.        so we don't bother to check for it.) */
  171.     pC = pPath;
  172.     while ((*(pC++) = *(pBeginName++)) != '\0')
  173.         ;
  174.  
  175.     return (pPath);
  176. } /* PUBLIC char *getcwd () */
  177.  
  178.  
  179.  
  180. /*
  181.  * Change the directory to a new default directory.
  182.  *
  183.  * Return 0 if successful, or -1 if there is an error.
  184.  */
  185. PUBLIC int chdir (char *pPath) {
  186.     WDPBRec WDPB;
  187.     VolumeParam vParam;
  188.     OSErr err;
  189.     int result;
  190.     char *pC;
  191.     char c;
  192.  
  193.     pPath = Unix2MacFName (pPath);
  194.  
  195.     /* Set up the directory */
  196.     c2pstr (pPath);
  197.     WDPB.ioCompletion = NULL;
  198.     WDPB.ioNamePtr = pPath;
  199.     WDPB.ioVRefNum = 0;
  200.     WDPB.ioWDProcID = 0;
  201.     WDPB.ioWDDirID = 0;
  202.     err = PBOpenWD (&WDPB, FALSE);
  203.     /* Restore path to a C-type string in case the caller wants
  204.        to use it after this call. */
  205.     p2cstr (pPath);
  206.     if (err != noErr) {
  207.         return (-1);
  208.     } /* if */
  209.  
  210.     /* Set up the volume if necessary */
  211.     if (*pPath != ':') {
  212.         for (pC = pPath + 1; (*pC != ':') && (*pC != '\0'); ++pC)
  213.             ;
  214.         c = *pC;
  215.         *pC = '\0';
  216.         vParam.ioCompletion = NULL;
  217.         vParam.ioNamePtr = c2pstr (pPath);
  218.         vParam.ioVRefNum = WDPB.ioVRefNum;
  219.         err = PBSetVol ((ParmBlkPtr) &vParam, FALSE);
  220.         p2cstr (pPath);
  221.         *pC = c;
  222.         result = ((err == noErr) ? 0 : -1);
  223.  
  224.     } else {
  225.         result = 0;
  226.     } /* if ... else */
  227.     
  228.     return (result);
  229. } /* PUBLIC int chdir () */
  230.  
  231.  
  232.  
  233. /*
  234.  * Change the modification time for the file to the current time.
  235.  *
  236.  * The normal version of utime can set the modification time to any
  237.  * time, this function aborts the function if this is tried.
  238.  *
  239.  * We return 0 if the modification time was updated and -1 if there
  240.  * was an error.
  241.  */
  242. PUBLIC int utime (char *pPath, time_t *pTimes) {
  243.     CInfoPBRec infoPB;
  244.     OSErr err;
  245.  
  246.     pPath = Unix2MacFName (pPath);
  247.  
  248.     if (pTimes != NULL) {
  249.         Fatal ("SUBROUTINE SHORTCOMING: utime cannot take a utimbuf struct");
  250.     } /* if */
  251.  
  252.     /* Get the old info */
  253.     infoPB.hFileInfo.ioCompletion = NULL;
  254.     infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
  255.     infoPB.hFileInfo.ioVRefNum = 0;
  256.     infoPB.hFileInfo.ioFDirIndex = 0;
  257.     infoPB.hFileInfo.ioDirID = 0;
  258.     err = PBGetCatInfo (&infoPB, FALSE);
  259.     if (err != noErr) {
  260.         p2cstr ((StringPtr) pPath);
  261.         return (-1);
  262.     } /* if */
  263.  
  264.     /* Change the modification time and set the new info */
  265.     GetDateTime (&(infoPB.hFileInfo.ioFlMdDat));
  266.     infoPB.hFileInfo.ioDirID = 0;
  267.     err = PBSetCatInfo (&infoPB, FALSE);
  268.     p2cstr ((StringPtr) pPath);
  269.     return ((err == noErr) ? 0 : -1);
  270. } /* PUBLIC int utime () */
  271.