diff --git a/reactos/include/getopt.h b/reactos/include/getopt.h index 43dc804d2fb..33c5e19ca91 100644 --- a/reactos/include/getopt.h +++ b/reactos/include/getopt.h @@ -1,53 +1,3 @@ -/* 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 /* EOF */ diff --git a/reactos/include/tchar.h b/reactos/include/tchar.h index bd2c2ee645a..c765cc52605 100644 --- a/reactos/include/tchar.h +++ b/reactos/include/tchar.h @@ -29,9 +29,9 @@ * 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 $ * */ @@ -52,8 +52,8 @@ #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; diff --git a/reactos/lib/tgetopt/Makefile b/reactos/lib/tgetopt/Makefile new file mode 100644 index 00000000000..91c13f33e03 --- /dev/null +++ b/reactos/lib/tgetopt/Makefile @@ -0,0 +1,19 @@ +# $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 diff --git a/reactos/lib/tgetopt/_wgetopt.c b/reactos/lib/tgetopt/_wgetopt.c new file mode 100644 index 00000000000..21a8ffb0a2a --- /dev/null +++ b/reactos/lib/tgetopt/_wgetopt.c @@ -0,0 +1,15 @@ +/* $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 + +#define _UNICODE +#include "getopt.c" + +/* EOF */ diff --git a/reactos/lib/tgetopt/getopt.c b/reactos/lib/tgetopt/getopt.c new file mode 100644 index 00000000000..23f1edc025a --- /dev/null +++ b/reactos/lib/tgetopt/getopt.c @@ -0,0 +1,135 @@ +/* $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 +#include +#include +#include + +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 */ diff --git a/rosapps/sysutils/tlist/Makefile b/rosapps/sysutils/tlist/Makefile index a20a7edc630..433b9527f0d 100644 --- a/rosapps/sysutils/tlist/Makefile +++ b/rosapps/sysutils/tlist/Makefile @@ -12,7 +12,7 @@ TARGET_APPTYPE = console 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 diff --git a/rosapps/sysutils/tlist/tlist.c b/rosapps/sysutils/tlist/tlist.c index e1d9b8da03a..3d9cf466bf9 100644 --- a/rosapps/sysutils/tlist/tlist.c +++ b/rosapps/sysutils/tlist/tlist.c @@ -1,4 +1,4 @@ -/* $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 @@ -15,6 +15,7 @@ #include #include +#include #ifndef PAGE_SIZE #define PAGE_SIZE 4096 @@ -68,14 +69,14 @@ int STDCALL PrintBanner (VOID) 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) @@ -441,29 +442,24 @@ int STDCALL PrintProcess (char * PidStr) 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 */