-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadPortNumber.c
80 lines (79 loc) · 1.49 KB
/
readPortNumber.c
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
79
80
#include <unistd.h>
//This function gets a char digit and returns the int value
//if the char is not a digit it returns -1
int charDigitToInt(char c){
int ret;
switch(c){
case '0':
ret = 0;
break;
case '1':
ret = 1;
break;
case '2':
ret = 2;
break;
case '3':
ret = 3;
break;
case '4':
ret = 4;
break;
case '5':
ret = 5;
break;
case '6':
ret = 6;
break;
case '7':
ret = 7;
break;
case '8':
ret = 8;
break;
case '9':
ret = 9;
break;
default:
ret = -1;
break;
}
return ret;
}
//This function gets a char array and returns the int equivalent
int convertCharPortToInt(char *buff, int size, int numOfRead){
int converted = 0;
int pow = 1;
int base = 10;
for(int position = numOfRead -1; position>=0; position--){
int temp = charDigitToInt(buff[position]);
if(temp==-1){
write(1, "Invalid Port Number\n", 20);
_exit(1);
}
converted += temp*pow;
pow *= base;
}
return converted;
}
//This function reads the port number using read()
//then passes it to another function to be converted into int
int readPortNumber(){
char buff[10];
char numbuff[1024];
int place = 0;
int n;
write(1, "Port: ", 6);
while( (n = read(0, buff, sizeof(buff)>0)) !=0){
if(n==1 && (buff[0]=='\n'))
break;
numbuff[place] = buff[0];
place++;
}
numbuff[place] = '\0';
return convertCharPortToInt(numbuff, 1024, place);
}
int main(void){
//reading the port number from stdin
int port_number = readPortNumber();
}