473,699 Members | 2,834 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
40 2468
>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.
The "implementation " is everything that comes with the compiler. Put another
way, your headers are free to work in the _[A-Z] namespace, while you are free
to work in the [A-Za-z] namespace (roughly speaking), with reduced odds of a
name collision.

"The implementation" does _not_ mean "The ISO C Standard".

(BTW I deleted your screen name, just for you;)
Aug 8 '08 #21
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>Phlip wrote:
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.
No, open() is not in ANY C implementation.
[...]

Yes, it certainly is in many C implementations . It's not defined by
the C standard, but it's allowed as an extension.

Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.
And guaranteed to buffer your input, require flushes, and - if I recall
correctly, conflict between read(), seek(), and write() calls...

--
Phlip
Aug 8 '08 #22
On Aug 8, 3:22 am, Phlip <phlip2...@gmai l.comwrote:
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.

The "implementation " is everything that comes with the compiler. Put another
way, your headers are free to work in the _[A-Z] namespace, while you are free
to work in the [A-Za-z] namespace (roughly speaking), with reduced odds of a
name collision.

"The implementation" does _not_ mean "The ISO C Standard".

(BTW I deleted your screen name, just for you;)
Troll or too stupid, either way, from now on you are ignored.
Aug 8 '08 #23
CBFalconer wrote:
Keith Thompson wrote:
>CBFalconer <cb********@yah oo.comwrites:
>>Phlip wrote:
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.
No, open() is not in ANY C implementation.
[...]

Yes, it certainly is in many C implementations . It's not defined by
the C standard, but it's allowed as an extension.

Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.
Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

I don't think I've ever use fopen in a Unix application.

--
Ian Collins.
Aug 8 '08 #24
In article <6g************ @mid.individual .net>,
>Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.
Not at all. You can't fopen() a pipe or socket (actually you can on
some systems), but you can fdopen() or popen() them. And of course
it's quite common for stdin/out to be pipes or sockets.

-- Richard
--
Please remember to mention me / in tapes you leave behind.
Aug 8 '08 #25
Richard Tobin wrote:
In article <6g************ @mid.individual .net>,
>Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

Not at all. You can't fopen() a pipe or socket (actually you can on
some systems), but you can fdopen() or popen() them. And of course
it's quite common for stdin/out to be pipes or sockets.
Maybe, but you can't select or poll on them or pass them to another process.

--
Ian Collins.
Aug 8 '08 #26
Ian Collins wrote:
CBFalconer wrote:
.... snip ...
>
>Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.

Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

I don't think I've ever use fopen in a Unix application.
Interesting. I don't think I have ever actually used a POSIX
system as such, so this is entirely new to me. Why can't pipes and
sockets be mapped into streams?

For comparison, 25 years ago I had no difficulty mapping anything
into Pascal files. That was proper Pascal, with f^, put, and get.
Once you have the proper type for the file items, everything
follows. Remember that Pascal creates new user types on demand,
and those types can be records.

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

Aug 8 '08 #27
CBFalconer <cb********@yah oo.comwrites:
>Ian Collins wrote:
>CBFalconer wrote:
... snip ...
>>
>>Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.

Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

I don't think I've ever use fopen in a Unix application.

Interesting. I don't think I have ever actually used a POSIX
system as such, so this is entirely new to me. Why can't pipes and
sockets be mapped into streams?
They can, as pointed out above, be mapped with 'fdopen'.

However, for each class of application usage one of open or fopen/fdopen
should be used based upon the needs of the application.

For reading a text file or e.g. http socket, where line-oriented semantics
are desired, fopen/fdopen and fgets are appropriate.

For high-performance I/O or for file multiplexing, one should use
open in combination with pread/pwrite, mmap or aio_read/aio_write/lio_listio
with poll/select operations as required.

For I/O to special devices (such as magnetic tapes) where the blocking
is based on the size of the I/O operation, open and read/write are
appropriate.

Buffering in stdio often hurts performance, and for certain types
of tape drives, causes undesired reblocking of the data.
scott
Aug 8 '08 #28
CBFalconer wrote:
Ian Collins wrote:
>CBFalconer wrote:
.... snip ...
>>Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.
Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

I don't think I've ever use fopen in a Unix application.

Interesting. I don't think I have ever actually used a POSIX
system as such, so this is entirely new to me. Why can't pipes and
sockets be mapped into streams?
As others have said, that can. But the extra layer of buffering is
often undesirable but a file descriptor is more flexible.

--
Ian Collins.
Aug 8 '08 #29
Scott Lurndal wrote:
CBFalconer <cb********@yah oo.comwrites:
>Ian Collins wrote:
>>CBFalconer wrote:
... snip ...
>>>
Why does everyone misinterpret my concern here? It must be what I
wrote. At any point, my point is that there is usually NO reason
to use open, when fopen is guaranteed to be documented and
available.

Quite the reverse in a POSIX environment. A FILE* restricts you to
working with files, a file descriptor can be used for other entities
such as pipes and sockets.

I don't think I've ever use fopen in a Unix application.

Interesting. I don't think I have ever actually used a POSIX
system as such, so this is entirely new to me. Why can't pipes and
sockets be mapped into streams?

They can, as pointed out above, be mapped with 'fdopen'.
Well, the quick look I just took at some manuals (not standards)
here indicates that fdopen is basically a way of converting a file
opened by 'open' to one opened by 'fopen'. I have virtually never
needed to use such POSIX extensions.

As I pointed out before, I had no problem mapping devices into the
Pascal file system. This involved a set of routines, that had to
be built for each such file type. I forget the details, but I
could look it up (I still have the assembly listings). There were
some complications, because Pascal doesn't require fopen/fclose,
rather it opens access automatically based on the declarations.
However, there are reset/rewrite routine involvement.

If interested see ppmanual.zip, available in my download section.
Unfortunately I lost most of the sources about 10 years ago in a
combination of disk failures and other disasters to backups.

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

Aug 9 '08 #30

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

Similar topics

2
2179
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
2303
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 advance of any help. Nick Roman <?xml version="1.0" encoding="UTF-8"?> <lyr3:L3Transaction...
16
2892
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 the preload has been completed.
6
2117
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 subfunction like: main() {
13
1981
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
5512
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
5933
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 the node, however it would be silly for me to parse it manually using methods like hasChildNodes...
0
1508
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 system (PILS Kvernbitr) which can produce
13
4508
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 that. fooB must generate a call to open, so it must generate code for that. That code is hidden from
0
9171
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.