From: <dan...@ya...> - 2001-09-18 20:25:14
|
Thanks, I have had a similar patch sitting around the SF patch tracker for ages. However, I think the correct fix is just to get rid of S_IFBLK (which is undocumented for MS and also absent im OS/2) and probably bogus. In any event, I can't get MS version of stat or fstat to ever set st_mode to S_IFBLK, even when I use CreateFile to get a handle to a physical drive (like a CD), which I thought should classify as block special. Can any one show me how to [f]stat to set S_IFBLK? --- stefan <st...@lk...> wrote: > Hello list, > > the following piece of code reveals a bug in <sys/stat.h>: > > #include <sys/types.h> > #include <sys/stat.h> > #include <fcntl.h> > > static const char * > stat_type (mode_t mode) > { > if (S_ISREG (mode)) > return "regular"; > else if (S_ISDIR (mode)) > return "directory"; > else if (S_ISBLK (mode)) > return "block special"; > else if (S_ISCHR (mode)) > return "char special"; > else if (S_ISFIFO (mode)) > return "fifo"; > > return "unknown"; > } > > int > main (int argc, char **argv) > { > > struct stat buf; > int p[2], f; > > fstat (0, &buf); > printf ("stdin: %s\n", stat_type (buf.st_mode)); > > _pipe (p, 256, O_BINARY); > fstat (p[0], &buf); > printf ("pipe[0]: %s\n", stat_type (buf.st_mode)); > fstat (p[1], &buf); > printf ("pipe[1]: %s\n", stat_type (buf.st_mode)); > > > f = open ("stat.exe", O_RDONLY); > fstat (f, &buf); > printf ("file: %s\n", stat_type (buf.st_mode)); > > return 0; > } > > The ouput is: > stdin: block special > pipe[0]: block special > pipe[1]: block special > file: regular > > With the following patch it works just fine: > --- oldstat.h Mon Jun 04 22:42:28 2001 > +++ stat.h Tue Sep 18 14:31:54 2001 > @@ -61,11 +61,11 @@ > #define _S_IWUSR _S_IWRITE > #define _S_IRUSR _S_IREAD > > -#define _S_ISDIR(m) ((m) & _S_IFDIR) > -#define _S_ISFIFO(m) ((m) & _S_IFIFO) > -#define _S_ISCHR(m) ((m) & _S_IFCHR) > -#define _S_ISBLK(m) ((m) & _S_IFBLK) > -#define _S_ISREG(m) ((m) & _S_IFREG) > +#define _S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR) > +#define _S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO) > +#define _S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR) > +#define _S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK) > +#define _S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG) > > #ifndef _NO_OLDNAMES > > @@ -83,11 +83,11 @@ > #define S_IWUSR _S_IWUSR > #define S_IRUSR _S_IRUSR > > -#define S_ISDIR(m) ((m) & S_IFDIR) > -#define S_ISFIFO(m) ((m) & S_IFIFO) > -#define S_ISCHR(m) ((m) & S_IFCHR) > -#define S_ISBLK(m) ((m) & S_IFBLK) > -#define S_ISREG(m) ((m) & S_IFREG) > +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) > +#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) > +#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) > +#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) > +#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) > > #endif /* Not _NO_OLDNAMES */ > > With this patch I got: > stdin: char special > pipe[0]: fifo > pipe[1]: fifo > file: regular > > All files attached again. Hope it helps. > > Cheers, > st...@lk... http://travel.yahoo.com.au - Yahoo! Travel - Got Itchy feet? Get inspired! |