i wrote this shell given below. please i need any suggestions and assistance in making this created shell support the following commands: cp (copy), del (delete), hos (prints the hostname of the machine), cd (changes directory), clr (clears the screen), dir (lists contents in directory), environ(lists the environment setting), echo (displays comment followed by a new line), help (displays the help associated with my shell) and quit ( exits the shell). please would be very grateful anyones attention shown too this. thank you

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

main ()
{
	char buf [1024];
	char *args [64];

	for (;;)
	{
	/*
	 *prompt for and read a command.
	 */
	printf ("Command:");

	if (gets (buf) == NULL)
	{
	printf ("\n");
	exit (0);
	}

	/*
	 * Split the string into Arguments.
	 */
	parse (buf, args);

	/*
	 * Execute the command.
	 */
	execute (args);
	} // endof for loop
}

/* parse -- Splits the command in buf into individual arguments */
parse (buf, args)
char *buf;
char **args;
{
	  while (*buf != NULL)
	{
	/*
	 *Strip White Spaces. Use Nulls, so
	 *that the previous argument is terminated automatically
	 */
	  while ((*buf == ' ') || (*buff == '\t'))
		*buf++ = NULL;

	/* Save the Argument. */
	 *args++ = buf;

	/* Skip over the argument. */
	 while ((* != NULL) && (*buf != ' ' ) && (*buf != '\t'))
	 buf++;
	}
	
	*args= NULL;
}

/* execute -- spawns a child process and execute the program */
execute (args)
char **args;
{
	int pid,status;

	/* Get a child process */

	if ((pid= fork()) <0)
	{	{
	perror("fork");
	exit (1);
	/* NOTE. perror() produces a short error message on the standard
	   error describing the last error encountered during a call to a
	   system or library function */
	}

	/*
	 * The Child executes the code inside the if
	 */
	if (pid == 0)
	{
	execvp (*args, args);
	perror (*args);
	exit (1)

/* NOTE. The execv() and execvp versions of execl() are useful when the number
 of arguments are unknown in advance:

The arguments to execv() abd execvp() are the name of the file to be executed
and a vector of strings containing the arguments. The last argument string must
be followed by a 0 pointer.

execlp() and execvp() are called with the same arguments as execl() and execv(),
but duplicate the shell's actions in searching for an executable file
environment. */

   ?* The parent executes the wait. */
	
	while (wait (&status) !=pid)
	 /*empty */

}

Dani AI

Generated

Quick roadmap and practical fixes that will get your shell supporting the commands you listed.

Treat some commands as built‑ins and others as external programs. Built‑ins must run in the parent process (handled before you fork): cd (use chdir()), quit (call exit()), environ (iterate environ), and help/clr (print text or emit ANSI clear codes). Commands that can be delegated to system tools — cp, del, dir, echo, hos — can either be implemented with system calls (unlink() for removes, open/read/write for copying) or simply executed by forking and execvp() /bin/cp, /bin/rm, /bin/ls, /bin/echo, /bin/hostname.

A small dispatch pattern before forking is simplest. Check args[0] with strcmp() and call the appropriate handler; if no built‑in matches, then fork and execvp() the command. Example (illustrative):

if (strcmp(args[0],"cd")==0) {
    if (!args[1]) chdir(getenv("HOME"));
    else if (chdir(args[1]) != 0) perror("cd");
    return;
}
if (strcmp(args[0],"quit")==0) exit(0);

For del use unlink() (and rmdir() for directories). For a user‑level cp you can either call /bin/cp via execvp() or implement a simple copy loop with open, read, write, and fsync. Always check return values and print perror() on errors.

Fixes to parsing and process control (these will break things if left as is): stop using gets() (use fgets()), parse tokens with strtok() or manual scanning using isspace() and '\0' (not NULL), and use waitpid() to wait for the spawned child. Include the proper headers (<unistd.h>, <sys/wait.h>, <fcntl.h>, <errno.h>, <string.h>). See chdir(2), unlink(2), exec(3), and fgets(3) for details: chdir(2) unlink(2) exec(3) fgets(3).

pointed toward a strcmp/if approach — that is the right direction. , start by making built‑in dispatch work, fix parsing and input safety, then add handlers for each command (or exec system utilities) and test each case with proper error messages.

Recommended Answers

All 3 Replies

> i wrote this shell given below.
I very much doubt it. I've seen the basic outline many times before.
It looks like boilerplate homework for you to fill in the blanks of your assignment yourself.

okay is it possible too push me in the right direction. hoping thats not to much of assistance

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.