Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // ******************************************************************
  6. // Here's a "quick and dirty" assignment for you. Write a program
  7. // that will take a command line and "parse" the arguments. So,
  8. // we'll assume that your command line
  9. // looks something like:
  10. //
  11. // pgmName options
  12. //
  13. // for example, it might be
  14. //
  15. // ls -ltga
  16. //
  17. // What your program should do is 1) list the pgm name (ls in this
  18. // example) and each of the options (l, t, g, a) on separate lines,
  19. // and THEN actually run the command.
  20. //
  21. // We'll assume that each command has one set of options that start
  22. // the '-' character and includes each option as a single letter.
  23. //
  24. // So, we could have sort -n, but not sort --key=1 since that wouldn't
  25. // fit our model at this point.
  26. //
  27. // ***********************************************************************
  28.  
  29. void doThatArgThing(int, char *[]);
  30.  
  31. int main(int argc, char** argv)
  32. {
  33. int i;
  34. for( i = 1; i < argc; i++ )
  35. {
  36. printf("%s\n", argv[i] );
  37. }
  38. printf("\n");
  39. doThatArgThing(argc, argv);
  40. }
  41.  
  42. void doThatArgThing(int argc, char *argv[])
  43. {
  44. int i;
  45. char *buffer;
  46.  
  47. int bufferSize = 0;
  48. for( i = 0; i < argc; i++ )
  49. bufferSize += strlen(argv[i]) + 1;
  50.  
  51. buffer = (char *)malloc(bufferSize);
  52.  
  53. buffer[0] = '\0';
  54. strcpy(buffer,argv[1]);
  55.  
  56. for( i = 0; i < argv[2]; i++ )
  57. {
  58.  
  59. }
  60.  
  61. printf("buffer = %s\n", buffer);
  62. system (buffer);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement