Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
* stat() broken under RH 5.0??
@ 1998-06-01 15:52 Mike
  1998-06-01 21:45 ` Quant-X Alpha Linux Support
  0 siblings, 1 reply; 5+ messages in thread
From: Mike @ 1998-06-01 15:52 UTC (permalink / raw)
  To: axp-list, joeyen


   We cannot get stat (in a C program, not the shell command) to work
correctly under RedHat 5.0 running on an alpha (kernel 2.0.30).  Any help 
would be appreciated or any work around that returns the file mode for
a given file.

   Here is the code we are using (it works fine under SunOS) and according
to the man pages it should work okay on the alpha as well.  It compiles
okay but segmentation faults.
   gdb gives the following error

   Program received signal SIGSEGV, Segmentation fault.
   __xstat () at ../sysdeps/unix/sysv/linux/xstat.c:52
   ../sysdeps/unix/sysv/linux/xstat.c:52: No such file or directory.


#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

main()
{
   struct stat *buf;
   stat("teststat.c",buf);
   printf("%d\n",buf->st_mode);
}

   Thanks Mike


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: stat() broken under RH 5.0??
  1998-06-01 15:52 stat() broken under RH 5.0?? Mike
@ 1998-06-01 21:45 ` Quant-X Alpha Linux Support
  1998-06-02 13:20   ` Bill Roman
  0 siblings, 1 reply; 5+ messages in thread
From: Quant-X Alpha Linux Support @ 1998-06-01 21:45 UTC (permalink / raw)
  To: axp-list; +Cc: joeyen

Hi !

>    We cannot get stat (in a C program, not the shell command) to work
> correctly under RedHat 5.0 running on an alpha (kernel 2.0.30).  Any help 

Ouups, your example is simply broken; it can't work.

> #include <sys/stat.h>
> #include <dirent.h>
> #include <unistd.h>
> 
> main()
> {
>    struct stat *buf;
>    stat("teststat.c",buf);
>    printf("%d\n",buf->st_mode);
> }

This cannot work, because you must allocate buf !
try something like:

#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

main()
{
   struct stat buf;
   stat("teststat.c", &buf);
   printf("%d\n",buf.st_mode);
}

or

#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>

main()
{
   struct stat *buf;  
   /* blah */
   buf = malloc(sizeof(buf));
   stat("teststat.c", buf);
   printf("%d\n",buf->st_mode);
   free(buf);
   /*blah*/
}

and it should work (untested, but may work if stat isn't broken ;-}).
BTW, what you really want is maybe:
   printf("%o\n",buf->st_mode & ~S_IFMT);

-- 

Quant-X Service &                         Ph:  (+43) 4212 6004-0
Consulting Ges.m.b.H.                     Fax: (+43) 4212 600420
http://www.quant-x.com                    Email: support@quant-x.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: stat() broken under RH 5.0??
       [not found] <Pine.SOL.3.96.980601182010.13382A-100000@tarkovsky.npac.syr.edu>
@ 1998-06-02  0:37 ` Quant-X Alpha Linux Support
  0 siblings, 0 replies; 5+ messages in thread
From: Quant-X Alpha Linux Support @ 1998-06-02  0:37 UTC (permalink / raw)
  To: syen5; +Cc: axp-list, joeyen

Hi Stefan !

> > main()
> > {
> >    struct stat *buf;
> >    stat("teststat.c",buf);
> >    printf("%d\n",buf->st_mode);
> > }

> 	Doh!  Makes sense. It's interesting that it compiled and ran
> under SunOS though. I wonder if its implementation of stat automatically
> mallocs the argument. Thanks for the suggestion.

Sorry, but ouups [again]. ;-]

Even good old SunOS can't automatically allocate the struct.
(ok, it theoretically can, but if you call stat(file, NULL); there
is no way to get the allocated "struct stat" pointer back !)
Trust me, it only works if you have a cool random pointer, this is simply
a platform-independent bug that gernerates platform/compiler-dependent 
results... Be careful.

-- 

Quant-X Service &                         Ph:  (+43) 4212 6004-0
Consulting Ges.m.b.H.                     Fax: (+43) 4212 600420
http://www.quant-x.com                    Email: support@quant-x.com


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: stat() broken under RH 5.0??
  1998-06-01 21:45 ` Quant-X Alpha Linux Support
@ 1998-06-02 13:20   ` Bill Roman
  1998-06-02 15:07     ` Michal Jaegermann
  0 siblings, 1 reply; 5+ messages in thread
From: Bill Roman @ 1998-06-02 13:20 UTC (permalink / raw)
  To: axp-list

Quant-X Alpha Linux Support wrote:
> 
> Hi !
> 
> >    We cannot get stat (in a C program, not the shell command) to work
> > correctly under RedHat 5.0 running on an alpha (kernel 2.0.30).  Any help 
> 
> Ouups, your example is simply broken; it can't work.
>> [original code deleted]
> This cannot work, because you must allocate buf !
> try something like:
> 
> [working example deleted]
>
> or
> 
> #include <stdlib.h>
> #include <sys/stat.h>
> #include <dirent.h>
> #include <unistd.h>
> 
> main()
> {
>    struct stat *buf;  
>    /* blah */
>    buf = malloc(sizeof(buf));
>    stat("teststat.c", buf);
>    printf("%d\n",buf->st_mode);
>    free(buf);
>    /*blah*/
> }
> 
> and it should work (untested, but may work if stat isn't broken ;-}).

Almost right!  But you wanted sizeof *buf; sizeof buf is the size of a
*pointer* to struct stat, not of a struct stat.

-- 
Bill Roman  (roman@songdog.eskimo.com / roman@songdog.uucp)   running linux


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: stat() broken under RH 5.0??
  1998-06-02 13:20   ` Bill Roman
@ 1998-06-02 15:07     ` Michal Jaegermann
  0 siblings, 0 replies; 5+ messages in thread
From: Michal Jaegermann @ 1998-06-02 15:07 UTC (permalink / raw)
  To: axp-list

Bill Roman wrote:
> 
> Quant-X Alpha Linux Support wrote:
> > 
> > 
> > #include <stdlib.h>
> > #include <sys/stat.h>
> > #include <dirent.h>
> > #include <unistd.h>
> > 
> > main()
> > {
> >    struct stat *buf;  
> >    /* blah */
> >    buf = malloc(sizeof(buf));
> >    stat("teststat.c", buf);
> >    printf("%d\n",buf->st_mode);
> >    free(buf);
> >    /*blah*/
> > }
> > 
> > and it should work (untested, but may work if stat isn't broken ;-}).
> 
> Almost right!  But you wanted sizeof *buf; sizeof buf is the size of a
> *pointer* to struct stat, not of a struct stat.

Double sigh!  What about simply that:

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main()
{
   struct stat buf;
   stat("teststat.c", &buf);
   printf("%o\n",buf.st_mode & 0xfff);
   return 0;
}

People who compile without -Wall, especially on Alpha, get what they
deserve.

And yes, this does work on Alpha with Red Hat 5.0 (big surprise :-).

  --mj


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1998-06-02 15:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-06-01 15:52 stat() broken under RH 5.0?? Mike
1998-06-01 21:45 ` Quant-X Alpha Linux Support
1998-06-02 13:20   ` Bill Roman
1998-06-02 15:07     ` Michal Jaegermann
     [not found] <Pine.SOL.3.96.980601182010.13382A-100000@tarkovsky.npac.syr.edu>
1998-06-02  0:37 ` Quant-X Alpha Linux Support

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox