~ysu/gemini-chatroom

ref: 91028756563599c78745ae4d5447ab09b88a7403 gemini-chatroom/bin/chat0/view.c -rw-r--r-- 1.2 KiB
91028756 — vps rewrote in c 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>

#include "config.h"

char *
fgetsr(char *str, unsigned int size, FILE *fp)	/* fgets, reversed */
{
	/* Stop if we're at the beginning of the file */
	if (ftell(fp) == 0)
		return NULL;

	unsigned int i;
	for (i = 0; ftell(fp) != 0 && i < size; i++ ) {
		fseek(fp, -1, SEEK_CUR);
		str[i] = (char)fgetc(fp);

		if (str[i] == '\n' && i != 0 ) {
		    break;
		}

		fseek(fp, -1, SEEK_CUR);
	}
	str[i] = '\0';
	return str;
}

void
reverseStr(char *start)
{
	size_t len = strlen(start);

	for (char *end = &start[len - 1]; start < end; start++, end--) {
		char tmp = start[0];
		start[0] = end[0];
		end[0] = tmp;
	}
}

int
main()
{
	/* open file and navigate to the end */
	FILE *ml = fopen("message_log", "r");	/* message log */
	fseek(ml, 0, SEEK_END);
 
	/* write links */
	puts("20 text/gemini\r");
	puts("# Chatroom 0");
	puts("=> postuser post");
	puts("=> post post anonymously");
	puts("=> /bin/chat"CHAT_NUM"/ refresh\n");

	/* print file backwards */
	char buff[BUFF_SIZE];
	puts("---");
	puts("``` messages");
	while (fgetsr(buff, BUFF_SIZE, ml) != NULL) {
		reverseStr(buff);
		printf("%s", buff);
	}
	fclose(ml);
	puts("```");
	puts("---");
}