home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / clib / fgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  1.8 KB  |  95 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: fgets.c,v 1.1 1997/01/17 16:23:06 digulla Exp $
  4.  
  5.     Desc: ANSI C function fgets()
  6.     Lang: english
  7. */
  8. #include <errno.h>
  9. #include <dos/dos.h>
  10. #include <dos/dosextens.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13.  
  14. /*****************************************************************************
  15.  
  16.     NAME */
  17. #include <stdio.h>
  18.  
  19.     char * fgets (
  20.  
  21. /*  SYNOPSIS */
  22.     char * buffer,
  23.     int    size,
  24.     FILE * stream)
  25.  
  26. /*  FUNCTION
  27.     Read one line of characters from the stream into the buffer.
  28.     Reading will stop, when a newline ('\n') is encountered, EOF
  29.     or when the buffer is full. If a newline is read, then it is
  30.     put into the buffer. The last character in the buffer is always
  31.     '\0' (Therefore at most size-1 characters can be read in one go).
  32.  
  33.     INPUTS
  34.     buffer - Write characters into this buffer
  35.     size - This is the size of the buffer in characters.
  36.     stream - Read from this stream
  37.  
  38.     RESULT
  39.     buffer or NULL in case of an error or EOF.
  40.  
  41.     NOTES
  42.  
  43.     EXAMPLE
  44.     // Read a file line by line
  45.     char line[256];
  46.  
  47.     // Read until EOF
  48.     while (fgets (line, sizeof (line), fh))
  49.     {
  50.         // Evaluate the line
  51.     }
  52.  
  53.     BUGS
  54.  
  55.     SEE ALSO
  56.     fopen(), gets(), fputs(), putc()
  57.  
  58.     INTERNALS
  59.  
  60.     HISTORY
  61.     10.12.1996 digulla created
  62.  
  63. ******************************************************************************/
  64. {
  65.     BPTR fh;
  66.  
  67.     switch ((IPTR)stream)
  68.     {
  69.     case 1: /* Stdin */
  70.     fh = Input ();
  71.  
  72.     case 2: /* Stdout */
  73.     case 3: /* Stderr */
  74.     errno = EINVAL;
  75.     return NULL;
  76.  
  77.     default:
  78.     fh = (BPTR)stream->fh;
  79.     break;
  80.     }
  81.  
  82.     buffer = FGets (fh, buffer, size);
  83.  
  84.     if (!buffer)
  85.     {
  86.     if (IoErr ())
  87.         stream->flags |= _STDIO_FILEFLAG_ERROR;
  88.     else
  89.         stream->flags |= _STDIO_FILEFLAG_EOF;
  90.     }
  91.  
  92.     return buffer;
  93. } /* fgets */
  94.  
  95.