home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18242 < prev    next >
Encoding:
Text File  |  1992-12-12  |  5.1 KB  |  137 lines

  1. Path: sparky!uunet!nwnexus!beauty!rwing!pat
  2. From: pat@rwing.UUCP (Pat Myrto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Unix System calls
  5. Message-ID: <1834@rwing.UUCP>
  6. Date: 12 Dec 92 05:00:14 GMT
  7. References: <Dec.8.02.02.25.1992.429@clam.rutgers.edu>
  8. Organization: Totally Unorganized
  9. Lines: 126
  10.  
  11. In article <Dec.8.02.02.25.1992.429@clam.rutgers.edu> dalessan@clam.rutgers.edu (Gene) writes:
  12. >
  13. > [ ... synopsis, etc deleted ... ]
  14. >
  15. >I began writing a program to call stat inorder to get to a few
  16. >values that I'm interested in comcerning different files. ie
  17. >file serial number,last modify time,blocks allocated etc.
  18. >Anyhow I wrote a little test prog to see how easy it would
  19. >be(I figured very easy), but I seem to have hit a snag. Here's
  20. >what I got:
  21. >
  22. >#include <stdio.h>
  23. >#include <sys/types.h>
  24. >#include <sys/stat.h>
  25. >
  26. >main (argc,argv)
  27. >int argc;
  28. >char *argv[];
  29. >{
  30. >struct stat *info; <--- only size of ptr var is allocated, not a structure.
  31. >off_t   bytes;
  32. >
  33. >stat(argv[1],&info);
  34. >}              \_____  uninitialized ptr var plus wrong indirection level...
  35.  
  36. The problem is you are using the pointer variable before its initialized
  37. to a meaningful value AND you are passing the ADDRESS of the pointer variable to
  38. stat(), so stat gets struct stat **info, pointing to unknown location.
  39. You should remove the '*' from the declaration so a full stat structure
  40. sized area is allocated, OR allocate the structure as another name, like
  41. statb, and then set info to the address of stat struct statb (handy if
  42. the same stat() call gets made with different stat structs, where the
  43. program sets the pointer variable (struct stat *info) to a different
  44. structure on each call.  This is useful for cases where one doesn't know
  45. till runtime what particular stat structure is used in the call (such
  46. as where several structs are passed, and then compared, etc).
  47.  
  48. Following are examples how I would call using both a pointer variable
  49. and using the address-of operator ('&') in the stat() call.
  50.  
  51. Also, just to be complete, I will mention that one should test the return
  52. value of the stat() syscall to be sure it succeeds, otherwise the data
  53. is meaningless (if stat() returns zero it is a successful call).
  54.  
  55. ============================ EXAMPLES ===============================
  56. /* passing address of stat buffer directly */
  57.  
  58. #include <stdio.h>
  59. #include <sys/types.h>
  60. #include <sys/stat.h>
  61.  
  62. main(argc, argv)
  63. int argc;
  64. char *argv[];
  65. {
  66.     struct stat info;     /* causes space for a stat STRUCT to be allocated */
  67.                           /* instead of an unitialized pointer variable */
  68.     off_t bytes;
  69.  
  70.     stat(argv[1], &info); /* pass stat the ADDRESS of the stat struct */
  71.                           /* stat syscall fills it in */
  72.     bytes = info.st_size; /* set bytes to the size returned */
  73.                           /* note if one is not going to use it other ways */
  74.                           /* one could simply access the element itself in */
  75.                           /* following expression(s) instead of the variable */
  76.     printf("Size is: %d\n", bytes);     /* or whatever */
  77.     exit(0);              /* define exit value */
  78. }
  79.  
  80. =================== OR, USING POINTER VARIABLE =========================
  81. /* passing addr of statbuf via use of a pointer variable  and testing
  82.  * whether stat() call succeeded or not.
  83.  */
  84.  
  85. #include <stdio.h>
  86. #include <sys/types.h>
  87. #include <sys/stat.h>
  88.  
  89. void die();                       /* something to do if stat() call fails */
  90.  
  91. main(argc, argv)
  92. int argc;
  93. char *argv[];
  94. {
  95.     struct stat statb;            /* allocate a stat structure */
  96.     struct stat *info = &statb    /* inits ptr variable with addr of statb */
  97.  
  98.                                   /* example of testing success of stat() */
  99.     if(stat(argv[1], info) != 0)  /* like above, but using pointer variable */
  100.         die("stat failed!\n");    /* function to print msg and exit program */
  101.  
  102.     printf("Size is: %d\n", info->st_size);    /* or whatever */
  103.     exit(0);                      /* define exit value */
  104. }
  105.  
  106. void
  107. die(s)
  108. char *s;
  109. {
  110.     fprintf(stderr, "ERROR: %s", s);   /* complain */
  111.     exit(1);                      /* non-zero value for problem exit */
  112. }
  113. ============================== END OF EXAMPLES ========================
  114.  
  115. >Every time I try to print a value (info->st_ino) or something
  116. >I get a segmentation fault(I really hate them). Info is a ptr
  117. >to a stat structure into which infomation is placed concerning
  118.  
  119. Until it is initialized, it points to <undefined>.  See examples above...
  120.  
  121. >a file.  Any idea on how to assign desired fields to vars.
  122. >This was a little test prog. It works as is, but when you try
  123. >to access a field -- seg fault.
  124.  
  125. That is because the field you are trying to address is evaluated
  126. as a location outside your allocated memory space (info being some
  127. random value, which is regarded as a memory address).
  128.  
  129. Hope this helps, and isn't TOO verbose.... I got nailed on similar stuff
  130. when I first tried some coding in C ...
  131.  
  132. -- 
  133. pat@rwing.uucp                                  (Pat Myrto),  Seattle, WA
  134.                       If all else fails, try:
  135.          ...!uunet!{pilchuck, polari}!rwing!pat
  136. WISDOM:    "Travelling unarmed is like boating without a life jacket" 
  137.