1 /*
2 * win32.c
3 * - utility functions for cvs under win32
4 *
5 */
6
7 #include <ctype.h>
8 #include <stdio.h>
9 #include <conio.h>
10
11 #define WIN32_LEAN_AND_MEAN
12 #include <windows.h>
13
14 #include <config.h>
15 #include <winsock.h>
16 #include <stdlib.h>
17
18 #include "cvs.h"
19
20 void
init_winsock()21 init_winsock ()
22 {
23 WSADATA data;
24
25 if (WSAStartup (MAKEWORD (1, 1), &data))
26 {
27 fprintf (stderr, "cvs: unable to initialize winsock\n");
28 exit (1);
29 }
30 }
31
32 void
wnt_cleanup(void)33 wnt_cleanup (void)
34 {
35 if (WSACleanup ())
36 {
37 #ifdef SERVER_ACTIVE
38 if (server_active || error_use_protocol)
39 /* FIXME: how are we supposed to report errors? As of now
40 (Sep 98), error() can in turn call us (if it is out of
41 memory) and in general is built on top of lots of
42 stuff. */
43 ;
44 else
45 #endif
46 fprintf (stderr, "cvs: cannot WSACleanup: %s\n",
47 sock_strerror (WSAGetLastError ()));
48 }
49 }
50
sleep(unsigned seconds)51 unsigned sleep(unsigned seconds)
52 {
53 Sleep(1000*seconds);
54 return 0;
55 }
56
57 /*
58 * Sleep at least useconds microseconds.
59 */
usleep(unsigned long useconds)60 int usleep(unsigned long useconds)
61 {
62 /* Not very accurate, but it gets the job done */
63 Sleep(useconds/1000 + (useconds%1000 ? 1 : 0));
64 return 0;
65 }
66
67 #if 0
68
69 /* WinSock has a gethostname. But note that WinSock gethostname may
70 want to talk to the network, which is kind of bogus in the
71 non-client/server case. I'm not sure I can think of any obvious
72 solution. Most of the ways I can think of to figure out whether
73 to call gethostname or GetComputerName seem kind of kludgey, and/or
74 might result in picking the name in a potentially confusing way
75 (I'm not sure exactly how the name(s) are set). */
76
77 int gethostname(char* name, int namelen)
78 {
79 DWORD dw = namelen;
80 BOOL ret = GetComputerName(name, &dw);
81 namelen = dw;
82 return (ret) ? 0 : -1;
83 }
84 #endif
85
win32getlogin()86 char *win32getlogin()
87 {
88 static char name[256];
89 DWORD dw = 256;
90 GetUserName (name, &dw);
91 if (name[0] == '\0')
92 return NULL;
93 else
94 return name;
95 }
96
97
98 pid_t
getpid()99 getpid ()
100 {
101 return (pid_t) GetCurrentProcessId();
102 }
103
104 char *
getpass(const char * prompt)105 getpass (const char *prompt)
106 {
107 static char pwd_buf[128];
108 size_t i;
109
110 fputs (prompt, stderr);
111 fflush (stderr);
112 for (i = 0; i < sizeof (pwd_buf) - 1; ++i)
113 {
114 pwd_buf[i] = _getch ();
115 if (pwd_buf[i] == '\r')
116 break;
117 }
118 pwd_buf[i] = '\0';
119 fputs ("\n", stderr);
120 return pwd_buf;
121 }
122