#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#define MAXLEN 256
#define nonull(x) ( (x) == NULL? (char *) L"*null*": (x) )



const char *status_names[] = { "OK", "paused", "disconnected", "network error", "connected", "reconnected" };
const char * asg_names[] = { "*wild*", "disk", "printq", "char device", "IPC" };



int main( int argc, char *argv[] )
{
	USE_INFO_2 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN];

	if ( argc != 2 )
	{
		puts( "usage: nusee \\\\server" );
		return 1;
	}

	mbstowcs( server, argv[1], MAXLEN );

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetUseEnum( (char *) server, 2, (BYTE **) &buf, 2048, &read, &total, &resumeh );
		if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
			break;

		printf( "\ngot %lu entries out of %lu\n", read, total );
		for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
		{
			// Note: the capital S in the format string will expect Unicode
			// strings, as this is a program written/compiled for ANSI.
			printf( "local:      %S\n", nonull( cur->ui2_local ) );
			printf( "remote:     %S\n", nonull( cur->ui2_remote ) );
			printf( "password:   %S\n", nonull( cur->ui2_password ) );
			printf( "status:     %lu [%s]\n", cur->ui2_status, status_names[cur->ui2_status] );
			printf( "asg_type:   %lu [%s]\n", cur->ui2_asg_type, asg_names[cur->ui2_asg_type + 1] );
			printf( "refcount:   %lu\n", cur->ui2_refcount );
			printf( "usecount:   %lu\n", cur->ui2_usecount );
			printf( "username:   %S\n", nonull( cur->ui2_username ) );
			printf( "domainname: %S\n\n", nonull( cur->ui2_domainname ) );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NetUseEnum() returned %lu\n", rc );

	return 0;
}
