fixed
This commit is contained in:
parent
87df230ecb
commit
d0f2593887
2 changed files with 82 additions and 41 deletions
121
img.c
121
img.c
|
@ -1,5 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
@ -24,6 +25,17 @@ struct UNIQ_TAGS {
|
|||
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) {
|
||||
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) {
|
||||
char *name = NULL;
|
||||
char new_path[PATH_MAX + 1];
|
||||
char *new_path;
|
||||
|
||||
while (1) {
|
||||
name = gen_id(list[0]);
|
||||
|
@ -85,7 +97,7 @@ void add(char **list, const int size, const char *desc) {
|
|||
exit(1);
|
||||
}
|
||||
|
||||
snprintf(new_path, sizeof(new_path), "%s/%s", ROOT, name);
|
||||
new_path = create_path("%s/%s", ROOT, name);
|
||||
|
||||
struct stat sb;
|
||||
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 */
|
||||
fprintf(fp, "%s|", name);
|
||||
|
||||
if (size > 1) {
|
||||
for (int i = 1; i < size; i++) {
|
||||
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);
|
||||
|
||||
/* Stats */
|
||||
char new_path[PATH_MAX + 1];
|
||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, db->filename);
|
||||
char *new_path = create_path("%s/%s", ROOT, db->filename);
|
||||
|
||||
struct stat sb;
|
||||
if (stat(new_path, &sb) < 0)
|
||||
|
@ -250,13 +260,7 @@ struct DB_STR *read_db(void) {
|
|||
struct DB_STR *db = NULL;
|
||||
struct DB_STR *i = NULL;
|
||||
|
||||
while ((bytes = getline(&line, &n, fp))) {
|
||||
if (bytes == -1)
|
||||
break;
|
||||
|
||||
else if (bytes <= 1)
|
||||
continue;
|
||||
|
||||
while ((bytes = getline(&line, &n, fp)) > 0) {
|
||||
db = new_db_value(line);
|
||||
if (db == NULL)
|
||||
goto READ_DB_CLOSE;
|
||||
|
@ -343,8 +347,7 @@ struct DB_STR *build_html_page(FILE *fp, const char *file, const struct UNIQ_TAG
|
|||
}
|
||||
|
||||
else {
|
||||
char new_path[PATH_MAX + 1];
|
||||
snprintf(new_path, sizeof(new_path), "%s-%zu.html", file, i);
|
||||
char *new_path = create_path("%s-%zu.html", file, i);
|
||||
|
||||
if (i == page)
|
||||
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) {
|
||||
struct DB_STR *i = p->next;
|
||||
|
||||
if (count >= POST_PER_PAGE) {
|
||||
if (count > POST_PER_PAGE) {
|
||||
count = 0;
|
||||
pages++;
|
||||
}
|
||||
|
@ -388,12 +391,12 @@ void build_html(const char *file, const struct UNIQ_TAGS ut, struct DB_STR *db)
|
|||
|
||||
p = db;
|
||||
while (1) {
|
||||
char new_path[PATH_MAX + 1];
|
||||
char *new_path;
|
||||
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
|
||||
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");
|
||||
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) {
|
||||
char new_path[PATH_MAX + 1];
|
||||
snprintf(new_path, sizeof(new_path) - 1, "%s/%s", ROOT, file);
|
||||
char *new_path = create_path("%s/%s", ROOT, file);
|
||||
|
||||
FILE *fp = file_open(new_path, "w");
|
||||
if (fp == NULL)
|
||||
|
@ -497,39 +499,75 @@ char **uniq(struct DB_STR *db, size_t *uniq_size) {
|
|||
void rebuild(void) {
|
||||
struct DB_STR *db = read_db();
|
||||
if (db == NULL)
|
||||
return;
|
||||
exit(1);
|
||||
|
||||
/* Uniq */
|
||||
size_t uniq_size = 0;
|
||||
char **uniq_tag = uniq(db, &uniq_size);
|
||||
if (uniq_tag != NULL) {
|
||||
struct UNIQ_TAGS ut = {.tags = uniq_tag, .size = uniq_size};
|
||||
|
||||
for (size_t i = 0; i < uniq_size; i++)
|
||||
build_html(uniq_tag[i], ut, db);
|
||||
|
||||
build_html("index", ut, db);
|
||||
build_xml(XML_FILE, db);
|
||||
if (uniq_tag == NULL) {
|
||||
fprintf(stderr, "8img: malloc: %s\n", strerror(errno));
|
||||
free_db(db);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
else
|
||||
fprintf(stderr, "8img: malloc: %s\n", strerror(errno));
|
||||
struct UNIQ_TAGS ut = {.tags = uniq_tag, .size = uniq_size};
|
||||
for (size_t i = 0; i < uniq_size; i++)
|
||||
build_html(uniq_tag[i], ut, db);
|
||||
|
||||
build_html("index", ut, db);
|
||||
build_xml(XML_FILE, db);
|
||||
|
||||
/* Close */
|
||||
if (uniq_tag != NULL)
|
||||
free(uniq_tag);
|
||||
|
||||
free(uniq_tag);
|
||||
free_db(db);
|
||||
}
|
||||
|
||||
void help(void) {
|
||||
puts("8img -[d:] [add/rebuild/version]:\n\tadd [img] [tag1] [tag2]...\n\trebuild\n\tversion\nOptions:\n\t-d STR set description");
|
||||
exit(0);
|
||||
void help(int ret) {
|
||||
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(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) {
|
||||
if (argc < 2)
|
||||
help();
|
||||
help(0);
|
||||
|
||||
mkdir(ROOT, 0777);
|
||||
srand(getpid());
|
||||
|
@ -544,7 +582,7 @@ int main(int argc, char **argv) {
|
|||
break;
|
||||
|
||||
default:
|
||||
help();
|
||||
help(1);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -555,6 +593,9 @@ int main(int argc, char **argv) {
|
|||
if (!strcmp(argv[0], "add") && argc > 1)
|
||||
add(argv + 1, argc - 1, desc);
|
||||
|
||||
else if (!strcmp(argv[0], "del"))
|
||||
del(*(argv + 1));
|
||||
|
||||
else if (!strcmp(argv[0], "rebuild"))
|
||||
rebuild();
|
||||
|
||||
|
@ -562,7 +603,7 @@ int main(int argc, char **argv) {
|
|||
puts("8img version: 1.1\nWritten under WTFPL License.");
|
||||
|
||||
else
|
||||
help();
|
||||
help(1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue