Skip to content

Commit aaf8290

Browse files
committed
Files Created
0 parents  commit aaf8290

22 files changed

+8322
-0
lines changed

CAT

16.7 KB
Binary file not shown.

CAT.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
void read_from_stdin(int* count,int flagLineNumber,int flagMarkEnd)
6+
{
7+
char buf[1000];
8+
while(1)
9+
{
10+
if(fgets(buf,sizeof(buf),stdin)==NULL)
11+
{
12+
printf("Error Occured");
13+
return;
14+
}
15+
++(*count);
16+
if(flagLineNumber==1)
17+
printf("%5d ",*count);
18+
int i=0;
19+
while(buf[i]!='\n')
20+
{
21+
printf("%c",buf[i]);
22+
++i;
23+
}
24+
if(flagMarkEnd==1)
25+
printf("$");
26+
printf("\n");
27+
}
28+
}
29+
30+
void read_from_file(FILE* file,int* count,int flagLineNumber,int flagMarkEnd)
31+
{
32+
char line[1000];
33+
while(fgets(line,sizeof(line),file))
34+
{
35+
++(*count);
36+
if(flagLineNumber==1)
37+
printf("%5d ",*count);
38+
int i=0;
39+
while(line[i]!='\n')
40+
{
41+
printf("%c",line[i]);
42+
++i;
43+
}
44+
if(flagMarkEnd==1)
45+
printf("$");
46+
printf("\n");
47+
}
48+
}
49+
50+
void print_file(char stream[1000],int* count,int flagLineNumber,int flagMarkEnd)
51+
{
52+
if(strcmp(stream,"-")==0)
53+
read_from_stdin(count,flagLineNumber,flagMarkEnd);
54+
else
55+
{
56+
FILE *file=fopen(stream,"r");
57+
if(file==NULL)
58+
printf("cat: '%s': No such file or directory\n",
59+
stream);
60+
else
61+
{
62+
read_from_file(file,count,flagLineNumber,flagMarkEnd);
63+
fclose(file);
64+
}
65+
}
66+
}
67+
68+
int main(int argc,char *argv[])
69+
{
70+
int count=0;
71+
int flagLineNumber=0;
72+
int flagMarkEnd=0;
73+
if(argc==1)
74+
read_from_stdin(&count,flagLineNumber,flagMarkEnd);
75+
int i=1;
76+
if(strcmp(argv[1],"-n")==0)
77+
{
78+
flagLineNumber=1;
79+
++i;
80+
}
81+
else if(strcmp(argv[1],"-E")==0)
82+
{
83+
flagMarkEnd=1;
84+
++i;
85+
}
86+
for(;i<argc;++i)
87+
print_file(argv[i],&count,flagLineNumber,flagMarkEnd);
88+
89+
return 0;
90+
}

DATE

16.5 KB
Binary file not shown.

DATE.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <time.h>
5+
6+
int main(int argc,char *argv[])
7+
{
8+
time_t t=time(NULL);
9+
struct tm *timeInfo;
10+
char printableTime[100];
11+
12+
if(argc==1)
13+
{
14+
timeInfo=localtime(&t);
15+
strftime(printableTime,sizeof(printableTime),"%A %d %B %Y %l:%M:%S %p %Z",timeInfo);
16+
printf("%s\n",printableTime);
17+
}
18+
else if(argc>2)
19+
printf("Excess arguments recived\n");
20+
else if(strcmp(argv[1],"-u")==0)
21+
{
22+
timeInfo=gmtime(&t);
23+
strftime(printableTime,sizeof(printableTime),"%A %d %B %Y %l:%M:%S %p %Z",timeInfo);
24+
printf("%s\n",printableTime);
25+
}
26+
else if(strcmp(argv[1],"-R")==0)
27+
{
28+
timeInfo=localtime(&t);
29+
strftime(printableTime,sizeof(printableTime),"%a, %d %b %Y %H:%M:%S %z",timeInfo);
30+
printf("%s\n",printableTime);
31+
}
32+
else
33+
printf("Invalid argument recieved\n");
34+
35+
return 0;
36+
}

LS

16.7 KB
Binary file not shown.

LS.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <dirent.h>
5+
6+
void print_sorted_directory()
7+
{
8+
struct dirent **files;
9+
int n=scandir(".",&files,NULL,alphasort);
10+
for(int i=0;i<n;++i)
11+
{
12+
if(strcmp(files[i]->d_name,".")!=0 && strcmp(files[i]->d_name,"..")!=0)
13+
printf("%s \n",files[i]->d_name);
14+
free(files[i]);
15+
}
16+
free(files);
17+
}
18+
void print_all_sorted_directory()
19+
{
20+
struct dirent **files;
21+
int n=scandir(".",&files,NULL,alphasort);
22+
for(int i=0;i<n;++i)
23+
{
24+
printf("%s \n",files[i]->d_name);
25+
free(files[i]);
26+
}
27+
free(files);
28+
}
29+
30+
int main(int argc,char *argv[])
31+
{
32+
if(argc==1)
33+
print_sorted_directory();
34+
else if(argc>2)
35+
printf("Excess arguments");
36+
else if(strcmp(argv[1],"-A")==0)
37+
print_sorted_directory();
38+
else if(strcmp(argv[1],"-a")==0)
39+
print_all_sorted_directory();
40+
else
41+
printf("Unrecognised flag\n");
42+
43+
return 0;
44+
}

