dos2unix.c

来自「编码方式的解码示范uuencode and uudecode are in th」· C语言 代码 · 共 60 行

C
60
字号
/*
 *	This program strips the CRs from a file.  MSDOS uses CRLF for
 * \n while Unix uses only a LF.  In addition, ^Z characters are
 * stripped from the input file.  Unix does not use ^Z to mark the
 * EOF, while some MSDOS editors put a ^Z at the end of a file.
 * ^D characters are stripped from the file also.  Some PC products
 * which produce Postscript output put ^Ds in the file.  Some
 * Postscript printers will complain about these files and refuse
 * to print them.
 *
 * Kenneth J. Hendrickson
 */

#ifdef MSDOS
#include <fcntl.h>
#include <io.h>
#include <process.h>
#endif
#include <stdio.h>

#define CTRL_D	'\004'	/* EOT */
#define CTRL_M	'\015'	/* CR */
#define CTRL_Z	'\032'	/* SUB */

main(argc, argv)
int argc;
char **argv;

{
	FILE *in;
	char buffer[2];

	/* optional input arg */
	if (argc > 1) {
		if ((in = fopen(argv[1], "r")) == NULL) {
			(void) perror(argv[1]);
			exit(1);
		}
		argc--;
	} else
		in = stdin;

	if (argc != 1) {
		(void) fprintf(stderr, "Usage: dos2unix [infile]\n");
		exit(2);
	}

#ifdef MSDOS
	/* don't translate CRLF into LF when reading */
	(void) setmode(fileno(in), O_BINARY);

	/* don't translate LF into CRLF when writing */
	(void) setmode(fileno(stdout), O_BINARY);
#endif

	while (fread(buffer, sizeof (char), 1, in) == 1)
		if ( (buffer[0] != CTRL_D) && (buffer[0] != CTRL_M) && (buffer[0] != CTRL_Z) )
			(void) fputc(buffer[0], stdout);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?