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