home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / volume.28 / text0093.txt < prev    next >
Encoding:
Text File  |  1992-08-17  |  2.5 KB  |  87 lines

  1. Submitted-by: davidf@mks.com ("David J. Fiander")
  2.  
  3. All references are to POSIX 1003.2 D11.2, unless stated
  4. otherwise.
  5.  
  6. The standard says (lines 11645--11651)
  7.  
  8.     When the shell is using standard input and it invokes a
  9.     command that also uses standard input, the shell shall
  10.     ensure that the standard input file pointer points
  11.     directly after the command it has read when the command
  12.     begins execution.  It shall not read ahead in such a
  13.     manner that any characters intended to be read by the
  14.     invoked command are consumed by the shell (whether
  15.     interpreted by the shell or not) or that characters
  16.     that are not read by the invoked command are not seen
  17.     by the shell.
  18.  
  19.  
  20. This seems to be attempting to ensure that a script like this
  21.  
  22.     foo=`sed 1q`
  23.     hi there
  24.     echo $foo
  25.     echo Done
  26.  
  27. When fed to the shell on standard input will generate the output
  28.  
  29.     hi there
  30.     Done
  31.  
  32. But the standard also says that (lines 5467--5471)
  33.  
  34.     When a standard utility reads a seekable input file and
  35.     terminates without an error before it reaches
  36.     end-of-file, the utility shall ensure that the file
  37.     offset in the open file description is properly
  38.     positioned just past the last byte processed by the
  39.     utility.  For files that are not seekable, the state of
  40.     the file offset in the open file description for that
  41.     file is unspecified.
  42.  
  43. This means that the above script is _only_ guaranteed to work
  44. in the case of
  45.  
  46.     sh <script
  47.  
  48. since sed is required to ensure that the file offset of the
  49. seekable file script is properly positioned when it exits.
  50.  
  51.     cat script | sh
  52.  
  53. does _not_ have to work, according to the standard.  Yes, the
  54. standard ensures that sed will read the right data, but the
  55. shell may miss a large chunk out of the middle of the script
  56. because sed is permitted to perform a buffered read, and not
  57. reset the file offset pointer.
  58.  
  59. In fact, the only case in which both 'sh < script' and
  60. 'cat script | sh' will give the same answer on all systems is
  61. that case in which the embedded data is all at the end of the
  62. file, and that _all_ of that data will be consumed by the last
  63. command executed by the shell.  That is:
  64.  
  65.     .
  66.     .
  67.     .
  68.     filter -args
  69.     data
  70.     data
  71.  
  72. Where filter will use all of the data.
  73.  
  74. What is most unfortunate is that in order to support this one
  75. case, there will have to be a lot of code in the shell which,
  76. when the script is be read from standard input and standard
  77. input is not a seekable file, reads the script unbuffered, a
  78. single byte at a time, in order to ensure that the last command
  79. in the file get the data it is expecting.
  80.  
  81. Is it really worth it?
  82.  
  83.  
  84.  
  85. Volume-Number: Volume 28, Number 93
  86.  
  87.