logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

fgets.3p (4889B)


  1. '\" et
  2. .TH FGETS "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
  3. .\"
  4. .SH PROLOG
  5. This manual page is part of the POSIX Programmer's Manual.
  6. The Linux implementation of this interface may differ (consult
  7. the corresponding Linux manual page for details of Linux behavior),
  8. or the interface may not be implemented on Linux.
  9. .\"
  10. .SH NAME
  11. fgets
  12. \(em get a string from a stream
  13. .SH SYNOPSIS
  14. .LP
  15. .nf
  16. #include <stdio.h>
  17. .P
  18. char *fgets(char *restrict \fIs\fP, int \fIn\fP, FILE *restrict \fIstream\fP);
  19. .fi
  20. .SH DESCRIPTION
  21. The functionality described on this reference page is aligned with the
  22. ISO\ C standard. Any conflict between the requirements described here and the
  23. ISO\ C standard is unintentional. This volume of POSIX.1\(hy2017 defers to the ISO\ C standard.
  24. .P
  25. The
  26. \fIfgets\fR()
  27. function shall read bytes from
  28. .IR stream
  29. into the array pointed to by
  30. .IR s
  31. until
  32. .IR n \-1
  33. bytes are read, or a
  34. <newline>
  35. is read and transferred to
  36. .IR s ,
  37. or an end-of-file condition is encountered. A null byte shall be
  38. written immediately after the last byte read into the array.
  39. If the end-of-file condition is encountered before any bytes
  40. are read, the contents of the array pointed to by
  41. .IR s
  42. shall not be changed.
  43. .P
  44. The
  45. \fIfgets\fR()
  46. function may mark the last data access timestamp of the file associated
  47. with
  48. .IR stream
  49. for update. The last data access timestamp shall be marked for update
  50. by the first successful execution of
  51. \fIfgetc\fR(),
  52. \fIfgets\fR(),
  53. \fIfread\fR(),
  54. \fIfscanf\fR(),
  55. \fIgetc\fR(),
  56. \fIgetchar\fR(),
  57. \fIgetdelim\fR(),
  58. \fIgetline\fR(),
  59. \fIgets\fR(),
  60. or
  61. \fIscanf\fR()
  62. using
  63. .IR stream
  64. that returns data not supplied by a prior call to
  65. \fIungetc\fR().
  66. .SH "RETURN VALUE"
  67. Upon successful completion,
  68. \fIfgets\fR()
  69. shall return
  70. .IR s .
  71. If the stream is at end-of-file, the end-of-file indicator for
  72. the stream shall be set and
  73. \fIfgets\fR()
  74. shall return a null pointer.
  75. If a read error occurs, the error indicator for the stream shall be set,
  76. \fIfgets\fR()
  77. shall return a null pointer,
  78. and shall set
  79. .IR errno
  80. to indicate the error.
  81. .SH ERRORS
  82. Refer to
  83. .IR "\fIfgetc\fR\^(\|)".
  84. .LP
  85. .IR "The following sections are informative."
  86. .SH EXAMPLES
  87. .SS "Reading Input"
  88. .P
  89. The following example uses
  90. \fIfgets\fR()
  91. to read lines of input. It assumes that the file it is reading is a text
  92. file and that lines in this text file are no longer than 16384 (or
  93. {LINE_MAX}
  94. if it is less than 16384 on the implementation where it is running)
  95. bytes long. (Note that the standard utilities have no line length limit if
  96. .IR sysconf (_SC_LINE_MAX)
  97. returns \-1 without setting
  98. .IR errno .
  99. This example assumes that
  100. .IR sysconf (_SC_LINE_MAX)
  101. will not fail.)
  102. .sp
  103. .RS 4
  104. .nf
  105. #include <limits.h>
  106. #include <stdio.h>
  107. #include <unistd.h>
  108. #define MYLIMIT 16384
  109. .P
  110. char *line;
  111. int line_max;
  112. if (LINE_MAX >= MYLIMIT) {
  113. // Use maximum line size of MYLIMIT. If LINE_MAX is
  114. // bigger than our limit, sysconf() cannot report a
  115. // smaller limit.
  116. line_max = MYLIMIT;
  117. } else {
  118. long limit = sysconf(_SC_LINE_MAX);
  119. line_max = (limit < 0 || limit > MYLIMIT) ? MYLIMIT : (int)limit;
  120. }
  121. .P
  122. // line_max + 1 leaves room for the null byte added by fgets().
  123. line = malloc(line_max + 1);
  124. if (line == NULL) {
  125. // out of space
  126. ...
  127. return error;
  128. }
  129. .P
  130. while (fgets(line, line_max + 1, fp) != NULL) {
  131. // Verify that a full line has been read ...
  132. // If not, report an error or prepare to treat the
  133. // next time through the loop as a read of a
  134. // continuation of the current line.
  135. ...
  136. // Process line ...
  137. ...
  138. }
  139. free(line);
  140. \&...
  141. .fi
  142. .P
  143. .RE
  144. .SH "APPLICATION USAGE"
  145. None.
  146. .SH RATIONALE
  147. None.
  148. .SH "FUTURE DIRECTIONS"
  149. None.
  150. .SH "SEE ALSO"
  151. .IR "Section 2.5" ", " "Standard I/O Streams",
  152. .IR "\fIfgetc\fR\^(\|)",
  153. .IR "\fIfopen\fR\^(\|)",
  154. .IR "\fIfread\fR\^(\|)",
  155. .IR "\fIfscanf\fR\^(\|)",
  156. .IR "\fIgetc\fR\^(\|)",
  157. .IR "\fIgetchar\fR\^(\|)",
  158. .IR "\fIgetdelim\fR\^(\|)",
  159. .IR "\fIgets\fR\^(\|)",
  160. .IR "\fIungetc\fR\^(\|)"
  161. .P
  162. The Base Definitions volume of POSIX.1\(hy2017,
  163. .IR "\fB<stdio.h>\fP"
  164. .\"
  165. .SH COPYRIGHT
  166. Portions of this text are reprinted and reproduced in electronic form
  167. from IEEE Std 1003.1-2017, Standard for Information Technology
  168. -- Portable Operating System Interface (POSIX), The Open Group Base
  169. Specifications Issue 7, 2018 Edition,
  170. Copyright (C) 2018 by the Institute of
  171. Electrical and Electronics Engineers, Inc and The Open Group.
  172. In the event of any discrepancy between this version and the original IEEE and
  173. The Open Group Standard, the original IEEE and The Open Group Standard
  174. is the referee document. The original Standard can be obtained online at
  175. http://www.opengroup.org/unix/online.html .
  176. .PP
  177. Any typographical or formatting errors that appear
  178. in this page are most likely
  179. to have been introduced during the conversion of the source files to
  180. man page format. To report such errors, see
  181. https://www.kernel.org/doc/man-pages/reporting_bugs.html .