-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.c
193 lines (149 loc) · 3.52 KB
/
io.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (C) 2015 173210 <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <pspkernel.h>
#include <psprtc.h>
#include "main.h"
#include "io.h"
typedef struct _queue_t queue_t;
struct _queue_t {
queue_t *next;
SceUID blockid;
size_t size;
};
static queue_t *queue = NULL;
static int left = 0;
static char buf[4096];
void cupIoWork()
{
SceUID log = -1;
SceUID fd;
const char *p;
int ret;
while (1) {
sceKernelSleepThread();
while (left) {
if (log < 0)
log = sceIoOpen("LOG.TXT",
PSP_O_WRONLY | PSP_O_APPEND | PSP_O_CREAT,
0777);
if (log >= 0) {
ret = sceIoWrite(log, buf, left);
if (ret > 0) {
left -= ret;
if (left < 0)
left = 0;
}
if (!sceIoClose(log))
log = -1;
}
}
while (queue != NULL) {
p = (char *)(queue + 1);
fd = sceIoOpen(p,
PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC,
0777);
if (fd >= 0) {
p += strlen(p) + 1;
sceIoWrite(fd, p, queue->size);
sceIoClose(fd);
}
sceKernelFreePartitionMemory(queue->blockid);
queue = queue->next;
}
}
}
int cupPrintTime()
{
int ret;
pspTime time;
if (left >= sizeof(buf))
return 0;
ret = sceRtcGetCurrentClockLocalTime(&time);
if (!ret) {
ret = snprintf(buf + left, sizeof(buf) - left,
"[%02d:%02d.%06d] ",
time.minutes, time.seconds, time.microseconds);
if (ret > 0)
left += ret;
}
return ret;
}
int cupPuts(const char *s)
{
char *p;
if (s == NULL)
return SCE_KERNEL_ERROR_ILLEGAL_ADDRESS;
if (left >= sizeof(buf))
return SCE_KERNEL_ERROR_NO_MEMORY;
cupPrintTime();
p = buf + left;
while (*s && left < sizeof(buf)) {
*p = *s;
p++;
s++;
left++;
}
sceKernelWakeupThread(thid);
return 0;
}
int cupPrintf(const char *fmt, ...)
{
va_list va;
int ret;
if (fmt == NULL)
return SCE_KERNEL_ERROR_ILLEGAL_ADDRESS;
if (left >= sizeof(buf))
return 0;
cupPrintTime();
va_start(va, fmt);
ret = vsnprintf(buf + left, sizeof(buf) - left, fmt, va);
if (ret > 0)
left += ret;
va_end(va);
sceKernelWakeupThread(thid);
return ret;
}
int cupIoWrite(const char *pre, const void *data, SceSize size)
{
pspTime time;
SceUID blockid;
size_t len;
char *p;
if (pre == NULL || data == NULL)
return SCE_KERNEL_ERROR_ILLEGAL_ADDR;
len = strlen(pre);
blockid = sceKernelAllocPartitionMemory(PSP_MEMORY_PARTITION_USER,
"cupIo", PSP_SMEM_Low, sizeof(queue_t) + len + 16 + size, NULL);
if (blockid < 0)
return blockid;
p = sceKernelGetBlockHeadAddr(blockid);
if (p == NULL)
return sceKernelFreePartitionMemory(blockid);
((queue_t *)p)->next = queue;
queue = (queue_t *)p;
queue->blockid = blockid;
queue->size = size;
p += sizeof(queue_t);
sceRtcGetCurrentClockLocalTime(&time);
len = sprintf(p, "%s_%02d%02d%06d.BIN", pre,
time.minutes, time.seconds, time.microseconds);
memcpy(p + len + 1, data, size);
sceKernelWakeupThread(thid);
return 0;
}