1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FGETS.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,131 @@
1.4 +/* FGETS.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +/*
1.28 +
1.29 +FUNCTION
1.30 + <<fgets>>---get character string from a file or stream
1.31 +INDEX
1.32 + fgets
1.33 +
1.34 +ANSI_SYNOPSIS
1.35 + #include <stdio.h>
1.36 + char *fgets(char *<[buf]>, int <[n]>, FILE *<[fp]>);
1.37 +
1.38 +TRAD_SYNOPSIS
1.39 + #include <stdio.h>
1.40 + char *fgets(<[buf]>,<[n]>,<[fp]>)
1.41 + char *<[buf]>;
1.42 + int <[n]>;
1.43 + FILE *<[fp]>;
1.44 +
1.45 +DESCRIPTION
1.46 + Reads at most <[n-1]> characters from <[fp]> until a newline
1.47 + is found. The characters including to the newline are stored
1.48 + in <[buf]>. The buffer is terminated with a 0.
1.49 +
1.50 +
1.51 +RETURNS
1.52 + <<fgets>> returns the buffer passed to it, with the data
1.53 + filled in. If end of file occurs with some data already
1.54 + accumulated, the data is returned with no other indication. If
1.55 + no data are read, NULL is returned instead.
1.56 +
1.57 +PORTABILITY
1.58 + <<fgets>> should replace all uses of <<gets>>. Note however
1.59 + that <<fgets>> returns all of the data, while <<gets>> removes
1.60 + the trailing newline (with no indication that it has done so.)
1.61 +
1.62 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.63 +<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
1.64 +*/
1.65 +
1.66 +#include <stdio.h>
1.67 +#include <string.h>
1.68 +#include "LOCAL.H"
1.69 +
1.70 +/**
1.71 +Get a string from a stream.
1.72 +@return On success, the string read is returned.
1.73 +On end-of-file or error, null pointer is returned.
1.74 +@param buf pointer to a buffer where to store data read.
1.75 +@param n max number of bytes to be read.
1.76 +@param fp pointer to an open file.
1.77 +*/
1.78 +EXPORT_C char *
1.79 +fgets (char *buf, int n, FILE * fp)
1.80 +{
1.81 + size_t len;
1.82 + char *s;
1.83 + unsigned char *p, *t;
1.84 +
1.85 + if (n < 2) /* sanity check */
1.86 + return 0;
1.87 +
1.88 + s = buf;
1.89 + n--; /* leave space for NUL */
1.90 + do
1.91 + {
1.92 + /*
1.93 + * If the buffer is empty, refill it.
1.94 + */
1.95 + if ((len = fp->_r) <= 0)
1.96 + {
1.97 + if (__srefill (fp))
1.98 + {
1.99 + /* EOF: stop with partial or no line */
1.100 + if (s == buf)
1.101 + return 0;
1.102 + break;
1.103 + }
1.104 + len = fp->_r;
1.105 + }
1.106 + p = fp->_p;
1.107 +
1.108 + /*
1.109 + * Scan through at most n bytes of the current buffer,
1.110 + * looking for '\n'. If found, copy up to and including
1.111 + * newline, and stop. Otherwise, copy entire chunk
1.112 + * and loop.
1.113 + */
1.114 + if ((int)len > n)
1.115 + len = n;
1.116 + t = (unsigned char *) memchr ((void*) p, '\n', len);
1.117 + if (t != 0)
1.118 + {
1.119 + len = ++t - p;
1.120 + fp->_r -= len;
1.121 + fp->_p = t;
1.122 + (void) memcpy ((void*) s, (void*) p, len);
1.123 + s[len] = 0;
1.124 + return (buf);
1.125 + }
1.126 + fp->_r -= len;
1.127 + fp->_p += len;
1.128 + (void) memcpy ((void*) s, (void*) p, len);
1.129 + s += len;
1.130 + }
1.131 + while ((n -= len) != 0);
1.132 + *s = 0;
1.133 + return buf;
1.134 +}