I have a problem that seems to be slipping past my knowledge. I am writing a simple shell to learn about some of the programming systems for internships at Unisys. In my shell, it seems that all the commands I try work in addition to lsand even now detect the command wc. lsand it wcworks when I type it on my own, but if I give him the arguments, it will not work and will give me an error in saying No such file or directory.
here is my code:
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#define BUF_SIZE 1024
#define DELIMS " -\r\t\n"
char * getInput (char **buffer, size_t buflen)
{
size_t bufsize = BUF_SIZE;
*buffer = malloc(sizeof(char) * bufsize + 1);
if (!*buffer)
{
fprintf(stderr, "Shell: buffer allocation error\n");
exit(EXIT_FAILURE);
}
printf("$$ ");
fflush(NULL);
int bytesRead = getline(&(*buffer), &bufsize, stdin);
if (bytesRead < 0)
{
printf("Getline error\n");
exit(EXIT_FAILURE);
}
return *buffer;
}
char ** splitLine(char *line)
{
int bufsize = BUF_SIZE;
int pos = 0;
char **tokens = malloc (sizeof(char) * BUF_SIZE + 1);
char *token;
if (!tokens)
{
fprintf(stderr, "Shell: buffer allocation error\n");
exit(EXIT_FAILURE);
}
token = strtok(line, DELIMS);
while (token != NULL)
{
tokens[pos] = token;
pos++;
if (pos > bufsize)
{
bufsize += BUF_SIZE;
tokens = realloc(tokens, bufsize * sizeof(char) + 1);
if (!tokens)
{
fprintf(stderr, "Shell: buffer allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, DELIMS);
}
tokens[pos] = NULL;
return tokens;
}
int main (int argc, char **argv)
{
char *buf;
pid_t pid;
int status;
while (getInput(&buf, sizeof(buf)))
{
char **args = splitLine(buf);
int i = 0;
while (1)
{
char *token = args[i++];
if (token != NULL)
printf("Token #%d: %s\n", i, token);
else
break;
}
fflush(NULL);
pid = fork();
switch(pid)
{
case -1:
{
fprintf(stderr, "Shell cannot fork: %s\n", strerror(errno));
continue;
}
case 0:
{
execvp(args[0], args);
fprintf(stderr, "Shell: couldn't execute %s: %s\n ", buf, strerror(errno));
exit(EX_DATAERR);
}
}
if ((pid = waitpid(pid, &status, 0) < 0))
{
fprintf(stderr, "Shell: waitpid error: %s\n", strerror(errno));
}
free(args);
}
free(buf);
exit(EX_OK);
}
For example, I tried the following commands with output:
ls -la (ISSUE)
$$ ls -la
Token
Token
ls: la: No such file or directory
$$
wc -l (ISSUE)
$$ wc -l
Token
Token
wc: l: open: No such file or directory
Ls
$$ ls
Token
Makefile driver driver.dSYM main.c main.o
$$
ps -la
$$ ps -la
Token #1: ps
Token #2: la
UID PID PPID CPU PRI NI VSZ RSS WCHAN STAT TT TIME COMMAND
0 2843 2405 0 31 0 2471528 8 - Us s000 0:00.08 login
501 2845 2843 0 31 0 2463080 1268 - S s000 0:01.08 -bash
501 4549 2845 0 31 0 2454268 716 - S+ s000 0:00.01 ./driv
0 4570 4549 0 31 0 2435020 932 - R+ s000 0:00.00 ps la
$$
, which the
$$ which which
Token
Token
/usr/bin/which
which -a which
$$ which -a which
Token
Token
Token
/usr/bin/which
and even finally man getline
GETLINE(3) BSD Library Functions Manual GETLINE(3)
NAME
getdelim, getline
LIBRARY
Standard C Library (libc, -lc)
.
.
.
- , ?