Copyright 1989 AT&T Copyright (c) 2001, Sun Microsystems, Inc. All Rights Reserved
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
cc [ flag ... ] file ... -lgen [ library ... ] #include <libgen.h> char *bgets(char *buffer, size_t count, FILE *stream, const char *breakstring);
The bgets() function reads characters from stream into buffer until either count is exhausted or one of the characters in breakstring is encountered in the stream. The read data is terminated with a null byte ('\e0') and a pointer to the trailing null is returned. If a breakstring character is encountered, the last non-null is the delimiter character that terminated the scan.
Note that, except for the fact that the returned value points to the end of the read string rather than to the beginning, the call
bgets(buffer, sizeof buffer, stream, "\en");
is identical to
fgets (buffer, sizeof buffer, stream);
There is always enough room reserved in the buffer for the trailing null character.
If breakstring is a null pointer, the value of breakstring from the previous call is used. If breakstring is null at the first call, no characters will be used to delimit the string.
NULL is returned on error or end-of-file. Reporting the condition is delayed to the next call if any characters were read but not yet returned.
Example 1 Example of the bgets() function.
The following example prints the name of the first user encountered in /etc/passswd, including a trailing ":"
#include <stdio.h> #include<libgen.h> int main() { char buffer[8]; FILE *fp; if ((fp = fopen("/etc/passwd","r")) == NULL) { perror("/etc/passwd"); return 1; } if (bgets(buffer, 8, fp, ":") == NULL) { perror("bgets"); return 1; } (void) puts(buffer); return 0; }
See attributes(5) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
MT-Level | MT-Safe |
gets(3C), attributes(5)
When compiling multithread applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in multithreaded applications.