The following is a project description of a project that I have to complete:

Write a shell-like program that illustrates how UNIX spawns processes. This program will provide its own prompt to the user, read the command from the input and execute the command. Let the shell allow arguments to the commands. For example, it should be able to execute commands such as more filename and ls –l etc. (MyShell.c -> MyShell)

#include <sys/types.h>

#include <unistd.h>

#include <iostream>

#include <wait.h>

#include <fstream>




int main(){

	char cmd[100], command_array[20];
	int flag = 0;

	int pid = fork();

	while(flag !=1){

		if (pid < 0) { // Create child process

			perror("Fork call.");

			return (2);

		}	



		if (pid == 0) {

			printf("myShell > ");

			scanf("%s", cmd);

		

			strncat(command_array, cmd, 100); //error on this line

			if (execvp(command_array[0],command_array) == -1){
				printf("Error: running command: '%s'\n", cmd);
				//exit(-1);
				flag = 0;
			}

			exit(0);
			flag = 1;

		}

	

		if (pid > 0) {

			wait((int *) 0);

		}
	}

	return (0);

}

I put together this piece of code from the TAs lecture and from various examples from websites.

My first question:

Since there isn't a boolean in type in C, I declared a int flag = 0 and until the value of the flag changes to 1it need to prompt the user. Basically if the user enters a wrong command, error message appears and prompts the user again for an input. This isn't working.

My second question:

What am I doing wrong? I am following my TA instructions.

I'm actually going to see him tomorrow during office hours but it would be great if I could finish it before as I have a packed day tomorrow.

Thanks

drjay

Recommended Answers

All 4 Replies

>strncat(command_array, cmd, 100); //error on this line

First let me ask you: what's in command_array?

command_array can only hold 20 chars including the '\0' of which we don't know the content since it hasn't been initialized. You are asking strncat to inject to it a 100 char more from cmd, which it can not hold; and append it to command_array starting at '\0', which may or may not be there.

C doesn't have this header file.
#include <iostream>
include stdio.h

exit(0);
flag = 1;

If exits, flag never gets changed.

well the while loop is working.

i want help with:

if (execvp(command_array[0],command_array) == -1){
				printf("Error: running command: '%s'\n", cmd);
				//exit(-1);
				flag = 0;
			}

command_array is the input string

Since you never initialized command_array , it starts out with junk in it. Then you added your input to that junk.

Is the first parameter to execvp supposed to be a single character? That's what you are passing.

After making changes you should repost the code so we can see it's current state.

#include <sys/types.h>

#include <unistd.h>

#include <wait.h>

#include <fstream>



int main(){
	while (printf( " > " )){
		char cmd[20] = {0};
		char base_cmd[20] = "/bin/";
		
		// await input of command; if none given, re-issue the prompt;
		// otherwise, remove ending newline character
		if (fgets(cmd, sizeof(cmd), stdin ) == NULL){
			printf( "\n" );
			continue;

		}
		cmd[strlen(cmd) - 1] = '\0';
		
		pid_t pid = fork();

		if (!strcmp(cmd, "quit") || !strcmp(cmd, "exit")){
			break;
		}
		if (pid < 0){
			perror("Fork failed!");
			break;
		}
		if (pid == 0) {
                        strncat(base_cmd, cmd, 200);
			
if (execl(base_cmd, cmd, (char *) 0) < 0) {

				perror("Command not found.");

				return (1);	

			}

			_exit(0);

		}

	

		if (pid > 0) {

			wait((int *) 0);

		}

	}	
	
	return (0);

}

This is what I have. This code compiles and runs.

date and ls and exit || quit commands work.

What I want now is for ls-l to work.

base_cmd is the base command and cmd is the string you input.

When it come to ls-l, ls-l will be in cmd.
strncat(base_cmd, cmd, 200); will make base_cmd = /bin/ls-l

I guess i got to use execvp() system call. Can some help me with it.

I keep getting errors when I call that method.

Thanks

drjay

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.