00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifdef WIN32
00014
00015 #ifdef UNDER_CE
00016
00017
00018 int getinput(char *buffer, int maxchars)
00019 {
00020 return -1;
00021 }
00022
00023 #else
00024
00025
00026 #include <stdio.h>
00027 #include <string.h>
00028
00029
00030
00031
00032
00033 int getinput(char *buffer, int maxchars)
00034 {
00035 if (NULL == fgets(buffer, maxchars, stdin))
00036 {
00037 if (feof(stdin))
00038 {
00039 return 0;
00040 }
00041
00042 return -1;
00043 }
00044
00045 return strlen(buffer);
00046 }
00047
00048 #endif
00049
00050 #else
00051
00052 #include <unistd.h>
00053 #include <fcntl.h>
00054
00055
00056
00057
00058
00059
00060 int getinput(char *buffer, int maxchars)
00061 {
00062 int flags;
00063 int nchars;
00064 int index = 0;
00065
00066
00067 flags = fcntl(STDIN_FILENO, F_GETFL);
00068
00069
00070 fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
00071
00072
00073 while (1 == (nchars = read(STDIN_FILENO, &buffer[index], 1)))
00074 {
00075 if (buffer[index++] == '\n')
00076 {
00077 buffer[index] = 0;
00078 break;
00079 }
00080 }
00081
00082
00083 fcntl(STDIN_FILENO, F_SETFL, flags);
00084
00085 if (nchars == -1)
00086 {
00087 return -1;
00088 }
00089
00090 if (nchars == 0)
00091 {
00092 return 0;
00093 }
00094
00095 return index;
00096 }
00097
00098 #endif