Not sure why my comparisons aren't working between my input (text) and my several char arrays. I'm trying to get the user's input to terminate certain functions as you'll see below.
I realise it's not the most efficient code but if there's anything obvious I'm missing please let me know.
-- Not very experienced in C programming as you can probably see.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
char homedir[] = "home";
char getp[] = "getpath";
char pwd[] = "pwd";
char end[] = "exit";
char seten[] = "setpath";
char text[20];
char notext[] = "";
void begin()
{
fputs("Enter either: \ncd \npwd \ngetpath \nsetpath \nhome\n", stdout);
printf("Or enter: \nexit\nto terminate program\n");
fflush(stdout);
fgets(text, sizeof text, stdin);
}
void entryerror()
{
printf("Please enter a valid choice.\n");
}
void home()
{
char *hme = getenv("HOME");
if (hme)
{
printf("Value of HOME is: \n%s\n", hme);
fflush(stdout);
}
}
void path()
{
/*fgets(text, sizeof text, stdin);
if (strcmp(text, getp) == 0)
{*/
char *env = getenv("PATH");
if (env)
{
printf("Value of PATH is: \n%s\n", env);
fflush(stdout);
}
}
/*entryerror();
begin();*/
void printwd()
{
char *pwd = getenv("PWD");
if(pwd)
{
printf("Value of PWD is: \n%s\n", pwd);
fflush(stdout);
}
}
void printcheck()
{
if (strcmp(text, pwd) > 0)
{
printwd();
}
else
{
entryerror();
main();
}
}
void setenviro()
{
int set = setenv("PATH", "PC", 1);
char *env1 = getenv("PATH");
if (env1)
printf("Value of PATH after 'setenv' is: \n%s\n", env1);
}
int main()
{
begin();
if (strcmp(text, pwd) >0)
{
printwd();
}
else if (strcmp(text, getp) >0)
{
path();
}
else if (strcmp(text, homedir) >0)
{
home();
}
else if (strcmp(text, end) >0)
{
exit(1);
}
else if (strcmp(text, seten) >0)
{
setenviro();
}
else
{
entryerror();
main();
}
main();
return 0;