os/ossrv/genericopenlibs/cstdlib/LSTDIO/FGETS.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* FGETS.C
     2  * 
     3  * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 /*
     8  * Copyright (c) 1990 The Regents of the University of California.
     9  * All rights reserved.
    10  *
    11  * Redistribution and use in source and binary forms are permitted
    12  * provided that the above copyright notice and this paragraph are
    13  * duplicated in all such forms and that any documentation,
    14  * advertising materials, and other materials related to such
    15  * distribution and use acknowledge that the software was developed
    16  * by the University of California, Berkeley.  The name of the
    17  * University may not be used to endorse or promote products derived
    18  * from this software without specific prior written permission.
    19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    22  */
    23 
    24 /*
    25 
    26 FUNCTION
    27         <<fgets>>---get character string from a file or stream
    28 INDEX
    29 	fgets
    30 
    31 ANSI_SYNOPSIS
    32         #include <stdio.h>
    33 	char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
    34 
    35 TRAD_SYNOPSIS
    36 	#include <stdio.h>
    37 	char *fgets(<[buf]>,<[n]>,<[fp]>)
    38         char *<[buf]>;
    39 	int <[n]>;
    40 	FILE *<[fp]>;
    41 
    42 DESCRIPTION
    43 	Reads at most <[n-1]> characters from <[fp]> until a newline
    44 	is found. The characters including to the newline are stored
    45 	in <[buf]>. The buffer is terminated with a 0.
    46 
    47 
    48 RETURNS
    49 	<<fgets>> returns the buffer passed to it, with the data
    50 	filled in. If end of file occurs with some data already
    51 	accumulated, the data is returned with no other indication. If
    52 	no data are read, NULL is returned instead.
    53 
    54 PORTABILITY
    55 	<<fgets>> should replace all uses of <<gets>>. Note however
    56 	that <<fgets>> returns all of the data, while <<gets>> removes
    57 	the trailing newline (with no indication that it has done so.)
    58 
    59 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
    60 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
    61 */
    62 
    63 #include <stdio.h>
    64 #include <string.h>
    65 #include "LOCAL.H"
    66 
    67 /**
    68 Get a string from a stream.
    69 @return   On success, the string read is returned.
    70 On end-of-file or error, null pointer is returned.
    71 @param buf pointer to a buffer where to store data read. 
    72 @param n max number of bytes to be read. 
    73 @param fp pointer to an open file.
    74 */
    75 EXPORT_C char *
    76 fgets (char *buf, int n, FILE * fp)
    77 {
    78   size_t len;
    79   char *s;
    80   unsigned char *p, *t;
    81 
    82   if (n < 2)			/* sanity check */
    83     return 0;
    84 
    85   s = buf;
    86   n--;				/* leave space for NUL */
    87   do
    88     {
    89       /*
    90        * If the buffer is empty, refill it.
    91        */
    92       if ((len = fp->_r) <= 0)
    93 	{
    94 	  if (__srefill (fp))
    95 	    {
    96 	      /* EOF: stop with partial or no line */
    97 	      if (s == buf)
    98 		return 0;
    99 	      break;
   100 	    }
   101 	  len = fp->_r;
   102 	}
   103       p = fp->_p;
   104 
   105       /*
   106        * Scan through at most n bytes of the current buffer,
   107        * looking for '\n'.  If found, copy up to and including
   108        * newline, and stop.  Otherwise, copy entire chunk
   109        * and loop.
   110        */
   111       if ((int)len > n)
   112 	len = n;
   113       t = (unsigned char *) memchr ((void*) p, '\n', len);
   114       if (t != 0)
   115 	{
   116 	  len = ++t - p;
   117 	  fp->_r -= len;
   118 	  fp->_p = t;
   119 	  (void) memcpy ((void*) s, (void*) p, len);
   120 	  s[len] = 0;
   121 	  return (buf);
   122 	}
   123       fp->_r -= len;
   124       fp->_p += len;
   125       (void) memcpy ((void*) s, (void*) p, len);
   126       s += len;
   127     }
   128   while ((n -= len) != 0);
   129   *s = 0;
   130   return buf;
   131 }