-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.c
More file actions
78 lines (67 loc) · 1.34 KB
/
read.c
File metadata and controls
78 lines (67 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "header.h"
//#include "execute.c"
//READ
#define RL_BUFSIZE 1024
char *read_line(void)
{
int bufsize=RL_BUFSIZE;
int position=0;
char *buffer=malloc(sizeof(char)*bufsize);
int c;
//unable to allocate buffer
if(!buffer)
{
fprintf(stderr, "allocation error\n");
exit(1);
}
//read character by character
while(1)
{
//we exceeded the buffer, reallocate it
if(position >= bufsize)
{
bufsize += RL_BUFSIZE;
buffer = realloc(buffer,bufsize);
if(!buffer)
{
fprintf(stderr, "my: allocation error\n");
exit(1);
}
}
c=getchar();
//end of list of arguments, make last character=null
if(c=='\n')
{
buffer[position]='\0';
return buffer;
}
else if(c==EOF)
{
ctrlD();
}
//keep reading
else
buffer[position]=c;
position++;
}
}
//END READONG
//SPLITTING INTO COMMANDS
#define TOK_BUFSIZE1 64
#define TOK_CDELIM ";\n"
int split_command(char* line)
{
int bufsize = TOK_BUFSIZE1, position = 0;
char *token;
char **args,*saveptr;
//printf("%s\n",line);
token = strtok_r(line, TOK_CDELIM,&saveptr);
while (token != NULL) {
// printf("c:%s\n",token);
args=split_line(token);
execute(args);
token = strtok_r(NULL, TOK_CDELIM,&saveptr);
}
return 1;
}
//END SPLITTING INTO COMMANDS