📄 setipaddrarray.cpp
字号:
);
// Note: Should check bRead.
int rc2 =
ArrangeData( // Convert readable IP addresses into network form.
pRecords, // Area with IP addresses in readable form.
ulBytesRead, // Number of bytes in said area.
pArr, // Area to get IP addresses in network form.
(int *)&ulBytesUsed // Variable to get bytes returned.
);
if (rcOK!=rc2)
{
printf(_T("GetData(): ArrangeData() had a problem, rc = 0x%08x\n"), rc2);
rc = rcErr;
ulBytesUsed = 0;
break;
}
break; // For now, end with a single iteration.
} // End 'for' go through records.
} while(0);
if (INVALID_HANDLE_VALUE!=hFile)
{
BOOL bClose = CloseHandle(hFile);
if (0==bClose)
{
printf(_T("GetData(): Error in closing file, rc = %d\n"), GetLastError());
}
}
if (NULL!=pRecords) // Allocated working storage?
free(pRecords);
if (rcErr==rc) // An error?
{
if (NULL!=*pOutVar) // Allocated output storage?
{
free(*pOutVar);
*pOutVar = NULL;
}
}
*pSzArr = ulBytesUsed;
return rc;
} // End GetData().
/**************************************************************************************************/
/* */
/* Comparison function used by qsort(). */
/* */
/* Note: This makes a comparison with the values reversed from network order, since, for ex- */
/* ample, 192.168.2.20 (0x1402a8c0) is less than 192.168.1.255 (0xff01a8c0) in network */
/* order. */
/* */
/**************************************************************************************************/
int
_cdecl
CompareIPAddrs(const void * pArg1, const void * pArg2)
{
#define GetNetwkAddrDw(a) /* Get address of a doubleword (4 bytes) in network form; */ \
/* eg, GetNetwkAddrDw(-xc0a80105) => 0x0501a8c0. */ \
(((a&0xFF)<<24) + ((a&0xFF00)<<8) + ((a&0xFF0000)>>8) + ((a&0xFF000000)>>24))
if (GetNetwkAddrDw(*(PULONG)pArg1) > GetNetwkAddrDw(*(PULONG)pArg2))
return 1;
else
if (GetNetwkAddrDw(*(PULONG)pArg1) < GetNetwkAddrDw(*(PULONG)pArg2))
return -1;
else
return 0;
} // End CompareIPAddrs().
/**************************************************************************************************/
/* */
/* Parse input string. */
/* */
/**************************************************************************************************/
int
ParseIt(
int nbrArgs, // Number of string parameters.
WCHAR * pArgv[], // Array of addresses of parameters.
PULONG pCmdOper, // Pointer to variable to receive parsed command number.
PWCHAR * ppSystemName, // Pointer to variable to receive address of system name. NULL returned if none.
PWCHAR * ppUserid, // Pointer to variable to receive address of userid. NULL returned if none.
PWCHAR * ppPassword, // Pointer to variable to receive address of password. NULL returned if none.
PWCHAR * ppAdapterName // Pointer to variable to receive address of adapter name. NULL returned if none.
)
{
int rc = rcOK,
i,
j,
CmdNbr = CmdUnknown;
WCHAR const AdapterParmEq[] = L"a=";
int const ulAdapterParmEq = wcslen(AdapterParmEq);
*ppSystemName = NULL; // Set
*ppUserid = NULL; // output parameters
*ppPassword = NULL; // to default
*ppAdapterName = NULL; // values.
// Parameters are (when supplied):
//
// 0 -- Program name. Always supplied (by OS).
// 1 -- Operand. Should be 'get', 'set', 'setdlft', 'help' or '?'. Required.
// 2 -- System name. Optional.
// 3 -- Userid. Optional but reconized only if system name supplied.
// 4 -- Password. Optional but recognized only if system name and userid supplied.
// 5 -- Adapter name. Optional but allowed only with 'set'. Identified by 'a='.
//
// Adapter name when given must be the final parameter. Consequently, this can validly be
// parm 2, parm 3 or parm 5.
for (i = 1; i < nbrArgs; i ++) // Go through the paramters (except the first).
{
if (0== // Does this parm appear to be an adapter name?
_wcsnicmp(pArgv[i], AdapterParmEq, ulAdapterParmEq))
if (i+1!=nbrArgs) // Is this not the last parm?
{
printf(_T("\nAdapter name must be the last parameter!\n\n"));
rc = rcErr;
goto ExitPoint;
}
else
{ // This is the last parm.
if (CmdSet!=CmdNbr) // Is the command something other than set?
{
printf(_T("Adapter name allowed only with set!\n\n"));
rc = rcErr;
goto ExitPoint;
}
*ppAdapterName = pArgv[i] + 2; // Point past the identifying string to the actual name.
break;
}
// The current parameter is not an adapter name. Handle the parm.
int lenArg,
lenInStr,
lenChk;
switch(i)
{
case 1: // Second parm.
lenArg = wcslen(pArgv[1]); // Get length of parm 1.
for (j = 1; j < lnInStrArr; j ++) // Go through expected parameters.
{
lenInStr = wcslen(InStrArr[j].pInStr); // Get length of current command from command set.
lenChk = // Get the greater of the 2 lengths.
lenArg > lenInStr ? lenArg : lenInStr;
if (0== // Does the second parm match the current command?
_wcsnicmp(pArgv[1], InStrArr[j].pInStr, lenChk))
break; // If yes, break.
}
if (j<lnInStrArr) // Found a supported command?
CmdNbr = InStrArr[j].CmdCode; // Set command code.
else
{
printf(_T("\nUnsupported command\n\n"));
ParmInfo();
rc = rcErr;
goto ExitPoint;
}
break;
case 2: // Third parm.
*ppSystemName = pArgv[2]; // Point to system name.
break;
case 3: // Fourth parm.
*ppUserid = pArgv[3]; // Point to userid.
break;
case 4: // Fifth parm.
*ppPassword = pArgv[4]; // Point to password.
break;
default:
printf(_T("\nUnsupported parameter = %S\n\n"), pArgv[i]);
rc = rcErr;
} // End 'switch(i)'.
} // End 'for' go through the paramters.
ExitPoint:
if ( // Check that neither userid nor password was supplied or that both were supplied.
(
NULL!=*ppUserid
&&
NULL==*ppPassword
)
||
(
NULL==*ppUserid
&&
NULL!=*ppPassword
)
)
{
printf(_T("\nIllegal userid/password combination\n\n"));
rc = rcErr;
}
*pCmdOper = CmdNbr; // Return command number.
return rc;
} // End ParseIt().
/**************************************************************************************************/
/* */
/* Give information about parameters. */
/* */
/**************************************************************************************************/
void
ParmInfo()
{
printf(_T("\nUsage: " JARtnName " < operation < operands > >\n\n"));
printf(_T(" where <operation> is one of these:\n\n"));
for (ULONG i = 1; i < lnInStrArr; i ++)
printf(_T(" %S\n"), InStrArr[i].pInStr);
printf(_T("\n <operands> is one or more of the following:\n"));
printf(_T("\n < system name >\n"));
printf(_T("\n < system name <userid password> >\n"));
printf(_T("\n < userid password > is allowed only after < system name >, eg,\n"));
printf(_T("\n mysystem myid mypassword\n"));
printf(_T("\n a=< adapter name> >\n"));
printf(_T("\n < adapter name > is allowed only with 'set' and must be the final parameter, eg,\n"));
printf(_T("\n set a=\"My Favorite NIC\"\n"));
printf(_T("\n or\n"));
printf(_T("\n set mysystem myid mypassword a=\"My Favorite NIC\"\n"));
printf(_T("\n (Double quotation marks are required if the name has imbedded blanks.)\n\n"));
} // End ParmInfo().
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -