f



code review: read in ps(1) result

/*thank you for your time*/

/*read in ps(1) result*/

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>

#define M 256
#define N 32

#define str(n) #n /*from pete's*/
#define xstr(n) str(n)

int main(void)
{
    char uid[N + 1];
    pid_t pid;
    pid_t ppid;
    char stime[N + 1];
    char cmd[M + 1];
    char *ps = "ps -ef";
    FILE *fp;
    int errnum;
    int n;

    errno = 0;
    if (!(fp = popen(ps, "r"))){
        errnum = errno;
        fprintf(stderr, "%s\n", strerror(errnum));
        return 1;
    }
    errno = 0;
    while((n = fscanf(fp, "%"xstr(N)"s %d %d %*s "
            "%"xstr(N)"s %*s %*s %"xstr(M)"[^\n]",
            uid, &pid, &ppid, stime, cmd)) != EOF){
        if (n != 5){
            fscanf(fp, "%*[^\n]");
        continue;
        }
        fprintf(stdout, "%s, %d, %d, %s, %s\n",
        uid, (int)pid, (int)ppid, stime, cmd);
        errno = 0;
    }
    errnum = errno;
    if (errnum)
        fprintf(stderr, "%s\n", strerror(errnum));
    pclose(fp);
    return 0
}
0
7/16/2010 12:44:31 PM
comp.lang.c 30603 articles. 3 followers. spinoza1111 (3246) is leader. Post Follow

7 Replies
448 Views

Similar Articles

[PageSpeed] 10

lovecreatesbea...@gmail.com ha scritto:

> #include<stdio.h>
> #include<errno.h>
> #include<sys/types.h>
>          fprintf(stderr, "%s\n", strerror(errnum));

you should #include <string.h>, this header
contains the strerror's prototype

>      return 0

and append a semicolon ";" to this line


-- 
Vincenzo Mercuri
0
Vincenzo
7/16/2010 1:33:39 PM
On 7/16/2010 8:44 AM, lovecreatesbea...@gmail.com wrote:
> [...]
>      errno = 0;
>      [...]
>      errnum = errno;
>      if (errnum)
>          fprintf(stderr, "%s\n", strerror(errnum));

     No.  Except as specifically documented (e.g., for strtod()),
any library function can set `errno' to a non-zero value, even
if no error occurs.  Thus, finding `errno != 0' does not mean
there has been an error.  Also, only a few library functions are
obliged to set `errno' when an error occurs -- for example, a
failing fopen() is not required to set it -- so `errno == 0' does
not imply the absence of errors.  You have to detect success/failure
by other means, and then (perhaps) consult `errno' for further
information about a failure.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
Eric
7/16/2010 6:18:30 PM
Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> On 7/16/2010 8:44 AM, lovecreatesbea...@gmail.com wrote:
>> [...]
>>      errno = 0;
>>      [...]
>>      errnum = errno;
>>      if (errnum)
>>          fprintf(stderr, "%s\n", strerror(errnum));
>
>      No.  Except as specifically documented (e.g., for strtod()),
> any library function can set `errno' to a non-zero value, even
> if no error occurs.  Thus, finding `errno != 0' does not mean
> there has been an error.  Also, only a few library functions are
> obliged to set `errno' when an error occurs -- for example, a
> failing fopen() is not required to set it -- so `errno == 0' does
> not imply the absence of errors.  You have to detect success/failure
> by other means, and then (perhaps) consult `errno' for further
> information about a failure.

To expand on that a bit, some secondary standards may specify that
certain standard C functions must set errno on failure.  For example,
POSIX requires fopen() to set errno to one of several values (none
of which are defined by the C standard).  And I don't know of any
C implementations, POSIX or otherwise, in which fopen() *doesn't*
set errno to some sensible value on failure (but my experience
is sufficiently limited that I probably wouldn't know about them
anyway).

