home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Directory Scanning / DirentTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-04  |  3.1 KB  |  113 lines  |  [TEXT/KAHL]

  1. /****************************************************************************************
  2.  *
  3.  *    File:        DirentTest.c
  4.  *    Created:    7/3/93        By:    George T. Talbot
  5.  *    Purpose:    This program tests the Mac dirent stuff.
  6.  *
  7.  *    Modifications:
  8.  *
  9.  *    Notes:
  10.  *            1) These routines will NOT work under A/UX.
  11.  *            2) WD = working directory
  12.  *            3) CD = change directory
  13.  *            4) FS = file system
  14.  *            5) Mac filesystems allow spaces as part of pathnames!
  15.  *            6) All routines which return a path use the default Macintosh path separator,
  16.  *               a colon (":").
  17.  *
  18.  ****************************************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include "dirent.h"
  22.  
  23. /****************************************************************************************
  24.  *
  25.  *    The main program opens the parent of the current working directory, prints the path
  26.  *    to this parent and the path to the current working directory, and prints the contents
  27.  *    of the opened directory.
  28.  *
  29.  *    Calls:            opendir(), printf(), fprintf(), print_cwd(), print_dpath(), telldir(),
  30.  *                    readdir(), rewinddir(), seekdir(), closedir()
  31.  *    Called By:        main()
  32.  *    Globals Used:    none
  33.  *    Parameters:        none
  34.  *    Returns:        nothing
  35.  *
  36.  ****************************************************************************************/
  37.  
  38. void    main()
  39.     {
  40.     DIR                *d;
  41.     struct dirent    *de;
  42.     char            path[MAXPATHLEN];
  43.  
  44.     /*    Open the current working directory's parent directory    */
  45.     d    = opendir("..");
  46.  
  47.     if (d == nil)
  48.         {
  49.         fprintf(stderr, "Couldn't open. err = %d\n", dd_errno);
  50.         return;
  51.         }
  52.  
  53.     /*    Print paths to both parent and current WD    */
  54.     printf("Current WD: ");
  55.     
  56.     if (getwd(path) == nil)
  57.         fprintf(stderr, "Couldn't get working directory. err = %d\n", dd_errno);
  58.     else
  59.         printf("%s\n", path);
  60.  
  61.     printf("opendir WD: ");
  62.     if (pathdir(d, path) == nil)
  63.         fprintf(stderr, "Couldn't get path to opened directory. err = %d\n", dd_errno);
  64.     else
  65.         printf("%s\n", path);
  66.  
  67.     /*    Iterate through the entire directory opened above    */
  68.     do    {
  69.         int    dirnum;
  70.         
  71.         /*    Get the index of this directory entry.  This index will be equal to the
  72.          *    ioFDirIndex of the entry minus 1 to maintain POSIX similarity.
  73.          */
  74.         dirnum    = telldir(d);
  75.  
  76.         /*    Read the directory entry    */
  77.         de        = readdir(d);
  78.         
  79.         /*    These two calls are redundant.  They are here to test if rewinddir() and
  80.          *    seekdir() actually work.  These may be omitted and the program will print
  81.          *    identical output.
  82.          */
  83.         rewinddir(d);
  84.         seekdir(d, dirnum+1);
  85.  
  86.         /*    If the directory entry actually was read, print the name and index    */
  87.         if (de != nil)
  88.             printf("  %d \"%s\"\n", dirnum, de->d_name);
  89.         }
  90.     /*    Until we get done reading entries or an error occurred    */
  91.     while (de != nil);
  92.  
  93.     if (dd_errno)
  94.         fprintf(stderr, "Error while reading directory. err = %d\n", dd_errno);
  95.  
  96.     /*    Close the directory opened above    */
  97.     closedir(d);
  98.     
  99.     /*    CD to the parent directory    */
  100.     printf("Attempting chdir(\"..\")...\n");
  101.  
  102.     if (chdir(".."))
  103.         fprintf(stderr, "Couldn't change to parent directory. err = %d\n", dd_errno);
  104.     else
  105.         {
  106.         printf("New WD: ");
  107.         if (getwd(path) == nil)
  108.             fprintf(stderr, "Couldn't get working directory. err = %d\n", dd_errno);
  109.         else
  110.             printf("%s\n", path);
  111.         }
  112.     }
  113.