fgetws.c (24990dfad088db7073e30b8870d62366fb510cd3) fgetws.c (7591ae56aeeefc978363f92d208d5e1140090f96)
1/*-
2 * Copyright (c) 2002 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

36#include "local.h"
37
38wchar_t *
39fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
40{
41 wchar_t *wsp;
42 wint_t wc;
43
1/*-
2 * Copyright (c) 2002 Tim J. Robbins.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

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

36#include "local.h"
37
38wchar_t *
39fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)
40{
41 wchar_t *wsp;
42 wint_t wc;
43
44 ORIENTLOCK(fp, 1);
44 FLOCKFILE(fp);
45 ORIENT(fp, 1);
45
46
46 if (n <= 0)
47 return (NULL);
47 if (n <= 0) {
48 errno = EINVAL;
49 goto error;
50 }
48
49 wsp = ws;
50 while (n-- > 1) {
51 /* XXX Inefficient */
51
52 wsp = ws;
53 while (n-- > 1) {
54 /* XXX Inefficient */
52 if ((wc = fgetwc(fp)) == WEOF && errno == EILSEQ)
53 return (NULL);
55 if ((wc = __fgetwc(fp)) == WEOF && errno == EILSEQ)
56 goto error;
54 if (wc == WEOF) {
55 if (wsp == ws)
56 /* EOF/error, no characters read yet. */
57 if (wc == WEOF) {
58 if (wsp == ws)
59 /* EOF/error, no characters read yet. */
57 return (NULL);
60 goto error;
58 break;
59 }
60 *wsp++ = (wchar_t)wc;
61 if (wc == L'\n')
62 break;
63 }
64 *wsp++ = L'\0';
61 break;
62 }
63 *wsp++ = (wchar_t)wc;
64 if (wc == L'\n')
65 break;
66 }
67 *wsp++ = L'\0';
68 FUNLOCKFILE(fp);
65
66 return (ws);
69
70 return (ws);
71
72error:
73 FUNLOCKFILE(fp);
74 return (NULL);
67}
75}