
/******************************************************************************

    MODULE
	GetWord.c

    DESCRIPTION
	Get a number and some strings on Commandline
	and write one of them to StdOut

    NOTES
	Kickstart 2.0+ required
	compiles w/ Dice 2.07R - inline-pragmas required
	compiles w/ SAS/C v6.51

    BUGS
	none known

    TODO

    EXAMPLES
	> getword 1 x y z
	y

    SEE ALSO

    INDEX

    HISTORY
	01-08-93 b_noll created

    AUTHOR
	Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
	b_noll@informatik.uni-kl.de

******************************************************************************/

/**************************************
	    Includes
**************************************/

#ifndef   EXEC_LIBRARIES_H
# include <exec/libraries.h>
#endif /* EXEC_LIBRARIES_H */

#ifndef   CLIB_EXEC_PROTOS_H
# include <clib/exec_protos.h>
#endif /* CLIB_EXEC_PROTOS_H */

#ifndef   DOS_DOS_H
# include <dos/dos.h>
#endif /* DOS_DOS_H */

#ifndef   CLIB_DOS_PROTOS_H
# include <clib/dos_protos.h>
#endif /* CLIB_DOS_PROTOS_H */

#include <proto/dos.h>
#include <proto/exec.h>

/**************************************
	    Structures
**************************************/

struct _arg {
    LONG  * num;
    STRPTR* words;
}; /* struct  */

/**************************************
	    Global Variables
**************************************/


/**************************************
	    Implementation
**************************************/

long _main (void)
{
    const char	  * version = "$VER: GetWord 1.0 (23.8.93)";
    long	    retval  = 20;
    struct Library* SysBase = *((struct Library**)4L);
    struct Library* DOSBase;

    if (DOSBase = OpenLibrary (DOSNAME, 37)) {
	struct _arg	argv	= { NULL, NULL };
	APTR args;
	retval	 = 10;
	if (args = (void*)ReadArgs( "NUMBER/N/A,WORDS/M", (LONG*)&argv, NULL)) {
	    long num;
	    long i;
	    num  = *(argv.num);
	    if (num < 0) {
		for (i = 0; argv.words[i]; ++i);
		num += i+1;
	    } /* if */
	    for (i = 0; (i < num) && argv.words[i]; ++i);
	    if ((i == num) && i) {
		retval = 5;
		if (PutStr (argv.words[i-1]) == 0) retval = 0;
	    } else {
		retval = 9;
	    } /* if */
	    PutStr("\n");
	    FreeArgs (args);
	} /* if */
	CloseLibrary (DOSBase);
    } /* if */
    return (retval);
} /* _main */

/******************************************************************************
*****  END GetWord.c
******************************************************************************/

