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

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!munnari.oz.au!bruce.cs.monash.edu.au!monu6!mdw033.cc.monash.edu.au!int233p
  3. From: int233p@mdw033.cc.monash.edu.au (Mr A.J. Bucknell)
  4. Subject: Trying to write my own ls
  5. Message-ID: <1992Sep9.125031.17878@monu6.cc.monash.edu.au>
  6. Summary: Help with inodes and struct direct
  7. Keywords: inodes
  8. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  9. Organization: Monash University
  10. Date: Wed, 9 Sep 1992 12:50:31 GMT
  11. Lines: 29
  12.  
  13.    I am trying to write a small program that lists the files and subdirectories
  14. of a specified directory. I have got the following program running, and it
  15. works fine when I run it on the home directory, but when I run it on any other
  16. directory, it gives incorrect file sizes. Can anyone point my error ? The 
  17. code is :
  18.  
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/dir.h>
  23. #include <sys/stat.h>
  24.  
  25. void main(int argc, char *argv[])
  26. {
  27.     struct stat buf;
  28.     DIR *dirp;
  29.     struct direct *dp;
  30.     
  31.     dirp = opendir(argv[1]);
  32.         for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  33.     {
  34.         stat(dp->d_name, &buf);
  35.         printf("%-15s%8d\n", dp->d_name, buf.st_size);
  36.     }
  37.     closedir(dirp);
  38. }
  39.  
  40. Thanks in advance for any help. Replies via email, or to here if you think its
  41. of interest to others.
  42.