C
c gordon liddy
What follows purports to be a soln for K&R 8-1 that prints bitshifted
control chars and non-ascii chars:
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
void filecopy(FILE *, FILE *);
if (argc < 2) printf("die");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "r")) == NULL)
{
printf("catv can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}
return 0;
}
/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int ch;
int result;
while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch)) {
/* ch is a control character */
int result;
/*
* The two characters we want to print. The first is '^';
* we don't know yet what the second is.
*/
int ch1 = '^';
int ch2;
/* Try to print the first character. */
result = putchar(ch1);
if (result == EOF) {
/* Failed, terminate the loop */
break;
}
if (ch == '\177') {
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else {
/*
* ch is another control character.
* Transform 1 to 'A', 2 to 'B', etc. using
* our intimate knowledge of ASCII encoding.
*/
ch2 = ch | 0100;
}
/* Print as above */
result = putchar(ch2);
if (result == EOF) {
break;
}
}
else putc(ch, ofp);
}
}
// gcc -o catv catv4.c
// catv text24.txt >text43.txt
Abridged output from text43 is:
^JTarget: i386-pc-mingw32^J
My guess is that J is what you get when you add octal 0100 to a carriage
return. Am I correct to think that the first 32 chars are control chars?
DEL appears to be 117 as well.
How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)
control chars and non-ascii chars:
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp;
void filecopy(FILE *, FILE *);
if (argc < 2) printf("die");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "r")) == NULL)
{
printf("catv can't open %s\n", *argv);
return 1;
}
else
{
filecopy(fp, stdout);
fclose(fp);
}
return 0;
}
/*filecopy */
void filecopy(FILE *ifp, FILE *ofp)
{
int c;
int ch;
int result;
while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch)) {
/* ch is a control character */
int result;
/*
* The two characters we want to print. The first is '^';
* we don't know yet what the second is.
*/
int ch1 = '^';
int ch2;
/* Try to print the first character. */
result = putchar(ch1);
if (result == EOF) {
/* Failed, terminate the loop */
break;
}
if (ch == '\177') {
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else {
/*
* ch is another control character.
* Transform 1 to 'A', 2 to 'B', etc. using
* our intimate knowledge of ASCII encoding.
*/
ch2 = ch | 0100;
}
/* Print as above */
result = putchar(ch2);
if (result == EOF) {
break;
}
}
else putc(ch, ofp);
}
}
// gcc -o catv catv4.c
// catv text24.txt >text43.txt
Abridged output from text43 is:
^JTarget: i386-pc-mingw32^J
My guess is that J is what you get when you add octal 0100 to a carriage
return. Am I correct to think that the first 32 chars are control chars?
DEL appears to be 117 as well.
How would I populate a file containing non-ascii and ctrl chars for test
purposes? (Does anyone have one?)