Citation :
/* * Utilisation de la fonction readdir() et mkdir():
* on cree un directory "junk" a partir du directory * courant. Ensuite on cree le fichier "bar" sous ce * nouveau directory. Enfin on ouvre le directory pour * lire son contenu.
*/
#include <dirent.h> #include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
char * path = "junk/bar";
main(){ struct dirent * t_readdir();
DIR * t_opendir(); int t_mkdir();
int t_open(); int t_close();
int t_closedir();
DIR * dirp; struct dirent * dp; int fd; t_mkdir("junk", 0752); fd = t_open(path, O_CREAT, 0777); dirp = t_opendir("junk" ); for(dp = t_readdir(dirp); dp != NULL ; dp = t_readdir(dirp))
printf("dp->d_name = %s\n", dp->d_name); t_close(fd); t_closedir(dirp); return 0; }
#define SYS_ERR -1
int t_mkdir(path, mode) char * path; int mode; {
if((mkdir(path, mode) == SYS_ERR)){
perror("mkdir" ); exit(1); }
return 0; }
struct dirent * t_readdir(dir) DIR * dir;{
struct dirent * dirp; if(((dirp = readdir(dir)) == NULL) && errno != 0){
perror("readdir" ); exit(1);
}
return dirp; }
|