macstat.c
上传用户:andy_li
上传日期:2007-01-06
资源大小:1019k
文件大小:5k
源码类别:

压缩解压

开发平台:

MultiPlatform

  1. /*---------------------------------------------------------------------------
  2.   macstat.c
  3.  *  This file provides a unix like file-stat routine
  4.  *  for V7 Unix systems that don't have such procedures.
  5.  *
  6.  *
  7.   ---------------------------------------------------------------------------*/
  8. /*****************************************************************************/
  9. /*  Includes                                                                 */
  10. /*****************************************************************************/
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <sound.h>
  14. #include "macstat.h"
  15. #include "helpers.h"
  16. #include "pathname.h"
  17. #include "macstuff.h"
  18. #include "mactime.h"
  19. /*****************************************************************************/
  20. /*  Global Vars                                                              */
  21. /*****************************************************************************/
  22. extern int errno;
  23. /*****************************************************************************/
  24. /*  Prototypes                                                               */
  25. /*****************************************************************************/
  26. /*****************************************************************************/
  27. /*  Functions                                                                */
  28. /*****************************************************************************/
  29. int UZmacstat(const char *path, struct stat *buf)
  30. {
  31.     Boolean isDirectory;
  32.     long dirID;
  33.     char fullpath[1024];
  34.     CInfoPBRec fpb;
  35.     HVolumeParam vpb;
  36.     FSSpec fileSpec;
  37.     OSErr err;
  38.     memset(buf,0,sizeof(buf));   /* zero out all fields */
  39.     GetCompletePath(fullpath, path, &fileSpec, &err);
  40.     printerr("GetCompletePath:", (err != -43) && (err != 0) && (err != -120),
  41.              err, __LINE__, __FILE__, path);
  42.     if (err != noErr) {
  43.         errno = err;
  44.         return -1;
  45.     }
  46.     /*
  47.      * Fill the fpb & vpb struct up with info about file or directory.
  48.      */
  49.     FSpGetDirectoryID(&fileSpec, &dirID, &isDirectory);
  50.     vpb.ioVRefNum = fpb.hFileInfo.ioVRefNum = fileSpec.vRefNum;
  51.     vpb.ioNamePtr = fpb.hFileInfo.ioNamePtr = fileSpec.name;
  52.     if (isDirectory) {
  53.         fpb.hFileInfo.ioDirID = fileSpec.parID;
  54.     } else {
  55.         fpb.hFileInfo.ioDirID = dirID;
  56.     }
  57.     fpb.hFileInfo.ioFDirIndex = 0;
  58.     err = PBGetCatInfo(&fpb, false);
  59.     if (err == noErr) {
  60.         vpb.ioVolIndex = 0;
  61.         err = PBHGetVInfoSync((HParmBlkPtr)&vpb);
  62.         if (err == noErr && buf != NULL) {
  63.             /*
  64.              * Files are always readable by everyone.
  65.              */
  66.             buf->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
  67.             /*
  68.              * Use the Volume Info & File Info to fill out stat buf.
  69.              */
  70.             if (fpb.hFileInfo.ioFlAttrib & 0x10) {
  71.                 buf->st_mode |= S_IFDIR;
  72.                 buf->st_nlink = 2;
  73.             } else {
  74.                 buf->st_nlink = 1;
  75.                 if (fpb.hFileInfo.ioFlFndrInfo.fdFlags & 0x8000) {
  76.                     buf->st_mode |= S_IFLNK;
  77.                 } else {
  78.                     buf->st_mode |= S_IFREG;
  79.                 }
  80.             }
  81.             if ((fpb.hFileInfo.ioFlAttrib & 0x10) ||
  82.                 (fpb.hFileInfo.ioFlFndrInfo.fdType == 'APPL')) {
  83.                 /*
  84.                  * Directories and applications are executable by everyone.
  85.                  */
  86.                 buf->st_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
  87.             }
  88.             if ((fpb.hFileInfo.ioFlAttrib & 0x01) == 0) {
  89.                 /*
  90.                  * If not locked, then everyone has write acces.
  91.                  */
  92.                 buf->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  93.             }
  94.             buf->st_ino = fpb.hFileInfo.ioDirID;
  95.             buf->st_dev = fpb.hFileInfo.ioVRefNum;
  96.             buf->st_uid = -1;
  97.             buf->st_gid = -1;
  98.             buf->st_rdev = 0;
  99.             buf->st_size = fpb.hFileInfo.ioFlLgLen;
  100.             buf->st_blksize = vpb.ioVAlBlkSiz;
  101.             buf->st_blocks = (buf->st_size + buf->st_blksize - 1)
  102.                             / buf->st_blksize;
  103.             /*
  104.              * The times returned by the Mac file system are in the
  105.              * local time zone.  We convert them to GMT so that the
  106.              * epoch starts from GMT.  This is also consistent with
  107.              * what is returned from "clock seconds".
  108.              */
  109.             buf->st_mtime = MacFtime2UnixFtime(fpb.hFileInfo.ioFlMdDat);
  110.             buf->st_ctime = MacFtime2UnixFtime(fpb.hFileInfo.ioFlCrDat);
  111.             buf->st_atime = buf->st_ctime;         /* best guess */
  112. #ifdef DEBUG_TIME
  113.             {
  114.             struct tm *tp = localtime(&buf->st_mtime);
  115.             printf(
  116.               "nUZmacstat: local buf->st_mtime is %ld = %d/%2d/%2d  %2d:%2d:%2d",
  117.               buf->st_mtime, tp->tm_year, tp->tm_mon+1, tp->tm_mday,
  118.               tp->tm_hour, tp->tm_min, tp->tm_sec);
  119.             tp = gmtime(&buf->st_mtime);
  120.             printf(
  121.               "nUZmacstat: UTC   buf->st_mtime is %ld = %d/%2d/%2d  %2d:%2d:%2dn",
  122.               buf->st_mtime, tp->tm_year, tp->tm_mon+1, tp->tm_mday,
  123.               tp->tm_hour, tp->tm_min, tp->tm_sec);
  124.             }
  125. #endif /* DEBUG_TIME */
  126.         }
  127.     }
  128.     if (err != noErr) {
  129.         errno = err;
  130.     }
  131.     return (err == noErr ? 0 : -1);
  132. }