⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nlcnvrt.c

📁 c语言库函数!里面包含了所以c语言中的系统函数的实现及其详细说明和代码!请大家及时下载!
💻 C
字号:
/* +++Date last modified: 05-Jul-1997 */

/*
**  NLCNVRT.C - A utility to convert newlines in text files
**
**  public domain by Bob Stout
**  Mac support by Norman Dodge and Bob Stout
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "errors.h"
#include "sniptype.h"

FILE *infile;
FILE *outfile;

static void usage(void);
static void dos2unix(void);
static void dos2mac(void);
static void unix2dos(void);
static void unix2mac(void);
static void mac2dos(void);
static void mac2unix(void);
static void Cout(int ch, FILE *outfile);
static void Sout(char *str, FILE *outfile);

main(int argc, char *argv[])
{
      char outname[L_tmpnam] = "";

      if (argc < 3)
            usage();
      if (NULL == strchr("-/", argv[1][0]))
            usage();

      infile = cant(argv[2], "rb");

      if (3 == argc || NULL == (outfile = fopen(argv[3], "wb")))
      {
            tmpnam(outname);
            outfile = cant(outname, "wb");
      }

      switch (argv[1][1])
      {
      case 'd':
            unix2dos();
            break;

      case 'D':
            mac2dos();
            break;

      case 'u':
            dos2unix();
            break;

      case 'U':
            mac2unix();
            break;

      case 'm':
            dos2mac();
            break;

      case 'M':
            unix2mac();
            break;

      default:
            usage();
      }
      fclose(infile);
      fclose(outfile);
      if (*outname)
      {
            if (Success_ != remove(argv[2]))
            {
                  remove(outname);
                  ErrExit("Can't remove %s", argv[2]);
            }
            rename(outname, argv[2]);
      }
      return EXIT_SUCCESS;
}

void usage(void)
{
      puts("Usage: NLCNVRT -[d | u | m | D | U | M] infile [outfile]");
      puts("Switches: -d  Convert to DOS  from Unix - converts LF   => CRLF");
      puts("        : -u  Convert to Unix from DOS  - converts CRLF => LF");
      puts("        : -m  Convert to Mac  from DOS  - converts CRLF => CR");
      puts("        : -D  Convert to DOS  from Mac  - converts CR   => CRLF");
      puts("        : -U  Convert to Unix from Mac  - converts CR   => LF");
      puts("        : -M  Convert to Mac  from Unix - converts LF   => CR");
      exit(EXIT_FAILURE);
}

void unix2dos(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)))
      {
            if ('\n' == ch && '\r' != lastch)
                  fputc('\r', outfile);
            fputc(lastch = ch, outfile);
      }
      if ('\n' != lastch)
            fputs("\r\n", outfile);
}

void dos2unix(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)) && '\x1a' != ch)
      {
            if ('\r' != ch)
                  fputc(lastch = ch, outfile);
      }
      if ('\n' != lastch)
            fputc('\n', outfile);
}

void mac2dos(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)))
      {
            fputc(lastch = ch, outfile);
            if ('\r' == ch)
                  fputc('\n', outfile);
      }
      if ('\r' != lastch)
            fputs("\r\n", outfile);
}

static void dos2mac(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)) && '\x1a' != ch)
      {
            if ('\r' != ch)
            {
                  if ('\n' == ch)
                        ch = '\r';
                  fputc(lastch = ch, outfile);
            }
      }
      if ('\r' != lastch)
            fputc('\r', outfile);
}

static void unix2mac(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)))
      {
            if ('\n' == ch)
                  ch = '\r';
            fputc(lastch = ch, outfile);
      }
      if ('\r' != lastch)
            fputc('\r', outfile);
}

static void mac2unix(void)
{
      int ch, lastch = 0;

      while (EOF != (ch = fgetc(infile)))
      {
            if ('\r' == ch)
                  ch = '\n';
            fputc(lastch = ch, outfile);
      }
      if ('\n' != lastch)
            fputc('\n', outfile);
}

void Cout(int ch, FILE *outfile)
{
      if (EOF == fputc(ch, outfile))
            exit(EXIT_FAILURE);
}

void Sout(char *str, FILE *outfile)
{
      if (EOF == fputs(str, outfile))
            exit(EXIT_FAILURE);
}

⌨️ 快捷键说明

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