Problem with strcmp()....
Code:
#include <stdio.h>
#include <getopt.h>
#include <string.h>
void ProcedeWithProg(int pwdYESNO)
{
char * string;
char prompt[100]="";
char * hostNamePtr = getenv("HOSTNAME");
char * pwdPtr = getenv("PWD");
char * colon = ":";
char exitstring[] = "exit";
int bytes_read;
int nbytes = 100;
if(!pwdYESNO)
{
strcpy(prompt, hostNamePtr);
}
else
{
strcpy(prompt, hostNamePtr);
strcat(prompt, colon);
strcat(prompt, pwdPtr);
}
/*Problem is here*/
do
{
printf("%s:> ", prompt);
string = malloc (nbytes + 1);
bytes_read = getline (&string, &nbytes, stdin);
if (bytes_read == -1)
{
printf("ERROR!\n", prompt);
}
else
{
printf("You Typed: %s", string);
}
} while (strcmp(exitstring, string) != 0);
}
The loop never ends because strcmp never spits out a 0. My best guess is that it's doing this because string is a pointer. Is this correct? The thing is I can''t get the getline function to work without string being a pointer. Is there a way I could either: Get strcmp to work with string being a pointer, or get getline to work with string not being a pointer? Thanks