The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
PHP 5 solaris opendir failure Esaia Shell Programming and Scripting 1 05-25-2008 08:08 PM
Listing directories and sub directories jinxor UNIX for Advanced & Expert Users 3 03-11-2008 07:27 AM
moving directories to new directories on multiple servers mackdaddy07 Shell Programming and Scripting 0 04-06-2007 08:30 AM
Two Files Created For Every One? Atama UNIX for Dummies Questions & Answers 1 04-12-2002 01:44 PM
Created FilePermission Reza Nazarian UNIX for Dummies Questions & Answers 6 10-19-2001 05:16 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-26-2008
Registered User
 

Join Date: Nov 2007
Posts: 73
opendir() + customly created directories

Gday all

In a program I am designing, I am using opendir() to test whether entries under a certain directory are sub-directories or not. This method works fine for the directory itself (.) and the parent directory (..), however it does not work for any sub-directories I manually create.

i.e. it does not work for current_directory/my_sub_directory

I am performing the test in the following manner

Code:
if( (sub_dir = opendir(sub_dir_name)) != NULL)
{
       /* Do whatever*/

}
Is there anything I am failing to understand or am I missing out on anything ?

regards
James
Reply With Quote
Forum Sponsor
  #2  
Old 03-26-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
One trap I always keep falling into is forgetting to glue back the entry from a readdir to the directory I read it from, i.e. using a relative file name in the wrong (e.g. grandparent) directory.
Reply With Quote
  #3  
Old 03-26-2008
Registered User
 

Join Date: Nov 2007
Posts: 73
Quote:
Originally Posted by era View Post
One trap I always keep falling into is forgetting to glue back the entry from a readdir to the directory I read it from, i.e. using a relative file name in the wrong (e.g. grandparent) directory.
I tried this and unfortunately had no luck
Reply With Quote
  #4  
Old 03-26-2008
Registered User
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 250
Doubly linked lists..yay.
Reply With Quote
  #5  
Old 03-26-2008
Registered User
 

Join Date: Nov 2007
Posts: 73
hey all

Ive discovered a better way to test for the presence of sub-directories within folders, however it does not involve the use of opendir()

What I did instead was the following

Code:
/*Check if it is current or parent directory*/

if( (strcmp(subfolder_name,".") != 0) &&  (strcmp(subfolder_name,"..") != 0)
{
        /*Check to see if it is a directory by using strrchr()*/

        ptr = strrchr(subfolder_name,'.');

        /*If  strrchr() returns NULL, the current entry
           is indeed a sub-folder*/
  
        if(ptr == NULL)
        {
             /*Do necessary stuff here*/
        }  

}
Reply With Quote
  #6  
Old 03-26-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
opendir() returns a pointer to a dir stream. readdir() is invoked in a loop afterwards to read the contents of the opened dir stream. Each dir entry is passed from readdir() to stat() which stores information in a stat structure. The S_ISDIR() macro can be used to test whether the stat structure is a dir or not. Look at the manpages of opendir() readdir() and stat().
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0