fixed
This commit is contained in:
parent
87df230ecb
commit
d0f2593887
2 changed files with 82 additions and 41 deletions
2
config.h
2
config.h
|
@ -1,5 +1,5 @@
|
||||||
/* Absolute path to */
|
/* Absolute path to */
|
||||||
#define ROOT "."
|
#define ROOT "8img_dir"
|
||||||
#define DB ROOT"/db"
|
#define DB ROOT"/db"
|
||||||
|
|
||||||
|
|
||||||
|
|
111
img.c
111
img.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
@ -24,6 +25,17 @@ struct UNIQ_TAGS {
|
||||||
size_t size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
char *create_path(const char *fmt, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
|
||||||
|
static char path[PATH_MAX + 1];
|
||||||
|
vsnprintf(path, sizeof(path) - 1, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
char *gen_id(const char *filename) {
|
char *gen_id(const char *filename) {
|
||||||
const char *alf = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
|
const char *alf = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
|
||||||
|
|
||||||
|
@ -76,7 +88,7 @@ void copy(const char *src, const char *dst) {
|
||||||
|
|
||||||
void add(char **list, const int size, const char *desc) {
|
void add(char **list, const int size, const char *desc) {
|
||||||
char *name = NULL;
|
char *name = NULL;
|
||||||
char new_path[PATH_MAX + 1];
|
char *new_path;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
name = gen_id(list[0]);
|
name = gen_id(list[0]);
|
||||||
|
@ -85,7 +97,7 @@ void add(char **list, const int size, const char *desc) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(new_path, sizeof(new_path), "%s/%s", ROOT, name);
|
new_path = create_path("%s/%s", ROOT, name);
|
||||||
|
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (stat(new_path, &sb) < 0)
|
if (stat(new_path, &sb) < 0)
|
||||||
|
@ -104,7 +116,6 @@ void add(char **list, const int size, const char *desc) {
|
||||||
|
|
||||||
/* Write "filename|tag1,tag2...|desc" to the db */
|
/* Write "filename|tag1,tag2...|desc" to the db */
|
||||||
fprintf(fp, "%s|", name);
|
fprintf(fp, "%s|", name);
|
||||||
|
|
||||||
if (size > 1) {
|
if (size > 1) {
|
||||||
for (int i = 1; i < size; i++) {
|
for (int i = 1; i < size; i++) {
|
||||||
if (strchr(list[i], ',') == NULL || strchr(list[i], ' ') == NULL)
|
if (strchr(list[i], ',') == NULL || strchr(list[i], ' ') == NULL)
|
||||||
|
@ -221,8 +232,7 @@ struct DB_STR *new_db_value(char *str) {
|
||||||
db->desc = strdup(tok);
|
db->desc = strdup(tok);
|
||||||
|
|
||||||
/* Stats */
|
/* Stats */
|
||||||
char new_path[PATH_MAX + 1];
|
char *new_path = create_path("%s/%s", ROOT, db->filename);
|
||||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, db->filename);
|
|
||||||
|
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
if (stat(new_path, &sb) < 0)
|
if (stat(new_path, &sb) < 0)
|
||||||
|
@ -250,13 +260,7 @@ struct DB_STR *read_db(void) {
|
||||||
struct DB_STR *db = NULL;
|
struct DB_STR *db = NULL;
|
||||||
struct DB_STR *i = NULL;
|
struct DB_STR *i = NULL;
|
||||||
|
|
||||||
while ((bytes = getline(&line, &n, fp))) {
|
while ((bytes = getline(&line, &n, fp)) > 0) {
|
||||||
if (bytes == -1)
|
|
||||||
break;
|
|
||||||
|
|
||||||
else if (bytes <= 1)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
db = new_db_value(line);
|
db = new_db_value(line);
|
||||||
if (db == NULL)
|
if (db == NULL)
|
||||||
goto READ_DB_CLOSE;
|
goto READ_DB_CLOSE;
|
||||||
|
@ -343,8 +347,7 @@ struct DB_STR *build_html_page(FILE *fp, const char *file, const struct UNIQ_TAG
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
char new_path[PATH_MAX + 1];
|
char *new_path = create_path("%s-%zu.html", file, i);
|
||||||
snprintf(new_path, sizeof(new_path), "%s-%zu.html", file, i);
|
|
||||||
|
|
||||||
if (i == page)
|
if (i == page)
|
||||||
fprintf(fp, "<a id='curr' href='%s'>%zu</a> ", new_path, i);
|
fprintf(fp, "<a id='curr' href='%s'>%zu</a> ", new_path, i);
|
||||||
|
@ -369,7 +372,7 @@ void build_html(const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db)
|
||||||
while (p != NULL) {
|
while (p != NULL) {
|
||||||
struct DB_STR *i = p->next;
|
struct DB_STR *i = p->next;
|
||||||
|
|
||||||
if (count >= POST_PER_PAGE) {
|
if (count > POST_PER_PAGE) {
|
||||||
count = 0;
|
count = 0;
|
||||||
pages++;
|
pages++;
|
||||||
}
|
}
|
||||||
|
@ -388,12 +391,12 @@ void build_html(const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db)
|
||||||
|
|
||||||
p = db;
|
p = db;
|
||||||
while (1) {
|
while (1) {
|
||||||
char new_path[PATH_MAX + 1];
|
char *new_path;
|
||||||
if (page == 0)
|
if (page == 0)
|
||||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s.html", ROOT, file);
|
new_path = create_path("%s/%s.html", ROOT, file);
|
||||||
|
|
||||||
else
|
else
|
||||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s-%zu.html", ROOT, file, page);
|
new_path = create_path("%s/%s-%zu.html", ROOT, file, page);
|
||||||
|
|
||||||
FILE *fp = file_open(new_path, "w");
|
FILE *fp = file_open(new_path, "w");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
|
@ -413,8 +416,7 @@ void build_html(const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db)
|
||||||
|
|
||||||
|
|
||||||
void build_xml(const char *file, struct DB_STR *db) {
|
void build_xml(const char *file, struct DB_STR *db) {
|
||||||
char new_path[PATH_MAX + 1];
|
char *new_path = create_path("%s/%s", ROOT, file);
|
||||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, file);
|
|
||||||
|
|
||||||
FILE *fp = file_open(new_path, "w");
|
FILE *fp = file_open(new_path, "w");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
|
@ -497,39 +499,75 @@ char **uniq(struct DB_STR *db, size_t *uniq_size) {
|
||||||
void rebuild(void) {
|
void rebuild(void) {
|
||||||
struct DB_STR *db = read_db();
|
struct DB_STR *db = read_db();
|
||||||
if (db == NULL)
|
if (db == NULL)
|
||||||
return;
|
exit(1);
|
||||||
|
|
||||||
/* Uniq */
|
/* Uniq */
|
||||||
size_t uniq_size = 0;
|
size_t uniq_size = 0;
|
||||||
char **uniq_tag = uniq(db, &uniq_size);
|
char **uniq_tag = uniq(db, &uniq_size);
|
||||||
if (uniq_tag != NULL) {
|
if (uniq_tag == NULL) {
|
||||||
struct UNIQ_TAGS ut = {.tags = uniq_tag, .size = uniq_size};
|
fprintf(stderr, "8img: malloc: %s\n", strerror(errno));
|
||||||
|
free_db(db);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct UNIQ_TAGS ut = {.tags = uniq_tag, .size = uniq_size};
|
||||||
for (size_t i = 0; i < uniq_size; i++)
|
for (size_t i = 0; i < uniq_size; i++)
|
||||||
build_html(uniq_tag[i], ut, db);
|
build_html(uniq_tag[i], ut, db);
|
||||||
|
|
||||||
build_html("index", ut, db);
|
build_html("index", ut, db);
|
||||||
build_xml(XML_FILE, db);
|
build_xml(XML_FILE, db);
|
||||||
}
|
|
||||||
|
|
||||||
else
|
|
||||||
fprintf(stderr, "8img: malloc: %s\n", strerror(errno));
|
|
||||||
|
|
||||||
/* Close */
|
/* Close */
|
||||||
if (uniq_tag != NULL)
|
|
||||||
free(uniq_tag);
|
free(uniq_tag);
|
||||||
|
|
||||||
free_db(db);
|
free_db(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
void help(void) {
|
void help(int ret) {
|
||||||
puts("8img -[d:] [add/rebuild/version]:\n\tadd [img] [tag1] [tag2]...\n\trebuild\n\tversion\nOptions:\n\t-d STR set description");
|
puts("8img -[d:] [add/rebuild/version/del]:\n\tadd [img] [tag1] [tag2]...\n\trebuild\n\tversion\n\tdel [image id]\nOptions:\n\t-d STR set description");
|
||||||
exit(0);
|
exit(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void del(const char *file) {
|
||||||
|
if (file == NULL)
|
||||||
|
help(1);
|
||||||
|
|
||||||
|
struct DB_STR *db = read_db();
|
||||||
|
if (db == NULL)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
FILE *out = fopen(DB, "w");
|
||||||
|
if (out == NULL) {
|
||||||
|
free_db(db);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DB_STR *bckp = db;
|
||||||
|
while (bckp != NULL) {
|
||||||
|
struct DB_STR *ptr = bckp->next;
|
||||||
|
|
||||||
|
if (!strcmp(file, bckp->filename)) {
|
||||||
|
char *path = create_path("%s/%s", ROOT, bckp->filename);
|
||||||
|
remove(path);
|
||||||
|
|
||||||
|
bckp = ptr;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(out, "%s|", bckp->filename);
|
||||||
|
for (size_t i = 0; i < bckp->size; i++)
|
||||||
|
fprintf(out, "%s%c", bckp->tags[i], (i == bckp->size - 1) ? '|' : ',');
|
||||||
|
|
||||||
|
fprintf(out, "%s\n", bckp->desc);
|
||||||
|
bckp = ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_db(db);
|
||||||
|
fclose(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
help();
|
help(0);
|
||||||
|
|
||||||
mkdir(ROOT, 0777);
|
mkdir(ROOT, 0777);
|
||||||
srand(getpid());
|
srand(getpid());
|
||||||
|
@ -544,7 +582,7 @@ int main(int argc, char **argv) {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
help();
|
help(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -555,6 +593,9 @@ int main(int argc, char **argv) {
|
||||||
if (!strcmp(argv[0], "add") && argc > 1)
|
if (!strcmp(argv[0], "add") && argc > 1)
|
||||||
add(argv + 1, argc - 1, desc);
|
add(argv + 1, argc - 1, desc);
|
||||||
|
|
||||||
|
else if (!strcmp(argv[0], "del"))
|
||||||
|
del(*(argv + 1));
|
||||||
|
|
||||||
else if (!strcmp(argv[0], "rebuild"))
|
else if (!strcmp(argv[0], "rebuild"))
|
||||||
rebuild();
|
rebuild();
|
||||||
|
|
||||||
|
@ -562,7 +603,7 @@ int main(int argc, char **argv) {
|
||||||
puts("8img version: 1.1\nWritten under WTFPL License.");
|
puts("8img version: 1.1\nWritten under WTFPL License.");
|
||||||
|
|
||||||
else
|
else
|
||||||
help();
|
help(1);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue