Question on order of headers and WEXITSTATUS


 
Thread Tools Search this Thread
Top Forums Programming Question on order of headers and WEXITSTATUS
# 1  
Old 06-04-2007
Question on order of headers and WEXITSTATUS

In one of the Unix Programming FAQ's they have the following headers in the program to catch SIGCHLD

Code:
#include <sys/types.h>  /* include this before any other sys headers */
#include <sys/wait.h>   /* header for waitpid() and various macros */
#include <signal.h>     /* header for signal functions */
#include <stdio.h>      /* header for fprintf() */
#include <unistd.h>     /* header for fork() */

Why does <sys/types.h> have to be placed before any other sys headers in this case?

And the second question. In the following code snippet, when I have status>>8 on a sep. line, I get an exit status of 256 when no one is logged on.

Code:
if (status != 0) {
    fprintf(stderr, "No one logged on.\n");
    WEXITSTATUS(status);
    status>>8;
    printf("%d\n", status);
    exit(EXIT_FAILURE);
    }


However, when I put status>>8 in printf() like in the following
Code:
if (status != 0) {
    fprintf(stderr, "No one logged on.\n");
    WEXITSTATUS(status);
    printf("%d\n", status>>8);
    exit(EXIT_FAILURE);
    }

I get an exit status of 1 when no one is logged on. I don't understand how the placement of status>>8 has an impact on the exit code.
# 2  
Old 06-05-2007
Q2 - POSIX says:
Quote:
WEXITSTATUS(stat_val)
If the value of WIFEXITED(stat_val) is non-zero, this macro evaluates to the low-order 8 bits of the status argument that the child process passed to _exit() or exit(), or the value the child process returned from main().
The lower eight bits of something is never 256 -- 8 bits range from 0 - 255. First. Did you check WIFEXITED?

the >> question: I do not know, but I do know function arguments are sequence points. That means they have to be fully evaluated before they are passed as argument to a function.

Are you using an old compiler, like the one that comes as part of HPUX?

Q1- sys/types.h is required to be included first by wait(), followed by sys/wait.h - if you read the man page for wait.

The real reason is so that test macros in the other include files do not "accidentally" turn off a declaration of some datatype, struct or typedef needed by somebody in another include file later on.
# 3  
Old 06-05-2007
The simple answer is "sys/types.h is a fundamental header and contains many types required by other headers".
# 4  
Old 06-05-2007
Quote:
Originally Posted by jim mcnamara
Q2 - POSIX says:


The lower eight bits of something is never 256 -- 8 bits range from 0 - 255. First. Did you check WIFEXITED?

the >> question: I do not know, but I do know function arguments are sequence points. That means they have to be fully evaluated before they are passed as argument to a function.

Are you using an old compiler, like the one that comes as part of HPUX?

Q1- sys/types.h is required to be included first by wait(), followed by sys/wait.h - if you read the man page for wait.

The real reason is so that test macros in the other include files do not "accidentally" turn off a declaration of some datatype, struct or typedef needed by somebody in another include file later on.

I'm using the GNU C Compiler. I'm suspecting it might have to do with that fact that the '>> isn't a sequence point.
# 5  
Old 06-05-2007
1. You are not using the macros correctly...

it should be
Code:
x=WEXITSTATUS(status);

saying
Code:
WEXITSTATUS(status);

by itself evaluates the exit status and discards the result.

2. Similarly
Code:
status>>8;

effectively does nothing, it shifts a copy of the number right by 8 places and throws the result away.
# 6  
Old 06-05-2007
Quote:
Originally Posted by porter
1. You are not using the macros correctly...

it should be
Code:
x=WEXITSTATUS(status);

saying
Code:
WEXITSTATUS(status);

by itself evaluates the exit status and discards the result.

2. Similarly
Code:
status>>8;

effectively does nothing, it shifts a copy of the number right by 8 places and throws the result away.

A while back, I was was working on a program that expand something like
a-z to abc...xyz. Anyhow, I have having similar issues with stuff being evaluated and discarding the results. I think this is because I'm missing the broader concept. What that broader concept is, I have no idea.

I think I just need to sleep on it and try to figure out what it is I'm not grasping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to compare files and validate order of headers

The below awk verifies the count and order of each text file in the directory. The script does execute and produce output, however the order of the headers are not compared to key. The portion in bold is supposed to do that. If the order of the headers in each text file is the same as key, then... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Programming

Oracle 11g optimizer best join order question

Hi team Optimizer max permutations and search limit are set to 3.5 million and 10 respectively.The parameter instruct optimizer to evaluate up to 3.5 million permutations for max 10 table joins I have query with 9 tables joined. How many combinations will optimizer perform to find best... (1 Reply)
Discussion started by: Perlbaby
1 Replies

3. Shell Programming and Scripting

CSv2dat file headers and columns order

Dear all, I have a csv file which is transformed to .dat. I have an awk file which is supposing to do the mapping of the dat file. the code from the awk file is the one below. The content of the dat file is looking like this (tab separated): ODT AGE CDT CO SEX TIME ... (9 Replies)
Discussion started by: grikoss
9 Replies

4. Programming

help with WEXITSTATUS in C

when a execute this line printf("%d\n",WEXITSTATUS(status)); i get no output, whats could be the possible causes? ---------- Post updated at 09:29 PM ---------- Previous update was at 09:27 PM ---------- nvm i figured it out, u can delete this thread (0 Replies)
Discussion started by: omega666
0 Replies

5. UNIX for Dummies Questions & Answers

tcpdump and prism headers question

Hello everyone! I installed OpenWRT on a WRT54G-TM (linux 2.4). No problem so far!. I also installed tcpdump on the box. I set the adapter in monitor mode. wlc monitor 1 It created the prism0 interface. Tcpdumpíng is also possible using this interface. root@cmWRT:/tmp# tcpdump -i... (1 Reply)
Discussion started by: aztroboy
1 Replies

6. Programming

C - WEXITSTATUS() question

I want to check if an application is still running under C. So far I've been using int rc; rc=WEXITSTATUS( ) if(rc > 0) { //then I exited } I was wondering seeing as WEXITSTATUS in man pages is close to the WAIT() if it was thread blocking and if it was how would I go about making... (4 Replies)
Discussion started by: james2432
4 Replies

7. Programming

Byte order question

Hi, The structure that will follow is supposed to hold the following RTP header field 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ... (3 Replies)
Discussion started by: emitrax
3 Replies

8. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

9. Shell Programming and Scripting

Remove text between headers while leaving headers intact

Hi, I'm trying to strip all lines between two headers in a file: ### BEGIN ### Text to remove, contains all kinds of characters ... Antispyware-Downloadserver.com (Germany)=http://www.antispyware-downloadserver.c om/updates/ Antispyware-Downloadserver.com #2... (3 Replies)
Discussion started by: Trones
3 Replies

10. Programming

C Headers

Where can i get C/C++ headers for OS MINIX 2.0.3? (0 Replies)
Discussion started by: biosdos
0 Replies
Login or Register to Ask a Question