home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3402 < prev    next >
Encoding:
Internet Message Format  |  1993-01-23  |  7.1 KB

  1. Path: sparky!uunet!olivea!sgigate!sgiblab!troi!steve
  2. From: steve@dbaccess.com (Steve Suttles)
  3. Newsgroups: comp.std.c
  4. Subject: Re: C things
  5. Message-ID: <151@mccoy.dbaccess.com>
  6. Date: 22 Jan 93 20:55:00 GMT
  7. References: <107937@bu.edu>
  8. Organization: Cross Access Corp., Santa Clara, CA
  9. Lines: 122
  10. X-Newsreader: Tin 1.1 PL4
  11.  
  12. spacefox@acs.bu.edu (Godfrey Degamo) writes:
  13. : This was posted awhile ago:
  14. : -------------------------------------------------------------------------------
  15. : In article <BzMzqC.n0u@jrd.dec.com> 
  16. : diamond@jit.dec.com (Norman Diamond) writes:
  17. : Remember, a language with gets() and other such niceties is intended for
  18. : hacking, not for security.  The "N" is intended to bring over a known portion
  19. : of the source string, not to protect other stuff near the target.  Now this
  20. : raises the question of why memcpy() was invented, and I don't know the answer.
  21. : On a more serious note, people actually do use gets().  I'd worry about this
  22. : kind of dangerous stuff long before caring about silly things like strncat().
  23. : -------------------------------------------------------------------------------
  24. :      I am  a person who programs in C for recreation.  -much technical jargon 
  25. : is beyond my comprehension.  Perhaps you can answer my two questions.
  26. :      I would like to know why it's "dangerous" to use gets().  I always
  27. : make certain I pass in the name of a char array or some pointer char that
  28. : has been allocated some memory.  It always seems to work for me.
  29.  
  30. The "danger" in get() is common to a large number of routines.  There is an
  31. axiom that is never taught, that one should only use routines that write to
  32. arrays FOR WHICH THE LENGTH OF THE ARRAY IS SPECIFIED.  The problem is that
  33. with gets and strcat and several other routines, you pass the address of a
  34. destination array and it fills it up.  If things are not the way you expect at
  35. the time you write the program (for example, if the user types a line longer
  36. than you expected), you will keep right on going past the end of the array, and
  37. write all over other variables (or worse).  Everyone I know who implicitly
  38. understands this rule learned it the hard way.  Usually more than once.
  39.  
  40. :      What other alternative is there for one to use?  I try to find some
  41. : other way of getting user input other than scanf().  I seem to have problems
  42. : with scanf.  For instance:
  43. :      scanf ("%s", &inputA);
  44. :      scanf ("%s", %inputB);
  45. :      What happens is that inputA gets the user input, then when the computer
  46. : comes to the second line, it reads the CR from the first input as the input
  47. : for inputB.  (I don't remember this: but, I think that I tried a variation
  48. : "%s\r" and it still wouldn't work.)  Anyways, things like this where the
  49. : input of one is "leaked" to the next seem to crop up when I use scanf.
  50. : So, that's why I avoid it.  For numerical input, I use gets() and then
  51. : call a number conversion function.
  52.  
  53. It is also an axiom never to use scanf.  Many programmers use gets or fgets
  54. and then sscanf, many write their own parse routines each time.
  55.  
  56. :      My next question is this:
  57. :      Let's say I have a database program that holds information about people;
  58. : address, telephone number, physical description.  All the data of all the
  59. : people is saved to one file.
  60. :      How do I modify then save the records of one person without loading up
  61. : all the data then saving all the data, which can be very slow when the file
  62. : gets large?
  63. :      I thought of scanning through the file until I get to the record to be
  64. : changed.  And from there, start saving the data, but I'm worried that I will
  65. : overwrite other records if the amount of data is larger then the old data.
  66. :      It's been awhile since I programmed in AppleSoft Basic, but in it, there
  67. : is the concept of Random Access Files and Sequential Files.  In Random 
  68. : Access Files, you can do what I just desribed to you, but each record has to
  69. : be a set size.  Sequential Files could be anysize, but you lose the ability
  70. : to save information without loading all the data into memory.
  71. :      Is there something similar to this in C/Unix or C/MS-DOS?
  72.  
  73. Yup.  Identical.  If you make the records all the same size (obviously, it will
  74. need to be large enough to hold everything you might want to say), then you can
  75. update them without growing.  You can also compute where the beginning of a
  76. record is.  This allows accessing the records in random order, hence the term
  77. Random Access.  If you are only going to access the records sequentially, you
  78. will never need to compute the address of a record.  They can therefore be
  79. varying in size.  They can be updated in place, but if they need to be bigger,
  80. you either have to "move" everything after, or mark that record as deleted and
  81. tack the bigger one on the end.
  82.  
  83. The concept is a universal one.  It is implementation you wanted to ask about.
  84. In C, fseek() and ftell() are the functions you are interested in.  fseek()
  85. will let you know the location of a record in a file, and ftell() will go to a
  86. predetermined address.  It is O/S dependent what the arguments to these
  87. functions, and their return values, should be.  (and on VAX/VMS, you could do
  88. it this way, but RMS has certain advantages too, and should be considered.)
  89.  
  90. :      If you don't have the time to answer my second question, could you
  91. : please point me to some C literature that does?
  92. : Also, if you do respond, I suppose it would be nice to post on this group,
  93. : (if it's not so trivial.)  but I don't read these C programming newsgroups
  94. : often and might forget I posted here!  So, please could you mail your
  95. : answers to me?
  96. Anything to help.  However, if it is not worth your time to read the group
  97. where you posted the question, I find it insulting that you think it is worth
  98. my time to not only read your worthless question but to consider spending my
  99. time to consider it and respond.  If the question is of general interest, and
  100. the answer is as well, then news is the place for it.  If the question is made
  101. public because the answer which is not generally interesting is simply
  102. difficult to find, then email is the place.  However, your statements boil down
  103. to "I have a question which I think everyone wants to know the answer to.
  104. So please answer it publicly.  However, I'm too lazy to get up and read the
  105. bulletin board, so send it by mail, because mail is delivered to my desk."
  106.  
  107. I'm sorry if this seems harsh; I've simply reached my tolerance for this
  108. particular implied insult.  I'm sure you didn't intend it that way, nor did any
  109. of the others.  But I find no moral difference between this scenario and
  110. calling someone up to ask a question, and finishing with "I don't have time to
  111. listen to the answer, so write me a letter.  But I need to know right away".
  112.  
  113. : Thanks for listening, sorry to waste your time, have a good day.
  114. :                                         -G. Degamo,
  115. :                                          spacefox@acs.bu.edu
  116. -- 
  117. Steve Suttles                Internet:  steve@dbaccess.com      Dr. DCL is IN!
  118. CROSS ACCESS Corporation     UUCP: {uunet,sgiblab}!troi!steve  Yo speako TECO!
  119. 2900 Gordon Ave, Suite 100   fax: (408) 735-0328              Talk data to me!
  120. Santa Clara, CA 95051-0718   vox: (408) 735-7545   HA! It's under 4 lines NOW!
  121.