If you're only concerned with POSIX-compliant systems, you can assume
that the value of errno is meaningful after a call to fopen() --
but *only* if you set errno to 0 before the call *and* fopen()
reported failure by returning a null pointer (otherwise errno
could be set to some spurious value, either by previous code or by
something called internally by fopen().

For non-POSIX systems, you can *probably* assume that errno is
meaningful under the above circumstances (and perror() and/or
strerror() will give you a useful message), but that's not at all
guaranteed by the C standard.

Note that the only errno values specified by the C standard itself
are 0, EDOM, EILSEQ, and ERANGE.  Anything beyond that is left to
secondary standards and to individual implementations.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Keith
7/16/2010 7:30:03 PM
Keith Thompson <kst-u@mib.org> writes:

> To expand on that a bit, some secondary standards may specify that
> certain standard C functions must set errno on failure.  For example,
> POSIX requires fopen() to set errno to one of several values (none
> of which are defined by the C standard).  And I don't know of any
> C implementations, POSIX or otherwise, in which fopen() *doesn't*
> set errno to some sensible value on failure (but my experience
> is sufficiently limited that I probably wouldn't know about them
> anyway).

malloc() may be a better example.  POSIX requires malloc() to set
errno to ENOMEM on failure, if memory is not available.  But a
lot of non-POSIX implementations in fact do not do this (and
ENOMEM is not defined by ISO C).
-- 
Ben Pfaff 
http://benpfaff.org
0
Ben
7/16/2010 8:11:25 PM
On Fri, 16 Jul 2010 12:30:03 -0700, Keith Thompson wrote:

> Note that the only errno values specified by the C standard itself
> are 0, EDOM, EILSEQ, and ERANGE.  Anything beyond that is left to
> secondary standards and to individual implementations.

That should provide a clue as to which functions are required (by the C
standard) to set errno under some circumstances (hint: not many of them).

errno is basically a POSIX feature which managed to leak into the C
standards. POSIX itself defines 81 possible non-zero values for errno,
while Linux currently defines 129 (1 through 131, with 41 and 58 unused).

http://www.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html


0
nobody (5159)
7/16/2010 8:40:46 PM
lovecreatesbea...@gmail.com wrote:
 
> #define str(n) #n /*from pete's*/
> #define xstr(n) str(n)

You can search for "xstr" in the C standard.
That's where I got those macros from.

-- 
pete
0
pete
7/16/2010 10:14:15 PM
Keith Thompson <kst-u@mib.org> wrote:
> 
> And I don't know of any
> C implementations, POSIX or otherwise, in which fopen() *doesn't*
> set errno to some sensible value on failure

Traditional Unix implementations didn't explicitly set errno but most
fopen() failures are caused by the failure of an underlying system call
which does, so it works in most cases.  But not all: there were some
obscure failure modes that left errno set to a nonsensical value.
-- 
Larry Jones

Any game without push-ups, hits, burns or noogies is a sissy game. -- Calvin
0
lawrence
7/27/2010 3:38:54 PM
Reply:

Similar Artilces:

[ace-bugs] FW: building ACE 6.2.0/6.2.2 win XL C/C++ v12.1 on AIX7.1 results in compiler error when instantiating ace templates
--_004_80317163A516B94B988E1615F9FAFB1989DFD215otwlxg22opentex_ Content-Type: multipart/alternative; boundary="_000_80317163A516B94B988E1615F9FAFB1989DFD215otwlxg22opentex_" --_000_80317163A516B94B988E1615F9FAFB1989DFD215otwlxg22opentex_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable To: ace-bugs@list.isis.vanderbilt.edu<mailto:ace-bugs@list.isis.vanderbilt.= edu> Subject: [area]: [synopsis] ACE VERSION: 6.2.2 HOST MACHINE and OPERATING SYSTEM: AIX 7.1 TARGET MACHINE and OPERATING SYS...

What is code review? (Java code review)
Hi,My boss just told me a concept I didn't know. (I am middle level Java programmer.) -- "code review".According to what I heard, "code review" is somebody reads the thousands lines of code written by other person and try to find if there are some errors (logic errors, I guess, since the code at least can be compiled and run).I feel this is crazy!!! Since the reviewer has to "read" the original code author's mind and make sure the code does what the author wants and no hidden surprises! How this could be possible?! This would be extremely time consuming an...

Any C code are valid C++ code?
Since C is a subset of C++, so any C code or C libraries (printf(), scanf(), etc...) are valid C++ code. Is that correct? so even though the whole program is written in C, but with .cpp extension, we still consider as C++ program? Please advise. Thanks jrefactors@hotmail.com wrote: > Since C is a subset of C++ [...] Wrong premise. Wrong conclusion. The answer to your subj is "no". jrefactors@hotmail.com wrote: > Since C is a subset of C++ C is not a subset of C++. C++ has some incompatible changes from C. However, they are compatible enough that a lot of code runs in ...

Why (eql #c(1 0) 1) but (not (eql #c(1.0 0.0) 1.0))?
Is there a deep reason--mathematical, implementational or otherwise--that numbers of type (complex float) can have a zero imaginary part while (complex rationals) can not? Or is it just one of those things? -Peter -- Peter Seibel peter@javamonkey.com Lisp is the red pill. -- John Fraser, comp.lang.lisp Peter Seibel <peter@javamonkey.com> writes: > Is there a deep reason--mathematical, implementational or > otherwise--that numbers of type (complex float) can have a zero > imaginary part while (complex rationals) can not? Or is i...

C/C++ source code to Read AND Write *.ini files
Do you know wher can I find C/C++ source code to Read AND Write *.ini files in the following format? demo.ini ======= [Section1] Ident1=100 Ident2=123.456 Ident3=Hello [Section2] Ident1=World Ident2=999 Ident3=98.765 I'm looking something like int ReadInteger(const string Section, const string Ident, int Default); double ReadFloat(const string Section, const string Name, double Default); void WriteInteger(const string Section, const string Ident, int Value); void WriteFloat(const string Section, const string Name, double Value); Any URL for this? THANKS ...

how to use the design results of the vhdl code for a program in C code
hello, i need help. i work over an algo in C langage that verifies a design described in VHDL for it, i want take all informations from my vhdl code for use them in my programme in C and i don't know how. On Mon, 15 Mar 2010 02:51:22 -0700 (PDT), lolita grenoble <lolita.grenoble@gmail.com> wrote: >hello, >i need help. >i work over an algo in C langage that verifies a design described in >VHDL >for it, i want take all informations from my vhdl code for use them in >my programme in C and i don't know how. One way is to write a file in your VHDL code (running it...

New C++ Example Source Codes: MyUppEdoc version 1.1
FOR IMMEDIATE RELEASE CONTACT: Yeoh HS author@yeohhs.com http://www.yeohhs.com Announcing New C++ Example Source Codes: MyUppEdoc version 1.1 Yeoh HS, who runs an IT consulting firm, is pleased to announce the release of a C++ example source codes package, "MyUppEdoc". Would you like to know how to create e-document programs using Visual C ++ 2005 Express Edition and the Platform SDK with Ultimate++ framework? MyUppEdoc is a Windows executable program that displays e-documents. It was built using Visual C++ 2005 Express Edition with the Platform SDK R2 (Microso...

calling Matlab code from C/C++ code
Hi, I am learning to call Matlab code from C/C++ code. Here are my questions (1)are using Matlab engine and converting Matlab code into a C or C++ shared library the only two ways? (2) what's the difference between these two methods? Advantages and disadvantages? In what case use which method is better? Thanks and regards! On Mon, 03 Aug 2009 10:33:00 -0400, Tim <timlee126@yahoo.com> wrote: > Hi, > I am learning to call Matlab code from C/C++ code. Here are my > questions > > (1)are using Matlab engine and converting Matlab code into a C or C++ > shared library...

I wrote a C++ code generator in Python, would anyone please help me to review the code? :)
fgen is a free command line tool that facilitates cross platform c++ development, including header generation, cpp file generation, makefile generation, unit test framework generation, etc. http://sf.net/projects/fgen I'm not very familiar with Python. Any feedback are appreciated! Or anyone like to develop it with me? Thanks. Hi, I am interested in your proposal. I am basically a C/C++ programmer, but recently fell in love with python. Please send more details on fgen. We could probably start working. :) Kevin Wan wrote: > fgen is a free command line tool that facilitates cross pl...

ANNC: Code Co-op v4.1 simplifies code review and merges
Seattle-based Reliable Software announces the release of Code Co-op version 4.1. Code Co-op is the affordable peer-to-peer version control system supporting distributed development through Email, LAN, and VPN. Code Co-op v4.1 features the inclusion of Beyond Compare Differ 2.1, a �lite' version of Beyond Compare� 2.1 by Scooter Software, Inc. This �lite' version was created especially for Code Co-op. Users can choose between the built-in differ and the Beyond Compare Differ. Version 4.1 also includes integration with the Beyond Compare 2.1 and Borland Delphi 8. Code Co-op 4....

mex compiling c++ code but not c code
i get the following error when i try to compile the following code: //hello.c #include <mex.h> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mexPrintf("Hello World!\n"); } / usr/local/MATLAB/R2012a/bin/mex: 1: eval: -c: not found mex: compile of ' "hello.c"' failed. Error using mex (line 206) Unable to complete successfully. when i use the same code but name it as hello.cpp it compiles fine & prints hello world. im using matlab 2012a,ubuntu 12.04,gcc-4.6,g++-4.6 my mex-opts.sh has C...

source code browser for C / C++ code
hi, which is the best source code browser for c / c++ source code... on windows.. better if its free download... i tried source navigator 5.1.4 and 5.2b2.. but it is crashing while parsing the c++ source code... i have a huge source code to dig into to fix bugs... pls help... -- rahul Rahul wrote: > which is the best source code browser for c / c++ source code... on > windows.. [..] Why don't you ask in a Windows programming newsgroup? V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask * Victor Baza...

Having C code looking like C++ code
Hi, I've been asked in a job interview how to make C code look like C++ code, and honestly I didn't know what to answer because I have never really done a lot of C. Now, I've been searching around the web about web sites that talk about this subject, but I've had no luck. Can anyone point me to some web site about this subject? Thanks a lot! Fronsac wrote: > Hi, > I've been asked in a job interview how to make C code look like C++ > code, and honestly I didn't know what to answer because I have never > really done a lot of C. Now, I've been s...

C Code to Access and Modify C-ISAM files on AIX 4.1
Hello, We have an IBM laptop running AIX 4.1 with Tetra T2000 accounting software installed. Part of this software are C-ISAM files with their definitions. Does anybody have any sample C code that will allow us to read and modify these C-ISAM files? We are competent in C, but by no means experts, so a helping hand would be well appreciated. Thanks in advance, - Richard Minnis - Richard, I strongly recommend getting hold of Informix Standard Engine. Unfortunately, you can only download this from www.iiug.org for Linux or Windows, so you will need to buy it from IBM for...

Web resources about - code review: read in ps(1) result - comp.lang.c

Resources last updated: 2/4/2016 3:44:25 AM