Added
Link Here
|
1 |
--- nvr.c.orig 2021-11-20 14:32:08 UTC |
2 |
+++ nvr.c |
3 |
@@ -1,4 +1,7 @@ |
4 |
+#include <stdlib.h> |
5 |
+#include <stdio.h> |
6 |
#include <string.h> |
7 |
+#include <sys/stat.h> |
8 |
#include "vt100.h" |
9 |
|
10 |
//ER1400 device. |
11 |
@@ -8,7 +11,36 @@ static u8 nvr_latch; |
12 |
static u32 nvr_addr; |
13 |
static u16 nvr_data; |
14 |
static FILE *f; |
15 |
+static char *filename = NULL; |
16 |
+#define NVRAM "vt100-nvram" |
17 |
|
18 |
+static void setfilename(void) { |
19 |
+ char *statehome, *home; |
20 |
+ |
21 |
+ if (filename != NULL) |
22 |
+ return; |
23 |
+ |
24 |
+ statehome = getenv("XDG_STATE_HOME"); |
25 |
+ if (statehome != NULL) { |
26 |
+ asprintf(&filename, "%s/%s", statehome, NVRAM); |
27 |
+ return; |
28 |
+ } |
29 |
+ |
30 |
+ home = getenv("HOME"); |
31 |
+ if (home != NULL) { |
32 |
+ asprintf(&statehome, "%s/.local", home); |
33 |
+ mkdir(statehome, 0777); |
34 |
+ free(statehome); |
35 |
+ asprintf(&statehome, "%s/.local/state", home); |
36 |
+ mkdir(statehome, 0777); |
37 |
+ free(statehome); |
38 |
+ asprintf(&filename, "%s/.local/state/%s", home, NVRAM); |
39 |
+ return; |
40 |
+ } |
41 |
+ |
42 |
+ filename = NVRAM; |
43 |
+} |
44 |
+ |
45 |
static u8 nvr_in (u8 port) |
46 |
{ |
47 |
LOG (NVR, "IN"); |
48 |
@@ -23,8 +55,9 @@ static void nvr_out (u8 port, u8 data) |
49 |
|
50 |
static void save (int addr) |
51 |
{ |
52 |
+ setfilename(); |
53 |
if (f == NULL) |
54 |
- f = fopen ("nvram", "wb"); |
55 |
+ f = fopen (filename, "wb"); |
56 |
if (f == NULL) |
57 |
return; |
58 |
fseek (f, 2 * addr, SEEK_SET); |
59 |
@@ -161,7 +194,8 @@ void reset_nvr (void) |
60 |
register_port (0x62, nvr_in, nvr_out); |
61 |
nvr_data = 0; |
62 |
nvr_addr = 0; |
63 |
- f = fopen ("nvram", "r+b"); |
64 |
+ setfilename(); |
65 |
+ f = fopen (filename, "r+b"); |
66 |
if (f != NULL) |
67 |
load (); |
68 |
else |