Hiya I have a really simple program here that basically repeats what the user types at prompt. Here is an example run:
dell4(1)>./driver -h
Synopsis: Matt's Shell - it doesn't do much.
Usage: driver [option]
-h, --help prints this help message
-p, --pwd tells the shell to include the current working directory in
the prompt
dell4(2)>./driver -h -p
Synopsis: Matt's Shell - it doesn't do much.
Usage: driver [option]
-h, --help prints this help message
-p, --pwd tells the shell to include the current working directory in
the prompt
dell4(3)>./driver -p
dell4:/afs/umr.edu/users/matt/data/284/program1:>ls
You typed: ls
dell4:/afs/umr.edu/users/matt/data/284/program1:>word up
You typed: word up
dell4:/afs/umr.edu/users/matt/data/284/program1:>this is a test
You typed: this is a test
dell4:/afs/umr.edu/users/matt/data/284/program1:>!!
You typed: this is a test
dell4:/afs/umr.edu/users/matt/data/284/program1:>!!
You typed: this is a test
dell4:/afs/umr.edu/users/matt/data/284/program1:>exit
Goodbye.
dell4(4)>./driver
dell4:>hellow world
You typed: hellow world
dell4:>The time is 4:13 pm....
You typed: The time is 4:13 pm....
dell4:>!!
You typed: The time is 4:13 pm....
dell4:>exit
Goodbye.
dell4(5)>
Here is my code, I keep getting a segmentation fault when trying to compile:
any help/suggestions is greatly appreciated.Code:#include <stdio.h> #include <getopt.h> #include <string.h> /* Precondition: pwdYESNO is passed from main without any mods * Postcondition: rest of program is ran */ void ProcedeWithProg(int pwdYESNO); /* Precondition: none * Postcondition: help message is printed */ void PrintHelpMessage(); int main(int argc, char *argv[]) { int userNeedsHelp = 0; int oc = 0; int pwdYESNO = 0; struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"pwd", no_argument, NULL, 'p'}, {0, 0, 0, 0} }; while((oc = getopt_long(argc, argv, "hp", longopts, NULL)) != -1) { switch(oc) { case 'h': PrintHelpMessage(); userNeedsHelp = 1; break; case 'p': pwdYESNO = 1; break; case 0: /* getopt_long set a variable - nothing to do */ break; case ':': /* missing option argument */ fprintf(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt); break; case '?': /* Invalid option */ default: /* Invalid option */ fprintf(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt); break; } } if(!userNeedsHelp) { ProcedeWithProg(pwdYESNO); } return 0; } void PrintHelpMessage() { printf("Synopsis: This program doesn't really do anything.\n"); printf("Usage: driver [option]\n"); printf(" -h, --help prints this help message.\n"); printf(" -p, --pwd tells the shell to include the working dir in the prompt"); } void ProcedeWithProg(int pwdYESNO) { char * string; char * prompt; char * hostNamePtr = getenv("HOSTNAME"); char * pwdPtr = getenv("PWD"); char * colon = ":"; int bytes_read; int nbytes = 100; /*build prompt */ if(!pwdYESNO) { strcpy(prompt, hostNamePtr); } else { strcpy(prompt, hostNamePtr); strcat(prompt, colon); strcat(prompt, pwdPtr); } printf("%s:> ", prompt); string = (char *) malloc (nbytes + 1); bytes_read = getline (&string, &nbytes, stdin); if (bytes_read == -1) { printf("%s:> ERROR!\n", prompt); } else { printf("%s You Typed: %s\n", prompt, string); } }



LinkBack URL
About LinkBacks



- You will always have some sort of limit on the size of the array, or the amount of returned memory from malloc. You just have to learn to deal with memory limitations.