#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#define MAXLEN 256



int main( int argc, char *argv[] )
{
	FILE_INFO_3 *buf, *cur;
	DWORD read, total, resumeh, rc, i;
	wchar_t server[MAXLEN], user[MAXLEN];

	if ( argc < 2 || argc > 3 )
	{
		puts( "usage: nfe \\\\server [username]" );
		return 1;
	}


	if ( argc > 2 )
		mbstowcs( user, argv[2], MAXLEN );
	else
		user[0] = L'\0';
	mbstowcs( server, argv[1], MAXLEN );

	resumeh = 0;
	do
	{
		buf = NULL;
		rc = NetFileEnum( (char *) server, NULL,
			user[0] == L'\0'? NULL: (char *) user, 3,
			(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 );
		printf( "%9.9s %-5.5s %5.5s %-20.20s %s\n",
			"id", "RWC", "locks", "user name", "path" );
		printf( "%9.9s %-3.3s %5.5s %-20.20s %s\n",
			"---------", "---", "-----", "--------------------", "--------------------------------------" );
		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( "%9lu %c%c%c %5lu %-20.20S %S\n",
				cur->fi3_id,
				cur->fi3_permissions & PERM_FILE_READ? 'R': ' ',
				cur->fi3_permissions & PERM_FILE_WRITE? 'W': ' ',
				cur->fi3_permissions & PERM_FILE_CREATE? 'C': ' ',
				cur->fi3_num_locks,
				cur->fi3_username, cur->fi3_pathname );
		}

		if ( buf != NULL )
			NetApiBufferFree( buf );

	} while ( rc == ERROR_MORE_DATA );

	if ( rc != ERROR_SUCCESS )
		printf( "NCE() returned %lu\n", rc );

	return 0;
}
