- modified tlist to use getopt() to parse the command line, just for fun. Seems to work
svn path=/trunk/; revision=4560
-/* temporary header for getopt. Please remove this file when MingW ships with
- its own */
-
-#ifndef __GETOPT_H_INCLUDED
-#define __GETOPT_H_INCLUDED
-
-#ifdef __cplusplus
-extern "C"
-{
-#endif
-
-extern char *optarg;
-extern int optind, opterr, optopt;
-
-#define no_argument (0)
-#define required_argument (1)
-#define optional_argument (2)
-
-struct option
-{
- const char *name;
- int has_arg;
- int *flag;
- int val;
-};
-
-extern int getopt(int, char * const [], const char *);
-
-extern int getopt_long
-(
- int,
- char * const [],
- const char *,
- const struct option *,
- int *
-);
-
-extern int getopt_long_only
-(
- int,
- char * const [],
- const char *,
- const struct option *,
- int *
-);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __GETOPT_H_INCLUDED */
+#include <tgetopt.h>
/* EOF */
* DISCLAIMED. This includes but is not limited to warranties of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
- * $Revision: 1.7 $
+ * $Revision: 1.8 $
* $Author: hyperion $
- * $Date: 2003/02/09 21:15:29 $
+ * $Date: 2003/04/22 03:20:25 $
*
*/
#ifndef _TCHAR_DEFINED
#ifndef RC_INVOKED
typedef wchar_t _TCHAR;
-typedef signed wchar_t _TSCHAR;
-typedef unsigned wchar_t _TUCHAR;
+typedef wchar_t _TSCHAR;
+typedef wchar_t _TUCHAR;
typedef wchar_t _TXCHAR;
/* #if !__STDC__ */
typedef wchar_t TCHAR;
--- /dev/null
+# $Id: Makefile,v 1.1 2003/04/22 03:20:25 hyperion Exp $
+
+PATH_TO_TOP = ../..
+
+TARGET_TYPE = library
+
+TARGET_NAME = tgetopt
+
+TARGET_OBJECTS = getopt.o _wgetopt.o
+
+DEP_OBJECTS = $(TARGET_OBJECTS)
+
+include $(PATH_TO_TOP)/rules.mak
+
+include $(TOOLS_PATH)/helper.mk
+
+include $(TOOLS_PATH)/depend.mk
+
+# EOF
--- /dev/null
+/* $Id: _wgetopt.c,v 1.1 2003/04/22 03:20:25 hyperion Exp $
+*/
+/*
+ tgetopt -- POSIX-compliant implementation of getopt() with string-type-generic
+ semantics
+
+ This is public domain software
+*/
+
+#include <wchar.h>
+
+#define _UNICODE
+#include "getopt.c"
+
+/* EOF */
--- /dev/null
+/* $Id: getopt.c,v 1.1 2003/04/22 03:20:25 hyperion Exp $
+*/
+/*
+ tgetopt -- POSIX-compliant implementation of getopt() with string-type-generic
+ semantics
+
+ This is public domain software
+*/
+
+#include <tchar.h>
+#include <string.h>
+#include <stdio.h>
+#include <tgetopt.h>
+
+int _topterr = 1;
+int _toptind = 1;
+int _toptopt;
+_TCHAR * _toptarg;
+
+int _tgetopt(int argc, _TCHAR * const argv[], const _TCHAR * optstring)
+{
+ static int s_nArgChar = 0;
+ _TCHAR * pcOptChar;
+
+ /* we're done */
+ if(_toptind >= argc) return -1;
+
+ /* last time we reached the end of argv[_toptind] */
+ if(s_nArgChar != 0 && argv[_toptind][s_nArgChar] == 0)
+ {
+ /* scan the next argument */
+ ++ _toptind;
+
+ /* we're done */
+ if(_toptind >= argc) return -1;
+
+ s_nArgChar = 0;
+ }
+
+ /* first time we scan argv[_toptind] */
+ if(s_nArgChar == 0)
+ {
+ /* argument is NULL - we're done */
+ if(argv[_toptind] == NULL)
+ return (int)-1;
+ /* argument is empty - we're done */
+ else if(argv[_toptind][0] == 0)
+ return (int)-1;
+ /* argument begins with '-' */
+ else if(argv[_toptind][0] == _T('-'))
+ {
+ /* argument is "--" */
+ if(argv[_toptind][1] == _T('-'))
+ {
+ /* increment optind */
+ ++ _toptind;
+ s_nArgChar = 0;
+
+ /* we're done */
+ return (int)-1;
+ }
+ /* argument is "-" */
+ else if(argv[_toptind][1] == 0)
+ {
+ /* we're done */
+ return (int)-1;
+ }
+ /* possible option */
+ else
+ ++ s_nArgChar;
+ }
+ /* argument doesn't begin with a dash - we're done */
+ else
+ return (int)-1;
+ }
+
+ /* return the current character */
+ _toptopt = argv[_toptind][s_nArgChar];
+
+ /* advance to the next character */
+ ++ s_nArgChar;
+
+ /* unrecognized option */
+ if(_toptopt == _T(':') || (pcOptChar = _tcschr(optstring, _toptopt)) == NULL)
+ {
+ /* print an error message */
+ if(_topterr && optstring[0] != _T(':'))
+ _ftprintf(stderr, _T("%s: illegal option -- %c\n"), argv[0], _toptopt);;
+
+ /* return an error */
+ return _T('?');
+ }
+
+ /* the option requires an argument */
+ if(pcOptChar[1] == _T(':'))
+ {
+ /* we are at the end of the argument */
+ if(argv[_toptind][s_nArgChar] == 0)
+ {
+ /* the argument of the option is the next argument */
+ ++ _toptind;
+ s_nArgChar = 0;
+
+ /* this is the last argument */
+ if(_toptopt >= argc)
+ {
+ /* print an error message */
+ if(_topterr && optstring[0] != _T(':'))
+ {
+ _ftprintf
+ (
+ stderr,
+ _T("%s: option requires an argument -- %c\n"),
+ argv[0],
+ _toptopt
+ );
+ }
+
+ /* return an error */
+ return ((optstring[0] == _T(':')) ? _T(':') : _T('?'));
+ }
+
+ /* return the argument */
+ _toptarg = argv[_toptind];
+ }
+ /* the rest of the argument is the argument of the option */
+ else
+ _toptarg = argv[_toptind] + s_nArgChar;
+ }
+
+ /* success */
+ return _toptopt;
+}
+
+/* EOF */
TARGET_NAME = tlist
-TARGET_SDKLIBS = epsapi.a kernel32.a user32.a ntdll.a
+TARGET_SDKLIBS = epsapi.a tgetopt.a kernel32.a user32.a ntdll.a
TARGET_OBJECTS = $(TARGET_NAME).o
-/* $Id: tlist.c,v 1.4 2003/04/14 01:19:08 hyperion Exp $
+/* $Id: tlist.c,v 1.5 2003/04/22 03:20:25 hyperion Exp $
*
* ReactOS Project
* TList
#include <ctype.h>
#include <epsapi.h>
+#include <getopt.h>
#ifndef PAGE_SIZE
#define PAGE_SIZE 4096
return EXIT_SUCCESS;
}
-int STDCALL PrintSynopsys (VOID)
+int STDCALL PrintSynopsys (int nRetVal)
{
PrintBanner ();
printf ("Usage: tlist [-t | PID | -l]\n\n"
" -t print the task list tree\n"
" PID print module information for this ID\n"
" -l print license information\n");
- return EXIT_SUCCESS;
+ return nRetVal;
}
int STDCALL PrintLicense (VOID)
int main (int argc, char * argv [])
{
- if (1 == argc)
+ int c;
+
+ if(1 == argc) return PrintProcessList(FALSE);
+
+ while((c = getopt(argc, argv, "tl")) != -1)
+ {
+ switch(c)
{
- return PrintProcessList (FALSE);
+ case 't': return PrintProcessList(TRUE);
+ case 'l': return PrintLicense();
+ default: return PrintSynopsys(EXIT_FAILURE);
}
- if (2 == argc)
- {
- if (('-' == argv [1][0]) && ('\0' == argv [1][2]))
- {
- if ('t' == argv [1][1])
- {
- return PrintProcessList (TRUE);
- }
- if ('l' == argv [1][1])
- {
- return PrintLicense ();
- }
- }
- if (isdigit(argv[1][0]))
- {
- return PrintProcess (argv[1]);
- }
- }
- return PrintSynopsys ();
+ }
+
+ if(isdigit(argv[optind][0]))
+ return PrintProcess (argv[1]);
+
+ return PrintSynopsys(EXIT_SUCCESS);
}
/* EOF */