* RE: stat problem ?
@ 2000-03-31 12:49 Greg Rigole
0 siblings, 0 replies; 2+ messages in thread
From: Greg Rigole @ 2000-03-31 12:49 UTC (permalink / raw)
To: 'gcc@gcc.gnu.org'
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3345 bytes --]
The function stat is defined in the manual (in my case, AIX 4.2.1) as
follows:
int stat (Path, Buffer)
const char *Path;
struct stat *Buffer;
The stat function takes a pointer to an ALREADY EXISTING BUFFER. It does not
allocate the buffer for you.
In program #1, you passed an UNITIALIZED POINTER (probably garbage) instead
of one pointing to a buffer large enough for a struct stat. The stat
function blindly attempts to write using the pointer, which probably points
at inaccessible memory. Hence the exception.
In program two, you pass a pointer to a buffer assigned to its own valid
memory, large enough to hold a struct stat. The stat function now correctly
puts the info for the file into that buffer.
A third alternative is:
main(void) {
DIR *dp;
struct dirent *ep;
struct stat *st;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
st=malloc( sizeof( struct stat ) );
if (st == NULL) {
puts ("Out of memory.");
exit(-1);
}
while (ep = readdir (dp))
{
rc=stat (ep->d_name,st);
if (S_ISDIR(st->st_mode)!=0)
puts (ep->d_name);
else
{
printf ("Not a directory : ");
puts(ep->d_name);
printf("\n");
}
}
free( st );
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return;
}
}
In this case, there is extra work involved for a situation where only one
buffer is needed for the entire program lifetime, an ideal situation for a
static (or in this case automatic) buffer.
-----------------------------------------------------------------
PS: Is this group the place for this kind of question? I don't know, I'm
just asking.
-----------------------------------------------------------------
Greg
-----Original Message-----
From: Borsos Zoltán [ mailto:zoltan.borsos@sysdata.siemens.hu ]
Sent: Thursday, March 30, 2000 1:31 AM
To: 'gcc@gcc.gnu.org'
Subject: stat problem ?
Hi everybody !
I'm new in this list and in C too.
I have written a small program that in one version dosen't work
but in the other does it. I'd like to ask the experts explain me the
difference between the programs.
I can compile both free from errors but when I let it run this version
give me the following message :
[main] D:\KOZOS\progik\c\exe\dirsiz_1.exe 1000 (0) handle_exceptions:
Exception:
STATUS_ACCESS_VIOLATION
[main] dirsiz_1 1000 (0) handle_exceptions: Dumping stack trace to
dirsiz_1.exe.
core
// dirsiz_1.c
#include <dirent.h>
#include <sys/stat.h>
main(void) {
DIR *dp;
struct dirent *ep;
struct stat *st;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
{
rc=stat (ep->d_name,st);
if (S_ISDIR(st->st_mode)!=0)
puts (ep->d_name);
else
{
printf ("Not a directory : ");
puts(ep->d_name);
printf("\n");
}
}
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return;
}
-------------------------------------------------
This version works fine :
// dirsiz_2.c
#include <dirent.h>
#include <sys/stat.h>
main(void) {
DIR *dp;
struct dirent *ep;
struct stat st;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
{
rc=stat (ep->d_name,&st);
if (S_ISDIR(st.st_mode)!=0)
puts (ep->d_name);
else
{
printf ("Not a directory : ");
puts(ep->d_name);
printf("\n");
}
}
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return;
}
Thanks for the answers !
The answers please to my own address too.
Szambi
^ permalink raw reply [flat|nested] 2+ messages in thread
* stat problem ?
@ 2000-03-29 22:27 Borsos Zoltán
0 siblings, 0 replies; 2+ messages in thread
From: Borsos Zoltán @ 2000-03-29 22:27 UTC (permalink / raw)
To: 'gcc@gcc.gnu.org'
Hi everybody !
I'm new in this list and in C too.
I have written a small program that in one version dosen't work
but in the other does it. I'd like to ask the experts explain me the
difference between the programs.
I can compile both free from errors but when I let it run this version
give me the following message :
[main] D:\KOZOS\progik\c\exe\dirsiz_1.exe 1000 (0) handle_exceptions:
Exception:
STATUS_ACCESS_VIOLATION
[main] dirsiz_1 1000 (0) handle_exceptions: Dumping stack trace to
dirsiz_1.exe.
core
// dirsiz_1.c
#include <dirent.h>
#include <sys/stat.h>
main(void) {
DIR *dp;
struct dirent *ep;
struct stat *st;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
{
rc=stat (ep->d_name,st);
if (S_ISDIR(st->st_mode)!=0)
puts (ep->d_name);
else
{
printf ("Not a directory : ");
puts(ep->d_name);
printf("\n");
}
}
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return;
}
-------------------------------------------------
This version works fine :
// dirsiz_2.c
#include <dirent.h>
#include <sys/stat.h>
main(void) {
DIR *dp;
struct dirent *ep;
struct stat st;
int rc;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
{
rc=stat (ep->d_name,&st);
if (S_ISDIR(st.st_mode)!=0)
puts (ep->d_name);
else
{
printf ("Not a directory : ");
puts(ep->d_name);
printf("\n");
}
}
(void) closedir (dp);
}
else
puts ("Couldn't open the directory.");
return;
}
Thanks for the answers !
The answers please to my own address too.
Szambi
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2000-03-31 12:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-31 12:49 stat problem ? Greg Rigole
-- strict thread matches above, loose matches on Subject: below --
2000-03-29 22:27 Borsos Zoltán
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).