Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between a unix directory and file in C and C++

Tags:

Given a path, say, /home/shree/path/def, I would want to determine if def is a directory or a file. Is there a way of achieving this in C or C++ code?

like image 568
Shree Avatar asked Jun 24 '09 06:06

Shree


People also ask

What is the difference between file and directory in Unix?

A Linux system, just like UNIX, makes no difference between a file and a directory, since a directory is just a file containing names of other files. Programs, services, texts, images, and so forth, are all files. Input and output devices, and generally all devices, are considered to be files, according to the system.

What is the difference between a directory and a file?

difference between directory and File : A file is any kind of computer document and a directory is a computer document folder or filing cabinet.

What is a directory in C?

The directory is a place/area/location where a set of the file(s) will be stored. Subdirectory is a directory inside the root directory, in turn, it can have another sub-directory in it. In C programming language you can list all files and sub-directories of a directory easily.

Are UNIX directories files?

Unix directories do not contain files. Instead, they contain the names of files paired with references to so-called inodes, which in turn contain both the file and its metadata (owner, permissions, time of last access, etc., but no name).


2 Answers

The following code uses the stat() function and the S_ISDIR ('is a directory') and S_ISREG ('is a regular file') macros to get information on the file. The rest is just error checking and enough to make a complete compilable program.

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
    int status;
    struct stat st_buf;

    // Ensure argument passed.

    if (argc != 2) {
        printf ("Usage: progName <fileSpec>\n");
        printf ("       where <fileSpec> is the file to check.\n");
        return 1;
    }

    // Get the status of the file system object.

    status = stat (argv[1], &st_buf);
    if (status != 0) {
        printf ("Error, errno = %d\n", errno);
        return 1;
    }

    // Tell us what it is then exit.

    if (S_ISREG (st_buf.st_mode)) {
        printf ("%s is a regular file.\n", argv[1]);
    }
    if (S_ISDIR (st_buf.st_mode)) {
        printf ("%s is a directory.\n", argv[1]);
    }

    return 0;
}

Sample runs are shown here:


pax> vi progName.c ; gcc -o progName progName.c ; ./progName
Usage: progName 
       where  is the file to check.

pax> ./progName /home
/home is a directory.

pax> ./progName .profile
.profile is a regular file.

pax> ./progName /no_such_file
Error, errno = 2
like image 123
paxdiablo Avatar answered Sep 23 '22 14:09

paxdiablo


Use the stat(2) system call. You can use the S_ISREG or S_ISDIR macro on the st_mode field to see if the given path is a file or a directory. The man page tells you about all the other fields.

like image 22
camh Avatar answered Sep 25 '22 14:09

camh