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
|
|
0
|
|
|
|
Reply
|
wim
|
8/18/2004 10:15: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.
|
|
0
|
|
|
|
Reply
|
Pascal
|
8/18/2004 10:29:19 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
|
|
0
|
|
|
|
Reply
|
iso
|
8/18/2004 10:44:14 PM
|
|
M=E5ns Rullg=E5rd wrote:
> wim.deprez+google@student.luc.ac.be (Wim Deprez) writes:
>=20
>=20
>>Hello Group,
>>
>>here is my attempt to translate creat() and write() to ISO C:
>=20
>=20
> Why are you doing this?
>=20
>=20
>> int creat(const char *pathname, mode_t mode)
>> {
>> FILE *output_file;
>>
>> if ((output_file =3D fopen(pathname, "a")) =3D=3D NULL)
>> //appending =3D> O_CREAT|O_WRONLY|O_TRUNC
>=20
>=20
> Incorrect. The equivalent of O_CREAT|O_WRONLY|O_TRUNC is "w".
>=20
>=20
>> return -1;
>>
>> return fileno(output_file);
>> }
>>
>> ssize_t write(int fd, const void *buf, size_t count)
>> {
>> FILE *output_file =3D fdopen(fd, "a");
>> unsigned int i;
>> for(i =3D 0; i < count; i++)
>> if(putc(( char ) * ( ( unsigned char * ) buf + i ), output_file)
>> =3D=3D EOF)
>> return -1;
>> return count;
>> }
>=20
>=20
> Why not fwrite(buf, 1, count, output_file)?
>=20
>=20
>>I think that should work now, any comments?
>>
>>Do I need to delete the output_file in write(...)?
>=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
|
|
0
|
|
|
|
Reply
|
Nick
|
8/18/2004 11:45:16 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
|
|
0
|
|
|
|
Reply
|
iso
|
8/18/2004 11:55:46 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 ***
|
|
0
|
|
|
|
Reply
|
Barry
|
8/19/2004 12:45:44 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 (-:)
|
|
0
|
|
|
|
Reply
|
wim
|
8/19/2004 10:16:08 AM
|
|
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>...
> >
> > 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
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.
|
|
0
|
|
|
|
Reply
|
Pascal
|
8/19/2004 3:26:23 PM
|
|
|
7 Replies
317 Views
(page loaded in 0.11 seconds)
Similiar Articles: translations of creat() and write() to ISO C - comp.unix ...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... Why gcc translate a c program into assemble as follow - comp.lang ...How to use Inline assembly on C64x+ Follow How to use Inline assembly on C64x+ Follow ... assembler that you write into the ... start by compiling a small 'c' program with ... .ini file C/C++ lib needed - comp.unix.programmertranslations of creat() and write() to ISO C - comp.unix ....ini file C/C++ lib needed - comp.unix.programmer Then again, writing your own parser isn't all that difficult. Creating simple menu in Pascal - comp.sys.apple2.programmer ...translations of creat() and write() to ISO C - comp.unix ... Creating simple menu in Pascal - comp.sys.apple2.programmer ... translations of creat() and write() to ISO C ... error compiling the latest glibc - comp.unix.programmerHi, Just for fun, I want to see if I can compile the latest glibc-2.3.5 for use on my linux system. I've tried this on a couple of different platform... Segmentation fault (core dumped) - comp.unix.solaristranslations of creat() and write() to ISO C - comp.unix ... Segmentation fault (core dumped) > Doing a back stacktrace through the core dump would > be an "interesting ... What is Difference between const int i and int const i - comp.lang ...hi all, i want to know the difference between following declarations ... const int i; int const i; is there any difference ? also are int const f... Detecting floating-point error code from Windows Gfortran - comp ...I would prefer if possible not to write and link in a C routine to do this job. ... ieee_arithmetic, using a combination of fenv.h and IA intrinsics, via iso_c ... Explanation needed for const int "error: variably modified ... at ...... visible from the > point of declaration to the end of the translation unit. ... const int i and int const i - comp.lang ..... creat() and write() to ISO C: int creat(const ... Usage of iso_c_binding - comp.lang.fortranIt's iso_c_binding, not _c++_binding, so the linker must see a C ... I updated the example to add a little write statement inside, just to make sure it was working ... translations of creat() and write() to ISO C - comp.unix ...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... How to Make an ISO Image of My C Drive | eHow.comHow to Make an ISO Image of My C Drive. An ISO file is an archival disc image that is easy ... How to Write an ISO Image to a Hard Drive 7/29/2012 10:34:07 AM
|