Skip to content

Commit 469c134

Browse files
committed
Beautify and move static stuff inside source files
1 parent 1e30f76 commit 469c134

15 files changed

+895
-993
lines changed

include/builtins.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
#ifndef BUILTINS_H
22
#define BUILTINS_H
3-
#include "stack.h"
4-
5-
63
void changedir(char **argv);
4+
#endif //BUILTINS_H
75

8-
9-
10-
11-
#endif

include/cmd_struct.h

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
#ifndef CMD_STRUCT_H
22
#define CMD_STRUCT_H
33

4+
// this structure is crazy
45
typedef struct cmd_struct
56
{
6-
char**** argp_arr; // contains cmd argv for each command for each pipeline
7-
int* n_pipes; // contains no of pipes info for pipeline
8-
int n_pipelines; // no of pipelines
9-
int* file_out; // contains output to file info about each pipeline
10-
int* cmd_flow; //contains pipeline flow info that is && separator implementation
11-
}cmd_struct;
7+
int n_pipelines; // no. of pipelines
8+
char**** argp_arr; // contains cmd argv for each command for each pipeline
9+
int* n_pipes; // contains no of pipes info for pipeline
10+
int* file_out; // contains output to file info about each pipeline
11+
int* cmd_flow; // contains pipeline flow info that is && separator implementation
12+
} cmd_struct;
1213

1314
void init_cmd_struct(cmd_struct** cmd);
1415
void free_cmd_struct(cmd_struct** cmd);
1516

16-
#endif
17+
#endif

include/execute.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#ifndef EXECUTE_H
22
#define EXECUTE_H
3-
43
#include "cmd_struct.h"
54

6-
int exec_w(char* path,char** argv); //execute and wait for cmd to exit
7-
8-
int exec_bg(char* path,char** argv);//execute and return pid of child
9-
10-
//executes a pipeline of commands cmd | cmd ...| cmd [> filename] | [>> filename]
11-
int execute_pipeline(char*** argp,int npipes,int file_output_flag);
5+
// execute and wait for cmd to exit
6+
int exec_w(char* path, char** argv);
127

13-
static int create_pipeline(int* pipefd_arr,int n_pipes);
8+
// execute and return pid of child
9+
int exec_bg(char* path, char** argv);
1410

15-
int execute_cmd_struct(cmd_struct* cmd);//executes parsed wish input
11+
// execute a pipeline of commands cmd | cmd ...| cmd [> filename] | [>> filename]
12+
int execute_pipeline(char*** argp, int npipes, int file_output_flag);
1613

14+
// execute parsed wish input
15+
int execute_cmd_struct(cmd_struct* cmd);
1716

18-
#endif
17+
#endif //EXECUTE_H
1918

include/w_env.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
#ifndef W_ENV_H
22
#define W_ENV_H
33
//i think it should be called GLOBAL_H or global.h
4+
#include "stack.h"
45

5-
#define INPUTSIZE 1000//sufficient length
6-
#define CMDPERINPUT 10 //sufficient size of cmd queue array
7-
#define PATHLEN 1000
8-
#define DIRSTCKSIZE 20 //
9-
#define TOKSIZE 100
6+
#define INPUTSIZE 1024 //sufficient length
7+
#define CMDPERINPUT 16 //sufficient size of cmd queue array
8+
#define PATHLEN 1024
9+
#define DIRSTCKSIZE 32
10+
#define TOKSIZE 128
1011

11-
12-
13-
extern struct sigaction act;//should be a Global since it wont be shared anyway
12+
extern struct sigaction act;
1413
extern char* stream;
1514
extern char PWD[1000];
1615
extern STACK DIRSTACK;
1716
extern STACK HISTSTACK;
1817
extern char *host_name;
1918
extern char *user_name;
20-
//extern char** environ;
21-
2219

20+
#endif //W_ENV_H
2321

24-
#endif

src/builtins.c

+56-73
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,72 @@
1-
#include "builtins.h"
2-
#include "stack.h"
3-
#include "w_env.h"
41
#include <stdio.h>
52
#include <stdlib.h>
63
#include <string.h>
74
#include <unistd.h>
85
#include <dirent.h>
6+
#include "builtins.h"
7+
#include "stack.h"
8+
#include "w_env.h"
99

