このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
hozonsite.c/prep.c

80 行
1.6 KiB
C

#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);
}