Assignment 2

Deliverables and Due Date

Submit two .c files, one called main.c and another named strtok.c, and submit one .h file named strtok.h. Document each properly. The programs for the two .c files are described below. The header file is to contain the function prototype for the function, strtok(), that you will be writing for this assignment. Be sure to #include strtok.h in both .c files.

This assignment is due by midnight March 23, 1999.

Introduction

I decided that the shell project would be too contrived if you work only with the standard library's strtok() function, so for this assignment you are to write your own improved version. Yours will have the same name as the library version, but that won't be a problem because you will make sure the linker finds yours before it looks in the standard library. (Yours will have a different prototype.)

Your function return its "internal pointer" as well as the terminating char as well as the next token as it parses a string passed to it. Otherwise, it behaves the same as the library function.

Here is the function prototype you are to use:

    char *
    strtok(  char        *stringToParse, 
             const char  *terminatorSet, 
                   char **continuationString,
             char        *terminatorChar
    );

strtok()

Write your definition of strtok() in strtok.c. If you try to put the statement, #include <string.h>, in your strtok.c you will get a syntax error because of the conflicting prototypes for strtok() in "strtok.h" and in the standard header file. I suggest that you deal with this by copying the prototypes you need from <string.h> into your "strtok.c". I used these two:
  size_t strspn(  const char *s, const char *set );
  size_t strcspn( const char *s, const char *set );
You will need <stdlib.h> for the typedef of size_t.

Unlike the presentation in class on March 12, the assignment uses the same return value and first two parameters as the standard library's strtok() function. The third parameter is used for returning a pointer to the next part of the string being parsed. If there is nothing left to parse in the string, return a value of NULL for this pointer. The fourth pointer is used to return the character that terminated the token.

main()

Write a main program, in main.c, that gets strings from the user, and prints messages describing the command lines it read, using your strtok(), to do the parsing.

Sample Output

    $ main

    Enter a command line:  first cmd ; second cmd | third
    Command 1 was: "first cmd"
       It terminated with ';'
    Command 2 was: "second cmd"
       It terminated with '|'
    Command 3 was: "third"
       It terminated with ''

    Enter a command line:  a command || b && c &
    Command 1 was: "a command"
       It terminated with '||'
    Command 2 was: "b"
       It terminated with '&&'
    Command 3 was "c"
       It terminated with '&'

    Enter a command line: ^C
    $ 

Notes