Unix Programming - translations of creat() and write() to ISO C

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > translations of creat() and write() to ISO C





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author translations of creat() and write() to ISO C
Wim Deprez

2004-08-18, 5:57 pm

Hello Group,

here is my attempt to translate creat() and write() to ISO C:

int creat(const char *pathname, mode_t mode)
{
FILE *output_file;

if ((output_file = fopen(pathname, "a")) == NULL)
//appending => O_CREAT|O_WRONLY|O_TRUNC
return -1;

return fileno(output_file);
}

ssize_t write(int fd, const void *buf, size_t count)
{
FILE *output_file = fdopen(fd, "a");
unsigned int i;
for(i = 0; i < count; i++)
if(putc(( char ) * ( ( unsigned char * ) buf + i ), output_file)
== EOF)
return -1;
return count;
}


I think that should work now, any comments?

Do I need to delete the output_file in write(...)?

Kind greetings,

--wim
Pascal Bourguignon

2004-08-18, 5:57 pm

wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:

> Hello Group,
>
> here is my attempt to translate creat() and write() to ISO C:
>
> int creat(const char *pathname, mode_t mode)
> {
> FILE *output_file;
>
> if ((output_file = fopen(pathname, "a")) == NULL)
> //appending => O_CREAT|O_WRONLY|O_TRUNC
> return -1;
>
> return fileno(output_file);
> }
>
> ssize_t write(int fd, const void *buf, size_t count)
> {
> FILE *output_file = fdopen(fd, "a");
> unsigned int i;
> for(i = 0; i < count; i++)
> if(putc(( char ) * ( ( unsigned char * ) buf + i ), output_file)
> == EOF)
> return -1;
> return count;
> }
>
>
> I think that should work now, any comments?


How do you think fopen is implemented?

> Do I need to delete the output_file in write(...)?


How do you think putc is implemented?


--
__Pascal Bourguignon__ http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
Måns Rullgård

2004-08-18, 5:57 pm

wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:

> Hello Group,
>
> here is my attempt to translate creat() and write() to ISO C:


Why are you doing this?

> int creat(const char *pathname, mode_t mode)
> {
> FILE *output_file;
>
> if ((output_file = fopen(pathname, "a")) == NULL)
> //appending => O_CREAT|O_WRONLY|O_TRUNC


Incorrect. The equivalent of O_CREAT|O_WRONLY|O_TRUNC is "w".

> return -1;
>
> return fileno(output_file);
> }
>
> ssize_t write(int fd, const void *buf, size_t count)
> {
> FILE *output_file = fdopen(fd, "a");
> unsigned int i;
> for(i = 0; i < count; i++)
> if(putc(( char ) * ( ( unsigned char * ) buf + i ), output_file)
> == EOF)
> return -1;
> return count;
> }


Why not fwrite(buf, 1, count, output_file)?

> I think that should work now, any comments?
>
> Do I need to delete the output_file in write(...)?


Unless you want to lose data and leak memory, yes. You could also use
some scheme to cache the FILE* between calls.

--
Måns Rullgård
mru@mru.ath.cx
Nick Landsberg

2004-08-18, 5:57 pm

M=E5ns Rullg=E5rd wrote:

> wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:
>=20
>=20
>=20
>=20
> Why are you doing this?
>=20
>=20
>=20
>=20
> Incorrect. The equivalent of O_CREAT|O_WRONLY|O_TRUNC is "w".
>=20
>=20
>=20
>=20
> Why not fwrite(buf, 1, count, output_file)?
>=20
>=20
>=20
>=20
> Unless you want to lose data and leak memory, yes. You could also use
> some scheme to cache the FILE* between calls.
>=20

Agreed, incomplete error checking, an obvious memory
leak and the "write" routine does not maintain the
semantics of the original "write" (e.g. what if there
was an "lseek()" in between?). All these need fixing.

But the major problem was pointed out by Pascal
in a previous reply.

I would expect that when this was compiled and run
that there would be a stack overflow almost immediately.
(I wonder if there is an errno for that or what exit
code would be generated by a Unix-like OS for this?
Doing a back stacktrace through the core dump would
be an "interesting" exercise.

Broader hint to the OP - at some point in time
the fopen() call must call some "system primitives"
(such as "creat()" when given the "a" option).
Unless I am totally mistaken, this will wind up
calling *your* creat() call again, which will
call fopen(), which will call your creat(), etc, etc,
etc,.

There are creative ways around this, possibly
using the pre-processor. (e.g.
#define creat MY_creat
#define write MY_write
) in all the original source files

But, these are only band-aids.
If you have access to the source to do such creative
things, you might as well change the source to use the
ISO/POSIX calls directly. It won't be a mechnical
process, and you'll have to inspect a good deal
of the source and run full regression tests.

NPL

--=20
"It is impossible to make anything foolproof
because fools are so ingenious"
- A. Bloch
Måns Rullgård

2004-08-18, 5:57 pm

Nick Landsberg <SPAMhukolauTRAP@SPAMworldnetTRAP.att.net> writes:

> I would expect that when this was compiled and run
> that there would be a stack overflow almost immediately.
> (I wonder if there is an errno for that or what exit
> code would be generated by a Unix-like OS for this?


Segmentation fault (core dumped)

> Doing a back stacktrace through the core dump would
> be an "interesting" exercise.


Been there, done that, got bored after a few pages.

--
Måns Rullgård
mru@kth.se
Barry Margolin

2004-08-18, 8:48 pm

In article <a9b0f241.0408181415.a24049c@posting.google.com>,
wim.deprez+google@student.luc.ac.be (Wim Deprez) wrote:

> Hello Group,
>
> here is my attempt to translate creat() and write() to ISO C:
>
> int creat(const char *pathname, mode_t mode)
> {
> FILE *output_file;
>
> if ((output_file = fopen(pathname, "a")) == NULL)
> //appending => O_CREAT|O_WRONLY|O_TRUNC
> return -1;
>
> return fileno(output_file);


fileno() is not part of ISO C, it's a POSIX function. If you have
fileno(), you almost certainly already have creat() and write().

> }
>
> ssize_t write(int fd, const void *buf, size_t count)
> {
> FILE *output_file = fdopen(fd, "a");


The same goes for fdopen().

So what's the point of this?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Wim Deprez

2004-08-19, 7:48 am

Barry Margolin <barmar@alum.mit.edu> wrote in message news:<barmar-CD916E.20454418082004@comcast.dca.giganews.com>...
>
> So what's the point of this?


i was told to port a framework (made for Solaris) to win32 and noticed
that win32 does not have creat() or write(), so i tried to figure out
a way around that and wanted to override the functions. Just an hour
ago i learned about the _creat and _write in io.h, so i guess my work
was pretty useless :-S

i want to thank all of you guys, because i learned some valuable
lessons today, thank you

greetings,

--wim (still learning (-
Pascal Bourguignon

2004-08-19, 5:58 pm

wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:

> Barry Margolin <barmar@alum.mit.edu> wrote in message news:<barmar-CD916E.20454418082004@comcast.dca.giganews.com>...
>
> i was told to port a framework (made for Solaris) to win32 and noticed
> that win32 does not have creat() or write(), so i tried to figure out
> a way around that and wanted to override the functions. Just an hour
> ago i learned about the _creat and _write in io.h, so i guess my work
> was pretty useless :-S


http://www.cygwin.com/


> i want to thank all of you guys, because i learned some valuable
> lessons today, thank you



--
__Pascal Bourguignon__ http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com