Name

getws - reads a wide character string from stdin

Library

libc.lib

Synopsis

  #include <wchar.h>
  wchar_t * getws (wchar_t * restrict ws);

Return values

Upon successful completion, getws returns ws. The getws function does not distinguish between end-of-file and error, and callers must use feof and ferror to determine which occurred.

Detailed description

The getws function is equivalent to fgetws with an infinite size and a stream of stdin, except that the wide character newline(if any) is not stored in the string. It is the caller’s responsibility to ensure that the input line, if any, is sufficiently short to fit in the string.


Examples

#include <stdio.h>
#include <wchar.h>
/* Illustrates how to use getws API */
int example_getws()
        {
        FILE* stdop = freopen("c:\stdop","w+",stdout);
        FILE* stdip = freopen("c:\stdip","w+",stdin);
        FILE* stder = freopen("c:\stder","w+",stderr);
        wchar_t* buf = (wchar_t*)malloc(sizeof(wchar_t)*50);
        wchar_t s[100];
        int ret = 0;
        size_t size = fwrite("abdcef\n", 1, 6, stdip);          //write to stdin
        fclose(stdip);
        stdip = freopen("c:\stdip","r", stdin);
        getws(s);               // read a line (from stdin)
        putws(s);               //write to stdout
        fclose(stdop);
        stdop = freopen("c:\stdop","r", stdout);
        fgetws(buf,7,stdop);            //read from stdout
        if(wcsncmp(s, buf,6))
                {
                ret = -1;
                }
        fclose(stdin);
        fclose(stder);
        fclose(stdop);
        remove("c:\stdop");
        remove("c:\stdip");
        remove("c:\stder");
        return ret;
        }

         

Output

abcdef
abcdef

         

Security considerations

The getws function cannot be used securely. Because of its lack of bounds checking, and the inability for the calling program to reliably determine the length of the next incoming line, the use of this function enables malicious users to arbitrarily change a running program’s functionality through a buffer overflow attack. It is strongly suggested that the fgetws function be used in all cases.


Errors

The getws function will fail if:
[EFAULT]
  The given ws argument is NULL.

The function getws may also fail and set errno for any of the errors specified for the routines fflush, fstat, read, fgetws, or malloc.


See also

feof, ferror, fgets, fgetws, gets

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top