C 게시판 소스
ChatGPT
C 언어와 CGI를 이용한 간단한 게시판(Bulletin Board System) 예제를 제공하겠습니다. 이 예제는 글 작성, 글 목록 보기, 글 상세 보기를 포함합니다. 데이터는 텍스트 파일을 사용하여 저장합니다.
1. 환경 설정
1.1 개발 환경 준비
C 컴파일러: GCC 사용을 권장합니다. 대부분의 Unix 시스템에서 기본적으로 설치되어 있습니다.
웹 서버: Apache 웹 서버를 사용합니다. CGI 모듈이 활성화되어 있어야 합니다.
1.2 Apache 설정 (Linux 기준)
Apache 설정 파일 httpd.conf 또는 apache2.conf에서 CGI 스크립트를 활성화해야 합니다:
apache
코드 복사
ScriptAlias /cgi-bin/ "/usr/lib/cgi-bin/"
<Directory "/usr/lib/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl .out
</Directory>
2. 파일 구조
bash
코드 복사
/bbs
/cgi-bin
bbs.cgi
/data
posts.txt
/templates
index.html
post.html
create.html
3. bbs.cgi - CGI 스크립트
c코드 복사
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DATA_FILE "../data/posts.txt"
#define BUFFER_SIZE 1024
void list_posts();
void view_post(const char *id);
void create_post(const char *title, const char *content);
void show_create_form();
int main() {
char *query_string = getenv("QUERY_STRING");
char *request_method = getenv("REQUEST_METHOD");
printf("Content-type: text/html\n\n");
if (query_string == NULL) {
list_posts();
} else if (strstr(query_string, "action=view") != NULL) {
char *id = strstr(query_string, "id=") + 3;
view_post(id);
} else if (strstr(query_string, "action=create") != NULL) {
if (strcmp(request_method, "POST") == 0) {
char title[BUFFER_SIZE] = {0};
char content[BUFFER_SIZE] = {0};
fgets(title, BUFFER_SIZE, stdin);
fgets(content, BUFFER_SIZE, stdin);
create_post(title, content);
} else {
show_create_form();
}
} else {
list_posts();
}
return 0;
}void list_posts() {
FILE *file = fopen(DATA_FILE, "r");
char line[BUFFER_SIZE];
if (file == NULL) {
printf("<h1>Error opening data file</h1>");
return;
}
printf("<html><head><title>글 목록</title></head><body>");
printf("<h1>글 목록</h1><ul>");
while (fgets(line, sizeof(line), file)) {
char *id = strtok(line, "::");
char *title = strtok(NULL, "::");
char *created_at = strtok(NULL, "::");
printf("<li><a href=\"?action=view&id=%s\">%s</a> (%s)</li>", id, title, created_at);
}
fclose(file);
printf("</ul><a href=\"?action=create\">새 글 쓰기</a></body></html>");
}void view_post(const char *id) {
FILE *file = fopen(DATA_FILE, "r");
char line[BUFFER_SIZE];
if (file == NULL) {
printf("<h1>Error opening data file</h1>");
return;
}
while (fgets(line, sizeof(line), file)) {
char *post_id = strtok(line, "::");
if (strcmp(post_id, id) == 0) {
char *title = strtok(NULL, "::");
char *content = strtok(NULL, "::");
char *created_at = strtok(NULL, "::");
printf("<html><head><title>%s</title></head><body>", title);
printf("<h1>%s</h1><p>%s</p><small>Posted on %s</small>", title, content, created_at);
printf("<p><a href=\"/cgi-bin/bbs.cgi\">뒤로</a></p></body></html>");
break;
}
}
fclose(file);
}void create_post(const char *title, const char *content) {
FILE *file = fopen(DATA_FILE, "a");
if (file == NULL) {
printf("<h1>Error opening data file</h1>");
return;
}
time_t now = time(NULL);
char *created_at = ctime(&now);
created_at[strlen(created_at) - 1] = '\0'; // Remove newline character
fprintf(file, "%ld::%s::%s::%s\n", now, title, content, created_at);
fclose(file);
printf("<meta http-equiv=\"refresh\" content=\"0; url=/cgi-bin/bbs.cgi\">");
}void show_create_form() {
printf("<html><head><title>새 글 쓰기</title></head><body>");
printf("<h1>새 글 쓰기</h1>");
printf("<form method=\"post\" action=\"/cgi-bin/bbs.cgi?action=create\">");
printf("<label for=\"title\">제목:</label><input type=\"text\" id=\"title\" name=\"title\" required><br>");
printf("<label for=\"content\">내용:</label><textarea id=\"content\" name=\"content\" required></textarea><br>");
printf("<button type=\"submit\">글 작성</button></form>");
printf("<p><a href=\"/cgi-bin/bbs.cgi\">뒤로</a></p></body></html>");
}4. index.html - 글 목록 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>글 목록</title>
</head>
<body>
<h1>글 목록</h1>
<ul>
5. post.html - 글 상세 보기 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
<p>{content}</p>
<small>Posted on {created_at}</small>
<p><a href="/cgi-bin/bbs.cgi">뒤로</a></p>
</body>
</html>
6. create.html - 글 작성 페이지 템플릿
html
코드 복사
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>새 글 쓰기</title>
</head>
<body>
<h1>새 글 쓰기</h1>
<form method="post" action="/cgi-bin/bbs.cgi?action=create">
<label for="title">제목:</label>
<input type="text" id="title" name="title" required>
<br>
<label for="content">내용:</label>
<textarea id="content" name="content" required></textarea>
<br>
<button type="submit">글 작성</button>
</form>
<p><a href="/cgi-bin/bbs.cgi">뒤로</a></p>
</body>
</html>
7. posts.txt - 데이터 파일
이 파일은 처음에는 비어 있어야 하며, 각 게시글은 아래 형식으로 저장됩니다:
arduino
코드 복사
id::title::content::created_at
8. 실행 방법
파일 배치: bbs.cgi 스크립트를 웹 서버의 cgi-bin 디렉토리에 복사합니다.
파일 권한 설정: CGI 스크립트에 실행 권한을 부여합니다.
bash
코드 복사
chmod +x /path/to/cgi-bin/bbs.cgi
웹 브라우저: 웹 브라우저에서 http://localhost/cgi-bin/bbs.cgi를 엽니다.
테스트: 게시판을 사용하여 글을 작성하고, 목록을 보고, 상세 보기를 테스트합니다.
참고
이 게시판 예제는 매우 기본적인 기능만을 제공하며, 실제 사용 시에는 보안 및 기능 확장 등을 고려해야 합니다.
데이터 저장을 위한 텍스트 파일 대신 SQLite 또는 MySQL과 같은 데이터베이스를 사용하는 것이 더 좋습니다. 이는 데이터 무결성과 확장성을 보장합니다.
CGI는 최신 웹 개발에서 다소 구식으로 간주되며, FastCGI 또는 웹 프레임워크를 사용하는 것이 더 나은 선택일 수 있습니다.