10f66f451Sopenharmony_ci/* dos2unix.c - convert newline format 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2012 Rob Landley <rob@landley.net> 40f66f451Sopenharmony_ci 50f66f451Sopenharmony_ciUSE_DOS2UNIX(NEWTOY(dos2unix, 0, TOYFLAG_BIN)) 60f66f451Sopenharmony_ciUSE_UNIX2DOS(NEWTOY(unix2dos, 0, TOYFLAG_BIN)) 70f66f451Sopenharmony_ci 80f66f451Sopenharmony_ciconfig DOS2UNIX 90f66f451Sopenharmony_ci bool "dos2unix/unix2dos" 100f66f451Sopenharmony_ci default y 110f66f451Sopenharmony_ci help 120f66f451Sopenharmony_ci usage: dos2unix [FILE...] 130f66f451Sopenharmony_ci 140f66f451Sopenharmony_ci Convert newline format from dos "\r\n" to unix "\n". 150f66f451Sopenharmony_ci If no files listed copy from stdin, "-" is a synonym for stdin. 160f66f451Sopenharmony_ci 170f66f451Sopenharmony_ciconfig UNIX2DOS 180f66f451Sopenharmony_ci bool "unix2dos" 190f66f451Sopenharmony_ci default y 200f66f451Sopenharmony_ci help 210f66f451Sopenharmony_ci usage: unix2dos [FILE...] 220f66f451Sopenharmony_ci 230f66f451Sopenharmony_ci Convert newline format from unix "\n" to dos "\r\n". 240f66f451Sopenharmony_ci If no files listed copy from stdin, "-" is a synonym for stdin. 250f66f451Sopenharmony_ci*/ 260f66f451Sopenharmony_ci 270f66f451Sopenharmony_ci#define FOR_dos2unix 280f66f451Sopenharmony_ci#include "toys.h" 290f66f451Sopenharmony_ci 300f66f451Sopenharmony_ciGLOBALS( 310f66f451Sopenharmony_ci char *tempfile; 320f66f451Sopenharmony_ci) 330f66f451Sopenharmony_ci 340f66f451Sopenharmony_cistatic void do_dos2unix(int fd, char *name) 350f66f451Sopenharmony_ci{ 360f66f451Sopenharmony_ci char c = toys.which->name[0]; 370f66f451Sopenharmony_ci int outfd = 1, catch = 0; 380f66f451Sopenharmony_ci 390f66f451Sopenharmony_ci if (fd) outfd = copy_tempfile(fd, name, &TT.tempfile); 400f66f451Sopenharmony_ci 410f66f451Sopenharmony_ci for (;;) { 420f66f451Sopenharmony_ci int len, in, out; 430f66f451Sopenharmony_ci 440f66f451Sopenharmony_ci len = read(fd, toybuf+(sizeof(toybuf)/2), sizeof(toybuf)/2); 450f66f451Sopenharmony_ci if (len<0) perror_msg_raw(name); 460f66f451Sopenharmony_ci if (len<1) break; 470f66f451Sopenharmony_ci 480f66f451Sopenharmony_ci for (in = out = 0; in < len; in++) { 490f66f451Sopenharmony_ci char x = toybuf[in+sizeof(toybuf)/2]; 500f66f451Sopenharmony_ci 510f66f451Sopenharmony_ci // Drop \r only if followed by \n in dos2unix mode 520f66f451Sopenharmony_ci if (catch) { 530f66f451Sopenharmony_ci if (c == 'u' || x != '\n') toybuf[out++] = '\r'; 540f66f451Sopenharmony_ci catch = 0; 550f66f451Sopenharmony_ci // Add \r only if \n not after \r in unix2dos mode 560f66f451Sopenharmony_ci } else if (c == 'u' && x == '\n') toybuf[out++] = '\r'; 570f66f451Sopenharmony_ci 580f66f451Sopenharmony_ci if (x == '\r') catch++; 590f66f451Sopenharmony_ci else toybuf[out++] = x; 600f66f451Sopenharmony_ci } 610f66f451Sopenharmony_ci xwrite(outfd, toybuf, out); 620f66f451Sopenharmony_ci } 630f66f451Sopenharmony_ci if (catch) xwrite(outfd, "\r", 1); 640f66f451Sopenharmony_ci 650f66f451Sopenharmony_ci if (fd) replace_tempfile(-1, outfd, &TT.tempfile); 660f66f451Sopenharmony_ci} 670f66f451Sopenharmony_ci 680f66f451Sopenharmony_civoid dos2unix_main(void) 690f66f451Sopenharmony_ci{ 700f66f451Sopenharmony_ci loopfiles(toys.optargs, do_dos2unix); 710f66f451Sopenharmony_ci} 720f66f451Sopenharmony_ci 730f66f451Sopenharmony_civoid unix2dos_main(void) 740f66f451Sopenharmony_ci{ 750f66f451Sopenharmony_ci dos2unix_main(); 760f66f451Sopenharmony_ci} 77