Linux cp command implementation to copy multiple files to a Directory - c

I'm currently learning systems programming and came across an implementation of the Linux cp command in C. This implementation though from my understanding allows for copying the contents of one file to another file in the same directory and also copying a file into a Directory in the current directory.
How could you change this code to allow for multiple files to be copied to a directory at once (i.e "copy f1.txt f2.txt f3.txt /dirInCurrentDir")
or even ("copy d1/d2/d3/f1 d4/d5/d6/f2 d ") which would copy the 2 files to directory d. I know that the changes would have to occur in main() but how could you add to the if-else statement?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#define BUFFERSIZE 1024
#define COPYMORE 0644
void oops(char *, char *);
int copyFiles(char *src, char *dest);
int dostat(char *filename);
int mode_isReg(struct stat info);
int main(int ac, char *av[])
{
/* checks args */
if(ac != 3)
{
fprintf(stderr, "usage: %s source destination\n", *av);
exit(1);
}
char *src = av[1];
char *dest = av[2];
if( src[0] != '/' && dest[0] != '/' )//cp1 file1.txt file2.txt
{
copyFiles(src, dest);
}
else if( src[0] != '/' && dest[0] == '/' )//cp1 file1.txt /dir
{
int i;
for(i=1; i<=strlen(dest); i++)
{
dest[(i-1)] = dest[i];
}
strcat(dest, "/");
strcat(dest, src);
copyFiles(src, dest);
}
else
{
fprintf(stderr, "usage: cp1 source destination\n");
exit(1);
}
}
int dostat(char *filename)
{
struct stat fileInfo;
//printf("Next File %s\n", filename);
if(stat(filename, &fileInfo) >=0)
if(S_ISREG(fileInfo.st_mode))
return 1;
else return 0;
return;
}
int copyFiles(char *source, char *destination)
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
/* open files */
if( (in_fd=open(source, O_RDONLY)) == -1 )
{
oops("Cannot open ", source);
}
if( (out_fd=creat(destination, COPYMORE)) == -1 )
{
oops("Cannot create ", destination);
}
/* copy files */
while( (n_chars = read(in_fd, buf, BUFFERSIZE)) > 0 )
{
if( write(out_fd, buf, n_chars) != n_chars )
{
oops("Write error to ", destination);
}
if( n_chars == -1 )
{
oops("Read error from ", source);
}
}
/* close files */
if( close(in_fd) == -1 || close(out_fd) == -1 )
{
oops("Error closing files", "");
}
return 1;
}
void oops(char *s1, char *s2)
{
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);
}

You would loop through all the argument values (from av[1] to av[ac - 2]) and copy it to the destination argument, which would be av[ac - 1].
In your case, you would pass av[i] and av[ac - 1] to the copyFiles function, where i would be your loop index.

Related

