Operating System - HP-UX
1820072 Members
2447 Online
109608 Solutions
New Discussion юеВ

getc() trap into infinite-loop

 
forcerick
Occasional Advisor

getc() trap into infinite-loop

Hi, All

The following source is euc_jp encoding,
aCC compiling, running result just output
"1" string, and it doesn't seem to jump out
of the first function getc(), and program
trap into infinite-loop, cannot exit. but
if changing source's encoding to shift-jis,
it works well.
How to fix this? Thanks a lot.
OS: HP-UX 11.0 LANG: euc_jp with JSE support
NOTICE: remote shell xsh's input parameter is
necessary for test

====================gdb info==================
(gdb) n
Single stepping until exit from function getc,
which has no line number information.
0x77c37650 in _nss_files_read_line () from /usr/lib/libnss_files.1
(gdb) n
Single stepping until exit from function _nss_files_read_line,
which has no line number information.

Breakpoint 1, 0x77fa1ff8 in getc () from /usr/lib/libc.2
(gdb) n
Single stepping until exit from function getc,
which has no line number information.
0x77c37650 in _nss_files_read_line () from /usr/lib/libnss_files.1
(gdb) n
Single stepping until exit from function _nss_files_read_line,
which has no line number information.

Breakpoint 1, 0x77fa1ff8 in getc () from /usr/lib/libc.2
(gdb)


***********************************************
#include
#include
#include
#include
#include
#include

char *host[] = { "192.168.3.61" };
char *user = "gyomu";
char *passwd = "gyomu";
char *cmd = "/export/disk1/p2/gyomu/jwu/rexec/xsh ??????????????????1??????";

main(int argc, char **argv)
{
int sd;
int n=0;
FILE *fp;
char ch;
char sBuf[1024];
struct servent *servent;

servent = getservbyname("exec", "tcp");
sd = rexec(host, servent->s_port, user, passwd, cmd, 0);
fp = fdopen(sd, "r");
printf("1\n");
getc(fp);
printf("2\n");
getc(fp);
printf("3\n");
getc(fp);
printf("4\n");

// while ((ch = getc(fp)) != EOF) {
// putchar(ch);
// }
}
8 REPLIES 8
Carlos Fernandez Riera
Honored Contributor

Re: getc() trap into infinite-loop

Read this extract from man getc:


getc() and getchar() are implemented both as library functions and
macros. The macro versions, which are used by default, are defined in
. To obtain the library function either use a #undef to
remove the macro definition or, if compiling in ANSI-C mode, enclose
the function name in parenthesis or use the function address. The
following example illustrates each of these methods :

#include
#undef getc
...
main()
{
int (*get_char()) ();
...
return_val=getc(c,fd);
...
return_val=(getc)(c,fd1);
...
get_char = getchar;
};

If the integer value returned by getc(), getc_unlocked(), getchar(),
getchar_unlocked(), or fgetc() is stored into a character variable
then compared against the integer constant EOF, the comparison may
never succeed because sign-extension of a character on widening to
integer is machine-dependent.

You are using char type.


unsupported
forcerick
Occasional Advisor

Re: getc() trap into infinite-loop

No, please notice, while loop has been commented in source, and infiniteloop occurs in FIRST getc() function, and it obviously related with source encoding because it works well when encoding is shift-jis.
Joseph Chakkery
Valued Contributor

Re: getc() trap into infinite-loop

Hello,

Commenting in C is
/* to be commented include here */

like this . in ur programe it // won't work.

Regards
Joe.
Knowledge is wealth
Joseph Chakkery
Valued Contributor

Re: getc() trap into infinite-loop

Hello,

while loop in ur program should be like following.

/* while ((ch = getc(fp)) != EOF) {
putchar(ch);
*/

Try this

Regards
Joe.
Knowledge is wealth
forcerick
Occasional Advisor

Re: getc() trap into infinite-loop

hi, I use aCC C++ compile, two comment methods are OKAY, it doesn't matter, the core problem is definiteloop in getc().
Joseph Chakkery
Valued Contributor

Re: getc() trap into infinite-loop

Sorry forcerick I didn't notice that.
I thought it is in ANSI-C.

Do u have fflush(stdin) in that. try giving this after getc() function.

Regards
Joe.
Knowledge is wealth
forcerick
Occasional Advisor

Re: getc() trap into infinite-loop

hi, infinite-loop occurs in the process of getc() function, neither before that nor after that
Carlos Fernandez Riera
Honored Contributor

Re: getc() trap into infinite-loop


Well.. check returns for gethostbyname, rexec and fdopen

Good luck.
unsupported