Dear Friends

i want help about the Simulate a Linux/UNIX shell, called MASH (for MiniAture SHell) in the C. plz read the the my problem and send the best solution of my problem as soon as possible.


Objectives:
After the completion of this assignment you’ll be able to:
Simulate a Linux/UNIX shell, called MASH (for MiniAture SHell) in the C language.
Description and Specifications:
The shell should have a prompt. It should accept commands, interpret them,
execute them, and then redisplay the prompt for the next command. The shell
should terminate on receiving EOF <Ctrl-D>. It is not acceptable for it to
terminate upon receiving a signal such as SIGINT <Ctrl-C>.

Built-in Functions:
The shell will implement six intrinsic functions:
cp copy a file
mv move a file
rm remove a file
cd ability to change to designated directory
mkdir for creating directory
rmdir for removing directory
By intrinsic, I mean built-in, i.e., the shell will have these functions built into it as
a process. This means that you will have to write the code for these commands
and not use the fork() and exec() system calls or the library function system().
History:
Maintain a history list of previously executed commands and allow the user to
access the commands in the history list with the “! operator.
1. Numerical Usage
!N means execution of the command which is N commands before the last
command such as !1 for executing the second to the last command. All the
commands have to be stored on disk upon exit. Assume that your shell will
serve last 100 commands i.e. !101 shall throw an error to the user.
2. Alphabetical Usage
!g would mean executing the last command that started with the letter g, such
as gcc assignment3.c.
Help Material:
You will use the fork() and execlp() or execvp() system calls to enable your
shell to support the above features unless specified otherwise. All other commands
should be passed to some parent shell and executed. This is done with the library
call system(). So, here is the list again:
. Ignore SIGINT <Ctrl-C>.
. Show your custom prompt.
. Get input and terminate on EOF <Ctrl-D>.
. Carry out (i.e., decode and execute) commands.
. Implement a simple parser to recognize "cp", "mv", "rm", and "cd" as
the first words of any set of input arguments. If recognized, carry out
the appropriate command by using your OWN code and NOT by using
the fork() and execlp() calls, or calling the library all system().
. If the first letter of a command is ! or the first letter of the second
word is |, use the fork() and execlp() system calls to implement the
required functionality.
. For all other commands, use the library call system() with the whole
input line as its argument.
. You MUST use the read() system call for reading input from the
keyboard.

Testing:
You have to encode the functionality so that to test MASH automatically, it can be
by using the following input parameters:
MASH -f input_file
[Note: MASH should terminate at the end of the input script-file.]
Reference:
signal(2)
system(3)
read(2)
open(2)
write(2)
stat(2)
fstat(2)
perror(3)
unlink(2)
close(2)
fork()
execlp()
execvp()

[/B]


thanks
Kashif07
<< moderator edit: removed email address >>

Dani AI

Generated

A few practical pointers to get MASH done cleanly (and without handing out a complete homework dump). — the spec in your first post is complete; is right to remind about forum rules, so the approach below is focused on explanation, small sketches, and pitfalls to watch for.

Start with a tiny, testable skeleton: a single loop that reads one line, trims it, tokenizes (respect quotes/escapes), and dispatches either a builtin handler or an execution path. Keep parsing separate from execution so you can unit-test tokenization. Example sketch:

/* very small loop sketch */
for (;;) {
  ssize_t n = read(fd, buf, sizeof buf-1);
  if (n <= 0) break;   /* EOF -> exit */
  buf[n] = '\0';
  parse_line(buf, argv, &argc);
  if (is_builtin(argv[0])) run_builtin(argc, argv);
  else if (has_pipe_tokens(argv)) run_pipeline(argv);
  else run_external(argv);
}

Builtins: implement cp, mv, rm, cd, mkdir, rmdir as functions called in the parent process. For cp use a buffered copy loop and preserve mode where sensible; for mv try a rename operation and fall back to copy+unlink when rename fails across filesystems. Minimal copy example:

int copy_file(const char *src, const char *dst) {
  int in = open(src, O_RDONLY);
  int out = open(dst, O_WRONLY|O_CREAT|O_TRUNC, 0644);
  char buf[8192];
  ssize_t r;
  while ((r = read(in, buf, sizeof buf)) > 0) write(out, buf, r);
  close(in); close(out);
  return 0;
}

History & scripting: keep a circular array of 100 command strings, write it to a history file on exit, and implement !N by computing the index relative to the last command (watch off-by-one: !1 should select the command just before the last entered). Support !x by scanning backwards for the last command starting with x. For -f scriptfile, read lines from the file and treat them exactly like interactive input, terminating at EOF.

Other notes: implement simple pipe handling by building a pipeline of forks and connected pipes and reap children with waitpid to avoid zombies. Ignore SIGINT in the shell process so Ctrl-C doesn’t kill the shell but passes to children. Test edge cases: empty input, quoted args, permissions errors, large files, and cross-filesystem moves. Debug iteratively (add verbose logging, run under strace/valgrind) and build features incrementally.

plz read the the my problem and send the best solution of my problem as soon as possible.

You're kidding, right?

Please read the Announcement.

This forum is meant for discussing programming languages in addition to exchanging code and algorithms. However, it has become a problem where too many students are posting homework problems expecting a quick solution without ever trying for themselves. Not only does this constitute cheating, but it is very discouraging, frustrating, and annoying to everyone who takes valuable time to answer programming support questions.

http://www.daniweb.com/techtalkforums/faq.php?faq=daniweb_faq#faq_rules

Keep it on the site
Please do not post asking for an answer to be sent to you via email. Problems and their responses assist others who read them. Please refrain from responding to people's questions via email for the same reason. Moderators may snip email addresses out of such posts without notice. That being said, please do not email or PM forum staff with your support questions. Also please do not ask support questions in the DaniWeb IRC chat. The chat is meant to offer a more laid back community atmosphere where members can get to know each other, and not as a venue to constantly ask/answer people's computer frustrations.

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.