ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define NAME_MAX 10
typedef struct {
int fd;
char name [NAME_MAX + 1];
} Dirent;
typedef struct {
int fd;
Dirent d;
} Dir;
/* that's just for portability (Mohsen)*/
/* Now you can use 3 functions: */
DIR *opendir (char* dirname);
Dirent *readdir (DIR *dfd);
void closedir (char*, struct stat*)
/*
The Dirent structure contains the inode
number and the name. The maximium length of a file
name component is NAME_MAX , which
is a SYSTEM DEPENDENT value. opendir returns
a pointer to a structure called DIR,
analogous to FILE, which is used
by readdir and closedir.
*/
Now you may use stat system call to determine if your name is a directory (take a look at manual page of stat).
Code:
Dirent *dp;
DIR *dfd;
if ((dfd = opendir ("MyDirName")) == NULL)
exit (1);
/* readdir returns
file names in directory. dp->name may be `.'
or `..' which should be ignored. */
while ((dp = readdir (dfd) != NULL) {
if (strcmp (dp->name, ".") == 0 || strcmp (dp->name, "..") == 0)
continue;
printf ("file: %s\n", dp->name);
}
P.S.
I'va wrote it in a hurry so check out the man pages on any question.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.