0% found this document useful (0 votes)
22 views4 pages

As 2

Hshbsbhsjnwnsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

As 2

Hshbsbhsjnwnsn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
// #include<sys/wait.h>
void count(int in, char t[])
{
int f = 0, w = 0, l = 0, ch = 0;
char c, prev;
while (read(in, &c, 1))
{
if (f == 0)
{
if (c != ' ' && c != '\n' && c != '\t')
{
w++;
}
ch++;
f = 1;
prev = c;

if (c == '\n')
{
l++;
}
}
else
{
if (c == '\n')
{
l++;
}
else if ((prev == '\t' || prev == ' ' || prev == '\n') && (c !=
'\t' && c != ' '))
{
w++;
}
ch++;
prev = c;
}
}
if (strcasecmp(t, "C") == 0)
{
printf("\nNo. of characters:%d", ch);
}
if (strcasecmp(t, "W") == 0)
{
printf("\nNo. of words:%d", w);
}
if (strcasecmp(t, "L") == 0)
{
printf("\nNo. of Lines:%d", l);
}
}
void countfile(int in)
{
int f = 0, w = 0, l = 0, ch = 0;
char c, prev;
while (read(in, &c, 1))
{
if (f == 0)
{
if (c != ' ' && c != '\n' && c != '\t')
{
w++;
}
ch++;
f = 1;
prev = c;

if (c == '\n')
{
l++;
}
}
else
{
if (c == '\n')
{
l++;
}
else if ((prev == '\t' || prev == ' ' || prev == '\n') && (c !=
'\t' && c != ' '))
{
w++;
}
ch++;
prev = c;
}
}
printf("\nNo. of characters:%d", ch);
printf("\nNo. of words:%d", w);
printf("\nNo. of Lines:%d", l);
}

int main()
{
char cmd[80], *token, t[10][20];
int pid, ntoken, e, in;
system("clear");
do
{
printf("\nMyShell$ ");
gets(cmd);
token = strtok(cmd, " ");
strcpy(t[0], token);
ntoken = 0;
while (token)
{
ntoken++;
token = strtok(NULL, " ");
if (token != NULL)
{
strcpy(t[ntoken], token);
}
}
switch (ntoken)
{
case 1:
if (strcasecmp(t[0], "exit") == 0)
{
exit(0);
}
else
{
pid = fork();
if (pid == 0)
{
e = execlp(t[0], t[0], (char *)0);
if (e < 0)
{
printf("Error occured.\n");
}
}
else
{
wait(NULL);
}
}
break;
case 2:
if (strcasecmp(t[0], "count") == 0)
{
in = open(t[1], O_RDONLY);
if (in == -1)
{
printf("\n Error:File Not Found");
continue;
}
else
{
countfile(in);
close(in);
continue;
}
}

pid = fork();
if (pid == 0)
{
e = execlp(t[0], t[0], t[1], (char *)0);
if (e < 0)
{
printf("Error Occured\n");
}
}
else
{
wait(NULL);
}
break;
case 3:
if (strcasecmp(t[0], "count") == 0)
{
if (strcasecmp(t[1], "c") == 0 || strcasecmp(t[1], "w") ==
0 || strcasecmp(t[1], "l") == 0)
{
in = open(t[2], O_RDONLY);
if (in == -1)
{
printf("\n Error:File Not Found");
continue;
}
else
{
count(in, t[1]);
close(in);
continue;
}
}
}
pid = fork();
if (pid == 0)
{
e = execlp(t[0], t[0], t[1], t[2], (char *)0);
if (e < 0)
{
printf("Error Occured\n");
}
}
else
{
wait(NULL);
}
break;
case 4:
pid = fork();
if (pid == 0)
{
e = execlp(t[0], t[0], t[1], t[2], t[3], (char *)0);
if (e < 0)
{
printf("Error Occured\n");
}
}
else
{
wait(NULL);
}
break;
}

} while (1);
return 0;
}

You might also like