MKDIR

16.7 KB
Binary file not shown.

MKDIR.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <dirent.h>
5+
#include <sys/stat.h>
6+
7+
int isPresent(char name[])
8+
{
9+
struct dirent **files;
10+
int n=scandir(".",&files,NULL,alphasort);
11+
int flag=0;
12+
for(int i=0;i<n;++i)
13+
{
14+
if(strcmp(name,files[i]->d_name)==0)
15+
if(files[i]->d_type==DT_DIR)
16+
flag=1;
17+
free(files[i]);
18+
}
19+
free(files);
20+
return flag;
21+
}
22+
23+
void print_help()
24+
{
25+
printf(" Functions Manual\n");
26+
printf("\n");
27+
printf("NAME\n");
28+
printf(" mkdir\n");
29+
printf("\n");
30+
printf("SYNOPSIS\n");
31+
printf(" mkdir [OPTION] fileName\n");
32+
33+
printf("DESCRIPTION\n");
34+
printf(" makes a directory.\n");
35+
printf("\n");
36+
printf("OPTION\n");
37+
printf(" mkdir fileName\n");
38+
printf(" makes a directory with name fileName\n");
39+
printf(" mkdir -v fileName\n");
40+
printf(" makes a directory and tell the status after creation\n");
41+
printf(" mkdir --help\n");
42+
printf(" display this help\n");
43+
44+
45+
}
46+
47+
int main(int argc,char *argv[])
48+
{
49+
if(argc==1)
50+
printf("Insufficient arguments recieved");
51+
else if(argc==2 && strcmp(argv[1],"--help")==0)
52+
print_help();
53+
else
54+
{
55+
int i=1;
56+
int flagExplain=0;
57+
if(strcmp(argv[1],"-v")==0)
58+
{
59+
flagExplain=1;
60+
++i;
61+
}
62+
if(flagExplain==1 && argc==2)
63+
printf("Insufficient arguments recieved");
64+
else
65+
{
66+
for(;i<argc;++i)
67+
{
68+
if(isPresent(argv[i]))
69+
printf("%s is a already present, can't create another\n",argv[i]);
70+
else
71+
{
72+
73+
int m=mkdir(argv[i],077);
74+
if(!m)
75+
{}
76+
else
77+
printf("%s could not be created\n",argv[i]);
78+
if(flagExplain==1)
79+
printf("%s is created\n",argv[i]);
80+
}
81+
}
82+
}
83+
}
84+
85+
86+
return 0;
87+
}

Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
shell: shell.o
2+
gcc shell.o -o shell
3+
4+
shell.o: shell.s
5+
gcc -c shell.s
6+
7+
shell.s: shell.i
8+
gcc -S shell.i
9+
10+
shell.i: shell.c
11+
gcc -E shell.c -o shell.i
12+
13+
run: shell
14+
./shell
15+
16+
all: LS.c CAT.c DATE.c RM.c MKDIR.c
17+
gcc LS.c -o LS
18+
gcc CAT.c -o CAT
19+
gcc DATE.c -o DATE
20+
gcc RM.c -o RM
21+
gcc MKDIR.c -o MKDIR
22+
23+
24+
clean:
25+
cp shell.c temp.c
26+
rm shell*
27+
cp temp.c shell.c
28+
rm temp.c

RM

16.6 KB
Binary file not shown.

RM.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <dirent.h>
5+
6+
int isFolder(char name[])
7+
{
8+
struct dirent **files;
9+
int n=scandir(".",&files,NULL,alphasort);
10+
int flag=0;
11+
for(int i=0;i<n;++i)
12+
{
13+
if(strcmp(name,files[i]->d_name)==0)
14+
if(files[i]->d_type==DT_DIR)
15+
flag=1;
16+
free(files[i]);
17+
}
18+
free(files);
19+
return flag;
20+
}
21+
22+
int main(int argc,char *argv[])
23+
{
24+
if(argc==1)
25+
printf("Insufficient arguments recieved");
26+
else
27+
{
28+
int i=1;
29+
int flagPrompt=0;
30+
int flagExplain=0;
31+
if(strcmp(argv[1],"-i")==0)
32+
{
33+
flagPrompt=1;
34+
++i;
35+
}
36+
else if(strcmp(argv[1],"-v")==0)
37+
{
38+
flagExplain=1;
39+
++i;
40+
}
41+
if((flagPrompt==1 || flagExplain==1) && argc==2)
42+
printf("Insufficient arguments recieved");
43+
else
44+
{
45+
char c='Y';
46+
for(;i<argc;++i)
47+
{
48+
if(isFolder(argv[i]))
49+
printf("%s is a directory, can't delete\n",argv[i]);
50+
else
51+
{
52+
if(flagPrompt==1)
53+
{
54+
printf("Do you want to remove %s[Y/N]\n",argv[i]);
55+
scanf(" %c",&c);
56+
if(c=='N')
57+
continue;
58+
}
59+
int r=remove(argv[i]);
60+
if(r==-1)
61+
printf("%s could not be removed\n",argv[i]);
62+
if(flagExplain==1)
63+
printf("%s is removed\n",argv[i]);
64+
}
65+
}
66+
}
67+
}
68+
69+
return 0;
70+
}

0 commit comments

Comments
 (0)