New app zmail - initial mail client; libc version 0.4.0 with major update - lot of...
[ZeXOS.git] / libc / stdio / fgets.c
blob16cc6413fec4f5b8ce0bc9c05b0d30a0e40f5124
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <errno.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
25 char *fgets (char *s, int size, FILE *stream)
27 if (!stream || !s || size < 2)
28 goto err;
30 if (stream->mode & O_WRONLY)
31 goto err;
33 int i;
34 for (i = 0; i < (size-1); i ++) {
35 int r = fgetc (stream);
37 if (!r)
38 break;
40 s[i] = r;
42 if (r == '\n')
43 break;
46 s[i+1] = '\0';
48 if (!i)
49 goto err;
51 stream->off += i;
53 return s;
54 err:
55 errno_update ();
57 return 0;