EXT2 Directory Content - c

Hi I reached inode 2 , the root directory. I know the direct block number of it, which is 265. How can I list the content of the root directory in C?

This should work. I suggest looking up the man pages for opendir() and readdir(). This is not based on inodes. Do you require to be able to look up directories based on inode?
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR *dir = opendir("/");
if(dir==NULL) {
perror("Couldn't open dir");
exit(1);
}
printf("opened\n");
struct dirent * entry;
while((entry = readdir(dir))) {
printf("%s\n", entry->d_name);
}
return 0;
}

Related

Scan all files in Linux using C [duplicate]

This question already has answers here:
stat() error 'No such file or directory' when file name is returned by readdir()
(2 answers)
Closed 5 years ago.
I am trying to scan all files in my computer, including files in subdirectories. The program should start in the root folder "/".
logfind.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <stdlib.h>:
#include <dirent.h>
int is_directory(const char *path) {
struct stat path_stat;
stat(path, &path_stat);
return S_ISDIR(path_stat.st_mode);
}
void list_files(char *pathus) {
struct dirent *de;
DIR *dr = opendir(pathus);
if (dr == NULL) {
printf("Could not open current directory");
exit(1);
}
while ((de = readdir(dr)) != NULL) {
if (is_directory(de->d_name) > 0)
list_files(de->d_name);
else
printf("%s\n", de->d_name);
}
closedir(dr);
}
int main(int argc, char **argv) {
list_files("/");
return 0;
}
The output I get when running this is basically "logfind.c" many times,
anyone has an idea as to why this happens?
This happens because there's nothing in your code to change the process' current directory, or build the full path of the directory to scan.
If you're in /home/danny/src/ when you run this, and find dev while listing /, you can't opendir("dev"), that'd mean /home/danny/src/dev, not /dev.

How to move the files from one directory to another in c?

I am creating a project on moving a directory files by creating sub-folders according to their particular type in c. I have made up to creating directories with the help of POSIX library dirent.h for the files having different extension present in the home directory but I don't know how to cut a file from home directory and paste in its particular sub-folder. So please guide me about how can I cut and paste a file from one directory to another in c.
Use rename(DestinationFilepath, SourceFilepath);
For more info check man page http://linux.die.net/man/2/rename
For two different system use cURL library:
http://en.wikipedia.org/wiki/CURL
Code in C:
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#define DESTINATION_FOLDER "/home/second/"
#define SOURCE_FOLDER "/home/first/"
void printdir()
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
struct tm *tm;
char src_folder[1024];
char dest_folder[1024];
if ((dp = opendir(SOURCE_FOLDER)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", SOURCE_FOLDER);
return;
}
chdir(SOURCE_FOLDER);
while ((entry = readdir(dp)) != NULL) {
lstat(entry->d_name, &statbuf);
if (!S_ISDIR(statbuf.st_mode)) {
sprintf(src_folder, "%s%s", SOURCE_FOLDER, entry->d_name);
sprintf(dest_folder, "%s%s", DESTINATION_FOLDER, entry->d_name);
printf("%s----------------%s\n", entry->d_name, dest_folder);
rename(src_folder, dest_folder);
}
}
chdir("..");
closedir(dp);
}
int main()
{
while (1) {
printdir();
}
rename("aaa.txt", "/home/second/bbb.txt");
printf("done.\n");
exit(0);
}

C Directory and Subdirectory Recursion

I tried to recursively get all files and folder list.But I can only get subdirectories of documents and inside of them.I can't get the other folders which in inside of subdirectory.
I don't know how to do it recursively.I hope you help me
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
#include <unistd.h>
#include <string.h>
void list(char *a);
void reader(char *path);
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("C:\\Users\\pen\\Documents\\");
if (dp != NULL)
{
while (ep = readdir (dp)){
GetFileAttributes(ep->d_name);
if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ep->d_name))
{
if (strcmp(".",ep->d_name)==0)
continue;
if (strcmp("..",ep->d_name)==0)
continue;
reader(ep->d_name);
}
}
closedir(dp);
}
else
perror ("Couldn't open the directory");
closedir(dp);
system("pause");
return 0;
}
void reader(char *path){
DIR *da;
struct dirent *ef;
da = opendir(path);
while (ef=readdir(da)){
printf ("%s\n",ef->d_name);
if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ef->d_name))
{
if (strcmp(".",ef->d_name)==0)
continue;
if (strcmp("..",ef->d_name)==0)
continue;
reader(ef->d_name);
}
}
closedir(da);
}
1) In reader you need to call closedir(da); after the while loop.
2) every call to reader needs to have the absolute path you need to concatenate path
to ef->d_name and then call reader.
3) also to enable debugging you should call perror after a failed readdir call.

scan a directory to find files in c

I'm trying to create a function in c which scans all my path C: \ temp (Windows) to search for a file that I pass (eg test.txt) and each time it finds one return the path to steps another function to write something in the bottom of this file.
I managed to do the function that writes to the file but can not figure out how to do that scans the folder and pass the address of the file found.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir);
return;
}
chdir(dir);
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4);
}
else printf("%*s%s\n",depth,"",entry->d_name);
}
chdir("..");
closedir(dp);
}
int main()
{
printf("Directory scan of /home:\n");
printdir("/home",0);
printf("done.\n");
exit(0);
}
Use FindFirstFile function. Here's a good example of this function using.

Infinite recursion while listing directories in linux

I try to write program where part of it is listing all directories (especially starting from /), but I have a problem with /proc/self which is infinitely recursive (I get /proc/self/task/4300/fd/3/proc/self/task/4300/fd/3/proc/self/task/4300/fd/3/proc/... and so on). What is nice way to deal with it?
EDIT: Program is written in C language and I use opendir(), readdir()
You can use the S_ISLNK macro to test the st_mode field returned by a call to lstat. If the file is a symbolic link, do not try to follow it.
[user#machine:~]:./list | grep link
/proc/mounts is a symbolic link
/proc/self is a symbolic link
Example code
#include <stdio.h> // For perror
#include <stdlib.h>
#include <sys/types.h> // For stat, opendir, readdir
#include <sys/stat.h> // For stat
#include <unistd.h> // For stat
#include <dirent.h> // For opendir, readdir
const char *prefix = "/proc";
int main(void)
{
DIR *dir;
struct dirent *entry;
int result;
struct stat status;
char path[PATH_MAX];
dir = opendir(prefix);
if (!dir)
{
perror("opendir");
exit(1);
}
entry = readdir(dir);
while (entry)
{
result = snprintf(path, sizeof(path), "%s", prefix);
snprintf(&path[result], sizeof(path) - result, "/%s", entry->d_name);
printf("%s", path);
result = lstat(path, &status);
if (-1 == result)
{
printf("\n");
perror("stat");
exit(2);
}
if (S_ISLNK(status.st_mode))
{
printf("%s", " is a symbolic link");
}
printf("\n");
entry = readdir(dir);
}
return(0);
}
From path_resolution(7):
Length limit
There is a maximum length for pathnames. If the pathname (or some intermediate pathname obtained while resolving symbolic links) is too long, an ENAMETOOLONG error
is returned ("File name too long").
I think you should employ similar behaviour: check for too long pathnames.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/param.h>
/* Short & sweet recursive directory scan, finds regular files only.
Good starting point, should work on Linux OS.
Pass the root path, and returns number of dirs and number of files
found.
*/
char *tree_scan( const char *path, int *ndirs, int *nfiles){
DIR *dir;
struct dirent *entry;
char spath[MAXPATHLEN] = "";
if( !(dir = opendir( path))){ perror("opendir"); exit(1);}
for( entry = readdir( dir); entry; entry = readdir( dir)){
sprintf( spath, "%s/%s", path, entry->d_name);
if( entry->d_type == DT_REG){ (*nfiles)++; printf( "%s\n", spath);}
if( entry->d_type == DT_DIR &&
(strcmp( ".", entry->d_name)) &&
(strcmp( "..", entry->d_name))){
(*ndirs)++; tree_scan( spath, ndirs, nfiles);
}
}
closedir( dir);
return(0);
}
/* Call it like so */
int i = 0, l = 0;
tree_scan( "/path", &i, &l);
printf("Scanned %d directories, %d files.\n", i, l);
I don't have a *nix terminal handy, but you could always take a look at the source for ls.c and see how it's done.
The source as part of the gnu core utils can be found here.
I created a ls clone a few years ago in school, and I think I got around it by watching the pathname size as ulidtko mentioned.

Resources