Parsing unix STAT structure


 
Thread Tools Search this Thread
Top Forums Programming Parsing unix STAT structure
# 1  
Old 12-01-2010
Parsing unix STAT structure

Hi

I am creating a utility which needs to create a log file under the path represented by an environment variable. The condition is that this path must be a valid DIRECTORY PATH. So i need to determine that the path is indeed a VALID DIRECTORY PATH.
I have written a function which will return me the stat structure of the PATH using lstat. Can anyone help me with a sample C or C++ code which parses through the UNIX STAT structure and determines if it indeed represents a directory only.
<sys/stat.h>

Thanks
# 2  
Old 12-01-2010
First off, assuming I understand what you have done, you called lstat against a link to a directory? lstat() works against links.

Do you know about the access() function? That may be what you really want.

Otherwise call readlink(), get the names of the directories (we hope they are) that make up the path, call stat() for each one, first for /dir1/dir2, then /dir1.
Code:
int isdir(char *fname)
{
    struct stat st;
    if(stat(fname, &st)!=-1)
       return S_ISDIR(st.st_mode);
    else
    {
         perror("Cannot stat file");
         exit(1);
    }
}


However a one liner is all you need.
Code:
 
 if( access(linkfilename, X_OK)==0)
       /* okay you can access the files under the directory*/

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-02-2010
Many Thanks Jim !!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting the Folder Structure in Unix

Hi All, I have a compute box and I want to tar directory structure under a directory and then Deploy/untar it in a new compute box so that the directory structure will be exactly the same. I do not want any of the file to be extracted and deployed but instead only the directory structure. ... (2 Replies)
Discussion started by: filter
2 Replies

2. Shell Programming and Scripting

hp-unix stat command to get last change date of file

I'm on hp-unix. I would like a variable to hold the last change date of a file. I looked at the man pages for stat, but I don't see any examples and can't get the syntax right. Can anyone help me? Thank you. (2 Replies)
Discussion started by: sboxtops
2 Replies

3. UNIX for Dummies Questions & Answers

Tree directory structure of Unix to get final node path

Hi, I want to list all the last directories from mentioned base path. for eg: If i have a base path say /base/base1/ How can i get the path till last node in tree like directory structure of unix by applying any command. so that i will get following output. ... (7 Replies)
Discussion started by: Shiv@jad
7 Replies

4. UNIX for Advanced & Expert Users

stat fails HP unix

Hi all, I have scenario here , In my C program i check the stat for a file as stat(fname, &stat_buf); If it is succesfull, i will process else i will make my program to fail. The file which i am checking has no stat information. What might be the reason, file has no stat... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

5. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

6. UNIX for Dummies Questions & Answers

Why cant i find my ftp directories in the unix structure

Hi, I'm new here so hello to everyone...i'm also new to linux/unix but got my first dedicated server about 2 weeks ago and have been using ssh to configure it on ubuntu linux. I have a question about something i can't work out....if i use ftp to transfer files ( php, html, javascript files... (2 Replies)
Discussion started by: elduderino
2 Replies

7. UNIX for Dummies Questions & Answers

Not quite related to Unix but CP/M file structure...

I'm not sure where to post this but I'm having some trouble with the directories in CP/M... I'm sorry about the length but I'm totally confused... I've seen that the directory entry in CP/M contains the following: 1 byte User Code 8 bytes Filename 3 bytes File extension 1 byte Extension 2... (3 Replies)
Discussion started by: Legend986
3 Replies

8. UNIX for Dummies Questions & Answers

SCO Unix inode structure.

I have read quite a few threads here about the unix file creation date. I was interested in finding how to display it using a unix command. find did not help me so i looked at man inode. I found direction to htino.h which is described as the structure of the inode for S51K (UNIX), HTFS, EAFS... (4 Replies)
Discussion started by: rbn
4 Replies

9. UNIX for Dummies Questions & Answers

What are some benefits of the UNIX hierarchical file structure?

What are some benefits of the UNIX hierarchical file structure? I am new to UNIX and researching some information about it for a class so please help if you can. Thanks. (1 Reply)
Discussion started by: 88923JJJSDK
1 Replies

10. UNIX for Dummies Questions & Answers

Unix/Linux Directory Structure

Does anyone know of a good Internet source that explains the directory structure of Unix/Linux?? Thanks Gregg (2 Replies)
Discussion started by: gdboling
2 Replies
Login or Register to Ask a Question