embox

Форк
0
/
hello_srs.c 
159 строк · 2.6 Кб
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <embox/cmd.h>
5

6
EMBOX_CMD(hello_srs_main);
7

8
const int N = 10000;
9

10
void Multiply(int a[], int b[], int result[]) {
11

12
	for (int i = 1; i <= a[0]; i++) {
13
		for (int j = 1; j <= b[0]; j++) {
14
			result[i + j - 1] += a[i] * b[j];
15
		}
16
	}
17

18
	result[0] = a[0] + b[0] + 1;
19
}
20

21
void Assigment(int a[], int c[]) {
22
	for (int i = 1; i <= c[0]; i++){
23
		a[i] = c[i];
24
		c[i] = 0;
25
	}
26

27
	a[0] = c[0];
28
	c[0] = 0;
29

30
}
31

32
void Carry(int c[]) {
33
	int length = c[0];
34
	for (int i = 1; i <= c[0]; i++) {
35
		if (c[i] >= 10) {
36
			c[i + 1] += c[i] / 10;
37
			c[i] %= 10;
38
		}
39
	}
40

41
	while (c[length] == 0) {
42
		length--;
43
		c[0] = length;
44
	}
45
}
46

47
void Read(int numb, int a[]) {
48
	char* number = (char*)malloc(100);
49
	sprintf(number, "%d", numb);
50

51
	int index = 1;
52

53
	int i;
54
	for (i = strlen(number) - 1; i >= 0; i--) {
55
		a[index] = number[i] - '0';
56
		index++;
57
	}
58
	a[0] = strlen(number);
59
}
60

61

62
void Out1(int a[]) {
63
	for (int i = a[0]; i > 0; i--)
64
		printf("%d", a[i]);
65
	printf("\n");
66
}
67
void Out2(char* str) {
68
	for (int i = 0; i < strlen(str); i++)
69
		printf("%c", str[i]);
70
	printf("\n");
71
}
72

73
static int hello_srs_main(int argc, char *argv[] )
74
{
75
	int a[N], b[N], c[2 * N], flag = 0;
76
	char str[100];
77
	char* query=NULL;
78
	char* query1="fact";
79
	char* query2="open";
80
	const char *method = getenv("REQUEST_METHOD");
81
	assert(method);
82

83
 	if( !strcmp(method,"POST") )
84
	{
85
 		unsigned int len;
86
  		len = atoi( getenv("CONTENT_LENGTH") );
87
 		query = (char*)malloc(len+1);
88
  		fread(query, 1, len, stdin);
89
 		query[len] = 0;
90
	}
91
 	else if( !strcmp(method,"GET") )
92
 	{
93
 		query=(char*)malloc(strlen(getenv("QUERY_STRING"))+1);
94
 		strcpy(query,getenv("QUERY_STRING"));
95
	}
96
 	else printf("unknown REQUEST_METHOD\n");
97

98

99
	if ( !strncmp(query, query1, 4) )
100
	{
101
		flag = 1;
102
		Read(1, a);
103
		char* tmp = strstr(query, "=");
104
		query = NULL;
105
		for(int i = 1; i < sizeof(tmp); i++)
106
			query[i-1] = tmp[i];
107
		for (int i = 2; i <= atoi(query); i++) {
108
			Read(i, b);
109
			Multiply(a, b, c);
110
			Carry(c);
111
			Assigment(a, c);
112
		}
113
	}
114

115
	if ( !strncmp(query, query2, 4) )
116
	{
117
		flag = 2;
118
		char* tmp = strstr(query, "=");
119
		query = NULL;
120
		for(int i = 1; i < sizeof(tmp); i++)
121
			query[i-1] = tmp[i];
122
		FILE *File = fopen(query, "rt");
123
	if (File != NULL)
124
	{
125
		fgets(str, sizeof(str), File);
126
		fclose(File);
127
	}
128
	else
129
	{
130
		str[0] = 'E';
131
	}
132
	}
133

134
	printf(
135
		"HTTP/1.1 %d %s\r\n"
136
		"Content-Type: %s\r\n"
137
		"Connection: close\r\n"
138
		"\r\n", 200, "OK", "text/html");
139

140
	printf("<HTML>"
141
		"<HEAD>"
142
   		"<TITLE>Fact</TITLE>"
143
		"</HEAD>"
144
		"<BODY>"
145
		"Body: ");
146
	if (flag == 1)
147
		Out1(a);
148
	else
149
	if (flag == 2)
150
		Out2(str);
151

152
	printf(
153
		"</BODY>"
154
		"</HTML>");
155

156
	free(query);
157
	fflush(stdout);
158
	return 0;
159
}
160

161

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.