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