Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-12-2012, 12:14 AM   #1
LQ Newbie
Registered: Apr 2012
Posts: 8

Rep: Reputation: Disabled
Checking file type with fstat

So I'm trying to figure out how to work this, but I'm not getting anywhere. We have an assignment where we have to sort a file we get from stdin. Part of the requirement is that we have to use fstat to get information about the file. Mainly, if it's a regular file or not. If it is, we have to allocate enough memory with malloc to store it.

The problem I'm having is figuring out how to get fstat to work correctly. I know that there's a macros S_ISREG(m) that we can check using the st_mode field...? How...?

Is it a bool and how do I even access it? The problem is I can find all these documents explaining WHAT all the things are, but not how to use them. Basically, I have:

struct stat buffer;
int status;
int size;
status = fstat(0, &buffer);
size = buffer.st_size;
That stores the stat struct with all the information. What I need is something that will let me do something like this:

if(/*is a regular file*/)
   do something else;
The problem being I don't think I'm even storing size correctly and I'm not sure how to use the st_mode field :\
Old 05-12-2012, 06:35 AM   #2
Senior Member
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650Reputation: 650
Try 'man fstat' and take a look at the switch block in the example near the bottom.

           switch (sb.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("directory\n");               break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("symlink\n");                 break;
           case S_IFREG:  printf("regular file\n");            break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get file icon from extension, file type, or mime-type Guitarist88 Programming 3 04-21-2008 10:58 AM
Disabling any type of checking in Exim feavel Linux - Software 0 06-19-2007 03:03 PM
KILE as you type spell checking ibex Linux - Software 0 12-13-2006 02:30 PM
checking NAS-Port-Type on freeradius luizgap Linux - Networking 0 11-23-2004 11:39 AM
Using fstat() tim_l Linux - Software 0 10-10-2003 10:20 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration