I just want to 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
The word directory doesn't even appear in the C standard. This is an OS concept.
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.
Related
I'm trying to create a folder using mkdir in C but it wont't work
the code won't create the folders
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
int main (){
char chemin[256];
char name[20];
//char fichier[100];
// FILE *fp;
strcpy(chemin,"/home/Deva/Documents/prog/C/la/sds");
mkdir(chemin,0755);
if (mkdir(chemin,0755)==-1){
printf("\nERROR\n");
} else { printf("fichier creer"); }
}
You have to create the intermediate directories first: e.g. you must create /home/Deva before creating /home/Deva/Documents, etc.
you are passing PATH not just directory name.
so please check below post would help
Recursive mkdir() system call on Unix
2 things:
1) You need to create the intermediate directories first.
2) You cannot create a directory twice, which you do here:
mkdir(chemin,0755);
if (mkdir(chemin,0755)==-1){
Here is what I need to do:
Write a C program to calculate the total size of the files in a
directory and the sub-directories of the directory. Note that the
total size should include the sizes of sub-directories (directories
are also files) and size of the top-level directory. If a file is a
symbolic link, the symbolic link should not be dereferenced. So the
size of the symbolic link is added to the total size, not the size of
the file pointed by the link. To facilitate testing, your program
should print out ONLY the total size in bytes, i.e., ONE number,
nothing else.
Here is what I have:
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
int main(int argc, char *argv[]){
char* folder = argv[1];
long length;
DIR* dir_p;
struct dirent* dir_element;
struct stat file_info;
dir_p = opendir(folder);
while(dir_element = readdir(dir_p)){
lstat(dir_element->d_name, &file_info);
length+=file_info.st_size;
printf("%s\n" , dir_element->d_name);
}
return 0;
}
The problem here is that I do not get the right number for some reason. I check the output of my program with du -abc "directory_pathname" call in Ubuntu(Linux).
You never initialize length. Also, you need to actually recurse into directories. Put the while into a function, then call that function for each directory you find (check man stat to figure out how to tell if the file is a directory; I forget the exact test off the top of my head but it's a member of struct stat).
Basically, this should be a simple piece of code that opens a directory stream and looks for symbolic links. Whenever a symbolic link is found, it should print ("Symbolic link found");
However, the lstat(dirp->d_name,&bufcall always returns a value < 0, and I don't know why.
I created the two symbolic link opening the file folder, opening a terminal window inside the folder and running
ln -s ciao.txt link1 and
ln -s ciao2.txt link2
I know I should call closedir() later in my code, please don't care about this.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
void main (int argc, char* argv[])
{
char buffer[100],dir[100];
struct stat buf;
int x;
DIR *dp;
struct dirent *dirp;
if((dp=opendir(argv[1]))==NULL)
{
printf("\nError opening directory stream, now exiting...\n");
exit(-1);
}
while((dirp=readdir(dp))!=NULL)
{
lstat(dirp->d_name,&buf);
if(S_ISLNK(buf.st_mode))
printf("\n%s Is a symbolic link\n",dirp->d_name);
else
printf("\n%s Is not a symbolic link\n",dirp->d_name);
}
}
Some help would be appreciated. Thanks.
d_name is the file name in the directory, not a full path name. You must eather chdir into the directory you are looking at, or construct full path names for the files.
The simplest solution is to add this line just before your while loop:
chdir(argv[1]);
I am wrting a shell. I need a function to determine if the command entered in the shell by
the user is a valid builtin command. I'm not sure how to go about doing this.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
int is_builtin(command_t* command) {
// TODO: Iterate through `valid_builtin_commands`
while (valid_builtin_commands[i] != NULL )
i++
if(valid_builtin_commands[i] == command){
return true
}
return -1;
}
im trying to accomplish more along these lines in limited in the libraries i can use.
I have a magic crystal ball which says:
int is_builtin(command_t* command) {
return (command->flags & CMD_BUILT_IN) != 0;
}
Try that! You might have to define flags in the command_t structure, and populate that at the time the command_t object is instantiated from parsing the command input. Also, to supply the CMD_BUILT_IN constant in some header file somewhere.
I am trying to create a 'test.txt' file in root directory. Currently I am not a root user. my code is as follows:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int fd;
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
perror("error: ");
close(fd);
return 0;
}
however, when I compile and execute it, I get the error:
error: : Permission denied
how can I make my code to have the permission to create a file in root directory?
P.S. I am trying to make this work so that I can use it later on to apply this method to my linux daemon program.
fd=open("/test.txt",O_CREAT|O_RDWR|O_TRUNC,0777);
In this line specify path correctly.(i.e) It is current working directory means, keep (dot) . before / like./test.txt