Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: "C. Gordon Liddy" <a@xxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 20:59:36 -0700
"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 probablyDon't know.
permanent. How do you tell the caller that things are broken and it
should stop calling you?
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?
We'll C. It's time for me to step away.
}snip commentary
/* Print as above */
result = putchar(ch2);
if (result == EOF) {
break;
}
}
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.
#include <stdio.h>
return 0;
}
Remove del for email
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
.
- References:
- || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: c gordon liddy
- Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: Keith Thompson
- Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: c gordon liddy
- Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: Keith Thompson
- Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: c gordon liddy
- Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- From: Barry Schwarz
- || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- Prev by Date: Re: CERT C Secure Coding Standard - last call for reviewers
- Next by Date: Re: CERT C Secure Coding Standard - last call for reviewers
- Previous by thread: Re: || putchar(ch == '\177' ? '?' : ch | 0100) == EOF)
- Next by thread: www.shopbb.com from china shop,Quality AAA,nike shoes =28USD,LV bags = 25USD,ALL NLF = 15USD, Jeans = 25USD,Coat = 33USD,T-shirt = 12USD,MK4=55USD,Nokia N95=260USD
- Index(es):
Relevant Pages
|