示例#1
0
NLM_EXTERN ValNodePtr LIBCALL Nlm_DirCatalog (Nlm_CharPtr pathname)

{
#ifdef OS_MAC
  long            dirID;
  OSErr           err;
  short           index;
  unsigned short  num;
  Nlm_Char        path[PATH_MAX];
  CInfoPBRec      pbc;
  HParamBlockRec  pbh;
  short           vRefNum;
#endif
#ifdef OS_UNIX
  Nlm_Uint1       choice;
#ifdef OS_UNIX_DARWIN
  DIR             *dirp;
  struct dirent   *dep;
#else
  Nlm_Char        buf [256];
  Nlm_Char        ch;
  Nlm_Char        cmmd [PATH_MAX + 20];
  FILE            *fp;
  Nlm_CharPtr     ptr;
#endif
#endif
  ValNodePtr      vnp = NULL;

  if (pathname != NULL && pathname [0] != '\0') {
#ifdef OS_MAC
    Nlm_StringNCpy_0 (path, pathname, sizeof (path));
    Nlm_CtoPstr ((Nlm_CharPtr) path);
    Nlm_MemSet ((Nlm_VoidPtr) (&pbh), 0, sizeof (HParamBlockRec));
    pbh.volumeParam.ioNamePtr = (StringPtr) path;
    pbh.volumeParam.ioVolIndex = -1;
    err = PBHGetVInfo (&pbh, FALSE);
    if (err != noErr) return NULL;
    vRefNum = pbh.volumeParam.ioVRefNum;
    Nlm_StringNCpy_0 (path, pathname, sizeof (path));
    Nlm_CtoPstr ((Nlm_CharPtr) path);
    Nlm_MemSet ((Nlm_VoidPtr) (&pbc), 0, sizeof (CInfoPBRec));
    pbc.dirInfo.ioNamePtr = (StringPtr) path;
    pbc.dirInfo.ioVRefNum = vRefNum;
    err = PBGetCatInfo (&pbc, FALSE);
    if (err != noErr) return NULL;
    if (pbc.dirInfo.ioFlAttrib & 16) {
      num = pbc.dirInfo.ioDrNmFls;
      dirID = pbc.dirInfo.ioDrDirID;
      for (index = 1; index <= num; index++) {
        Nlm_MemSet ((Nlm_VoidPtr) (&pbc), 0, sizeof (CInfoPBRec));
        pbc.dirInfo.ioNamePtr = (StringPtr) path;
        pbc.dirInfo.ioVRefNum = vRefNum;
        pbc.dirInfo.ioFDirIndex = index;
        pbc.dirInfo.ioDrDirID = dirID;
        pbc.dirInfo.ioACUser = 0;
        err = PBGetCatInfo (&pbc, FALSE);
        if (err == noErr) {
          Nlm_PtoCstr ((Nlm_CharPtr) path);
          if (pbc.dirInfo.ioFlAttrib & 16) {
            ValNodeCopyStr (&vnp, 1, path);
          } else {
            ValNodeCopyStr (&vnp, 0, path);
          }
        }
      }
    }
#endif
#if defined(WIN32)
    {{
      Nlm_Char x_path[PATH_MAX];
      WIN32_FIND_DATA fData;
      HANDLE hFindFile;
      Nlm_StringNCpy_0(x_path, pathname, sizeof(x_path) - 5);
      Nlm_StringCat(x_path, "\\*.*");
      hFindFile = FindFirstFile(x_path, &fData);
      if (hFindFile == INVALID_HANDLE_VALUE)
        return 0;
      do {
        if (fData.cFileName[0] != '.'  ||
            (fData.cFileName[1] != '.'  &&  fData.cFileName[1] != '\0'))
          ValNodeCopyStr
            (&vnp, (fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0,
             fData.cFileName);
      } while ( FindNextFile(hFindFile, &fData) );
      FindClose(hFindFile);
    }}
#endif
#ifdef OS_UNIX
#ifdef OS_UNIX_DARWIN
    dirp = opendir(pathname);
    if (dirp == NULL) return NULL;
    while ((dep = readdir(dirp)) != NULL) {
      /* ignore 'invisible' files. */
      if (dep->d_namlen < 1 || dep->d_name[0] == '.')
        continue;
      if (dep->d_type == DT_DIR) /* directory */
        choice = 1;
      else          /* all other file types. */
        choice = 0;
      ValNodeCopyStr (&vnp, choice, dep->d_name);
    }
    closedir(dirp);
#else
    sprintf (cmmd, "ls -1p %s 2>/dev/null", pathname);
    fp = popen (cmmd, "r");
    if (fp == NULL) return NULL;
    while (Nlm_FileGets (buf, sizeof (buf), fp) != NULL) {
      ptr = buf;
      ch = *ptr;
      while (ch != '\0' && ch != '\n' && ch != '\r') {
        ptr++;
        ch = *ptr;
      }
      *ptr = '\0';
      choice = 0;
      ptr = Nlm_StringChr (buf, '/');
      if (ptr != NULL) {
        *ptr = '\0';
        choice = 1;
      }
      ValNodeCopyStr (&vnp, choice, buf);
    }
    pclose (fp);
#endif
#endif
#ifdef OS_VMS
#endif
  }
  return vnp;
}
示例#2
0
P4 (PUBLIC pascal trap, OSErr, FSMakeFSSpec,
    int16, vRefNum, int32, dir_id,
    Str255, file_name, FSSpecPtr, spec)
{
    Str255 local_file_name;
    OSErr retval;
    HParamBlockRec hpb;

    if (!file_name)
        warning_unexpected("!file_name");

    /*
     * Colons make things tricky because they are used both to identify volumes
     * and to delimit directories and sometimes to identify partial paths.  Right
     * now, most of these uses of colon shouldn't cause problems, but there are
     * some issues that need to be checked on a real Mac.  What do all these
     * mean?
     *
     *     dir_id      file_name
     *     310         foo:
     *     310         :foo:
     *     310         foo:bar
     *     310         :foo:bar
     *     310         :foo:bar:
     *
     * what about all those file_names, but with dir_id = 0, dir_id = 1, dir_id = 2
     * and dir_id = a number that isn't a directory id?
     */

    if (pstr_index_after (file_name, ':', 0))
        warning_unexpected ("colon found");

    str255assign (local_file_name, file_name);
    hpb.volumeParam.ioNamePtr = (StringPtr) RM ((Ptr) local_file_name);
    hpb.volumeParam.ioVRefNum = CW (vRefNum);
    if (file_name[0])
        hpb.volumeParam.ioVolIndex = CLC (-1);
    else
        hpb.volumeParam.ioVolIndex = CLC (0);

    retval = PBHGetVInfo (&hpb, FALSE);

    if (retval == noErr)
    {
        CInfoPBRec cpb;

        str255assign (local_file_name, file_name);
        cpb.hFileInfo.ioNamePtr = (StringPtr) RM ((Ptr) local_file_name);
        cpb.hFileInfo.ioVRefNum = CW (vRefNum);
        if (file_name[0])
            cpb.hFileInfo.ioFDirIndex = CWC (0);
        else
            cpb.hFileInfo.ioFDirIndex = CWC (-1);
        cpb.hFileInfo.ioDirID = CL (dir_id);
        retval = PBGetCatInfo (&cpb, FALSE);
        if (retval == noErr)
        {
            spec->vRefNum = hpb.volumeParam.ioVRefNum;
            spec->parID = cpb.hFileInfo.ioFlParID;
            extract_name ((StringPtr) spec->name, MR (cpb.hFileInfo.ioNamePtr));
        }
        else if (retval == fnfErr)
        {
            OSErr err;

            cpb.hFileInfo.ioNamePtr = CLC (0);
            cpb.hFileInfo.ioVRefNum = CW (vRefNum);
            cpb.hFileInfo.ioFDirIndex = CWC (-1);
            cpb.hFileInfo.ioDirID = CL (dir_id);
            err = PBGetCatInfo (&cpb, FALSE);
            if (err == noErr)
            {
                if (cpb.hFileInfo.ioFlAttrib & ATTRIB_ISADIR)
                {
                    spec->vRefNum = hpb.volumeParam.ioVRefNum;
                    spec->parID = CL (dir_id);
                    extract_name ((StringPtr) spec->name, file_name);
                }
                else
                    retval = dupFNErr;
            }
        }
    }
    return retval;
}