xref: /aosp_15_r20/external/mtools/dos2unix.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1996,1997,1999,2001-2003,2008,2009,2021 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  */
17*d5c9a868SElliott Hughes 
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "msdos.h"
20*d5c9a868SElliott Hughes #include "mtools.h"
21*d5c9a868SElliott Hughes #include "codepage.h"
22*d5c9a868SElliott Hughes 
23*d5c9a868SElliott Hughes typedef struct Filter_t {
24*d5c9a868SElliott Hughes 	struct Stream_t head;
25*d5c9a868SElliott Hughes 
26*d5c9a868SElliott Hughes 	int mode;
27*d5c9a868SElliott Hughes 	/* int convertCharset; */
28*d5c9a868SElliott Hughes } Filter_t;
29*d5c9a868SElliott Hughes 
30*d5c9a868SElliott Hughes /* read filter filters out messy dos' bizarre end of lines and final 0x1a's */
31*d5c9a868SElliott Hughes 
read_filter(Stream_t * Stream,char * buf,size_t len)32*d5c9a868SElliott Hughes static ssize_t read_filter(Stream_t *Stream, char *buf, size_t len)
33*d5c9a868SElliott Hughes {
34*d5c9a868SElliott Hughes 	DeclareThis(Filter_t);
35*d5c9a868SElliott Hughes 	size_t i,j;
36*d5c9a868SElliott Hughes 	ssize_t ret;
37*d5c9a868SElliott Hughes 	char newchar;
38*d5c9a868SElliott Hughes 
39*d5c9a868SElliott Hughes 	ret = READS(This->head.Next, buf, len);
40*d5c9a868SElliott Hughes 	if ( ret < 0 )
41*d5c9a868SElliott Hughes 		return ret;
42*d5c9a868SElliott Hughes 
43*d5c9a868SElliott Hughes 	j = 0;
44*d5c9a868SElliott Hughes 	for (i=0; i< (size_t) ret; i++){
45*d5c9a868SElliott Hughes 		if ( buf[i] == '\r' )
46*d5c9a868SElliott Hughes 			continue;
47*d5c9a868SElliott Hughes 		if (buf[i] == 0x1a)
48*d5c9a868SElliott Hughes 			break;
49*d5c9a868SElliott Hughes 		newchar = buf[i];
50*d5c9a868SElliott Hughes 		/*
51*d5c9a868SElliott Hughes 		if (This->convertCharset) newchar = contents_to_unix(newchar);
52*d5c9a868SElliott Hughes 		*/
53*d5c9a868SElliott Hughes 		buf[j++] = newchar;
54*d5c9a868SElliott Hughes 	}
55*d5c9a868SElliott Hughes 
56*d5c9a868SElliott Hughes 	return (ssize_t) j;
57*d5c9a868SElliott Hughes }
58*d5c9a868SElliott Hughes 
59*d5c9a868SElliott Hughes static Class_t FilterClass = {
60*d5c9a868SElliott Hughes 	read_filter,
61*d5c9a868SElliott Hughes 	0,
62*d5c9a868SElliott Hughes 	0,
63*d5c9a868SElliott Hughes 	0,
64*d5c9a868SElliott Hughes 	0, /* flush */
65*d5c9a868SElliott Hughes 	0,
66*d5c9a868SElliott Hughes 	0, /* set geometry */
67*d5c9a868SElliott Hughes 	get_data_pass_through,
68*d5c9a868SElliott Hughes 	0,
69*d5c9a868SElliott Hughes 	0, /* get_dosconvert */
70*d5c9a868SElliott Hughes 	0  /* discard */
71*d5c9a868SElliott Hughes };
72*d5c9a868SElliott Hughes 
open_dos2unix(Stream_t * Next,int convertCharset UNUSEDP)73*d5c9a868SElliott Hughes Stream_t *open_dos2unix(Stream_t *Next, int convertCharset UNUSEDP)
74*d5c9a868SElliott Hughes {
75*d5c9a868SElliott Hughes 	Filter_t *This;
76*d5c9a868SElliott Hughes 
77*d5c9a868SElliott Hughes 	This = New(Filter_t);
78*d5c9a868SElliott Hughes 	if (!This)
79*d5c9a868SElliott Hughes 		return NULL;
80*d5c9a868SElliott Hughes 	init_head(&This->head, &FilterClass, Next);
81*d5c9a868SElliott Hughes 	/*
82*d5c9a868SElliott Hughes 	  This->convertCharset = convertCharset;
83*d5c9a868SElliott Hughes 	*/
84*d5c9a868SElliott Hughes 
85*d5c9a868SElliott Hughes 	return &This->head;
86*d5c9a868SElliott Hughes }
87