Hey all,
This is my first time programming in C, but i am used to C++ so i am trying to use the same layout as I would with C++ for my program. I am a little confused about the chdir() part of my code. If someone could help me that would be great. Just disregard the other "crap" in the program for now, unless you see something that could be beneficial to me. Right now I am trying to focus on getting my chdir() to work!
I am able to read in the cwd. The prompt is supposed to take in 'cd ..' and then go back one (as you all know how that works!). When I put cd in my prompt, it just exits out of the program. So that is where I am now....Thanks in advance!!
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/param.h>
#define mystring 100
int changedir();
unsigned int pid; //variable to hold id number
int status;
//------------------------
void signal_handler(int sig) //for the ctrl-z part
{
char answer[mystring];
char *buf;
long size;
char *dir;
printf("\n\n%s%d\n\n%s",
"The Shell Has Been Locked ", sig ,
"Please Enter the Password: ");
scanf("%s", answer);
if (*answer == 'I love operating systems')
{
signal(SIGTSTP, signal_handler);
dir= getcwd(buf, size);
printf(dir);
}
else{
printf("That is the wrong password. Try again.");
}
}
//------------------------
void read_parse(char input_string[mystring], char command[], int pipecount)
{
char *comm;
command = strtok(input_string, " ");
if(comm == "|")
{
command = strtok(input_string, "|");
pipecount = pipecount + 1;
}
while(command != NULL)
{
command = strtok (NULL,"\n" ,"\n" );
}
}
//------------------------
int main() {
char *buf; //pointer to where the directory is
long size; //size of the array, which is the directory name
char *dir; //this is the variable that will store the direcory name and then print it out
char *path; //variable pointer to the changed directory
char input_string[mystring];
char command[mystring];
int flag = 1;
int pipecount = 0;
signal(SIGTSTP, signal_handler);
printf("mysh$>");
if ((buf = (char *)malloc((size_t)size)) != NULL)
dir = getcwd(buf, size); //this is the call to get the directory
printf(dir); printf(" > "); //will print the directory
fgets (input_string, mystring, stdin); //gets command the user enters
read_parse(input_string, command, pipecount); //reads command the user enters
if (input_string == 'cd')
{
chdir(command); ///?????? I dont think my parameter is correct.......
}
return 0;
}
PS....also my signal handler wont lock the program...I can enter any password in and it will just exit.....