Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com To: cygwin AT cygwin DOT com From: "Mikael" Subject: Why does stat fail for some files? Date: Thu, 15 Sep 2005 21:41:58 +0200 Lines: 137 Message-ID: X-IsSubscribed: yes Hello, I'm doing homework and I'm having some problems with stat() failing. Consider the following C program: #include #include #include #include #include #include #include #include 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 AT 9&ER M&4-"D]"2D5#5%,@/2!D:7)S8V%N+F\-"D]"2D5#5%,R(#T AT 9&ER M("0H3$1& M3$%'4RD-"@T*)2YO.B E+F,-"@DD*$-#*2 D*$-&3$%'4RD@)$ @)#P-"@T* M8VQE86XZ#0H)2AA2 ]($Y53$P[#0H@("!S=')U8W0 AT 9&ER96YT("IE;G1R>2 ]($Y53$P[#0H@ M("!S=')U8W0@2 E2DI*0T*(" @>PT* M(" @(" @2T^9%]N86UE+" F