tags:

views:

56

answers:

2

I just wanna 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
+1  A: 

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.

Nathon
@Nathon: `stat` isn't standard C as far as I know.
Jens Gustedt
@Jens: True. It's standard *nix though, and vabz didn't specify so I assumed.
Nathon
@nathon Sorry, But wasn't able to interprete as how can i use Stat to solve my question. can u just elaborate a a bit. _My question is simple Take any name Validate if it is Directory or not, If yeh print all the file names containing it.__
vabz
+1  A: 

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

Jens Gustedt