VPN termux about China.net
Friday, 30 October 2026
Sunday, 2 August 2026
Drought escape calcium
#include <SDL2/SDL.h>
#include <vector>
#include <cstdlib>
#include <ctime>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
// Struktura dla surowców (gnój i wapno)
struct Resource {
SDL_Rect rect;
int type; // 0 = gnój, 1 = wapno
};
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) return 1;
SDL_Window* window = SDL_CreateWindow("Skarabeusz: Alchemik Pustyni", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
bool running = true;
SDL_Event e;
// Gracz - Skarabeusz
SDL_Rect player = { SCREEN_WIDTH / 2 - 25, SCREEN_HEIGHT / 6 - 25, 50, 50 };
// Surowce na planszy
std::vector<Resource> resources;
std::srand(std::time(nullptr));
for (int i = 0; i < 15; ++i) {
Resource res;
res.rect = { std::rand() % (SCREEN_WIDTH - 20), std::rand() % (SCREEN_HEIGHT - 20), 20, 20 };
res.type = std::rand() % 2; // 50% szans na gnój, 50% na wapno
resources.push_back(res);
}
int inventoryGnoj = 0;
int inventoryWapno = 0;
int waterLevel = 0; // Nasz cel: zdobyć wodę
bool isNight = false;
Uint32 lastTime = SDL_GetTicks();
// Główna pętla gry
while (running) {
// Czas gry (przełączanie dzień/noc)
Uint32 currentTime = SDL_GetTicks();
if (currentTime - lastTime > 5000) { // Co 5 sekund zmiana
isNight = !isNight;
lastTime = currentTime;
// Jeśli zaczyna się noc, przetwarzamy zebrane zasoby w wodę
if (isNight) {
if (inventoryGnoj >= 5 && inventoryWapno >= 3) {
waterLevel += 10; // Sukces!
SDL_Log("Noc alchemii! Woda zdobyta! Poziom wody: %d", waterLevel);
} else {
SDL_Log("Noc nieudana. Za mało zasobów (potrzeba 5 gnoju, 3 wapna).");
}
// Reset zapasów po nocy
inventoryGnoj = 0; inventoryWapno = 0;
}
}
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) running = false;
else if (!isNight && e.type == SDL_MOUSEMOTION) {
player.x = e.motion.x - (player.w / 2);
player.y = e.motion.y - (player.h / 2);
}
}
// Dzień: zbieranie zasobów
if (!isNight) {
for (size_t i = 0; i < resources.size(); ++i) {
if (SDL_HasIntersection(&player, &resources[i].rect)) {
if (resources[i].type == 0) inventoryGnoj++;
else inventoryWapno++;
// Usuń zebrany zasób i dodaj nowy w losowym miejscu
resources.erase(resources.begin() + i);
Resource newRes;
newRes.rect = { std::rand() % (SCREEN_WIDTH - 20), std::rand() % (SCREEN_HEIGHT - 20), 20, 20 };
newRes.type = std::rand() % 2;
resources.push_back(newRes);
break; // Unikaj problemów z iteratorem
}
}
}
// Renderowanie
if (isNight) {
// Noc: ciemno, chłodno (granatowe tło)
SDL_SetRenderDrawColor(renderer, 10, 10, 50, 255);
} else {
// Dzień: jasno, gorąco (żółto-piaskowe tło)
SDL_SetRenderDrawColor(renderer, 240, 230, 140, 255);
}
SDL_RenderClear(renderer);
// Rysuj zasoby (tylko w dzień)
if (!isNight) {
for (const auto& res : resources) {
if (res.type == 0) SDL_SetRenderDrawColor(renderer, 165, 42, 42, 255); // Gnój - brąz
else SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // Wapno - biel
SDL_RenderFillRect(renderer, &res.rect);
}
}
// Rysuj skarabeusza (ciemny kolor)
SDL_SetRenderDrawColor(renderer, 85, 107, 47, 255);
SDL_RenderFillRect(renderer, &player);
int boxSize = 20 + waterLevel;
SDL_Rect waterBox = { 50, 50, boxSize, boxSize };
// Rysujemy niebieski kwadrat wody
SDL_SetRenderDrawColor(renderer, 0, 191, 255, 255);
SDL_RenderFillRect(renderer, &waterBox);
// SDL_RenderPresent(renderer);
// Wskaźnik wody w norce (np. prostokąt w rogu)
SDL_Rect waterBar = { 10, 10, waterLevel * 2, 20 };
SDL_SetRenderDrawColor(renderer, 0, 191, 255, 255);
SDL_RenderFillRect(renderer, &waterBar);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Thursday, 28 May 2026
Server C try this ♠️
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
/*
gcc serv.c -o serv
./serv 9999
in chrome tolbar 127.0.0.1:9999
*/
/* C is not C++ use gcc*/
#define BUFSIZE 1024
void cerror(FILE *stream, char *cause, char *errno_str, char *shortmsg, char *longmsg) {
fprintf(stream, "HTTP/1.1 %s %s\nContent-type: text/html\n\n", errno_str, shortmsg);
fprintf(stream, "<html><body><h1>%s: %s</h1><p>%s: %s</p></body></html>", errno_str, shortmsg, longmsg, cause);
fflush(stream);
}
void serve_directory(FILE *stream, char *dirname) {
DIR *dir = opendir(dirname);
if (!dir) {
cerror(stream, dirname, "403", "Forbidden", "Cannot read directory");
return;
}
fprintf(stream, "HTTP/1.1 200 OK\nContent-type: text/html\n\n");
fprintf(stream, "<html><body><h1>Index of %s</h1><ul>", dirname);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') continue;
// ZAWSZE generujemy link z wiodącym "/"
// Jeśli jesteśmy w ".", link to /nazwa. Jeśli w "./katalog", link to /katalog/nazwa
if (strcmp(dirname, ".") == 0) {
fprintf(stream, "<li><a href=\"/%s\">%s</a></li>", entry->d_name, entry->d_name);
} else {
fprintf(stream, "<li><a href=\"%s/%s\">%s</a></li>", dirname + 1, entry->d_name, entry->d_name);
}
}
fprintf(stream, "</ul></body></html>");
fflush(stream);
closedir(dir);
}
int main(int argc, char **argv) {
int parentfd, childfd, portno, optval;
socklen_t clientlen;
struct sockaddr_in serveraddr, clientaddr;
FILE *stream;
char buf[BUFSIZE], method[BUFSIZE], uri[BUFSIZE], version[BUFSIZE];
char filename[BUFSIZE], filetype[BUFSIZE];
struct stat sbuf;
if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); }
portno = atoi(argv[1]);
parentfd = socket(AF_INET, SOCK_STREAM, 0);
optval = 1;
setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
bind(parentfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
listen(parentfd, 5);
while (1) {
clientlen = sizeof(clientaddr);
childfd = accept(parentfd, (struct sockaddr *)&clientaddr, &clientlen);
stream = fdopen(childfd, "r+");
if (fgets(buf, BUFSIZE, stream) == NULL) { fclose(stream); continue; }
sscanf(buf, "%s %s %s", method, uri, version);
while(strcmp(buf, "\r\n") && strcmp(buf, "\n")) { fgets(buf, BUFSIZE, stream); }
if (strcasecmp(method, "GET")) {
cerror(stream, method, "501", "Not Implemented", "Tiny does not implement this method");
} else {
strcpy(filename, ".");
strcat(filename, uri);
if (stat(filename, &sbuf) < 0) {
cerror(stream, filename, "404", "Not found", "Couldn't find this file");
} else if (S_ISDIR(sbuf.st_mode)) {
serve_directory(stream, filename);
} else {
if (strstr(filename, ".html")) strcpy(filetype, "text/html");
else strcpy(filetype, "text/plain");
fprintf(stream, "HTTP/1.1 200 OK\nServer: Tiny Web Server\nContent-length: %d\nContent-type: %s\n\n", (int)sbuf.st_size, filetype);
fflush(stream);
int fd = open(filename, O_RDONLY);
void *p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
fwrite(p, 1, sbuf.st_size, stream);
munmap(p, sbuf.st_size);
close(fd);
}
}
fclose(stream);
}
}
Tuesday, 5 May 2026
Tiny.c 2026 V
/* * tiny.c - a minimal HTTP server that serves static and
* dynamic content with the GET method. Neither
* robust, secure, nor modular. Use for instructional
* purposes only.
* Dave O'Hallaron, Carnegie Mellon
*Compile:gcc tiny.c -o tiny
* usage: tiny <port>
* dynamic content with the GET method. Neither
* robust, secure, nor modular. Use for instructional
* purposes only.
* Dave O'Hallaron, Carnegie Mellon
*Compile:gcc tiny.c -o tiny
* usage: tiny <port>
*usage tiny 9999
*Open chrome 127.0.0.1:9999/tiny.c
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> /* Zawiera deklaracje bzero */
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1024
#define MAXERRS 16
extern char **environ; /* the environment */
/*
* error - wrapper for perror used for bad syscalls
*/
void error(char *msg) {
perror(msg);
exit(1);
}
/*
* cerror - returns an error message to the client
*/
void cerror(FILE *stream, char *cause, char *errno_str,
char *shortmsg, char *longmsg) {
fprintf(stream, "HTTP/1.1 %s %s\n", errno_str, shortmsg);
fprintf(stream, "Content-type: text/html\n");
fprintf(stream, "\n");
fprintf(stream, "<html><title>Tiny Error</title>");
fprintf(stream, "<body bgcolor=\"#ffffff\">\n");
fprintf(stream, "%s: %s\n", errno_str, shortmsg);
fprintf(stream, "<p>%s: %s\n", longmsg, cause);
fprintf(stream, "<hr><em>The Tiny Web server</em>\n");
}
int main(int argc, char **argv) {
/* variables for connection management */
int parentfd; /* parent socket */
int childfd; /* child socket */
int portno; /* port to listen on */
int clientlen; /* byte size of client's address */
struct hostent *hostp; /* client host info */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
/* variables for connection I/O */
FILE *stream; /* stream version of childfd */
char buf[BUFSIZE]; /* message buffer */
char method[BUFSIZE]; /* request method */
char uri[BUFSIZE]; /* request uri */
char version[BUFSIZE]; /* request method */
char filename[BUFSIZE];/* path derived from uri */
char filetype[BUFSIZE];/* path derived from uri */
char cgiargs[BUFSIZE]; /* cgi argument list */
char *p; /* temporary pointer */
int is_static; /* static request? */
struct stat sbuf; /* file status */
int fd; /* static content filedes */
int pid; /* process id from fork */
int wait_status; /* status from wait */
/* check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
/* open socket descriptor */
parentfd = socket(AF_INET, SOCK_STREAM, 0);
if (parentfd < 0)
error("ERROR opening socket");
/* allows us to restart server immediately */
optval = 1;
setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/* bind port to socket */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
if (bind(parentfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/* get us ready to accept connection requests */
if (listen(parentfd, 5) < 0) /* allow 5 requests to queue up */
error("ERROR on listen");
/* * main loop: wait for a connection request, parse HTTP,
* serve requested content, close connection.
*/
clientlen = sizeof(clientaddr);
while (1) {
/* wait for a connection request */
childfd = accept(parentfd, (struct sockaddr *) &clientaddr, &clientlen);
if (childfd < 0)
error("ERROR on accept");
/* determine who sent the message */
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
/* open the child socket descriptor as a stream */
if ((stream = fdopen(childfd, "r+")) == NULL)
error("ERROR on fdopen");
/* get the HTTP request line */
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
sscanf(buf, "%s %s %s\n", method, uri, version);
/* tiny only supports the GET method */
if (strcasecmp(method, "GET")) {
cerror(stream, method, "501", "Not Implemented",
"Tiny does not implement this method");
fclose(stream);
close(childfd);
continue;
}
/* read (and ignore) the HTTP headers */
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
while(strcmp(buf, "\r\n")) {
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
}
/* parse the uri [crufty] */
if (!strstr(uri, "cgi-bin")) { /* static content */
is_static = 1;
strcpy(cgiargs, "");
strcpy(filename, ".");
strcat(filename, uri);
if (uri[strlen(uri)-1] == '/')
strcat(filename, "/sdcard");
}
/* else {
is_static = 0;
p = strchr(uri, '?');
if (p) {
strcpy(cgiargs, p+1);
*p = '\0';
}*/
else {
strcpy(cgiargs, "");
}
strcpy(filename, ".");
strcat(filename, uri);
// }
/* make sure the file exists */
if (stat(filename, &sbuf) < 0) {
cerror(stream, filename, "404", "Not found",
"Tiny couldn't find this file");
fclose(stream);
close(childfd);
continue;
}
/* serve static content */
if (is_static) {
if (strstr(filename, ".html"))
strcpy(filetype, "text/html");
else if (strstr(filename, ".gif"))
strcpy(filetype, "image/gif");
else if (strstr(filename, ".jpg"))
strcpy(filetype, "image/jpg");
else
strcpy(filetype, "text/plain");
/* print response header */
fprintf(stream, "HTTP/1.1 200 OK\n");
fprintf(stream, "Server: Tiny Web Server\n");
fprintf(stream, "Content-length: %d\n", (int)sbuf.st_size);
fprintf(stream, "Content-type: %s\n", filetype);
fprintf(stream, "\r\n");
fflush(stream);
/* Use mmap to return arbitrary-sized response body */
fd = open(filename, O_RDONLY);
p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
fwrite(p, 1, sbuf.st_size, stream);
munmap(p, sbuf.st_size);
}
/* serve dynamic content */
else {
/* make sure file is a regular executable file */
if (!(S_IFREG & sbuf.st_mode) || !(S_IXUSR & sbuf.st_mode)) {
cerror(stream, filename, "403", "Forbidden",
"You are not allow to access this item");
fclose(stream);
close(childfd);
continue;
}
/* a real server would set other CGI environ vars as well*/
setenv("QUERY_STRING", cgiargs, 1);
/* print first part of response header */
sprintf(buf, "HTTP/1.1 200 OK\n");
write(childfd, buf, strlen(buf));
sprintf(buf, "Server: Tiny Web Server\n");
write(childfd, buf, strlen(buf));
/* create and run the child CGI process so that all child
output to stdout and stderr goes back to the client via the
childfd socket descriptor */
pid = fork();
if (pid < 0) {
perror("ERROR in fork");
exit(1);
}
else if (pid > 0) { /* parent process */
wait(&wait_status);
}
else { /* child process*/
close(0); /* close stdin */
dup2(childfd, 1); /* map socket to stdout */
dup2(childfd, 2); /* map socket to stderr */
if (execve(filename, NULL, environ) < 0) {
perror("ERROR in execve");
}
}
}
/* clean up */
fclose(stream);
close(childfd);
}
}
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> /* Zawiera deklaracje bzero */
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1024
#define MAXERRS 16
extern char **environ; /* the environment */
/*
* error - wrapper for perror used for bad syscalls
*/
void error(char *msg) {
perror(msg);
exit(1);
}
/*
* cerror - returns an error message to the client
*/
void cerror(FILE *stream, char *cause, char *errno_str,
char *shortmsg, char *longmsg) {
fprintf(stream, "HTTP/1.1 %s %s\n", errno_str, shortmsg);
fprintf(stream, "Content-type: text/html\n");
fprintf(stream, "\n");
fprintf(stream, "<html><title>Tiny Error</title>");
fprintf(stream, "<body bgcolor=\"#ffffff\">\n");
fprintf(stream, "%s: %s\n", errno_str, shortmsg);
fprintf(stream, "<p>%s: %s\n", longmsg, cause);
fprintf(stream, "<hr><em>The Tiny Web server</em>\n");
}
int main(int argc, char **argv) {
/* variables for connection management */
int parentfd; /* parent socket */
int childfd; /* child socket */
int portno; /* port to listen on */
int clientlen; /* byte size of client's address */
struct hostent *hostp; /* client host info */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
/* variables for connection I/O */
FILE *stream; /* stream version of childfd */
char buf[BUFSIZE]; /* message buffer */
char method[BUFSIZE]; /* request method */
char uri[BUFSIZE]; /* request uri */
char version[BUFSIZE]; /* request method */
char filename[BUFSIZE];/* path derived from uri */
char filetype[BUFSIZE];/* path derived from uri */
char cgiargs[BUFSIZE]; /* cgi argument list */
char *p; /* temporary pointer */
int is_static; /* static request? */
struct stat sbuf; /* file status */
int fd; /* static content filedes */
int pid; /* process id from fork */
int wait_status; /* status from wait */
/* check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
/* open socket descriptor */
parentfd = socket(AF_INET, SOCK_STREAM, 0);
if (parentfd < 0)
error("ERROR opening socket");
/* allows us to restart server immediately */
optval = 1;
setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/* bind port to socket */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
if (bind(parentfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/* get us ready to accept connection requests */
if (listen(parentfd, 5) < 0) /* allow 5 requests to queue up */
error("ERROR on listen");
/* * main loop: wait for a connection request, parse HTTP,
* serve requested content, close connection.
*/
clientlen = sizeof(clientaddr);
while (1) {
/* wait for a connection request */
childfd = accept(parentfd, (struct sockaddr *) &clientaddr, &clientlen);
if (childfd < 0)
error("ERROR on accept");
/* determine who sent the message */
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
/* open the child socket descriptor as a stream */
if ((stream = fdopen(childfd, "r+")) == NULL)
error("ERROR on fdopen");
/* get the HTTP request line */
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
sscanf(buf, "%s %s %s\n", method, uri, version);
/* tiny only supports the GET method */
if (strcasecmp(method, "GET")) {
cerror(stream, method, "501", "Not Implemented",
"Tiny does not implement this method");
fclose(stream);
close(childfd);
continue;
}
/* read (and ignore) the HTTP headers */
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
while(strcmp(buf, "\r\n")) {
fgets(buf, BUFSIZE, stream);
printf("%s", buf);
}
/* parse the uri [crufty] */
if (!strstr(uri, "cgi-bin")) { /* static content */
is_static = 1;
strcpy(cgiargs, "");
strcpy(filename, ".");
strcat(filename, uri);
if (uri[strlen(uri)-1] == '/')
strcat(filename, "/sdcard");
}
/* else {
is_static = 0;
p = strchr(uri, '?');
if (p) {
strcpy(cgiargs, p+1);
*p = '\0';
}*/
else {
strcpy(cgiargs, "");
}
strcpy(filename, ".");
strcat(filename, uri);
// }
/* make sure the file exists */
if (stat(filename, &sbuf) < 0) {
cerror(stream, filename, "404", "Not found",
"Tiny couldn't find this file");
fclose(stream);
close(childfd);
continue;
}
/* serve static content */
if (is_static) {
if (strstr(filename, ".html"))
strcpy(filetype, "text/html");
else if (strstr(filename, ".gif"))
strcpy(filetype, "image/gif");
else if (strstr(filename, ".jpg"))
strcpy(filetype, "image/jpg");
else
strcpy(filetype, "text/plain");
/* print response header */
fprintf(stream, "HTTP/1.1 200 OK\n");
fprintf(stream, "Server: Tiny Web Server\n");
fprintf(stream, "Content-length: %d\n", (int)sbuf.st_size);
fprintf(stream, "Content-type: %s\n", filetype);
fprintf(stream, "\r\n");
fflush(stream);
/* Use mmap to return arbitrary-sized response body */
fd = open(filename, O_RDONLY);
p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
fwrite(p, 1, sbuf.st_size, stream);
munmap(p, sbuf.st_size);
}
/* serve dynamic content */
else {
/* make sure file is a regular executable file */
if (!(S_IFREG & sbuf.st_mode) || !(S_IXUSR & sbuf.st_mode)) {
cerror(stream, filename, "403", "Forbidden",
"You are not allow to access this item");
fclose(stream);
close(childfd);
continue;
}
/* a real server would set other CGI environ vars as well*/
setenv("QUERY_STRING", cgiargs, 1);
/* print first part of response header */
sprintf(buf, "HTTP/1.1 200 OK\n");
write(childfd, buf, strlen(buf));
sprintf(buf, "Server: Tiny Web Server\n");
write(childfd, buf, strlen(buf));
/* create and run the child CGI process so that all child
output to stdout and stderr goes back to the client via the
childfd socket descriptor */
pid = fork();
if (pid < 0) {
perror("ERROR in fork");
exit(1);
}
else if (pid > 0) { /* parent process */
wait(&wait_status);
}
else { /* child process*/
close(0); /* close stdin */
dup2(childfd, 1); /* map socket to stdout */
dup2(childfd, 2); /* map socket to stderr */
if (execve(filename, NULL, environ) < 0) {
perror("ERROR in execve");
}
}
}
/* clean up */
fclose(stream);
close(childfd);
}
}
Subscribe to:
Posts (Atom)

