fgets.c (d1d015864103b253b3fcb2f72a0da5b0cfeb31b6) fgets.c (031986c4f39f3baf381baa5fa02add0c2b9c5124)
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 23 unchanged lines hidden (view full) ---

32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include "namespace.h"
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 23 unchanged lines hidden (view full) ---

32
33#if defined(LIBC_SCCS) && !defined(lint)
34static char sccsid[] = "@(#)fgets.c 8.2 (Berkeley) 12/22/93";
35#endif /* LIBC_SCCS and not lint */
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39#include "namespace.h"
40#include <errno.h>
40#include <stdio.h>
41#include <string.h>
42#include "un-namespace.h"
43#include "local.h"
44#include "libc_private.h"
45
46/*
47 * Read at most n-1 characters from the given file.
48 * Stop when a newline has been read, or the count runs out.
49 * Return first argument, or NULL if no characters were read.
50 */
51char *
52fgets(char * __restrict buf, int n, FILE * __restrict fp)
53{
54 size_t len;
55 char *s;
56 unsigned char *p, *t;
57
41#include <stdio.h>
42#include <string.h>
43#include "un-namespace.h"
44#include "local.h"
45#include "libc_private.h"
46
47/*
48 * Read at most n-1 characters from the given file.
49 * Stop when a newline has been read, or the count runs out.
50 * Return first argument, or NULL if no characters were read.
51 */
52char *
53fgets(char * __restrict buf, int n, FILE * __restrict fp)
54{
55 size_t len;
56 char *s;
57 unsigned char *p, *t;
58
58 if (n <= 0) /* sanity check */
59 return (NULL);
60
61 FLOCKFILE(fp);
62 ORIENT(fp, -1);
59 FLOCKFILE(fp);
60 ORIENT(fp, -1);
61
62 if (n <= 0) { /* sanity check */
63 fp->_flags |= __SERR;
64 errno = EINVAL;
65 FUNLOCKFILE(fp);
66 return (NULL);
67 }
68
63 s = buf;
64 n--; /* leave space for NUL */
65 while (n != 0) {
66 /*
67 * If the buffer is empty, refill it.
68 */
69 if ((len = fp->_r) <= 0) {
70 if (__srefill(fp)) {
71 /* EOF/error: stop with partial or no line */
69 s = buf;
70 n--; /* leave space for NUL */
71 while (n != 0) {
72 /*
73 * If the buffer is empty, refill it.
74 */
75 if ((len = fp->_r) <= 0) {
76 if (__srefill(fp)) {
77 /* EOF/error: stop with partial or no line */
72 if (s == buf) {
78 if (!__sfeof(fp) || s == buf) {
73 FUNLOCKFILE(fp);
74 return (NULL);
75 }
76 break;
77 }
78 len = fp->_r;
79 }
80 p = fp->_p;

--- 29 unchanged lines hidden ---
79 FUNLOCKFILE(fp);
80 return (NULL);
81 }
82 break;
83 }
84 len = fp->_r;
85 }
86 p = fp->_p;

--- 29 unchanged lines hidden ---