LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2007, 07:43 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Strange Segfault in windows


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; 
}
 
Old 07-24-2007, 08:07 PM   #2
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
Compiling with Visual C++? Running the first one with/without args? If you're running with args, what args?
 
Old 07-24-2007, 08:47 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Being compiled using mingw and it fails with or without arguments.

only argument you could give it is a path.

Sorry I didn't specify the language but this is C

Last edited by exvor; 07-24-2007 at 08:54 PM.
 
Old 07-24-2007, 09:32 PM   #4
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
I realize it's C =)

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")

Last edited by rocket357; 07-24-2007 at 09:54 PM.
 
Old 07-25-2007, 05:21 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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 ??

very strange.
 
Old 07-25-2007, 05:35 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You never allocated space for fstat etc...

Try this:
Code:
int main (int argc , char *argv[]) 
{ 
    DIR *curdir = NULL; 
    struct dirent *dname = NULL; 
    struct tm *ftime = NULL; 
    struct stat fstat; 
...
'Hope that helps .. PSM

Last edited by paulsm4; 07-25-2007 at 08:18 PM.
 
Old 07-25-2007, 05:52 PM   #7
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Yeah but if I do that then i get compile errors about invalid assignment.
 
Old 07-25-2007, 06:18 PM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.

Last edited by exvor; 07-25-2007 at 06:57 PM.
 
Old 07-25-2007, 08:19 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Try this:
Code:
#include <stdio.h>
#include <errno.h>
#include <dirent.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;

  /* Open directory */
  curdir = opendir(".");
  if (curdir == NULL)
  {
    perror ("opendir failed");
    return 1;
  }

  /* List directories */
  while ( (dname = readdir (curdir)) != NULL )
  {
    if (stat (dname->d_name, &fstat) < 0)
    {
      printf ("stat (%s) failed, errno: %d",
        dname->d_name, errno);
      continue;
    }
    if (S_ISDIR (fstat.st_mode))
      printf ("DIR: %s, st_mode: 0x%x, st_mtime: 0x%x\n",
        dname->d_name, (unsigned)fstat.st_mode, (unsigned)fstat.st_mtime);
    else
      printf ("file: %s, st_mode: 0x%x, st_mtime: 0x%x\n",
        dname->d_name, (unsigned)fstat.st_mode, (unsigned)fstat.st_mtime);
  }

  /* Close DIR */
  closedir (curdir);
  printf ("Done.\n");
  return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problems with FC4 and windows. ncaditya Fedora 4 07-11-2006 01:34 AM
strange windows vs linux problem wrat General 8 09-28-2005 03:47 PM
Very strange display problems - my windows are huge!!!!!!!! cdavidson Linux - Newbie 0 01-21-2004 12:23 AM
Strange Ports in windows 98 lub0 General 7 10-05-2003 06:59 PM
Strange fodlers windows partition banana2 Linux - General 1 02-26-2003 10:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration