This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why does stat fail for some files?


Hello, I'm doing homework and I'm having some problems with stat() failing. 
Consider the following C program:

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

static void scan_directory(const char *);

int
main(int argc, char **argv)
{
   if(argc == 1)
   {
      fprintf(stderr, "Usage: dirscan path\n");

      return EXIT_FAILURE;
   }

   scan_directory(argv[1]);

   return EXIT_SUCCESS;
}

static void
scan_directory(const char *path)
{
   DIR *directory = NULL;
   struct dirent *entry = NULL;
   struct stat statbuf;
   int return_value = 0;

   directory = opendir(path);

   if(!directory)
   {
      fprintf(stderr, "Directory %s could not be opened.\n", path);
      fprintf(stderr, "Reason: %s\n", strerror(errno));

      /* Don't exit program, we may have been called recursively and
         we want to scan what we can.                                */
      return;
   }

   while((entry = readdir(directory)))
   {
      return_value = stat(entry->d_name, &statbuf);

      if(return_value)
         fprintf(stderr, "stat() failed for %s. Reason: %s\n",
                   entry->d_name, strerror(errno));
      else
         printf("stat() called successfully on: %s\n", entry->d_name);
   }

   return_value = closedir(directory);

   assert(!return_value); /* closedir() returns 0 on success */
}

And this is what happens when I run it (first ls -al):
$ ls -al /c/cygwin/home/mikael/coding/cygwin/c++/read_directory/
total 773
drwxrwxrwx+  2 mikael None      0 Sep 15 16:36 ./
drwxrwxrwx+ 16 mikael None      0 May 20 22:53 ../
-rwxrwxrwx   1 mikael None    273 Sep 15 16:36 Makefile*
-rwxrwxrwx   1 mikael None    792 Aug 19  2004 read_directory.cpp*
-rwxr-xr-x   1 mikael None 604746 Sep 15 16:36 read_directory.exe*
-rw-r--r--   1 mikael None 129520 Sep 15 16:36 read_directory.o

$ ./dirscan.exe /c/cygwin/home/mikael/coding/cygwin/c++/read_directory/
stat() called successfully on: .
stat() called successfully on: ..
stat() called successfully on: Makefile
stat() failed for read_directory.cpp. Reason: No such file or directory
stat() failed for read_directory.exe. Reason: No such file or directory
stat() failed for read_directory.o. Reason: No such file or directory

Why is this happening? I'm attaching the C-file and a Makefile. Perform a "$ 
make dirscan2" if you want to build.

/ Mikael 


begin 666 Makefile.dat
M0T,@/2!G8V,-"D-&3$%'4R ]("U786QL("U7("UA;G-I("UP961A;G1I8R M
M9R M3S @+6,@+6\-"DQ$1DQ!1U,@/2 M;R D*$5814,I#0I%6$5#(#T@9&ER
M<V-A;BYE>&4-"D]"2D5#5%,@/2!D:7)S8V%N+F\-"D]"2D5#5%,R(#T@9&ER
M<V-A;C(N;PT*#0IA;&PZ("0H3T)*14-44RD-"@DD*$-#*2 D7B D*$Q$1DQ!
M1U,I#0H-"F1I<G-C86XR.B D*$]"2D5#5%,R*0T*"20H0T,I("1>("0H3$1&
M3$%'4RD-"@T*)2YO.B E+F,-"@DD*$-#*2 D*$-&3$%'4RD@)$ @)#P-"@T*
M8VQE86XZ#0H)<FT@+68@)"A/0DI%0U13*2 D*$]"2D5#5%,R*2 D*$5814,I
1("I^("HN<W1A8VMD=6UP#0H`
`
end

begin 666 dirscan2.c
M(VEN8VQU9&4@/&%S<V5R="YH/@T*(VEN8VQU9&4@/&5R<FYO+F@^#0HC:6YC
M;'5D92 \<W1D:6\N:#X-"B-I;F-L=61E(#QS=&1L:6(N:#X-"B-I;F-L=61E
M(#QS=')I;F<N:#X-"@T*(VEN8VQU9&4@/&1I<F5N="YH/@T*(VEN8VQU9&4@
M/'-Y<R]S=&%T+F@^#0HC:6YC;'5D92 \<WES+W1Y<&5S+F@^#0H-"G-T871I
M8R!V;VED('-C86Y?9&ER96-T;W)Y*&-O;G-T(&-H87(@*BD[#0H-"FEN= T*
M;6%I;BAI;G0@87)G8RP@8VAA<B J*F%R9W8I#0I[#0H@("!I9BAA<F=C(#T]
M(#$I#0H@("![#0H@(" @("!F<')I;G1F*'-T9&5R<BP@(E5S86=E.B!D:7)S
M=&%T('!A=&A<;B(I.PT*#0H@(" @("!R971U<FX@15A)5%]&04E,55)%.PT*
M(" @?0T*#0H@("!S8V%N7V1I<F5C=&]R>2AA<F=V6S%=*3L-"@T*(" @<F5T
M=7)N($58251?4U5#0T534SL-"GT-"@T*<W1A=&EC('9O:60-"G-C86Y?9&ER
M96-T;W)Y*&-O;G-T(&-H87(@*G!A=&@I#0I[#0H@("!$25(@*F1I<F5C=&]R
M>2 ]($Y53$P[#0H@("!S=')U8W0@9&ER96YT("IE;G1R>2 ]($Y53$P[#0H@
M("!S=')U8W0@<W1A="!S=&%T8G5F.PT*(" @:6YT(')E='5R;E]V86QU92 ]
M(# [#0H-"B @(&1I<F5C=&]R>2 ](&]P96YD:7(H<&%T:"D[#0H-"B @(&EF
M*"%D:7)E8W1O<GDI#0H@("![#0H@(" @("!F<')I;G1F*'-T9&5R<BP@(D1I
M<F5C=&]R>2 E<R!C;W5L9"!N;W0@8F4@;W!E;F5D+EQN(BP@<&%T:"D[#0H@
M(" @("!F<')I;G1F*'-T9&5R<BP@(E)E87-O;CH@)7-<;B(L('-T<F5R<F]R
M*&5R<FYO*2D[#0H-"B @(" @("\J($1O;B=T(&5X:70@<')O9W)A;2P@=V4@
M;6%Y(&AA=F4@8F5E;B!C86QL960@<F5C=7)S:79E;'D@86YD#0H@(" @(" @
M("!W92!W86YT('1O('-C86X@=VAA="!W92!C86XN(" @(" @(" @(" @(" @
M(" @(" @(" @(" @(" @(" J+PT*(" @(" @<F5T=7)N.PT*(" @?0T*#0H@
M("!W:&EL92@H96YT<GD@/2!R96%D9&ER*&1I<F5C=&]R>2DI*0T*(" @>PT*
M(" @(" @<F5T=7)N7W9A;'5E(#T@<W1A="AE;G1R>2T^9%]N86UE+" F<W1A
M=&)U9BD[#0H-"B @(" @(&EF*')E='5R;E]V86QU92D-"B @(" @(" @(&9P
M<FEN=&8H<W1D97)R+" B<W1A="@I(&9A:6QE9"!F;W(@)7,N(%)E87-O;CH@
M)7-<;B(L#0H@(" @(" @(" @(" @(" @(&5N=')Y+3YD7VYA;64L('-T<F5R
M<F]R*&5R<FYO*2D[#0H@(" @("!E;'-E#0H@(" @(" @("!P<FEN=&8H(G-T
M870H*2!C86QL960@<W5C8V5S<V9U;&QY(&]N.B E<UQN(BP@96YT<GDM/F1?
M;F%M92D[#0H@("!]#0H-"B @(')E='5R;E]V86QU92 ](&-L;W-E9&ER*&1I
M<F5C=&]R>2D[#0H-"B @(&%S<V5R="@A<F5T=7)N7W9A;'5E*3L@+RH@8VQO
D<V5D:7(H*2!R971U<FYS(# @;VX@<W5C8V5S<R J+PT*?0T*
`
end



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]