Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // ******************************************************************
- // Here's a "quick and dirty" assignment for you. Write a program
- // that will take a command line and "parse" the arguments. So,
- // we'll assume that your command line
- // looks something like:
- //
- // pgmName options
- //
- // for example, it might be
- //
- // ls -ltga
- //
- // What your program should do is 1) list the pgm name (ls in this
- // example) and each of the options (l, t, g, a) on separate lines,
- // and THEN actually run the command.
- //
- // We'll assume that each command has one set of options that start
- // the '-' character and includes each option as a single letter.
- //
- // So, we could have sort -n, but not sort --key=1 since that wouldn't
- // fit our model at this point.
- //
- // ***********************************************************************
- void doThatArgThing(int, char *[]);
- int main(int argc, char** argv)
- {
- int i;
- for( i = 1; i < argc; i++ )
- {
- printf("%s\n", argv[i] );
- }
- printf("\n");
- doThatArgThing(argc, argv);
- }
- void doThatArgThing(int argc, char *argv[])
- {
- int i;
- char *buffer;
- int bufferSize = 0;
- for( i = 0; i < argc; i++ )
- bufferSize += strlen(argv[i]) + 1;
- buffer = (char *)malloc(bufferSize);
- buffer[0] = '\0';
- strcpy(buffer,argv[1]);
- for( i = 0; i < argv[2]; i++ )
- {
- }
- printf("buffer = %s\n", buffer);
- system (buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement