windows fix main.cpp for windows chrome
[gssweb.git] / json_gssapi / main.cpp
1 //
2 #include <iostream>
3 #ifdef WIN32
4 #include <stdio.h>
5 #include <fcntl.h>
6 #include <io.h>
7 #else
8 #include <unistd.h>
9 #include <string.h>
10 #endif
11 #include <GSSRequest.h>
12
13
14 using std::cin;
15 using std::cout;
16 using std::endl;
17 using std::getline;
18
19 int main(int argc, char **argv) {
20   /* Variables */
21   char *input;
22   char *output;
23   int32_t len;
24   
25   /* Error checking */
26   
27   /* Setup */
28   
29   /* Main processing */
30 #ifdef WIN32
31   _setmode(_fileno(stdin), _O_BINARY);
32 #endif
33   int readThisRound, readTotal, readRemaining;
34   do 
35   {
36     // Read 32 bit length
37     len = 0;
38     readThisRound = readTotal = 0;
39     while(4 != readTotal)
40     {
41       readThisRound = _read(0, ((&len) + readTotal), 4 - readTotal);
42       readTotal += readThisRound;
43     }
44     
45     // Reads the number of bytes indicated by the above read
46     input = new char[len + 1];
47     readTotal = readThisRound = 0;
48     while (readTotal < len)
49     {
50       readRemaining = len - readTotal;
51       readThisRound = _read( 0, &(input[readTotal]), readRemaining);
52       if (-1 == readThisRound)
53         break;
54       else
55         readTotal += readThisRound;
56     }
57     // ... and null-terminate it
58     input[len] = '\0';
59     
60     output = gss_request(input);
61     len = (int32_t )strlen(output);
62     
63     cout.write((char *)&len, 4);
64     cout << output;
65     deallocate_reply(output);
66     cout.flush();
67   } while(1);
68   return 0;
69 }