2 * © 2007-2009 phil_fry, sECuRE, sur5r
3 * See LICENSE for license information
16 /* NOTE: undocumented function which scratches files
17 We need to use this function because linking unistd.h
18 makes our program break at runtime.
20 unsigned char __fastcall__ _sysremove(const char *name);
22 /* 8192 bytes of log buffer on the heap used for storing one entire logfile in
23 * memory before writing (unless flushed). */
25 int log_heap_offset = 0;
26 int log_heap_flushed = 0;
28 const int LOG_SIZE = 8192;
31 log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
32 if (log_heap_buf == NULL) {
33 cprintf("malloc(log_heap_buf) failed");
38 void print_the_buffer(void) {
41 c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
43 c128_perror(c, "cbm_open(printer)");
44 if (retry_or_quit() == 'q')
49 c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
50 if (c != strlen(print_buffer)) {
51 c128_perror(c, "write(printer)");
52 if (retry_or_quit() == 'q')
57 log_file(print_buffer);
60 void print_header(void) {
61 sprintf(print_buffer, "%c----------------------------------------------------"
62 "----------------------------\r",
68 "%c#kauf Uhrzeit - Ding - Preis - Restguthbn - # - Nickname\r",
72 sprintf(print_buffer, "%c----------------------------------------------------"
73 "----------------------------\r",
79 * Flushes the current log buffer to disk. Called either by log_file() when one
80 * entire log file is completed or interactively.
83 void log_flush(void) {
85 static char filename[8];
86 sprintf(filename, "log-%d", log_num);
88 /* If we have written to this logfile before, we need to remove it first */
89 if (log_heap_flushed > 0)
92 if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
93 c128_perror(c, "cbm_open(log)");
96 c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
97 if (c != log_heap_offset) {
98 textcolor(TC_LIGHT_RED);
99 cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), "
100 "please make sure the floppy is not full!\n",
102 c128_perror(c, "cbm_write");
107 log_heap_flushed = log_heap_offset;
111 * Logs to memory and eventually to file (log_flush() is called when one entire
112 * logfile was completed in memory).
115 void log_file(const char *s) {
116 strcpy(log_heap_buf + log_heap_offset, s);
117 log_heap_offset += strlen(s);
119 /* Force a flush when there are only five lines left */
120 if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
124 log_heap_flushed = 0;