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

📄 csplit.c

📁 c语言库函数!里面包含了所以c语言中的系统函数的实现及其详细说明和代码!请大家及时下载!
💻 C
📖 第 1 页 / 共 3 页
字号:

        *w = 0;
        ch = *beg++;
      }
    }

    if (ch == '\t')               /* if TAB character */
    {                             /* space to next tab stop */
      /*
       * TS: The following code has been changed to pad to the next
       * tab stop, rather than insert a fixed number of spaces.
       */
      spaces = tabstop - (((int)(tofs + beg - sbuf) - 1) % tabstop);
      memmove (beg + spaces - 1, beg, strlen (beg) + 1);

      for (q = beg - 1; spaces > 0; --spaces)
        *q++ = ' ';

      ch = ' ';                   /* change TAB to space */
    }

    *p++ = (char)ch;              /* update output string */
    ++cnt;

    if ((cnt == len - 1) && ('\n' != ch))   /* if need to wrap line */
    {
      beg -= 2;                   /* make room for "\\\n" characters */
      e = beg;
      p -= 2;
      q = p;

      /* unget characters to 1st previous space */
      while ((e > sbuf) && (' ' != *(e - 1)))
      {
        --q;
        --e;
      }

      /*if (((beg - e) < len) && (e != sbuf))*/
      if (e != sbuf)              /* if wrap on space char */
      {                           /* ( else wrap asis ) */
        p = q;
        beg = e;
      }

      *p++ = '\\';                /* terminate current line */
      *p++ = '\n';
      wrap = B_TRUE;              /* flag wrap line pending */
    }
  } /*end_while*/

  if ('\n' == ch)
    tofs = 0;

  *p = 0;
  return s;
}

/*
 * disp_help - Display help screen to user.
 */
void disp_help (void)
{
  puts ("This utility processes C/C++ source files for the purpose of transmission");
  puts ("through a FidoNet (tm) echo so as to circumvent oversize message problems.\n");
  puts ("Split:    CSplit  [ [/tn] [/wn] [/ln] [/sc] ]  outfile  srcfile [ ... ]");
  puts ("Extract:  CSplit  /x  infile  [ ... ]\n");
  puts ("Where:      /t n - For TAB character expansion, the number of columns");
printf ("                   for each TAB stop.  (min/default/max: %d/%d/%d)\n", TABMIN, TABDEF, TABMAX);
  puts ("            /w n - For width control, the column number to be used for");
printf ("                   breaking long lines.  (min/default/max: %d/%d/%d)\n", WIDMIN, WIDDEF, WIDMAX);
  puts ("            /l n - For length control, the number of lines to limit");
printf ("                   each section.  (0 disable, min/default/max: %d/%d/%d)\n", LENMIN, LENDEF, LENMAX);
  puts ("            /s c - Use 'c' as the line separator character instead");
printf ("                   of the default separator character, '%c'.\n", SEP_CDEF);
  puts ("         outfile - Output file name.  The extension will be the");
  puts ("                   sequential section number.");
#if defined(__STDC__)
  puts ("         srcfile - Source file(s)  (no wildcard support)");
#else
  puts ("         srcfile - Source file(s)  (wildcards supported)");
#endif
  puts ("          infile - Input file name.  An extension indicates file contains the");
  puts ("                   sections in proper consecutive order ready for extraction.");
  puts ("                   Otherwise, infile.001, infile.002, etc., will be used.");
}

/*
 * extr_file - This function processes a properly concatenated file
 *             of consequtive sections to extract the original source.
 */
