473,568 Members | 2,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: programi parsing question

Scott Lurndal wrote:
>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)
The open system call is defined to return '-1' on failure, not any
negative number. So the above check is incorrect. This can
cause problems with mmap, lseek and other system calls whose return
values, if declared or cast as signed, while legal are negative.

mmap() should always be checked against MAP_FAILED, and lseek return
should always be compared with the expected value (i.e. the offset
arg for SEEK_SET, and the expected offset for the other SEEK_* variants).
Would it also cause a problem if your file descriptors go "around the horn", and
exceed some platform-specific limit, such as 0x80000000? They would "go
negative" when expressed as signed integers...
It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).
Does some relatively standard .h file define a good constant there?

--
Phlip
Aug 7 '08 #1
40 2445
[Followups to c.u.p since this subthread is getting Unix-specific]

On Aug 6, 8:08 pm, Phlip <phlip2...@gmai l.comwrote:
Scott Lurndal wrote:
if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)
The open system call is defined to return '-1' on failure, not any
negative number. So the above check is incorrect. This can
cause problems with mmap, lseek and other system calls whose return
values, if declared or cast as signed, while legal are negative.
mmap() should always be checked against MAP_FAILED, and lseek return
should always be compared with the expected value (i.e. the offset
arg for SEEK_SET, and the expected offset for the other SEEK_* variants).

Would it also cause a problem if your file descriptors go "around the horn", and
exceed some platform-specific limit, such as 0x80000000? They would "go
negative" when expressed as signed integers...
That's guaranteed not to happen. A file descriptor is always a non-
negative integer. So a 32-bit machine wouldn't be allowed to have
more than 2^31 open file descriptors.

So for 'open' in particular, testing for < 0 is the same as testing
for -1, since it promises never to return any other negative value.
As Scott correctly points out, though, there are other functions for
which this is not the case, and so it can be argued it's better to
form the habit of checking for the "error" value (usually -1, but not
always) specifically.

On the other hand, though, testing for < 0 is rather idiomatic. And
another common convention (though not for Unix system calls) is for a
function to be able to return any of several negative values on error,
in which case you must test for < 0. It is common for kernels to use
this convention, because they are inherently multi-threaded and can't
conveniently use something like errno. The case where the return
value is an address can complicate this approach. I believe the Linux
kernel dealt with this issue by guaranteeing that the highest page of
the address space would never be mapped, so that the values -1 through
-4096 could be used for error codes without fear of conflicting with
addresses.
It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).

Does some relatively standard .h file define a good constant there?
I don't believe so.

Aug 7 '08 #2
Phlip wrote:
Scott Lurndal wrote:
>>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)
.... snip ...
>
>It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).

Does some relatively standard .h file define a good constant there?
Please don't delete attributions for quoted material.

No standard .h file describes 'open', because open is not a
standard C defined routine. This does not apply to fopen, which
opens a FILE*.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.

Aug 7 '08 #3
On Aug 7, 7:10 am, CBFalconer <cbfalco...@yah oo.comwrote:
Phlip wrote:
Scott Lurndal wrote:
>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)

... snip ...
It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).
Does some relatively standard .h file define a good constant there?

Please don't delete attributions for quoted material.

No standard .h file describes 'open', because open is not a
standard C defined routine. This does not apply to fopen, which
opens a FILE*.
Also note that _PATH_UTMP is an invalid identifier in standard C,
since it's reserved for the implementation.

Aug 7 '08 #4
vi******@gmail. com wrote:
On Aug 7, 7:10 am, CBFalconer <cbfalco...@yah oo.comwrote:
>Phlip wrote:
Scott Lurndal wrote:
>>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)

... snip ...
>It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).
Does some relatively standard .h file define a good constant there?

Please don't delete attributions for quoted material.

No standard .h file describes 'open', because open is not a
standard C defined routine. This does not apply to fopen, which
opens a FILE*.

Also note that _PATH_UTMP is an invalid identifier in standard C,
since it's reserved for the implementation.
But the code need not have defined _PATH_UTMP. It could be just using
it.

Aug 7 '08 #5
vi******@gmail. com writes:
On Aug 7, 7:10 am, CBFalconer <cbfalco...@yah oo.comwrote:
>Phlip wrote:
Scott Lurndal wrote:
>>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)

... snip ...
>It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).
Does some relatively standard .h file define a good constant there?

