vabz
vabz

Reputation: 171

program to validate directory name

I just want to write a program which takes a directory name as argument

  • Validate that it is in fact a directory
  • Get a listing of all files in the directory and print it

Upvotes: 0

Views: 302

Answers (2)

nmichaels
nmichaels

Reputation: 50943

Look at stat. It will provide you with the information you want; all you have to do is interpret it.

Edit: A brief example.

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

#define TRUE 1
#define FALSE 0

int is_dir(char *path)
{
    struct stat dir_stats;

    stat(path, &dir_stats);
    if (S_ISDIR(dir_stats.st_mode))
        return TRUE;
    return FALSE;
}

For the list of files in the directory, use readdir.

Upvotes: 1

Jens Gustedt
Jens Gustedt

Reputation: 78903

The word directory doesn't even appear in the C standard. This is an OS concept.

Upvotes: 1

Related Questions