1010
void changedir(char **argv)
1111
{
12-
//char *temp;
13-
int dir_found=2;
14-
if(argv[1]==NULL)//cd
12+
int dir_found=2;
13+
if (!argv[1]) //cd
14+
{
15+
push(&DIRSTACK, PWD);
16+
dir_found = chdir(getenv("HOME"));
17+
}
18+
else if (strlen(argv[1]) > 1)
19+
{
20+
if (argv[1][0] == '/')
1521
{
16-
push(&DIRSTACK,PWD);
17-
dir_found=chdir(getenv("HOME"));
18-
22+
dir_found = chdir(argv[1]);
1923
}
20-
else if(strlen(argv[1])>1){
21-
if(argv[1][0]=='/')
22-
{
23-
24-
dir_found=chdir(argv[1]);
25-
//getcwd(PWD,PATHLEN);
26-
}
27-
else
28-
{
29-
push(&DIRSTACK,PWD);
30-
strcat(PWD,"/");
31-
strcat(PWD,argv[1]);
32-
dir_found=chdir(PWD);
33-
}
34-
//dir_found=chdir(argv[1]);
35-
24+
else
25+
{
26+
push(&DIRSTACK, PWD);
27+
strcat(PWD, "/");
28+
strcat(PWD, argv[1]);
29+
dir_found = chdir(PWD);
3630
}
37-
else if(strlen(argv[1])==1){
38-
if(argv[1][0]=='-')
39-
{
40-
41-
if(DIRSTACK.top!=-1)dir_found=chdir(pop(&DIRSTACK));
42-
else printf("No previous working directory!\n");
43-
push(&DIRSTACK,PWD);
44-
//push(&DIRSTACK,PWD);
45-
//getcwd(PWD,PATHLEN);
46-
}
47-
else if(argv[1][0]=='~')
48-
{
49-
push(&DIRSTACK,PWD);
50-
dir_found=chdir(getenv("HOME"));
51-
//push(&DIRSTACK,PWD);
52-
//getcwd(PWD,PATHLEN);
53-
}
54-
else if(argv[1][0]=='/')
55-
{
56-
push(&DIRSTACK,PWD);
57-
dir_found=chdir(argv[1]);
58-
//push(&DIRSTACK,PWD);
59-
//getcwd(PWD,PATHLEN);
60-
}
61-
else if(argv[1][0]=='.')
62-
{
63-
//push(&DIRSTACK,PWD);
64-
//strcat(PWD,"/");
65-
//strcat(PWD,argv[1]);
66-
//dir_found=chdir(PWD);
67-
//getcwd(PWD,PATHLEN);
68-
}
69-
else {
70-
//dir_found=chdir(argv[1]);
71-
push(&DIRSTACK,PWD);
72-
strcat(PWD,"/");
73-
strcat(PWD,argv[1]);
74-
dir_found=chdir(PWD);
75-
}
31+
}
32+
else if (strlen(argv[1]) == 1)
33+
{
34+
if (argv[1][0] == '-') // cd -
35+
{
36+
if (DIRSTACK.top != -1)
37+
dir_found = chdir(pop(&DIRSTACK));
38+
else
39+
printf("No previous working directory!\n");
40+
push(&DIRSTACK, PWD);
7641
}
77-
if(dir_found==-1){
78-
perror("\n");
42+
else if(argv[1][0] == '~')
43+
{
44+
push(&DIRSTACK, PWD);
45+
dir_found = chdir(getenv("HOME"));
7946
}
80-
else if(dir_found==0){
81-
82-
getcwd(PWD,PATHLEN);
47+
else if(argv[1][0] == '/')
48+
{
49+
push(&DIRSTACK, PWD);
50+
dir_found = chdir(argv[1]);
8351
}
84-
85-
86-
52+
else if(argv[1][0] == '.')
53+
{
54+
}
55+
else
56+
{
57+
push(&DIRSTACK, PWD);
58+
strcat(PWD, "/");
59+
strcat(PWD, argv[1]);
60+
dir_found = chdir(PWD);
61+
}
62+
}
63+
if (dir_found == -1)
64+
{
65+
perror("cd");
66+
}
67+
else if(dir_found == 0)
68+
{
69+
getcwd(PWD, PATHLEN);
70+
}
8771
}
8872

89-

src/cmd_struct.c

+27-20
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,38 @@
55

66
void init_cmd_struct(cmd_struct** cmd)
77
{
8-
*cmd = (cmd_struct*)malloc(sizeof(cmd_struct));
9-
(*cmd)->argp_arr =(char****)malloc(sizeof(char***)*MAXPIPELINES); //parsed commands to be executed
10-
(*cmd)->n_pipes =(int*)malloc(sizeof(int*)*MAXPIPELINES);//contains no of pipes of info;
11-
(*cmd)->file_out =(int*)malloc(sizeof(int*)*MAXPIPELINES);
12-
(*cmd)->cmd_flow =(int*)malloc(sizeof(int*)*MAXPIPELINES);
13-
14-
int i ;
15-
for(i=0;i<MAXPIPELINES;i++)(*cmd)->n_pipes[i] = 0;
16-
for(i=0;i<MAXPIPELINES;i++)(*cmd)->file_out[i] = 0;
17-
for(i=0;i<MAXPIPELINES;i++)(*cmd)->cmd_flow[i] = 0;
8+
*cmd = (cmd_struct*)malloc(sizeof(cmd_struct));
9+
(*cmd)->argp_arr = (char****)malloc(sizeof(char***) * MAXPIPELINES); //parsed commands to be executed
10+
(*cmd)->n_pipes = (int*)malloc(sizeof(int*) * MAXPIPELINES);//contains no of pipes of info;
11+
(*cmd)->file_out = (int*)malloc(sizeof(int*) * MAXPIPELINES);
12+
(*cmd)->cmd_flow = (int*)malloc(sizeof(int*) * MAXPIPELINES);
1813

19-
(*cmd)->n_pipelines = 0;
14+
int i;
15+
for (i = 0; i < MAXPIPELINES; i++)
16+
{
17+
(*cmd)->n_pipes[i] = 0;
18+
(*cmd)->file_out[i] = 0;
19+
(*cmd)->cmd_flow[i] = 0;
20+
}
21+
(*cmd)->n_pipelines = 0;
2022
}
2123

22-
23-
2424
void free_cmd_struct(cmd_struct** cmd)
2525
{
26-
if(*cmd==NULL)return;
27-
int i;
28-
for(i=0;i< (*cmd)->n_pipelines;i++)
29-
{
30-
free((*cmd)->argp_arr[i]);
31-
}
26+
if (!cmd || !(*cmd))
27+
return;
28+
int i;
29+
for (i = 0; i < (*cmd)->n_pipelines; i++)
30+
{
31+
if ((*cmd)->argp_arr[i])
32+
free((*cmd)->argp_arr[i]);
33+
(*cmd)->argp_arr[i] = NULL;
34+
}
35+
if ((*cmd)->n_pipes)
3236
free((*cmd)->n_pipes);
37+
if ((*cmd)->file_out)
3338
free((*cmd)->file_out);
39+
if ((*cmd)->cmd_flow)
3440
free((*cmd)->cmd_flow);
35-
}
41+
}
42+

0 commit comments

Comments
 (0)