-
Notifications
You must be signed in to change notification settings - Fork 0
/
netuse.cpp
82 lines (56 loc) · 1.37 KB
/
netuse.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <ctype.h>
static char whitespace[] = " \t";
static char* GetToken (char* &buffer);
int main (int argc, char *argv[])
{
char buffer[512];
FILE *results = _popen ("net use", "rt");
if (results == NULL)
{ printf ("_popen failed\n");
return 1;
}
while (!feof (results))
{
if (fgets (buffer, sizeof(buffer), results) == NULL)
continue;
char *ptr = buffer;
char *token = buffer;
token = GetToken (ptr);
if ( (0 != _stricmp (token, "ok"))
&& (0 != _stricmp (token, "disconnected"))
)
{
continue;
}
token = GetToken (ptr);
if (!isalpha(token[0]) || (token[1] != ':'))
continue;
char *drive = token;
token = GetToken (ptr);
if ((token[0] != '\\') || (token[1] != '\\'))
continue;
printf ("%s \"%s\"\n", drive, token);
}
_pclose (results);
return 0;
}
static char* GetToken (char* &buffer)
{
char *ptr = buffer;
if (!*ptr)
return NULL;
while (*ptr && isspace(*ptr))
++ptr;
char *token = ptr;
if (!*ptr) return token;
while (*ptr && !isspace(*ptr))
++ptr;
if (*ptr)
*ptr++ = 0;
buffer = ptr;
return token;
}