-
Notifications
You must be signed in to change notification settings - Fork 0
/
dos2unix.c
86 lines (69 loc) · 1.51 KB
/
dos2unix.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
/*
* Title: dos2unix
* Author: Leigh Brown
* Created: 18/06/2023
* Last Updated: 18/06/2023
*
* Modinfo:
* 18/06/2023: Initial version
*/
#include <ez80.h>
#include <stdio.h>
#include <stdlib.h>
#include <ERRNO.H>
#include "mos-interface.h"
int errno; // needed by standard library
#define BUFFER_SIZE 4096
static char buf_in[BUFFER_SIZE];
static char buf_out[BUFFER_SIZE];
void usage(void)
{
printf("Usage: dos2unix <input filename> <output filename>\r\n");
return;
}
int main(int argc, char * argv[])
{
unsigned char fd_in, fd_out;
if (argc != 3) {
usage();
return ERR_INVALID_PARAMETER;
}
fd_in = mos_fopen(argv[1], fa_read | fa_open_existing);
if (fd_in == 0) {
printf("Could not open '%s' for reading\r\n", argv[1]);
return ERR_INVALID_PARAMETER;
}
fd_out = mos_fopen(argv[2], fa_write | fa_create_always);
if (fd_out == 0) {
mos_fclose(fd_in);
printf("Could not open '%s' for writing\r\n", argv[2]);
return ERR_INVALID_PARAMETER;
}
while (1) {
unsigned int i, j;
unsigned int wrote, got;
char c;
got = mos_fread(fd_in, buf_in, sizeof(buf_in));
if (got == 0)
break;
j = 0;
for (i = 0; i < got; ++i) {
c = buf_in[i];
if (c != '\r')
buf_out[j++] = c;
}
// Write the output buffer
if (j > 0) {
wrote = mos_fwrite(fd_out, buf_out, j);
if (wrote != j) {
printf("Error writing to '%s'\r\n", argv[2]);
mos_fclose(fd_in);
mos_fclose(fd_out);
return ERR_INVALID_PARAMETER;
}
}
}
mos_fclose(fd_in);
mos_fclose(fd_out);
return 0;
}