Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)





"Barry Schwarz" <schwarzb@xxxxxxxx> wrote in message
news:c8jvu3hdp3cefr8gkni8ccpadbtlcih06o@xxxxxxxxxx
On Sat, 29 Mar 2008 22:36:19 -0700 (PDT), c gordon liddy

Your main calls this function in a loop. A failure here is probably
permanent. How do you tell the caller that things are broken and it
should stop calling you?
Don't know.

I would hope that an OS would just ignore to ignore a main call from the
same place a million times a second. My guess is that you have a means for
an OS to decide without burning the chip.



You may want to have the function return a status and let the calling
function evaluate that status before iterating the loop.

}

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;

Why limit yourself to ASCII? Why use an octal constant to obfuscate
the code? If you want to transform integers to letters, build a
static array and select the character using the integer as the index.
Make it constant and give it file scope. Something along the lines of
const char transform[] = "@ABCD...XYZ~";
ch2 = transform[ch];
You will need to add a range check on ch since I'm not aware of any
guarantee that all control characters have an integer value <= 26.

How do I do that? Are ctrl chars defined by ascii?




}

/* Print as above */
result = putchar(ch2);
if (result == EOF) {
break;
}
}

snip commentary


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

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die");

Avoid portability issues and include a \n in your print string.

else
while (--argc > 0)

As a matter of style, the absence of braces will eventually cause you
problems. Right now your if and else are close enough to be visually
obvious. That will not always be the case. Many adopt the style of
using braces even when the range of the loop is a simple one-line
statement. I'm not terribly consistent myself in that situation but I
always use braces when the range occupies multiple lines.

if ((fp = fopen(*++argv, "r")) == NULL)

Since you expect the file to contain control character, you should
open it for binary input, not text. The reason is that some operating
systems will cause getc to return EOF when reading the control
character that they think marks the end of text.

{
printf("catv can't open %s\n", *argv);
return 1;

Use EXIT_FAILURE instead of 1 for portability.

}
else
{
filecopy(fp, stdout);
fclose(fp);
}

Avoid portability issues and
putchar('\n');
when you finish.

If you run in Windows, a
getchar();
here will keep the window open long enough for you to read the output.
We'll C. It's time for me to step away.




return 0;
}


Remove del for email
#include <stdio.h>

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

FILE *fp;
void filecopy(FILE *, FILE *);

if (argc < 2) printf("die\n");
else
while (--argc > 0)
if ((fp = fopen(*++argv, "rb")) == 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;
int ch1 = '^';
int ch2;
int ch3 = 'M';

while((ch=getc(ifp)) != EOF)
{
if (iscntrl(ch))
{

result = putchar(ch1);
if (result == EOF)
{
/* Failed, terminate the loop */
break;
}

if (ch == '\177')
{
/* ch is DEL, we want "^?" */
ch2 = '?';
}
else if(ch == 26)
{
/* we don't want ctrl-z coming out of here */
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;
}


// outer brace of if (iscntrl(ch))
}


else if (!isascii(ch))
{
if (putchar('M') == EOF || putchar('-') == EOF)
break;
ch = toascii(ch);
putchar(ch);
}



else putchar(ch);


// outer brace of while control

putchar('\n');
// ready to exit

}

getchar();



// outer brace of function
}
// gcc -o catv catv9.c >text22.txt 2>text23.txt
// catv text42.txt >text43.txt



--

"I am waiting for them to prove that God is really American."

~~ Lawrence Ferlinghetti


.



Relevant Pages

  • Re: writing to pipe
    ... int main ... while)!= EOF) ... // outer brace of if ) ... had trouble displaying link information because some control character ...
    (comp.lang.c)
  • Re: || putchar(ch == 177 ? ? : ch | 0100) == EOF)
    ... It's a character constant that uses an escape sequence. ... ch | 0100) == EOF) ... iscntrlreturns true if ch is a "control character". ... int result; ...
    (comp.lang.c)
  • Re: scanf behaviour
    ... char, i have to reread the input until I get the needed pos. ... user input a number that's too large to be stored in an integer. ... static int ignoreblks ... which may be \n or EOF ...
    (comp.lang.c)
  • Re: How to check for errors when inputting a number
    ... int read_num{ ... detect the character that terminated a numerical field. ... which may be \n or EOF ... Skipblks returns the char that getc will next return, ...
    (comp.lang.c)
  • Re: How to check for errors when inputting a number
    ... The numerical input routines *NEVER* absorb a terminating ... readxint a further getc will return the character ... static int ignoreblks ... which may be \n or EOF ...
    (comp.lang.c)