2 * © 2007-2009 phil_fry, sECuRE, sur5r
3 * See LICENSE for license information
17 /* NOTE: undocumented function which scratches files
18 We need to use this function because linking unistd.h
19 makes our program break at runtime.
21 unsigned char __fastcall__ _sysremove(const char *name);
23 /* 8192 bytes of log buffer on the heap used for storing one entire logfile in
24 * memory before writing (unless flushed). */
26 int log_heap_offset = 0;
27 int log_heap_flushed = 0;
29 const int LOG_SIZE = 8192;
32 log_heap_buf = malloc(sizeof(char) * LOG_SIZE);
33 if (log_heap_buf == NULL) {
34 cprintf("malloc(log_heap_buf) failed");
39 void print_the_buffer(void) {
47 status = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL);
49 c128_perror(status, "cbm_open(printer)");
52 cprintf("\r\nr)etry, c)ontinue or q)uit?\r\n");
54 } while ((*c != 'r') && (*c != 'c') && (*c != 'q'));
65 status = cbm_write((BYTE)4, print_buffer, strlen(print_buffer));
66 if (status != strlen(print_buffer)) {
67 c128_perror(status, "write(printer)");
68 if (retry_or_quit() == 'q')
73 log_file(print_buffer);
76 void print_header(void) {
77 sprintf(print_buffer, "%c----------------------------------------------------"
78 "----------------------------\r",
84 "%c#kauf Uhrzeit - Ding - Preis - Restguthbn - # - Nickname\r",
88 sprintf(print_buffer, "%c----------------------------------------------------"
89 "----------------------------\r",
95 * Flushes the current log buffer to disk. Called either by log_file() when one
96 * entire log file is completed or interactively.
99 void log_flush(void) {
101 static char filename[8];
102 sprintf(filename, "log-%d", log_num);
104 /* If we have written to this logfile before, we need to remove it first */
105 if (log_heap_flushed > 0)
106 _sysremove(filename);
108 if ((c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, filename)) != 0) {
109 c128_perror(c, "cbm_open(log)");
112 c = cbm_write((BYTE)8, log_heap_buf, log_heap_offset);
113 if (c != log_heap_offset) {
114 textcolor(TC_LIGHT_RED);
115 cprintf("\r\nCould not save logfile (wrote %d bytes, wanted %d bytes), "
116 "please make sure the floppy is not full!\n",
118 c128_perror(c, "cbm_write");
123 log_heap_flushed = log_heap_offset;
127 * Logs to memory and eventually to file (log_flush() is called when one entire
128 * logfile was completed in memory).
131 void log_file(const char *s) {
132 strcpy(log_heap_buf + log_heap_offset, s);
133 log_heap_offset += strlen(s);
135 /* Force a flush when there are only five lines left */
136 if (log_heap_offset > (LOG_SIZE - (5 * 80))) {
140 log_heap_flushed = 0;