* Write to a file. Returns 0 on success, negative on error, and * the number of characters _not_ written on partial success. * `mode' exists for historical reasons and must be ignored. * The return value is either: * A positive number representing the number of characters not written * (so any nonzero return value denotes a failure of some sort). * A negative number indicating an error. */
| 198 | * A negative number indicating an error. |
| 199 | */ |
| 200 | int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode) |
| 201 | { |
| 202 | #ifdef DFS_USING_POSIX |
| 203 | int size; |
| 204 | #endif /* DFS_USING_POSIX */ |
| 205 | |
| 206 | if (fh == STDOUT || fh == STDERR) |
| 207 | { |
| 208 | #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) |
| 209 | rt_device_t console; |
| 210 | console = rt_console_get_device(); |
| 211 | if (console) |
| 212 | { |
| 213 | rt_device_write(console, -1, buf, len); |
| 214 | } |
| 215 | return 0; /* success */ |
| 216 | #else |
| 217 | return 0; /* error */ |
| 218 | #endif /* defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE) */ |
| 219 | } |
| 220 | else if (fh == STDIN) |
| 221 | { |
| 222 | return -1; /* 100% error */ |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | #ifdef DFS_USING_POSIX |
| 227 | size = write(fh, buf, len); |
| 228 | if (size >= 0) |
| 229 | { |
| 230 | /* |
| 231 | fflush doesn't have a good solution in Keil-MDK, |
| 232 | so it has to sync/flush when for each writen. |
| 233 | */ |
| 234 | fsync(fh); |
| 235 | return len - size; /* success */ |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | return 0; /* error */ |
| 240 | } |
| 241 | #else |
| 242 | LOG_W("%s: %s", __func__, _WARNING_WITHOUT_FS); |
| 243 | return 0; /* error */ |
| 244 | #endif /* DFS_USING_POSIX */ |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Flush any OS buffers associated with fh, ensuring that the file |
nothing calls this directly
no test coverage detected