📄 csplit.c
字号:
if (0 == strncmp (line, "\\\n", 2)) /* if wrapped line */
{
strcpy (line, line2);
line[strlen (line) - 2] = 0; /* remove wrap EOL */
}
else
{
strcat (line2, line);
strcpy (line, line2);
}
pos_wrap = B_FALSE;
}
else if ('\\' == line[len - 2]) /* if possible wrapped line */
{
strcpy (line2, line);
pos_wrap = B_TRUE;
}
if ((B_FALSE == pos_wrap) &&
((NULL == fout) || (EOF == fputs (line, fout))))
{
puts ("Error writing output\n");
return FILEIO;
}
}
}
}
} /*end_while*/
/* TS: Test for incompete processing. */
if (B_TRUE == in_section)
{
printf ("Error: end of input while processing section %d of %d\n", seppart, sepmax);
return PROCESS;
}
if (seppart != sepmax)
{
printf ("Error: end of input after processing section %d of %d\n", seppart, sepmax);
return PROCESS;
}
return NOERR;
}
/*
* free_list - This function simply frees each linked list item.
*/
void free_list (void)
{
while (NULL != head)
{
cur = head->next;
free (head);
head = cur;
}
}
/*
* init_list - This function creates a linked list of input source
* files. Wildcard specifications are accommodated when
* ANSI mode is not in effect.
*/
int init_list (int argc, char **argv, int argo)
{
int i;
#if !defined(__STDC__)
char filename[MAXFSPEC + 1];
char path[PATHMAX + 1];
char *sptr;
int done;
DOSFileData fd;
#endif
for (i = argo; i < argc; ++i) /* process CL arguments */
{
#if !defined(__STDC__)
if (strlen (argv[i]) > (MAXFSPEC - FNAMELEN))
{
printf ("Input file argument too long: %s\n", argv[i]);
return SYNTAX;
}
done = FIND_FIRST(argv[i], 0x20, &fd); /* david nugent */
if (done)
{
printf ("Error with filespec: %s\n", argv[i]);
return PROCESS;
}
strcpy (path, argv[i]); /* preserve path */
if (NULL != (sptr = strrchr (path, '\\')))
*(sptr + 1) = '\0';
else
path[0] = '\0';
while (!done)
{
if ('\0' != path[0])
{
strcpy (filename, path);
if (NULL != (sptr = strrchr (ff_name(&fd), '\\')))
strcat (filename, sptr + 1); /* just in case */
else
strcat (filename, ff_name(&fd));
}
else
strcpy (filename, ff_name(&fd));
if (NULL == add_list (filename)) /* david nugent */
return MEMORY;
done = FIND_NEXT(&fd);
}
FIND_END(&fd); /* david nugent */
#else
if (strlen (argv[i]) > MAXFSPEC)
{
printf ("Input file argument too long: %s\n", argv[i]);
return SYNTAX;
}
if (NULL == add_list (argv[i]))
return MEMORY;
#endif
}
return NOERR;
}
/*
* split_src - This function takes a linked list of input source
* files, concatenates the source and then splits it
* into sections of controlled size.
*/
int split_src (SLST *filelist, char *outfname, int length, int width, int tabstop, char sepchar)
{
char line[WIDMAX + 1];
char *ext = 0;
char filename[MAXFSPEC + 1];
char outfile[MAXFSPEC + 1];
char sep_ln[32];
char *sptr = 0;
int curpart = 0;
int key = 0;
int lines = 0;
int maxpart = 1;
int retc = 0;
int temp = 0;
unsigned short crc = 0;
strcpy (outfile, outfname);
if (NULL == (ext = strchr (outfile, '.'))) /* ignore any ext */
ext = &outfile[strlen (outfile)];
*ext = 0; /* make temp file name */
strcpy (tempfile, outfile);
strcat (tempfile, ".CSP");
if (NULL == (ftmp = fopen (tempfile, "w")))
{
printf ("Error creating temp file: %s\n", tempfile);
return FILEIO;
}
for (temp = 0; temp < 10; ++temp)
sep_ln[temp] = (char)sepchar;
sep_ln[temp] = '\0';
for (cur = filelist, lines = 0; NULL != cur; cur = cur->next)
{
if (NULL == finp)
{
if (NULL == (finp = fopen (cur->srcfile, "r")))
{
printf ("Error opening source file: %s\n", cur->srcfile);
return FILEIO;
}
if ((NULL != (sptr = strrchr (cur->srcfile, '\\'))) ||
(NULL != (sptr = strrchr (cur->srcfile, ':')))) /* Darin McBride */
strcpy (filename, sptr+1);
else
strcpy (filename, cur->srcfile);
retc = fprintf (ftmp, "%s %s%s%s %s\n", sep_ln, SEP_ID, SEP_BF, filename, sep_ln);
if (0 == retc)
{
puts ("Error writing output\n");
return FILEIO;
}
++lines;
}
while (NULL != finp)
{
/*
* The function csp_fgets() is equivalent to fgets() in that it
* too reads n-1 characters or up to a newline character. This
* function additionally expands TAB characters, deletes trailing
* whitespace and wraps lines exceeding the specified length.
*/
if (NULL == csp_fgets (line, width, finp, tabstop))
{
if (feof (finp))
{
fclose (finp);
finp = NULL;
retc = fprintf (ftmp, "%s %s%s%s %s\n", sep_ln, SEP_ID, SEP_EF, filename, sep_ln);
if (0 == retc)
{
puts ("Error writing output\n");
return FILEIO;
}
++lines; /* adjust line count */
}
else
{
puts ("Error reading input\n");
return FILEIO;
}
}
else
{
if (EOF == fputs (line, ftmp))
{
puts ("Error writing output\n");
return FILEIO;
}
++lines;
}
} /*end_while*/
} /*end_for*/
if (0 != length)
{
/* There are 3 lines of overhead per section. */
maxpart = lines / (length - 3);
if ((lines % (length - 3)) > 0)
++maxpart; /* for partial section */
}
curpart = 1;
sprintf (ext, ".%03d", curpart); /* make 1st output file name */
/* warn user if the output filename is already in use */
if (NULL != (fout = fopen (outfile, "r")))
{
key = 0;
printf ("Output file already exists: %s\n", outfile);
do
{
printf ("Overwrite? (y/n): ");
key = (toupper) (getchar ()); /* prevent using toupper macro */
puts ("");
temp = key;
while (temp != '\n')
{
temp = getchar (); /* eat all extra keystrokes */
}
if ('N' == key)
return ABORT;
} while ('Y' != key);
fclose (fout);
fout = NULL;
}
if (NULL == freopen (tempfile, "r", ftmp))
{
printf ("Error reopening temp file: %s\n", tempfile);
return FILEIO;
}
initcrctab ();
while (NULL != ftmp)
{
lines = 0;
if (NULL == fout)
{
sprintf (ext, ".%03d", curpart); /* make output file name */
if (NULL == (fout = fopen (outfile, "w")))
{
printf ("Error opening output file: %s\n", outfile);
return FILEIO;
}
retc = fprintf (fout, "%s %s%s%s %s\n", sep_ln, SEP_ID, SEP_VR, VERSION, sep_ln);
if (0 == retc)
{
puts ("Error writing output\n");
return FILEIO;
}
++lines;
retc = fprintf (fout, "%s %s%s%d/%d %s\n", sep_ln, SEP_ID, SEP_BP, curpart, maxpart, sep_ln);
if (0 == retc)
{
puts ("Error writing output\n");
return FILEIO;
}
++lines;
}
crc = 0;
while (((0 == length ) || (lines < (length - 1))) &&
(NULL != ftmp))
{
if (NULL == fgets (line, WIDMAX, ftmp))
{
if (feof (ftmp))
{
fclose (ftmp);
ftmp = NULL;
}
else
{
puts ("Error reading input\n");
return FILEIO;
}
}
else
{
crc = updcrc (crc, (unsigned char *)line, strlen (line));
if (EOF == fputs (line, fout))
{
puts ("Error writing output\n");
return FILEIO;
}
++lines; /* increment line count */
}
} /*end_while*/
if (0 == fprintf (fout, "%s %s%s%d/%d crc: %04x %s\n", sep_ln, SEP_ID, SEP_EP, curpart, maxpart, crc, sep_ln))
{
puts ("Error writing output\n");
return FILEIO;
}
fclose (fout);
fout = NULL;
++curpart;
} /*end_while*/
return NOERR;
}
/*
* CRC-16f.c, from Snippets. Calculate, intelligently, the CRC
* of a dataset incrementally given a buffer full at a time.
* Initialize crc to 0 for XMODEM, -1 for CCITT.
*/
/*
* P, the CRC polynomial, is used by XMODEM (almost CCITT).
* If you change P, you must change crctab[]'s initial value
* to what is printed by initcrctab().
*/
#define P 0x1021
#define W 16 /* number of bits in CRC: don't change it */
#define B 8 /* number of bits per char: don't change it */
unsigned short crctab[1<<B];
void initcrctab (void)
{
register b, v, i;
for (b = 0; b <= (1<<B) - 1; ++b)
{
for (v = b<<(W-B), i = B; --i >= 0; )
{
v = v & 0x8000 ? (v<<1)^P : v<<1;
}
crctab[b] = v;
}
}
unsigned short updcrc (unsigned short icrc, unsigned char *icp, unsigned int icnt)
{
register unsigned short crc = icrc;
register unsigned char *cp = icp;
register unsigned int cnt = icnt;
while (cnt--)
{
crc = (crc<<B) ^ crctab[(crc>>(W-B)) ^ *cp++];
}
return crc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -