このコミットが含まれているのは:
守矢諏訪子 2023-05-01 13:42:44 +09:00
コミット 082a899d24
12個のファイルの変更279行の追加134行の削除

ファイルの表示

@ -2,7 +2,7 @@ VERSION = 1.0.0
PREFIX = /usr # Linux
PREFIX = /usr/local # BSD
MANPREFIX = ${PREFIX}/share/man
SRC = main.c
SRC = main.c stringex.c prep.c pager.c srv.c
HDR = config.h
OBJ = ${SRC:.c=.o}
CC = cc
@ -12,7 +12,7 @@ STD = c99 # Linux
#INCS = -I
LIBS = -lcurl
CPPFLAGS = -DVERSION=\"${VERSION}\"
CPPFLAGS = -DVERSION=\"${VERSION}\" -D_POSIX_C_SOURCE=200809L
CFLAGS = -std=${STD} -Os ${INCS} ${CPPFLAGS}
LDFLAGS = ${LIBS}

バイナリ
favicon.ico ノーマルファイル

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 15 KiB

バイナリ
git.png ノーマルファイル

バイナリファイルは表示されません。

変更後

幅:  |  高さ:  |  サイズ: 8.3 KiB

139
main.c
ファイルの表示

@ -1,142 +1,17 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <curl/curl.h>
#include <time.h>
#include <sys/stat.h>
//#include <libxml/tree.h>
#include <stdlib.h>
//#include <errno.h>
#include<unistd.h>
// http
// regex
#include "prep.h"
#include "pager.h"
#include "srv.h"
// http://かhttps://で始まるかどうか
bool checkprefix (const char *url) {
return strncmp("http://", url, 7) == 0 || strncmp("https://", url, 8) == 0;
}
void mkdirs (char *path, int perm) {
char **res = NULL;
char *subpath = strtok(path, "/");
int nsla = 0, i;
while (subpath) {
res = realloc(res, sizeof(char*) * ++nsla);
if (res == NULL) exit(-1);
res[nsla-1] = subpath;
subpath = strtok(NULL, "/");
}
res = realloc (res, sizeof (char*) * (nsla+1));
res[nsla] = 0;
for (i = 0; i < (nsla+1); ++i) {
mkdir(res[i], perm);
chdir(res[i]);
}
free(res);
}
char *preparepage (char *url) {
// URLは「/」で終わなければ、追加
char *ext = url + strlen(url)-1;
char surl[strlen(url)+1];
strncpy(surl, url, sizeof(surl));
if (!strcmp(ext, "/") == 0) {
strcat(surl, "/");
}
else {
strcat(surl, "");
}
// タイムスタンプ
int timestamp = time(NULL);
char strtimestamp[10];
// パス
int sublen;
if (strncmp("http://", url, 7) == 0) sublen = 6;
else if (strncmp("https://", url, 8) == 0) sublen = 7;
int reslen = strlen(surl);
char nurl[reslen];
int j = 0;
int i = 0;
while (surl[i] != '\0') {
i++;
if (i >= sublen) {
nurl[j] = surl[i];
j++;
}
}
// タイムスタンプはintからchar *に交換
sprintf(strtimestamp, "%d", timestamp);
// パスの創作
char *path = malloc(strlen("archive/") + strlen(strtimestamp) + strlen(nurl));
strcpy(path, "archive/");
strcat(path, strtimestamp);
strcat(path, nurl);
// フォルダの創作
char mpath[strlen(path)];
strcpy(mpath, path);
mkdirs(mpath, 0755);
return(path);
}
// TODO
void scanpage (char *url) {}
int getpage (char *url, char *path) {
char fpath[strlen(path) + strlen("index.html")];
strcpy(fpath, path);
strcat(fpath, "index.html");
//strncpy(fpath, path, sizeof(fpath));
//strncat(fpath, "index.html", sizeof(fpath));
printf("FPATH: %s\n", fpath);
FILE* file = fopen(fpath, "w");
CURL* curl = curl_easy_init();
CURLcode curlres;
// ウェブページを読み込む
if (curl) {
// CURLの実行
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
// リターンコード
curlres = curl_easy_perform(curl);
// エラーの確認
if (curlres != CURLE_OK) {
fprintf(stderr, "curl_easy_perform()の実行に失敗しました。 %s\n", curl_easy_strerror(curlres));
return(-1);
}
// 掃除
curl_easy_cleanup(curl);
// TODO: JS、動画等の削除
// TODO: CSS、画像等ファイルのダウンロード
// TODO: 全部/usr/(local/)share/hozonsite/archiveに保存する
return(0);
}
return(-1);
}
// TODO
void startsrv (int port) {
printf("http://127.0.0.1:%d でサーバーを実行中。終了するには、CTRL+Cを押して下さい。\n", port);
}
int main (int argc, char *argv[]) {
if (argc == 2) {
if (!strcmp("-v", argv[1])) {
@ -149,9 +24,9 @@ int main (int argc, char *argv[]) {
}
else {
if (checkprefix(argv[1]) == 1) {
printf("archive\n");
char * path = preparepage(argv[1]);
getpage(argv[1], path);
char * path = preparepage(argv[1]); // mkdir等
getpage(argv[1], path); // ページの保存
scanpage(argv[1], path);
free(path);
}
else {

124
pager.c ノーマルファイル
ファイルの表示

@ -0,0 +1,124 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
#include <unistd.h>
#include "stringex.h"
char* scanurl (char *url, int resnum) {
char *res;
char *pch;
int i = 0;
pch = strtok(url, ":/");
while (pch != NULL) {
i++;
if (i == resnum) res = pch;
pch = strtok(NULL, ":/");
}
return(res);
}
// TODO
// TODO: 画像等ファイルのダウンロード
// TODO: JS、動画等の削除
// TODO: 全部/usr/(local/)share/hozonsite/archiveに保存する
void scanpage (char *url, char *path) {
chdir(path);
FILE *file = fopen("index.html", "r");
char line[2000];
char *furl = url;
// URLの修正
char *rurl = scanurl(url, 2);
while (fgets(line, sizeof(line), file)) {
// TODO: JS
if (strstr(line, ".js")) {
printf("JS: %s\n", line);
}
if (strstr(line, "<script")) {
printf("スクリプト: %s\n", line);
}
// TODO: 画像
// CSS
if (strstr(line, ".css")) {
char* cut = str_split(line);
// パスの創作
char *link = malloc(strlen(url) + strlen("://") + strlen(rurl) + strlen(cut));
strcpy(link, url);
strcat(link, "://");
strcat(link, rurl);
strcat(link, cut);
// ファイル名
char *fn;
char *pchh;
pchh = strtok(cut, "/");
while (pchh != NULL) {
if (pchh != NULL) fn = pchh;
pchh = strtok(NULL, "/");
}
FILE *file2 = fopen(fn, "w");
CURL* curl = curl_easy_init();
CURLcode curlres;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, link);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file2);
curlres = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(file2);
}
}
}
fclose(file);
}
int getpage (char *url, char *path) {
// フォルダに移動すると、index.htmlとして保存する
chdir(path);
FILE *file = fopen("index.html", "w");
if (file == NULL) {
// ファイルを開けなければ、エラーが出しますと、終了
perror("fopenエラー。");
return(-1);
}
CURL* curl = curl_easy_init();
CURLcode curlres;
// ウェブページを読み込む
if (curl) {
// CURLの実行
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
// リターンコード
curlres = curl_easy_perform(curl);
// エラーの確認
if (curlres != CURLE_OK) {
fprintf(stderr, "curl_easy_perform()の実行に失敗しました。 %s\n", curl_easy_strerror(curlres));
return(-1);
}
// 掃除
curl_easy_cleanup(curl);
fclose(file);
// 性効!!あ、ごめん…成功!!
return(0);
}
return(-1);
}

7
pager.h ノーマルファイル
ファイルの表示

@ -0,0 +1,7 @@
#ifndef PAGER_H
#define PAGER_H
void scanpage (char *url, char *path);
int getpage (char *url, char *path);
#endif

79
prep.c ノーマルファイル
ファイルの表示

@ -0,0 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
void mkdirs (char *path, int perm) {
char **res = NULL;
char *subpath = strtok(path, "/");
int n = 0, i;
while (subpath) {
res = realloc(res, sizeof(char*) * ++n);
if (res == NULL) exit(-1);
res[n-1] = subpath;
subpath = strtok(NULL, "/");
}
res = realloc(res, sizeof (char*) * (n+1));
res[n] = 0;
for (i = 0; i < (n+1); ++i) {
mkdir(res[i], perm);
chdir(res[i]);
}
free(res);
}
char *preparepage (char *url) {
// URLは「/」で終わなければ、追加
char *ext = url + strlen(url)-1;
char surl[strlen(url)+1];
strncpy(surl, url, sizeof(surl));
if (!strcmp(ext, "/") == 0) {
strcat(surl, "/");
}
else {
strcat(surl, "");
}
// タイムスタンプ
int timestamp = time(NULL);
char strtimestamp[10];
// パス
int sublen;
if (strncmp("http://", url, 7) == 0) sublen = 6;
else if (strncmp("https://", url, 8) == 0) sublen = 7;
int reslen = strlen(surl);
char nurl[reslen];
int j = 0;
int i = 0;
while (surl[i] != '\0') {
i++;
if (i >= sublen) {
nurl[j] = surl[i];
j++;
}
}
// タイムスタンプはintからchar *に交換
sprintf(strtimestamp, "%d", timestamp);
// パスの創作
char *path = malloc(strlen("archive/") + strlen(strtimestamp) + strlen(nurl));
strcpy(path, "archive/");
strcat(path, strtimestamp);
strcat(path, nurl);
// フォルダの創作
char mpath[strlen(path)];
strcpy(mpath, path);
mkdirs(mpath, 0755);
return(path);
}

7
prep.h ノーマルファイル
ファイルの表示

@ -0,0 +1,7 @@
#ifndef PREP_H
#define PREP_H
void mkdirs (char *path, int perm);
char *preparepage (char *url);
#endif

6
srv.c ノーマルファイル
ファイルの表示

@ -0,0 +1,6 @@
#include <stdio.h>
// TODO
void startsrv (int port) {
printf("http://127.0.0.1:%d でサーバーを実行中。終了するには、CTRL+Cを押して下さい。\n", port);
}

6
srv.h ノーマルファイル
ファイルの表示

@ -0,0 +1,6 @@
#ifndef SRV_H
#define SRV_H
void startsrv (int port);
#endif

32
stringex.c ノーマルファイル
ファイルの表示

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "stringex.h"
bool str_begin (const char *a, const char *b) {
return strncmp(a, b, strlen(b)) == 0;
}
char* str_split (char *str) {
char *res;
char *pch;
pch = strtok(str, " <>");
while (pch != NULL) {
if (str_begin(pch, "href=")) {
char *pch2;
pch2 = strtok(pch, "=\"'");
while (pch2 != NULL) {
if (pch2 != "href") {
res = pch2;
}
pch2 = strtok(NULL, "=\"'");
}
}
pch = strtok(NULL, " <>");
}
return(res);
}

9
stringex.h ノーマルファイル
ファイルの表示

@ -0,0 +1,9 @@
#ifndef STRINGEX_H
#define STRINGEX_H
#include <stdbool.h>
bool str_begin (const char *a, const char *b);
char* str_split(char *a_str);
#endif