How to run this c program in Ubuntu Linux [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I just saw a file about copying a file online but I dont know how to run it, I tried "gcc -o a a.c" to create a program, then when I typed "./a" it says "usage: ./a source destination" as error. What should I do?
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
void oops(char *, char *);
void copy_file(char *, char *);
void copy(char *, char *);
int main(int ac, char *av[]) {
if (ac != 3) {
fprintf(stderr, "usage: %s source destination\n", *av);
exit(1);
}
copy(av[1], av[2]);
return 0;
}
void oops(char *s1, char *s2) {
fprintf(stderr, "Error: %s ", s1);
perror(s2);
exit(1);
}
void copy_file(char *file1, char *file2)
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE];
struct stat in_stat, out_stat;
Check if two files are existed
if (stat(file1, &in_stat) == 0 && stat(file2, &out_stat) == 0)
{
//Check if two files are same
if (in_stat.st_ino == out_stat.st_ino)
{
//if same, do nothing
return;
}
}
if ( (in_fd=open(file1, O_RDONLY)) == -1 )
oops("Cannot open ", file1);
if ( (out_fd=creat( file2, COPYMODE)) == -1 )
oops( "Cannot creat", file2);
while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 )
if ( write( out_fd, buf, n_chars ) != n_chars )
oops("Write error to ", file2);
if ( n_chars == -1 )
oops("Read error from ", file1);
if ( close(in_fd) == -1 || close(out_fd) == -1 )
oops("Error closing files","");
if (chmod(file2, in_stat.st_mode) != 0) {
oops("Error change file mode", "");
}
}
void copy(char *file1, char *file2)
{
struct stat in_stat, out_stat;
struct dirent *direntp;
DIR *dir_ptr;
if (stat(file1, &in_stat) != 0)
{
oops("File not exist", "");
}
if (S_ISDIR(in_stat.st_mode))
{
if (stat(file2, &out_stat) != 0)
{
if (mkdir(file2, in_stat.st_mode) != 0)
{
oops("Failed to create directory", "");
}
}
if ((dir_ptr = opendir(file1)) == NULL)
{
oops("Failed to open directory", "");
} else {
while ((direntp = readdir(dir_ptr)) != NULL)
{
if (strcmp(direntp->d_name, ".") && strcmp(direntp->d_name, ".."))
{
char *new_file1 = (char *)malloc(strlen(file1)+strlen(direntp->d_name)+2);
strcpy(new_file1, file1);
strcat(new_file1, "/");
strcat(new_file1, direntp->d_name);
char *new_file2 = (char *)malloc(strlen(file2)+strlen(direntp->d_name)+2);
strcpy(new_file2, file2);
strcat(new_file2, "/");
strcat(new_file2, direntp->d_name);
copy(new_file1, new_file2);
free(new_file1);
free(new_file2);
}
}
closedir(dir_ptr);
}
}else {
copy_file(file1, file2);
}
}
I also tried to add pathname after "./a" but still not working
Sorry that I am pretty new to c language
Take a look at the first few lines of main.
int main(int ac, char *av[]) {
if (ac != 3) {
fprintf(stderr, "usage: %s source destination\n", *av);
exit(1);
}
The first argument to main, ac, is the number of arguments. You have code that prints an error message when ac is not equal to exactly three, and then it exits.
When you use
./a
The number of arguments to the program is 1, just the program itself.
It appears that your program expects a source file and a destination file to work correctly. You need to provide them in the command line.
./a file-1 file-2
where file-1 is the source file and file-2 is the destination file.
Your code wants to take 2 arguments after the “.\a” which are 2 textfile path. After adding this arguments, you can run your code without error messages.
Usage:
“.\a -filepath1 -filepath2”

Program opens the same named pipe and writes to it many times with C

I created two programs, which will communicate via named pipe, one will be reading from it and another one will be writing to it. It works pretty fine now, except for the fact, that it opens and writes to the same fifo exactly 3 times. It's my first time with C and pipes, and I don't understand why is this writing three times. Can you see why is this writing three times?
writing.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#define BUFFSIZE 512
#define err(mess) { fprintf(stderr,"Error: %s.", mess); exit(1); }
void writing(char *s)
{
int fd;
ssize_t n;
char buf[BUFFSIZE];
printf("writing to %s\n",s);
if ( (fd = open(s, O_WRONLY)) < 0)
err("open")
while( (n = read(STDIN_FILENO, buf, sizeof buf -1) ) > 0) {
buf[n-1] = '\0';
printf("Received: %s\n", buf);
if ( write(fd, buf, n) != n) {
err("write");
}
if(strcmp(buf,"END")==0){
printf("%s","exit");
break;
}
}
close(fd);
}
char* concat(const char *s1, const char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
strcpy(result, s1);
strcat(result, s2);
return result;
}
int file_stat(char *argv){
int isfifo = 0;
struct stat sb;
printf("%s",argv);
if (stat(argv, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
printf("File type: ");
if (sb.st_mode & S_IFMT == S_IFIFO) {
printf("FIFO/pipe\n");
isfifo = 1;
}
printf("Ownership: UID=%ld GID=%ld\n",
(long) sb.st_uid, (long) sb.st_gid);
//exit(EXIT_SUCCESS);
return isfifo;
}
int main(int argc, char *argv[])
{
// READ ALL FILES IN DIRECTORY
if (argc != 2) {
fprintf(stderr, "Usage: %s /<pathname>/\n", argv[0]);
exit(EXIT_FAILURE);
}
DIR *d;
struct dirent *dir;
if ((d = opendir (argv[1])) != NULL) {
/* print all the files and directories within directory */
while ((dir = readdir (d)) != NULL) {
printf ("%s\n", dir->d_name);
char* s = concat(argv[1], dir->d_name);
if (file_stat(s) == 1) {
writing(s);
}
else {
mkfifo("fifo_x", 0666);
writing("fifo_x");
}
free(s);
}
closedir (d);
}
else {
/* could not open directory */
perror ("error: ");
return EXIT_FAILURE;
}
}
reading file is the same except for "reading" function and call to reading()
reading
void reading(char *s)
{
int fd;
ssize_t n;
char buf[BUFFSIZE];
printf("%s",s);
if ( (fd = open(s, O_RDONLY)) < 0)
err("open");
while( (n = read(fd, buf, sizeof buf - 1) ) > 0) {
buf[n-1] = '\0';
if(strcmp(buf,"END")==0){
printf("%s\n", "exit");
break;
}
buf[n-1] = '\n';
if ( write(STDOUT_FILENO, buf, n) != n) {
exit(1);
}
}
close(fd);
}
the output
/home/..File type: Ownership: UID=0 GID=0
writing to fifo_x
END
Received: END
exitola
/home/olaFile type: Ownership: UID=1001 GID=1001
writing to fifo_x
END
Received: END
exit.
/home/.File type: Ownership: UID=0 GID=0
writing to fifo_x
END
Received: END
exit
You have three files in the directory with whose pathname you called your program. All three files are not fifo's so for each you write to fifo_x.
The file names are
.
..
olaFile
Maybe you should explicitly exclude the files
.
..
which happen to be in every directory in linux and represent the current directory . and the parent directory ...

A C program that copies all the content a directory including files and folders

The last Code I just posted now works. That is, it is able to copy all files from one directory to another. But now, I wanted to update it in such a way that it copies also directories including it contents be it files or folders.
Here is what I did so far, but this has been unable to accomplish my dream.
I really don't know what is wrong with the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#define Max 8192
int copy_files(char *src, char *dest);
int copy_dir(char *srcpath, char *destpath);
int copy_dir(char *srcpath, char *destpath)
{
DIR *sdp = NULL;
DIR *ddp = NULL;
struct dirent *entry;
struct stat sb;
char tempsrc[strlen(srcpath)+1];
char tempdest[strlen(destpath)+1];
strcat(srcpath, "/");
strcat(destpath, "/");
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
if( (sdp = opendir(srcpath)) == NULL )
{
printf ("%s is not an existing directory\n", srcpath);
return 0;
}
else
{
while( (entry = readdir(sdp)) )
{
stat(entry->d_name, &sb);
// printf("Cannot open directory\n");
// exit(EXIT_FAILURE);
switch (sb.st_mode & S_IFMT)
{
case S_IFREG:
{
strcat(tempdest, entry->d_name);
strcat(tempsrc, entry->d_name);
copy_files(tempsrc, tempdest);
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
break;
}
case S_IFDIR:
{
strcat(tempsrc, entry->d_name);
strcat(tempdest, entry->d_name);
mkdir(tempdest, 0777);
ddp = opendir(tempdest);
copy_dir(tempsrc, tempdest);
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
break;
}
}
}
closedir(sdp);
closedir(ddp);
return 1;
}
}
int copy_files(char *src, char *dest)
{
int sfd, dfd, ret_in, ret_out;
char buff[Max];
if ( (sfd = open(src, O_RDONLY)) == -1 )
{
printf("Error while reading %s\n", src);
perror(src);
exit(1);
}
if ( (dfd = creat(dest, 0644)) == -1 )
{
printf("Error while creating %s\n", dest);
perror(dest);
exit(1);
}
while( (ret_in = read(sfd, &buff, Max)) > 0 )
{
ret_out = write (dfd, &buff, ret_in);
if (ret_out != ret_in)
{
printf("write error to %s", dest);
perror(dest);
exit(1);
}
if (ret_in == -1)
{
printf("read error from %s", src);
perror(src);
exit(1);
}
}
close(sfd);
close(dfd);
return 1;
}
int main(int argc, char *argv[])
{
int i;
if (argc != 3)
{
printf ("Usage: Programme_name src dest\n e.g. ./cp src dest\n");
exit(1);
}
char *srcp = argv[1];
char *destp = argv[2];
if (srcp[0] == '/' && destp[0] == '/')
{
for (i = 1; i <= strlen(destp); i++)
destp[(i-1)] = destp[i];
for (i = 1; i <= strlen(srcp); i++)
srcp[(i-1)] = srcp[i];
copy_dir(srcp, destp);
}
else if (srcp[0] != '/' && destp[0] == '/') //./ass1 test /t2
{
for (i = 1; i <= strlen(destp); i++)
destp[i-1] = destp[i];
strcat(destp, "/");
strcat(destp, srcp);
copy_files(srcp, destp);
}
else
{
printf ("Usage: Programme_name src dest\n e.g. ./cp src dest\n");
exit(1);
}
}
You are indefinitely adding /. to the temporary source and destination paths when the directory entry . is read, which is present in all directories. Instead, you should skip the . and .. entries.
Another error is the dimensioning of the temporary paths:
char tempsrc[strlen(srcpath)+1];
char tempdest[strlen(destpath)+1];
The arrays are made just long enough to hold the original paths, though sub-directory names are then appended, overflowing the arrays. Better:
char tempsrc[PATH_MAX];
char tempdest[PATH_MAX];

Opening a file using relative path

The following code is supposed to work as follows: print the list of the files in a directory, and print the content of each .c file.
it works fine when executed in UNIX for the same directory: ./a.out ./
However, I was not able to make it work for ./a.out ../differentDir execution.
I know that if the absolute path is provided as an argument, I could use argv[1] for that. However, when it is provided in a form of a relative path I am lost.
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFSIZE 32768
int main(int argc, char **argv) {
char buf[BUFFSIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
if ((dp = opendir(argv[1])) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}
Upon opening the file to read, the file pathname needs to also be relative.
// Form prefix for complete relative file name
char filename[MAXPATH];
strcpy(filename, argv[1]);
// append '/' if directory path does not end in '/'
if (TBD_code(filename)) {
strcat(filename, "/");
}
char *end = filename[strlen(filename)];
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
if (findC(dirp->d_name)) {
// append filename to prefix
strcpy(end, dirp->d_name);
fp=fopen(filename, "r");
...
You can use realpath(argv1...) like in this example. realpath will return the absolute path for a relative path.
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(int argc, char **argv) {
char *path = "../..";
char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */
char *res = realpath(path, buff);
if (res) {
printf("This source is at %s.\n", buff);
} else {
perror("realpath");
exit(EXIT_FAILURE);
}
return 0;
}
To include the desired behavior in your program, you can use realpathin your code:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#define BUFFSIZE 32768
int main(int argc, char **argv) {
char buf[BUFFSIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length;
FILE *fp;
char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
char *res = realpath(argv[1], buff);
if ((dp = opendir(res)) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}
You could first change to the directory chdir either with relative or absolute path and the get the absolute path via the getcwd
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFSIZE 32768
#define PATH_SIZE 512
int main(int argc, char **argv) {
char buf[BUFFSIZE];
char path[PATH_SIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length, r;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
strcpy(path, argv[1]);
r = chdir(path);
if( r != 0 )
{
printf("Invalid path '%s'\n",path);
exit(1);
}
getcwd(path,PATH_SIZE);
if ((dp = opendir(path)) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}

stat() returns error

I have to know the modification date of some files in a folder. It works, but not with all types of files.
For example it works with .c, .txt, but it doesn't work with other types such .mp4, .jpg and .mp3 (the application I'm creating have to work with multimedia files in general). It prints "Cannot display the time.", so I suppose the problem is on stat(). Thanks.
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
char parola[12]="", hash[32]="", esadecimale[1000]="", system3[100]="./md5 ";
int i, len, len2;
int bytes;
char cwd[1024];
int main(void)
{
char t[100] = "";
struct stat b;
DIR *dp;
char destinationFolder[100] = "/Users/mattiazeni/Desktop/Prova/"; //Me la passa da sopra
struct dirent *dir_p;
dp = opendir(destinationFolder);
if ( dp == NULL ) exit(1);
len = strlen(destinationFolder);
for (i=0;i<len;i++) {
system3[i+6]=destinationFolder[i];
}
while( ( dir_p = readdir(dp) ) != NULL ) {
if (dir_p -> d_name[0] != '.') {
//printf("%s\n", dir_p -> d_name);
len2 = strlen(dir_p -> d_name);
for (i=0;i<len2;i++) {
if (dir_p -> d_name[i] == ' '){ //Mi serve per correggere i nomi dei file con spazi
system3[i+len+6]='\\';
}
else system3[i+len+6]=dir_p -> d_name[i];
}
system(system3); //Passa il valore a md5 che calcola l'hash e lo stampa nel file che ci serve insieme al persorso/nome del file
FILE *fp;
if((fp=fopen("userDatabase.txt", "ab"))==NULL) {
printf("Error while opening the file..\n");
fclose (fp);
}
else {
if (!stat(dir_p -> d_name, &b)) {
strftime(t, 100, "%d/%m/%Y %H:%M:%S", localtime( &b.st_mtime)); //C'è ancora qualche errore!!
fprintf(fp, "%s", t);
}
else {
perror(0);
fprintf(fp, "error");
}
fprintf(fp, " initialized");
fprintf(fp, "\n");
}
fclose (fp);
for (i=len+6;i<len+6+len2;i++) {
system3[i]=' ';
}
}
}
closedir(dp);
return 0;
}
Use perror(). Also shouldn't you use st_mtime?
stat:
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
99% sure it is because dir_p -> d_name does not exist, which in turn probably is because of a localization issue.
You could do something like:
fprintf(stderr,
"Unable to stat %s\n",
dir_p->d_name);
perror(0);
Also; shouldn't it be ->f_name and not ->d_name if you are checking file status? - (Unless you use d_name for file name off course.)
And your fclose(fp) is outside your fp == NULL check. As you do not return or otherwise abort the flow you risk an SIGSEGV if the fopen fail.
Edit: What do you get with something like this?
#include <unistd.h>
char cwd[1024];
...
} else {
fprintf(stderr,
"Unable to stat '%s'\n",
dir_p->d_name);
perror(0);
if (getcwd(cwd, sizeof(cwd)) == NULL) {
perror("getcwd() error");
} else {
fprintf(stderr,
"in directory '%s'\n",
cwd);
}
}
Edit2:
First; I said getcwd() != NULL should be ==. Se change. (Bad by me.)
The problem in your code. (There is a few more) but regarding stat - you use d_name from readdir. This is only filename; not dir+filename. Thus; you get i.e.:
stat(dir_p->d_name, ...)
Which becomes i.e.:
stat("file.mp4", ...)
Easiest quick-fix (tho dirty) would be:
/* you need to terminate the system string after your for loop */
system3[i + len + 6] = '\0';
system(system3);
if (!stat(system3 + 6, &b)) {
You should use the complete pathname for stat(). Stat does not know which directory you are interested in.
...
char bigbuff[PATH_MAX];
sprintf( bigbuff, "%s/%s", destinationFolder, dir_p->d_name);
rc = stat (bigbuff, &b);
...
This is the final working code in order to scan a directory for files, and print them on a txt output file with the modification date:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
char system3[6]="./md5 ";
int main(void)
{
char t[100] = "";
char bigbuff[200];
struct stat b;
char destinationFolder[100] = "/Users/mattiazeni/Desktop/Prova"; //Me la passa da sopra
DIR *dp;
struct dirent *dir_p;
dp = opendir(destinationFolder);
if ( dp == NULL ) exit(1);
while( ( dir_p = readdir(dp) ) != NULL ) {
if (dir_p -> d_name[0] != '.') {
sprintf( bigbuff, "%s%s/%s",system3, destinationFolder, dir_p->d_name);
system(bigbuff);
FILE *fp;
if((fp=fopen("userDatabase.txt", "ab"))==NULL) {
printf("Error while opening the file..\n");
fclose (fp);
}
else {
sprintf( bigbuff, "%s/%s", destinationFolder, dir_p->d_name);
if (!stat(bigbuff, &b)) {
strftime(t, 100, "%d/%m/%Y %H:%M:%S", localtime( &b.st_mtime)); //C'è ancora qualche errore!!
fprintf(fp, "%s", t);
}
else {
perror(0);
fprintf(fp, "error");
}
fprintf(fp, "\n");
}
fclose (fp);
}
}
closedir(dp);
return 0;
}
Thanks all for the help!

Resources