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() {
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);
61 sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
64 sprintf(print_buffer, "%c#kauf Uhrzeit - Ding - Preis - Restguthbn - # - Nickname\r", 17);
67 sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
73 * Flushes the current log buffer to disk. Called either by log_file() when one
74 * entire log file is completed or interactively.
77 void log_flush(void) {
79 static char filename[8];
80 sprintf(filename, "log-%d", log_num);
82 /* If we have written to this logfile before, we need to remove it first */
83 if (log_heap_flushed > 0)
86 if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
87 c128_perror(c, "cbm_open(log)");
90 c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
91 if (c != log_heap_offset) {
92 textcolor(TC_LIGHT_RED);
93 cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), please make sure the floppy is not full!\n", c, log_heap_offset);
94 c128_perror(c, "cbm_write");
99 log_heap_flushed = log_heap_offset;
103 * Logs to memory and eventually to file (log_flush() is called when one entire
104 * logfile was completed in memory).
107 void log_file(const char *s) {
108 strcpy(log_heap_buf+log_heap_offset, s);
109 log_heap_offset += strlen(s);
111 /* Force a flush when there are only five lines left */
112 if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
116 log_heap_flushed = 0;