Current uni student and need help writing a c program that displays the contents and all information in a directory.
The listing should have the filename, mode, links, user, group size and modtime.
this is what i have completed:
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
void do_ls(char [], struct stat *);
void show_stat_info(char [], struct stat *);
main(int ac, char *av[])
{
struct stat info;
if ( ac == 1 )
do_ls( ".",&info );
else
while ( --ac ){
printf("%s:\n", *++av );
do_ls( *av,&info );
}
}
void do_ls( char dirname[], struct stat *buf )
/*
* list files in directory called dirname
*/
{
DIR *dir_ptr; /* the directory */
struct dirent *direntp; /* each entry */
if ( ( dir_ptr = opendir( dirname ) ) == NULL )
fprintf(stderr,"ls1: cannot open %s\n", dirname);
else
{
while ( ( direntp = readdir( dir_ptr ) ) != NULL )
{
printf("%s\n", direntp->d_name );
printf(" mode: %o\n", buf->st_mode); /* type + mode */
printf(" links: %d\n", buf->st_nlink); /* # links */
printf(" user: %d\n", buf->st_uid); /* user id */
printf(" group: %d\n", buf->st_gid); /* group id */
printf(" size: %d\n", buf->st_size); /* file size */
printf("modtime: %d\n", buf->st_mtime); /* modified */
//printf(" name: %s\n", fname ); /* filename */
}
closedir(dir_ptr);
}
}
right now it only lists the files inside a directory but all the details come back as zero
when I pass a directory to the function it will say "can not open filename"
This is what I am trying to achieve:
if no argument is passed through the program will print all the names and the details of the current directory
if a directory is passed through the program will print the information for all files inside the directory
I removed the stat pointer argument, used early return to simplify code, and called stat on each path which is build by combining the directory and file name (crudely), also fixed the format strings:
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
void do_ls(char dirname[]) {
DIR *dir_ptr;
if(!(dir_ptr = opendir( dirname ))) {
fprintf(stderr,"ls1: cannot open %s\n", dirname);
return;
}
struct dirent *direntp;
while((direntp = readdir(dir_ptr))) {
struct stat buf;
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s/%s", dirname, direntp->d_name);
if(stat(path, &buf)) {
fprintf(stderr,"stat failed\n");
break;
}
printf("%s\n", direntp->d_name );
printf(" mode: %o\n", buf.st_mode); /* type + mode */
printf(" links: %ld\n", buf.st_nlink); /* # links */
printf(" user: %d\n", buf.st_uid); /* user id */
printf(" group: %d\n", buf.st_gid); /* group id */
printf(" size: %ld\n", buf.st_size); /* file size */
printf("modtime: %ld\n", buf.st_mtime); /* modified */
printf(" name: %s\n", path ); /* filename */
}
closedir(dir_ptr);
}
int main(int ac, char *av[]) {
if(ac == 1) {
do_ls(".");
return 0;
}
while(--ac) {
printf("%s:\n", *++av);
do_ls(*av);
}
return 0;
}
Related
I want to write a C program that takes as an argument the path to a folder and shows some info about the files it contains.
So far I have written this:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv){
char* dir_path = argv[1];
char* dir_path_bar = strcat(dir_path, "/");
DIR* dir = opendir(dir_path);
for(struct dirent* entry = readdir(dir); entry != NULL; entry = readdir(dir)){
printf("Next entry is %s\n", entry->d_name);
char* entry_path = strcat(dir_path_bar, entry->d_name);
printf("%s\n", entry_path);
struct stat buf;
stat(entry_path, &buf);
printf("Its inode number is %s\n", entry->d_ino);
printf("Its inode number is %s\n", buf.st_ino);
printf("Its uid is %s\n", buf.st_uid);
printf("Its size is %s bytes\n", buf.st_size);
};
closedir(dir);
}
Which compiles, but the stat call is giving me a SEGFAULT. What is going on?
As others have mentioned, you can't append to argv[1]. You can't keep appending to it inside the loop. And, you can't use %s to output numbers.
Here is your code with the bugs annotated and fixed [using #if 0 to show the old code]:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main(int argc, char **argv)
{
char *dir_path = argv[1];
// NOTE/BUG: argv[1] has a fixed size you can't append to it
#if 0
char *dir_path_bar = strcat(dir_path, "/");
#else
char dir_path_bar[PATH_MAX];
strcpy(dir_path_bar,dir_path);
strcat(dir_path_bar,"/");
#endif
DIR *dir = opendir(dir_path);
#if 1
if (dir == NULL) {
perror(dir_path);
exit(1);
}
#endif
for (struct dirent *entry = readdir(dir); entry != NULL;
entry = readdir(dir)) {
printf("Next entry is %s\n", entry->d_name);
// NOTE/BUG: because you don't reset dir_path_bar, this just keeps appending
// to it
#if 0
char *entry_path = strcat(dir_path_bar, entry->d_name);
#else
char entry_path[PATH_MAX];
strcpy(entry_path,dir_path_bar);
strcat(entry_path,entry->d_name);
#endif
printf("\n");
printf("%s\n", entry_path);
struct stat buf;
stat(entry_path, &buf);
// NOTE/BUG: these need one or more of: %d/%ld/%lld (vs %s)
#if 0
printf("Its inode number is %s\n", entry->d_ino);
printf("Its inode number is %s\n", buf.st_ino);
printf("Its uid is %s\n", buf.st_uid);
printf("Its size is %s bytes\n", buf.st_size);
#else
printf("Its inode number is %ld\n", entry->d_ino);
printf("Its inode number is %ld\n", buf.st_ino);
printf("Its uid is %d\n", buf.st_uid);
printf("Its size is %ld bytes\n", buf.st_size);
#endif
};
closedir(dir);
return 0;
}
Two problems:
You're appending continuously to the input (argv[1]) argument which is undefined behaviour. You can't append to the strings of argv.
Also printing integer values using %s which is undefined as well. %s expects a char * argument but you wanted to print integer values.
You can instead use a temporary buffer and pass it to stat(2):
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
if (argc != 2) {
printf("Usage: %s dir\n", argv[0]);
exit(1);
}
char* dir_path = argv[1];
DIR* dir = opendir(dir_path);
if (!dir) {
perror("opendir");
exit(1);
}
for(struct dirent* entry = readdir(dir); entry != NULL; entry = readdir(dir)) {
char entry_path[PATH_MAX] = {0};
int rc = snprintf(entry_path, sizeof entry_path, "%s/%s", dir_path, entry->d_name);
if ( rc < 0 || rc >= sizeof entry_path) {
fprintf(stderr, "Path truncated for '%s'\n", entry->d_name);
continue;
}
printf("Next entry is: %s\n", entry_path);
struct stat buf;
if (stat(entry_path, &buf) == 0) {
printf("Its inode number is %ju\n", (uintmax_t)entry->d_ino);
printf("Its inode number is %ju\n", (uintmax_t)buf.st_ino);
printf("Its uid is %jd\n", (intmax_t)buf.st_uid);
printf("Its size is %jd bytes\n", (intmax_t)buf.st_size);
} else {
perror("stat");
}
}
closedir(dir);
}
I have also added some error checking.
Not shown in the other 2 earlier answers is a nice avoidance of excessive copying.
When forming the entry_path, only the entry itself needs to be overwritten, not the entire string. This becomes valuable with a long pre-fixed directory string.
dir_path_len = strlen(dir_path);
if (dir_path_len >= PATH_MAX - 1) { return EXIT_FAILURE; } // too long
char entry_path[PATH_MAX];
strcpy(entry_path, dir_path);
strcpy(entry_path + dir_path_len++, "/"); // Can use strcpy() here
DIR *dir = opendir(dir_path);
...
for (struct dirent *entry = readdir(dir); entry != NULL; entry = readdir(dir)) {
printf("Next entry is %s\n", entry->d_name);
entry_len = strlen(entry->d_name);
if (dir_path_len + entry_len >= PATH_MAX) {
continue;
// or
return EXIT_FAILURE; // too long
}
strcpy(path + dir_path_len, entry->d_name); // strcpy(), not strcat()
printf("\n%s\n", entry_path);
struct stat buf;
if (stat(entry_path, &buf) ...
...
Trying to make a pwd for the c shell. This is what I found on a website and wanted to learn more about it.
I have use debugging printf statements all the way through the program already and it returns the "." instead of the actual dir name all the way through. What am I missing? Why would this be happening?
#include <dirent.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main()
{
struct stat stat_buf;
struct dirent *file_info;
ino_t itself_ino; /* holds current folder inode */
ino_t parent_ino; /* holds parent folder inode */
char Current[PATH_MAX]; /* folder name */
char Path[PATH_MAX]; /* holds the full path */
char Slash[PATH_MAX]; /* add / before the folder name */
DIR *dir;
while (1)
{
dir = opendir(".");
if(dir == NULL) {
fprintf(stderr, "cannot get current directory.\n");
exit(-1);
}
/* read the information about the current folder */
file_info = readdir(dir);
lstat(file_info->d_name, &stat_buf);
itself_ino = stat_buf.st_ino;
closedir(dir);
chdir(".."); /* go to parent directory */
dir = opendir(".");
file_info = readdir(dir);
lstat(file_info->d_name, &stat_buf);
parent_ino = stat_buf.st_ino;
if(itself_ino == parent_ino) {
/*closedir(dir);*/
break;
} else {
strcpy(Slash, "/");
strcpy(Current, file_info->d_name);
strcat(Slash, Current); /* add "/" as the first */
strcat(Slash, Path); /* charcter of the directory */
/* check the length of the pathname */
if(strlen(Slash) >= PATH_MAX) {
fprintf(stderr, "Error! Path too long!\n");
exit(0);
}
/* save the full pathname */
strcpy(Path, Slash);
}
closedir(dir);
}
/* print the full path of the current working directory */
printf("%s\n", Path);
return 0;
}
It's just realpath:
if (realpath(".", &Path) == NULL) {
// handle error
}
However maybe you aim at getcwd or get_current_dir_name.
printf("%s\n", get_current_dir_name());
I am trying to copy files from one directory to another using pthreads. Each thread is responsible for copying exactly one file. The maximum number of threads is specified via a command line argument.
What I want to do is IF the current threads is less than the max threads, create a thread to do work. ELSE, wait for the current threads and when one of them finishes, decrease the number of current threads.
I can't figure out how to wait for a thread via pthread_join without blocking the main thread.
Here is what I have so far:
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#define MAXNAME 80
#define R_FLAGS O_RDONLY
#define W_FLAGS (O_WRONLY | O_CREAT)
#define W_PERMS (S_IRUSR | S_IWUSR)
#define KILOBYTE 0.001
void *copyfilepass(void *arg);
int main(int argc, char* argv[]){
int num_threads; //number of threads to execute in parallel
int cur_threads = 0; //number of threads currently executing
char filename[MAXNAME]; //will temporarily hold the file name
DIR* source; //pointer to the source directory
DIR* dest; //pointer to the destination directory
struct dirent* dentry; //pointer to the internal structure of the source directory
struct stat fst; //stats for each file in a directory
int error;
void *status;
//***BEGIN ERROR CHECKING***
if (argc != 4) {
fprintf(stderr, "Usage: %s sourceDir destDir numThreads\n", argv[0]);
return 1;
}
//check if source directory name is too long
if ( snprintf(filename, MAXNAME, "%s", argv[1]) == MAXNAME ) {
fprintf(stderr, "Source directory name %s too long\n", argv[1]);
return 1;
}
//check if destination directory name is too long
if ( snprintf(filename, MAXNAME, "%s", argv[2]) == MAXNAME ) {
fprintf(stderr, "Source directory name %s too long\n", argv[2]);
return 1;
}
//check if we can successfully open the source directory
if( (source = opendir(argv[1])) == NULL ) {
fprintf(stderr, "Error opening source directory %s\n", argv[1]);
return 1;
}
//check if we can successfully open the destination directory
if( (dest = opendir(argv[2])) == NULL ) {
fprintf(stderr, "Error opening destination directory %s\n", argv[2]);
return 1;
}
//***END ERROR CHECKING***
num_threads = atoi(argv[3]);
while( (dentry = readdir(source)) != NULL ){
//source path
char* path = (char*)malloc(sizeof(char) * (strlen(dentry->d_name) + strlen(argv[1]) + 2)); //need '.' + '/' + '\0'
sprintf(path, "%s%c%s", argv[1], '/', dentry->d_name);
//destination path
char* dest_path = (char*)malloc(sizeof(char) * (strlen(dentry->d_name) + strlen(argv[2]) + 2)); //need '.' + '/' + '\0'
sprintf(dest_path, "%s%c%s", argv[2], '/', dentry->d_name);
if(!stat(path, &fst)){ //stat() return 0 if successful
if(S_ISREG(fst.st_mode)){
int args[3];
pthread_t tid;
if ( (args[0] = open(path, R_FLAGS)) == -1 ) {
fprintf(stderr, "Failed to open source file %s: %s\n", path, strerror(errno));
continue;
}
if ( (args[1] = open(dest_path, W_FLAGS, W_PERMS)) == -1 ) {
fprintf(stderr, "Failed to open destination file %s: %s\n", dest_path, strerror(errno));
continue;
}
if(cur_threads < num_threads) {
++cur_threads;
if ( (error = pthread_create((&tid), NULL, copyfilepass, args)) ) {
--cur_threads;
fprintf(stderr, "Failed to create thread: %s\n", strerror(error));
tid = pthread_self(); /* cannot be value for new thread */
}
printf("file: %.03fKB %s\n", (fst.st_size * KILOBYTE), path);
}
}
}
}
//close directory
closedir(source);
return 0;
}
Better than spawning and reaping threads is to just create a fixed-size pool at the beginning and have them all consume from a work queue. This will reduce overhead and simplify your code.
By the way, using threads to solve this problem may not improve performance, depending on the filesystem you're operating on. Food for thought.
I am working on a version of graphical ls, representing the output of ls with a tree. I have gotten most of my code working but would like to be able to determine what directory to read from in the command line. I have tried using
DIR *d
d = opendir(argv[1]);
But this does not work and results in errors opening files as well as the size and other information not updating for the file.
Any information would be greatly appreciated! Thanks.
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#define _GNU_SOURCE
#include <limits.h>
void helper(DIR *, struct dirent *, struct stat, char *, int, char **);
void dircheck(DIR *, struct dirent *, struct stat, char *, int, char **);
int main(int argc, char *argv[]){
DIR *d;
// Create dirent struct
struct dirent *dir;
// Create stat struct
struct stat buf;
// current path for file
char currentPath[FILENAME_MAX];
// Depth for tree
int depth = 0;
// Checking for correct usage
if (argc != 2) {
printf("Usage: './gls <directory_name>'\n");
exit(0);
}
// Checking that file provided in command line actually exists
if (lstat(argv[(argc - 1)], &buf) < 0) {
printf("Error: No such file exists.\n");
return 1;
exit(0);
}
// General resource for printing files: http://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program%20*/
// Open the current directory
d = opendir (".");
if(d == NULL) {
printf("Error opening directory.\n");
return 1;
}
// Store the current directory into currentPath
if((getcwd(currentPath, FILENAME_MAX)) == NULL) {
printf("Error: No such file exists.\n");
return 1;
}
// Iterate through all items in directory
while((dir = readdir(d)) != NULL){
// Do not process . and ..
if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
continue;
// Forms the path for lstat
getcwd(currentPath, FILENAME_MAX);
strcat(currentPath, "/");
strcat(currentPath, dir->d_name);
if(lstat(currentPath, &buf) == -1){
perror("stat");
printf("Error could not open file\n");
}
getcwd(currentPath, FILENAME_MAX);
// Checks if file is a regular file
if(S_ISREG(buf.st_mode))
printf("| %s (regular file - %d - !checksum)\n", dir->d_name, (int)buf.st_size);
// Checks if file is a directory
else if(S_ISDIR(buf.st_mode)) {
printf("| %s (directory)\n", dir->d_name);
dircheck(d, dir, buf, currentPath, depth, argv);
}
// Checks if file is a symbolic link
else if(S_ISLNK(buf.st_mode)) {
// Resource used for absolute and relative paths: http://www.apiexamples.com/c/stdlib/realpath.html
char resolved_path[PATH_MAX];
realpath(currentPath, resolved_path);
printf("| %s (symbolic link - %s)\n", dir->d_name, resolved_path);
}
// Checks if file is a FIFO
else if(S_ISFIFO(buf.st_mode)) {
printf("| %s (fifo (named pipe))\n", dir->d_name);
}
}
// Close the directory and return 0 for success
closedir(d);
return 0;
}
// Recursive helper
// Resource used for some of helper function and dircheck function: http://stackoverflow.com/questions/4989431/how-to-use-s-isreg-and-s-isdir-posix-macros
void helper(DIR *d, struct dirent *dir, struct stat buf, char currentPath[FILENAME_MAX], int depth, char *argv[]){
int i = 0;
// Open directory in currentPath
if((d = opendir(currentPath)) == NULL)
printf("Error: Failed to open Directory ==> %s\n", currentPath);
// Read through directory
while((dir = readdir(d)) != NULL){
// If file is . or .. ignore
if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
continue;
getcwd(currentPath, FILENAME_MAX);
strcat(currentPath, "/");
strcat(currentPath, dir->d_name);
getcwd(currentPath, FILENAME_MAX);
// If file is a register
if(S_ISREG(buf.st_mode)){
for(i = 0; i < depth; i++) {
printf(" ");
printf("%s (%d bytes)\n", dir->d_name, (int)buf.st_size);
}
}
// If file is a directory
if(S_ISDIR(buf.st_mode)) {
if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
dircheck(d, dir, buf, currentPath, depth, argv);
}
}
}
// Change directory back
chdir("..");
closedir(d);
}
// Resource used as guideline in order to create helper and dircheck functions
// http://stackoverflow.com/questions/4989431/how-to-use-s-isreg-and-s-isdir-posix-macros
void dircheck(DIR *d, struct dirent *dir, struct stat buf, char currentPath[FILENAME_MAX], int depth, char *argv[]){
int i = 0;
strcat(currentPath, "/");
strcat(currentPath, dir->d_name);
// If two directories exists at the same section in the tree
if((chdir(currentPath)) == -1){
getcwd(currentPath, FILENAME_MAX);
strcat(currentPath, "/");
strcat(currentPath, dir->d_name);
getcwd(currentPath, FILENAME_MAX);
// Add --- based on the depth of the tree
for(i = 0; i <= depth; i++)
printf ("---");
printf("| %s (subdirectory)\n", dir->d_name);
depth++;
helper(d, dir, buf, currentPath, depth, argv);
}
else{
// Add --- based on the depth of the tree
for(i =0; i <= depth; i++)
printf("---");
printf("| %s (subdirectory)\n", dir->d_name);
chdir(currentPath);
depth++;
helper(d, dir, buf, currentPath, depth, argv);
}
}
You are reading stat before while loop, this is dir in your case.
Then for every file in directory you are checking st_mode, but this is not updated nowhere in the while loop.
if (stat(argv[(argc - 1)], &statBuf) < 0)
this line just query the dir info, so you will always get directory type.
you should query specific file under that dir,
so you can change the code like this:
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
/* Resource: http://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program */
/* Resource: http://cboard.cprogramming.com/linux-programming/131-stat.html */
#define MAX_FILE_NAME_LEN 256
int main(int argc, char *argv[])
{
char path[MAX_FILE_NAME_LEN] = {0};
struct stat statBuf;
if (argc != 2) {
printf("Usage: './gls <directory_name>'\n");
exit(0);
}
if (stat(argv[(argc - 1)], &statBuf) < 0) {
printf("Error: No such file exists.\n");
exit(0);
}
DIR *d;
struct dirent *dir;
//char currentPath[FILENAME_MAX];
d = opendir(argv[1]);
while ((dir = readdir(d)) != NULL) {
//getcwd(currentPath, FILENAME_MAX);
//strcat(currentPath, "/");
//strcat(currentPath, dir->d_name);
//if(stat(currentPath, &statBuf) == -1){
//printf("N")
//}
memset(path, 0, MAX_FILE_NAME_LEN);
snprintf(path,MAX_FILE_NAME_LEN,"%s%s",argv[1],dir->d_name);
//getcwd(currentPath, FILENAME_MAX);
if (stat(path, &statBuf) < 0) {
printf("Error: No such file exists.\n");
continue;
// exit(0);
}
if(S_ISREG(statBuf.st_mode)) {
printf("| %s (regular file - %d - !checksum!)\n", dir->d_name, (int)statBuf.st_size); /* If regular file */
}
if(S_ISDIR(statBuf.st_mode)) {
printf("| %s (directory - size)\n", dir->d_name);
}
if(S_ISLNK(statBuf.st_mode)) {
printf("| %s (symbolic link - points to !!!\n", dir->d_name);
}
if(S_ISFIFO(statBuf.st_mode)) {
printf("| %s (fifo (named pipe))\n", dir->d_name);
}
}
closedir(d);
return(0);
}
I have a some 50 files which is moved to a directory "/tmp" on some interval of time when its get modified. I am using inotify to watch this directory /tmp for these files moved to this directory so that I can merge these files to another file in another directory.
But the rate at which the files getting moved to this directory ("/tmp"), inotify is not able to give notifications for other files except one file.
How to watch the directory if multiple files with different names (unknown names) being created or moved to the directory using inotify.
I know I can create multiple watch discriptors for each file with its name .
But I dont know the file name which is getting created or moved to this directory. Dynamically the files gets created so I cannot create watch descriptors for each file.
Below is my code.
How can I check notifications for the multiple files gettign created in this directory.
Please help with the solution.
Your help is highly appreciated.
Thanks
int main( int argc, char **argv )
{
int length, i = 0;
int fd;
int wd;
char buffer[BUF_LEN];
fd = inotify_init();
if ( fd < 0 ) {
perror( "inotify_init" );
}
wd = inotify_add_watch( fd, "/tmp/", IN_MOVED_TO);
while (1){
struct inotify_event *event;
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if ( event->mask & IN_CREATE || IN_MOVED_TO) {
printf( "The file %s was created.\n", event->name );
}
}
}
( void ) inotify_rm_watch( fd, wd );
( void ) close( fd );
exit( 0 );
}
To monitor a directory for file creation or deletion, you can create an inotify instance and monitor with the flags IN_CREATE | IN_DELETE. To monitor a file or directory, you first create the inotify instance with inotify_init which will return a file descriptor. You can then add files/directories to monitor with inotify_add_watch and supply the proper flags to look for the desired changes. You can then simply use read which will block until changes meeting you criteria are detected.
A simple example to monitor a directory (given as input as the first argument [./tmp default]) is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int dir_exists (char *d);
int main (int argc, char **argv)
{
int length, i = 0;
int fd;
int wd;
char buffer[EVENT_BUF_LEN];
char *path = argc > 1 ? argv[1] : "./tmp";
/* check directory to monitor exists */
if (!dir_exists (path)) {
fprintf (stderr, "error: directory does not exist '%s'.\n", path);
return 1;
}
/* create inotify instance & validate */
if ((fd = inotify_init ()) < 0) {
perror ("inotify_init");
}
/* add path to inotify watch list monitor for file create, delete.
add IN_MOVED_FROM | IN_MOVED_TO or move to/from directory */
wd = inotify_add_watch (fd, path, IN_CREATE | IN_DELETE);
/* monitor path for new file creation or deletion
(read blocks until the change event occurs) */
if ((length = read (fd, buffer, EVENT_BUF_LEN)) < 0) {
perror ("read");
}
/* report name of file created or deleted */
while (i < length) {
struct inotify_event *event = (struct inotify_event *) &buffer[i];
if (event->len) {
if (event->mask & IN_CREATE) {
if (event->mask & IN_ISDIR) {
printf ("New directory %s created.\n", event->name);
} else {
printf ("New file %s created.\n", event->name);
}
} else if (event->mask & IN_DELETE) {
if (event->mask & IN_ISDIR) {
printf ("Directory %s deleted.\n", event->name);
} else {
printf ("File %s deleted.\n", event->name);
}
}
}
i += EVENT_SIZE + event->len;
}
/* remove monitoring of path from the watch list. */
inotify_rm_watch (fd, wd);
/* close the inotify instance */
close (fd);
return 0;
}
/** test that directory exists (>=1 success, 0 otherwise)
* NOTE: no directory is actually created. fail occurs instead.
*/
int dir_exists (char *d)
{
int flags = O_DIRECTORY | O_RDONLY;
int mode = S_IRUSR | S_IWUSR;
int fd = open (d, flags, mode);
if (fd < 0) /* directory does not exist */
return 0;
else if (fd) { /* directory exists, rtn fd */
close (fd);
}
return fd;
}
Compile
gcc -Wall -Wextra -o bin/inotify_watch inotify_watch.c
Example Use
$ ./bin/inotify_watch &
[1] 16916
$ touch tmp/file.txt
New file file.txt created.
[1]+ Done ./bin/inotify_watch
Use functions FindFirstFile and FindNextFile. For example, show this code:
#include <windows.h>
#include <stdio.h>
#include <sys/stat.h>
#include <io.h>
/*
HANDLE FindFirstFile
(
LPCTSTR lpFileName, // какой файл ищем, можно указывать маску *, ?
LPWIN32_FIND_DATA lpFindFileData // указатель на структуру с информацией
);*/
//В случае ошибке вернет INVALID_HANDLE_VALUE. Для продолжения поиска используется функция:
/*
BOOL FindNextFile
(
HANDLE hFindFile, // указатель на поиск
LPWIN32_FIND_DATA lpFindFileData // указатель на структуру с информацией
);*/
//Write this any address
#define DISK "C:\\"
void main()
{
WIN32_FIND_DATA FindFileData;
HANDLE hf;
struct stat st;
hf=FindFirstFile(DISK"*", &FindFileData);
if (hf!=INVALID_HANDLE_VALUE)
{
do
{
char s[MAX_PATH] = DISK;
int a;
strcat(s, FindFileData.cFileName);
stat(s, &st);
a = access(s, 04);
printf("%s\t\t%s\n", FindFileData.cFileName, st.st_mode & S_IFDIR ? "Directory" : (st.st_mode & S_IFREG ? "File" : "Other"));
}
while (FindNextFile(hf,&FindFileData)!=0);
FindClose(hf);
}
getchar();
}