Re: problem w/ function write(...)

From: Floyd L. Davidson (floyd_at_apaflo.com)
Date: 09/28/05


Date: Wed, 28 Sep 2005 07:56:18 -0800


"ferbar" <fbarsoba@gmail.com> wrote:
>Maxim Yegorushkin wrote:
>> ferbar wrote:
>>
>>
>> > fd = open(pathnameX, O_RDWR|O_CREAT|S_IWGRP);
>>
>> You messed up flags. It sould be:
>>
>> open(pathnameX, O_RDWR | O_CREAT, S_IWGRP);
>
>Thanks.. that helped. However, I still having problems.. when the file
>exist, now I get a permission denied message. I change the instruction
>with this:
>
>>> fd = open(pathnameX, O_RDWR | O_CREAT, S_IWUSR);

That gives the user write permission... but not read permission.
Before you were giving the group write permission only. But in
neither case does the user have read permission too.

>With user rights now works until de point it creates the file... but
>the second time I execute it, I get an error. So I changed it for
>this..
>
>>> fd = open(pathnameX, O_RDWR | O_APPEND, S_IWUSR);
>
>But now I get an error if the file exists.

Try this:

  fd = open(pathnameX, O_RDWR | O_APPEND | O_CREAT, (S_IWUSR | S_IRUSR);

With that you will creat the file if it does not exist, it will
be in append mode, and will have read and write permissions for
the owner only.

If you are going to write to one fd and read from another, you
should probably open the first as O_WRONLY and the second as
O_RDONLY, though using O_RDWR will not harm anything.

>Also, when I try to write with this function, does not work either..
>let me show it..
>
>void write1test(int fd, int fd_o, void *buffer, int block_size, char
>*pathnameX) {
>
> ssize_t bytesw, bytesr;
> double t, t_t=0;
> size_t nbytes;

nbytes is not initialized.

> while (bytesr != 0) {
> bytesr = read(fd, buffer, block_size);
> t = now();
> bytesw = write(fd_o, buffer, nbytes);

So how many bytes is this going to write? Undetermined...

> t = now() - t;
> t_t += t;
> if (bytesw < 0) {
> perror("write:");
> exit(bytesw);

The value of bytesw is guaranteed to be negative here, but
exit() needs an argument that is an unsigned char value, between
0 and 255.

It is the exit status returned to the shell, which *will* be a
value from 0 to 255, regardless of the argument you give to
exit(). In other words, a -1 will cause a return status of 255,
and a 256 will cause a return status of 0. There is no harm in
providing values outside the range of 0-255, but it will not be
of any benefit either, and merely confuses the potential value
in having a meaningful exit status for your program.

> }
> }
> printf("time to write blocksizes %d to file %s: %g\n", block_size,
>pathnameX, t_t);
>}
>
>fd is a file desc. of an open file, pointing to the beginning..
>now() gets time of the day..

You might be better off keeping track of time using a long int rather
than a double?

-- 
Floyd L. Davidson            <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska)                         floyd@apaflo.com


Relevant Pages

  • Re: if clause
    ... 3: if (permission < operator) ... Some people think a function should have just ONE clear exit ... Your opinion may vary. ... the exit reason could be lovely described as in "// we really need an open file here" and it could be logged better as ...
    (comp.lang.c)
  • Re: if clause
    ... 3: if (permission < operator) ... Some people think a function should have just ONE clear exit ... Personally I don't mind indenting if it makes the ... Your opinion may vary. ...
    (comp.lang.c)
  • Re: if clause
    ... 3: if (permission < operator) ... Some people think a function should have just ONE clear exit ... Personally I don't mind indenting if it makes the ... Your opinion may vary. ...
    (comp.lang.c)
  • Re: if clause
    ... 3: if (permission < operator) ... Some people think a function should have just ONE clear exit ... We have a whole runtime-enabled logger system so, as long as you log the entry and exit, debugging is easy and the code is easy to read. ... I'm speaking as someone who might not be able to run a debugger on live code (I maintain chunks that are compiled with older compilers that have crufty debuggers that don't run on later releases of Windows, ...
    (comp.lang.c)
  • Re: [SLE] Problem with links to items on vfat partition - SOLVED?
    ... linux:/ # exit ... linux:/home/samjnaa # chmod 0755 /win ... So apparently I need to have entry permission on all parent, grandparent, ... linux:/ # chmod 0755 foo/goo ...
    (SuSE)