home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / programm / 4589 < prev    next >
Encoding:
Text File  |  1992-09-09  |  2.5 KB  |  64 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!caen!batcomputer!snake.tc.cornell.edu!reed
  3. From: reed@snake.tc.cornell.edu (Michael G. Reed)
  4. Subject: Re: Trying to write my own ls
  5. Message-ID: <1992Sep9.170531.27422@tc.cornell.edu>
  6. Keywords: inodes
  7. Sender: news@tc.cornell.edu
  8. Nntp-Posting-Host: snake.tc.cornell.edu
  9. Reply-To: reed@TC.Cornell.EDU
  10. Organization: Cornell National Supercomputing Facility
  11. References:  <1992Sep9.125031.17878@monu6.cc.monash.edu.au>
  12. Date: Wed, 9 Sep 1992 17:05:31 GMT
  13. Lines: 49
  14.  
  15. In article <1992Sep9.125031.17878@monu6.cc.monash.edu.au>, int233p@mdw033.cc.monash.edu.au (Mr A.J. Bucknell) writes:
  16. |>    I am trying to write a small program that lists the files and subdirectories
  17. |> of a specified directory. I have got the following program running, and it
  18. |> works fine when I run it on the home directory, but when I run it on any other
  19. |> directory, it gives incorrect file sizes. Can anyone point my error ? The 
  20. |> code is :
  21. |> 
  22. |> 
  23. |> #include <stdio.h>
  24. |> #include <sys/types.h>
  25. |> #include <sys/dir.h>
  26. |> #include <sys/stat.h>
  27. |> 
  28. |> void main(int argc, char *argv[])
  29. |> {
  30. |>     struct stat buf;
  31. |>     DIR *dirp;
  32. |>     struct direct *dp;
  33. |>     
  34. |>     dirp = opendir(argv[1]);
  35. |>         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  36. |>     {
  37. |>         stat(dp->d_name, &buf);
  38.  
  39. BINGO!  Here's your problem.  The string pointed to by dp->d_name is local to
  40. the directory, not global to the filesystem.  Therefore, when you the stat on the
  41. files, it is checking your CURRENT directory (not the directory you want it to
  42. check) for the file information.  You could get around this by prepending argv[1]
  43. to dp->d_name in temporary memory before doing the stat, or by issuing a
  44. chdir(argv[1]) at the beginning of your code.  Does anyone know if fchdir(dirp)
  45. might work (I didn't take a look at the structure definitions for FILE and DIR)?
  46.  
  47. |>         printf("%-15s%8d\n", dp->d_name, buf.st_size);
  48. |>     }
  49. |>     closedir(dirp);
  50. |> }
  51. |> 
  52. |> Thanks in advance for any help. Replies via email, or to here if you think its
  53. |> of interest to others.
  54.  
  55. -Michael
  56.  
  57. -----------------------------------------------------------------------------
  58. Michael G. Reed                                         (reed@TC.Cornell.EDU)
  59. Cornell National Supercomputing Facility                       (607)/254-8806
  60. -----------------------------------------------------------------------------
  61.         Why be normal, it's boring; and boring people should be shot.
  62.  
  63. Note:  These are not the views of my employer (and probably not mine either).
  64.