Additional Utility Programs
Three additional utility programsOptions, SkipArg, and GetArgsare sufficiently useful to list here. None, however, is dependent on Win32.
Options.c
This function scans the command line for words with the "-" (hyphen) prefix, examines the individual characters, and sets Boolean parameters. It is similar to the UNIX getopt function, but it is not as powerful.
Program A-7. Options Function
/* Utility function to extract option flags from the command line. */
#include "EvryThng.h"
#include <stdarg.h>
DWORD Options (int argc, LPCTSTR argv [], LPCTSTR OptStr, ...)
/* argv is the command line. The options, if any, start
with a '-' in argv [1], argv [2], ....
OptStr is a text string containing all possible
options, in one-to-one correspondence with the addresses of
Boolean variables in the variable argument list (...). These
flags are set if and only if the corresponding option character
occurs in argv [1], argv [2], .... The return value is the argv
index of the first argument beyond the options. */
{
va_list pFlagList;
LPBOOL pFlag;
int iFlag = 0, iArg;
va_start (pFlagList, OptStr);
while ((pFlag = va_arg (pFlagList, LPBOOL)) != NULL
&& iFlag < (int) _tcslen (OptStr)) {
*pFlag = FALSE;
for (iArg = 1;
!(*pFlag) && iArg < argc &&
argv [iArg] [0] == '-';
iArg++)
*pFlag = _memtchr (argv [iArg], OptStr [iFlag],
_tcslen (argv [iArg])) != NULL;
iFlag++;
}
va_end (pFlagList);
for (iArg = 1; iArg < argc && argv [iArg] [0] == '-'; iArg++);
return iArg;
}
SkipArg.c
This function processes a command line string to skip over a white-space-delimited field. It is first used in timep, Program 6-2.
Program A-8. SkipArg Function
/* SkipArg.c
Skip one command line argument -- skip tabs and spaces. */
#include "EvryThng.h"
LPTSTR SkipArg (LPCTSTR targv)
{
LPTSTR p;
p = (LPTSTR) targv;
/* Skip up to the next tab or space. */
while (*p != '\0' && *p != TSPACE && *p != TAB) p++;
/* Skip over tabs and spaces to the next arg. */
while (*p != '\0' && (*p == TSPACE || *p == TAB)) p++;
return p;
}
GetArgs.c
This function scans a string for space- and tab-delimited words and puts the results in a string array passed to the function. It is useful for converting a command line string into an argv [] array, and it is used initially with JobShell in Chapter 6. The Win32 function CommandLineToArgvW performs the same function but is limited to Unicode characters.
Program A-9. GetArgs Function
/* GetArgs. Put command line string in argc/argv form. */
#include "EvryThng.h"
VOID GetArgs (LPCTSTR Command, int *pArgc, LPTSTR argstr [])
{
int i, icm = 0;
DWORD ic = 0;
for (i = 0; ic < _tcslen (Command); i++) {
while (ic < _tcslen (Command) &&
Command [ic] != TSPACE && Command [ic] != TAB) {
argstr [i] [icm] = Command [ic];
ic++; icm++;
}
argstr [i] [icm] = '\0';
while (ic < _tcslen (Command) &&
(Command [ic] == TSPACE || Command [ic] == TAB))
ic++;
icm = 0;
}
if (pArgc != NULL) *pArgc = i;
return;
}