pclose: returning termination status of command

S

Stu

I am running a simple c program on Solaris 2.8 that calls a ksh shell
script x which contains an exit statement of 58 (exit 58).


cat xxx.c

#include <stdio.h>
#include <errno.h>

int
main()
{

FILE *ptr;

char buf[BUFSIZ];
char *cmd="./x";

if ((ptr = popen(cmd, "r")) != NULL)
{
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);

printf("\nProcess returned %d\n", pclose(ptr));

}
return 0;
}

cat x
#!/bin/ksh
exit 58


When I run the C program I get the following:
../xxx
Process returned 14848

I would have expected to see 58. Can anybody tell me why I am getting
14848.

man pclose
RETURN VALUES
The pclose() function returns the termination status of the
command. It returns -1 if stream is not associated with a
popen() command and sets errno to indicate the error.

Thanks in advance for all that answer this post
 
H

Howard

Stu said:
I am running a simple c program on Solaris 2.8 that calls a ksh shell
script x which contains an exit statement of 58 (exit 58).


cat xxx.c

#include <stdio.h>
#include <errno.h>

int
main()
{

FILE *ptr;

char buf[BUFSIZ];
char *cmd="./x";

if ((ptr = popen(cmd, "r")) != NULL)
{
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);

printf("\nProcess returned %d\n", pclose(ptr));

}
return 0;
}

cat x
#!/bin/ksh
exit 58


When I run the C program I get the following:
./xxx
Process returned 14848

I would have expected to see 58. Can anybody tell me why I am getting
14848.

man pclose
RETURN VALUES
The pclose() function returns the termination status of the
command. It returns -1 if stream is not associated with a
popen() command and sets errno to indicate the error.

Thanks in advance for all that answer this post

This post asks about running a C program from a shell script on a Solaris
platform, none of which are at all related to the topic of this newsgroup,
which is the use of the standard C++ language. Try in a newsgroup related
to one of those topics. You might also check your doc's or header files (or
use google) to see if 14848 is a known error code in your system.

-Howard
 
C

ClaudiaE

I am running a simple c program on Solaris 2.8 that calls a ksh shell
script x which contains an exit statement of 58 (exit 58).


cat xxx.c

#include <stdio.h>
#include <errno.h>

int
main()
{

FILE *ptr;

char buf[BUFSIZ];
char *cmd="./x";

if ((ptr = popen(cmd, "r")) != NULL)
{
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);

printf("\nProcess returned %d\n", pclose(ptr));

}
return 0;
}

cat x
#!/bin/ksh
exit 58


When I run the C program I get the following:
./xxx
Process returned 14848

I would have expected to see 58. Can anybody tell me why I am getting
14848.

man pclose
RETURN VALUES
The pclose() function returns the termination status of the
command. It returns -1 if stream is not associated with a
popen() command and sets errno to indicate the error.

Thanks in advance for all that answer this post



The returnvalue of the child process is in the top 16 bits. You have
to divide the returned value of pclose by 256, then you get the
searched return value of the child process.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top