int extr_file (char *infile, char sepchar)
{
  static char  line [WIDMAX * 2];
  static char  line2[WIDMAX * 2];

  char         outfile[MAXFSPEC + 1];
  char         sep_ln[32];
  char        *sptr = 0;
  char         tchar      = 0;

  int          curpart    = 0;
  int          found      = B_FALSE;
  int          indx       = 0;
  int          in_section = B_FALSE;
  int          len        = 0;
  int          key        = 0;
  int          lines      = 0;
  int          maxpart    = 0;
  int          pos_wrap   = B_FALSE;
  int          sepmax     = 0;
  int          seppart    = 0;
  int          sep_id_len = 0;
  int          temp       = 0;

  unsigned short crc = 0;
  unsigned short sepcrc = 0;


  for (temp = 0; temp < SEP_CLEN; ++temp)
    sep_ln[temp] = (char)sepchar;

  sep_ln[temp++] = ' ';
  strcpy (&sep_ln[temp], SEP_ID);
  sep_id_len = strlen (sep_ln);

  if (NULL == (finp = fopen (infile, "r")))
  {
    char fname[MAXFSPEC + 1];

    if ((NULL == (sptr = strrchr (infile, '\\'))) && /* ignore path */
        (NULL == (sptr = strrchr (infile, ':'))))
    {
      sptr = infile;                        /* V2.2#3 correction */
    }

    if (NULL != strchr (sptr, '.'))         /* if extension exists */
    {
      printf ("Error opening input file for extraction.\n");
      return FILEIO;
    }

    if ((strlen (infile) + 4) > MAXFSPEC)   /* if file spec too large */
    {
      printf ("Input file name argument too long.\n");
      return SYNTAX;
    }

    indx = 1;
    sprintf (fname, "%s.%03d", infile, indx);

    if (NULL != (finp = fopen (fname, "r")))
    {
      sprintf (tempfile, "%s.CSP", infile);

      if (NULL == (ftmp = fopen (tempfile, "w")))
      {
        printf ("Unable to open \"%s\" temp file.\n", tempfile);
        return FILEIO;
      }

      printf ("Processing input files: %s ...\n", fname);

      do
      {
        while (NULL != fgets (line, sizeof (line), finp))
          fputs (line, ftmp);

        fclose (finp);
        ++indx;
        sprintf (fname, "%s.%03d", infile, indx);
        finp = fopen (fname, "r");

      } while (NULL != finp);

      fclose (ftmp);

      if (NULL == (finp = fopen (tempfile, "r")))
      {
        printf ("Error opening temp file \"%s\" for extraction.\n", outfile);
        return FILEIO;
      }
    }
    else
    {
      printf ("Error opening input file \"%s\" for extraction.\n", infile);
      return FILEIO;
    }
  }
  else
  {
    printf ("Processing input file: %s\n", infile);
  }

  crc = curpart = maxpart = lines = 0;
  in_section = pos_wrap = B_FALSE;
  fout = NULL;
  initcrctab ();
  *line2 = 0;

  while (NULL != finp)
  {
    /* AR: increased line input size */
    sptr = fgets (line, sizeof (line), finp);

    if (NULL == sptr)
    {
      if (feof (finp))            /* end of file */
      {
        fclose (finp);
        finp = NULL;
      }
      else
      {
        if (lines)
          printf ("(line %d) ", lines);

        printf ("Unable to read from input file:  %s\n", infile);
        return FILEIO;
      }
    }
    else                          /* process line */
    {
      /* TS: eliminate any added trailing spaces */
      for (indx = strlen (sptr) - 1; indx && (' ' == line[indx - 1]); --indx)
        continue;

      line[indx]   = '\n';
      line[indx+1] = '\0';
      ++lines;

      /*
       * Find 1st separator line to determine if correct separator
       * character is being used and use the one that was found.
       */
      if (!found && (0 != strstr (line, SEP_ID)))
      {
        for (temp = 0; isspace (line[temp]); ++temp)
          ;  /* null statement */

        tchar = line[temp];                 /* skip leading ws */

        if (sepchar != tchar)
          for (temp = 0; temp < SEP_CLEN; ++temp)
            sep_ln[temp] = tchar;

        found = B_TRUE;
      }

      if (0 != (sptr = strstr (line , sep_ln))) /* if separator line */
      {
        sptr += sep_id_len;

        if (sptr == strstr (sptr, SEP_BF))  /* if begin file */
        {
          if (NULL != fout)
          {
            printf ("(line %d) ", lines);
            puts ("Encountered 2nd \"Begin file\" separator");
            puts ("line before expected \"End file\" separator line:");
            puts (line);
            return PROCESS;
          }

          sptr += strlen (SEP_BF);

          if (1 != sscanf (sptr, "%s", outfile))
          {
            printf ("(line %d) ", lines);
            puts ("Unable to parse filename from separator line:");
            puts (line);
            return PROCESS;
          }

          if (NULL != (fout = fopen (outfile, "r")))
          {
            key = 0;
            printf ("\nOutput file already exists:  %s\n", outfile);

            do
            {
              printf ("Overwrite? (y/n) ");
              key = getchar ();
              puts ("");
              temp = key;

              while (temp != '\n')
                temp = getchar();  /* eat any/all extra keystrokes */

              if (('n' == key) || ('N' == key))
                return ABORT;

            } while (('y' != key) && ('Y' != key));

            if (NULL == freopen (outfile, "w", fout))
            {
              printf ("Unable to open file for output:  %s\n", outfile);
              return FILEIO;
            }
          }
          else
          {
            if (NULL == (fout = fopen (outfile, "w")))
            {
              printf ("Unable to open file for output:  %s\n", outfile);
              return FILEIO;
            }
          }

          printf ("Extracting file %s\n", outfile);   /* Phi Nguyen */
          crc = updcrc (crc, (unsigned char *)line, strlen (line));
        }
        else if (sptr == strstr (sptr, SEP_EF))  /* if end file */
        {
          if (NULL == fout)
          {
            printf ("(line %d) ", lines);
            puts ("Encountered an \"End file\" separator line");
            puts ("before a \"Begin file\" separator line:");
            puts (line);
            return PROCESS;
          }

          if (fclose (fout))
          {
            printf ("Unable to close output file:  %s\n", outfile);
            return FILEIO;
          }

          fout = NULL;
          crc = updcrc (crc, (unsigned char *)line, strlen (line));
        }
        else if (sptr == strstr (sptr, SEP_BP))  /* if begin part */
        {
          if (B_TRUE == in_section)
          {
            printf ("(line %d) ", lines);
            puts ("Encountered 2nd \"Begin part\" separator");
            puts ("line before expected \"End part\" separator line:");
            puts (line);
            return PROCESS;
          }
          sptr += strlen (SEP_BP);

          if (2 != sscanf (sptr, "%d/%d", &seppart, &sepmax))
          {
            printf ("(line %d) ", lines);
            puts ("Unable to parse \"n/x\" from separator line:");
            puts (line);
            return PROCESS;
          }

          if (0 == maxpart)
            maxpart = sepmax;

          if (curpart+1 != seppart)
          {
            printf ("(line %d) ", lines);
            printf ("Encountered part %d while expecting part %d:\n", seppart, curpart+1);
            puts (line);
            return PROCESS;
          }
          in_section = B_TRUE;
          ++curpart;
        }
        else if (sptr == strstr (sptr, SEP_EP))  /* if end part */
        {
          if (B_FALSE == in_section)
          {
            printf ("(line %d) ", lines);
            puts ("Encountered 2nd \"End part\" separator line");
            puts ("before expected \"Begin part\" separator line:");
            puts (line);
            return PROCESS;
          }
          sptr += strlen (SEP_EP);
          sptr = strstr (sptr, ": ");

          if (1 != sscanf (sptr+2, "%hx", &sepcrc))   /* DN2: 32 bit fix */
          {
            printf ("(line %d) ", lines);
            puts ("Corrupt CRC in separator line:");
            puts (line);
            return PROCESS;
          }

          if (crc != sepcrc)
          {
            printf ("(line %d) ", lines);
            printf ("Calculated CRC mismatch (0x%04x):\n", crc);
            puts (line);           /* KC: report CRC mismatch only */
          }
          crc = 0;
          in_section = B_FALSE;

          if (curpart == maxpart)                /* if finished */
            break;
        }
        else
        {
          if (sptr != strstr (sptr, SEP_VR))     /* if not version */
          {
            printf ("(line %d) ", lines);
            puts ("Unrecognized separator line:");
            puts (line);
            return PROCESS;
          }
        }
      }
      else                        /* else process data line */
      {
        if (B_TRUE == in_section) /* save only file data */
        {
          len = strlen (line);
          crc = updcrc (crc, (unsigned char *)line, len);

          if (B_TRUE == pos_wrap) /* if possible line wrap in progress */
          {

⌨️ 快捷键说明

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