ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am having a rather strange issue. I keep getting a segfault in windows when I attempt to run the first program but if I remove the if and else statements it works properly. Also they both compile without any errors given.
Anyone have any ideas why ?
Program with if and else
Code:
#include<stdio.h>
#include<dirent.h>
#include<time.h>
#include<sys/stat.h>
#include<sys/types.h>
int main (int argc , char *argv[])
{
DIR *curdir = NULL;
struct dirent *dname = NULL;
struct stat *fstat;
struct tm *ftime;
int fcount = 0;
int dircount = 0;
unsigned int totalfsize = 0;
unsigned int fsize;
time_t *ptime = NULL;
if (argc == 1)
{
curdir = opendir(".");
printf("\nOpening .\n");
}
else
{
curdir = opendir(argv[1]);
printf("\nOpening %s\n",argv[1]);
}
while ((dname = readdir(curdir)))
{
stat(dname->d_name,fstat);
if((S_ISDIR(fstat->st_mode)))
{
fsize = (unsigned int)fstat->st_size;
ptime = &fstat->st_mtime;
ftime = localtime(ptime);
printf("\n%02i:%02i on %i/%i/%-8i",ftime->tm_hour,ftime->tm_min,(ftime->tm_mon +
1),ftime->tm_mday,(ftime->tm_year + 1900));
printf("%-22s%s","<DIR>",dname->d_name);
dircount++;
}
else
{
fsize = (unsigned int)fstat->st_size;
ptime = &fstat->st_mtime;
ftime = localtime(ptime);
printf("\n%02i:%02i on %i/%i/%i",ftime->tm_hour,ftime->tm_min,(ftime->tm_mon +
1),ftime->tm_mday,(ftime->tm_year + 1900));
printf("%19u bytes %s",fsize,dname->d_name);
totalfsize = totalfsize + fsize;
fcount++;
}
}
closedir(curdir);
printf("\nThere are %i files %i directorys found.\n",fcount,dircount);
printf("Size of files: %i\n",totalfsize);
return 0;
}
Program without if and else
Code:
#include<stdio.h>
#include<dirent.h>
#include<time.h>
#include<sys/stat.h>
#include<sys/types.h>
int main (int argc , char *argv[])
{
DIR *curdir = NULL;
struct dirent *dname = NULL;
struct stat *fstat;
struct tm *ftime;
int fcount = 0;
int dircount = 0;
unsigned int totalfsize = 0;
unsigned int fsize;
time_t *ptime = NULL;
curdir = opendir(".");
while ((dname = readdir(curdir)))
{
stat(dname->d_name,fstat);
if((S_ISDIR(fstat->st_mode)))
{
fsize = (unsigned int)fstat->st_size;
ptime = &fstat->st_mtime;
ftime = localtime(ptime);
printf("\n%02i:%02i on %i/%i/%-8i",ftime->tm_hour,ftime->tm_min,(ftime->tm_mon +
1),ftime->tm_mday,(ftime->tm_year + 1900));
printf("%-22s%s","<DIR>",dname->d_name);
dircount++;
}
else
{
fsize = (unsigned int)fstat->st_size;
ptime = &fstat->st_mtime;
ftime = localtime(ptime);
printf("\n%02i:%02i on %i/%i/%i",ftime->tm_hour,ftime->tm_min,(ftime->tm_mon +
1),ftime->tm_mday,(ftime->tm_year + 1900));
printf("%19u bytes %s",fsize,dname->d_name);
totalfsize = totalfsize + fsize;
fcount++;
}
}
closedir(curdir);
printf("\nThere are %i files %i directorys found.\n",fcount,dircount);
printf("Size of files: %i\n",totalfsize);
return 0;
}
I'll look at it...let me get mingw installed...heh
Edit - Umm...hrmmm...
I downloaded/installed mingw (the mingw-runtime, gcc-core, binutils, and w32api packages...all from CURRENT), then compiled both sets of source you've listed above. Both compiled without error, and both ran without error...additionally, the first one ran fine with or without args (and I double checked the output for accuracy). Do you have an outdated/broken mingw install? What options are you passing to gcc.exe? (I compiled as such: "bin\gcc.exe -o WithIfElse.exe WithIfElse.c" and "bin\gcc.exe -o WithoutIfElse.exe WithoutIfElse.c")
I have narrowed it down it seams to sprintf being called before calling stat. If I do a sprintf before I call stat then the program crashes but if I just do a printf or anything else it works properly
so it seams like it has something to do with the interaction of sprintf and stat ??
Never mind I see what the issue is. The memory for that location is being overwritten by another process. Its actually just fortunate that it even works at all. This is correct for readdir tho as far as i can tell from the manpage
this seams to have something to do with reentrant and non reentrant versions of each function. One takes the structure to place the data as the other just returns a pointer to a structure created in the function. How is this memory freed and is this common for functions defined in sys ?
Quote:
The pointer returned by readdir() points to data which may be overwritten by another call to readdir() on the same directory stream. This data is not overwritten by another call to readdir() on a different directory stream.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.