Please don't delete attributions for quoted material.

No standard .h file describes 'open', because open is not a
standard C defined routine. This does not apply to fopen, which
opens a FILE*.

Also note that _PATH_UTMP is an invalid identifier in standard C,
since it's reserved for the implementation.
No and yes, respectively. As it happens, _PATH_UTMP is defined by the
implementation (in one of the non-C-standard headers included by the
code we're discussing).

--
Keith Thompson (The_Other_Keit h) ks***@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"
Aug 7 '08 #6
CBFalconer <cb********@yah oo.comwrites:
Phlip wrote:
>Scott Lurndal wrote:
>>>if((fd = open(_PATH_UTMP , O_RDONLY)) < 0)
... snip ...
>>
>>It's good practice to always test for the defined return code
(which in most cases is -1, not < 0).

Does some relatively standard .h file define a good constant there?

Please don't delete attributions for quoted material.

No standard .h file describes 'open', because open is not a
standard C defined routine.
That should have been 'no standardized C-header' (the C-standard does
not require that 'a header' corresponds with 'a file') The IEEE Std
1003.1 defined header <fcntl.his supposed to contain a prototype for
open.
Aug 7 '08 #7
CBFalconer wrote:
Please don't delete attributions for quoted material.
If they are not important to understand one post, alone, they are still
accessible in modern newsreaders' threading systems. Nobody is stealing anyone's
verbiage.

Aug 7 '08 #8
vi******@gmail. com wrote:
Also note that _PATH_UTMP is an invalid identifier in standard C,
since it's reserved for the implementation.
Yet open() _is_ the implementation. It's just not (apparently) in the narrowest
C Standard.
Aug 7 '08 #9
On Aug 7, 3:01 pm, Phlip <phlip2...@gmai l.comwrote:
vipps...@gmail. com wrote:
Also note that _PATH_UTMP is an invalid identifier in standard C,
since it's reserved for the implementation.

Yet open() _is_ the implementation. It's just not (apparently) in the narrowest
C Standard.
open is the implementation? What are you trying to say?
open is POSIX, it was in older UNIX systems and it's mentioned in K&R,
but it's NOT mentioned or defined in the C standard.
Aug 7 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2175
by: Todd Moyer | last post by:
I would like to use Python to parse a *python-like* data description language. That is, it would have it's own keywords, but would have a syntax like Python. For instance: Ob1 ('A'): Ob2 ('B'): Ob3 ('D') Ob3 ('E') Ob2 ('C')
4
2297
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise SAX wouldn't do the parsing. What's wrong with this string (between <audit> and </audit> tags)? I am using SAX/Xerces 2.3.0 on Sun 8. Thanks in...
16
2869
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after...
6
2110
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic question: I want to parse a string like "3.12" to get two integers 3 and 12. I wanted to use the function STRTOK() I wrote a main- and a...
13
1970
by: 31337one | last post by:
Hello everyone, I am writing an application that uses a command line interface. It will be configurable by passing arguments on the command line. The program is going to run in windows and linux. I was wondering what most linux/windows tools use to parse arguments. Example: ls -a or ls --help
5
5504
by: bmichel | last post by:
Hey, What I'm doing is the following: - Load XML data a file - Parsing the XML data - Printing some parsed content The problem is that the script execution is stopping before all the content is parsed and printed. Maybe the PHP is out of memory after a while. That would make sense
6
5915
by: jackwootton | last post by:
Hello everyone, I understand that XML can be parsed using JavaScript using the XML Document object. However, it is possible to parse XHTML using JavaScript? I currently listen for DOMMutation events, when the events occur I access the node which was inserted or removed (event.target). There is only ever about 5 lines of XHTML nested in...
0
1502
by: Ole Nielsby | last post by:
(sorry, wrong button, here is the real post:) I'm working on a C++ parser which is to be used for various code analysis and transformation tools. (It's part of my PILS programming system which will be released as opensource in a not too distant future.) I want to do the parsing using a homebrew recursive-descent- parser-generator...
13
4475
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
13
209
by: CBFalconer | last post by:
fjblurt@yahoo.com wrote: Considering the crosspost, I won't complain about using the non-standard open in place of fopen. However it is inappropriate on comp.programming. I have renamed the two functions. I consider fooA superior. I disapprove of initialization code more complex than a simple value, such as 0, and discourage even...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.