Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 12 Aug 2001 13:48:15 +0200
From:      Gabriel Ambuehl <gabriel_ambuehl@buz.ch>
To:        freebsd-questions@freebsd.org
Subject:   write() failing..
Message-ID:  <94356165699.20010812134815@buz.ch>

next in thread | raw e-mail | index | archive | help
-----BEGIN PGP SIGNED MESSAGE-----

Hello,

I've there got a problem with write() which is failing for a reason I
can't figure out:

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

//C++ STL
#include <map>
#include <string>


class class_logfiles
  {
    private:
      map<string, int> files;

    public:
      int write_to_log(const string &logfile_path, const string
&message);
      //class_logfiles(void): default constructor
  };


int class_logfiles::write_to_log(const string &logfile_path, const
string &message)
  {
    map<string, int>::iterator miter;
    miter = this->files.find(logfile_path);
    if(miter == this->files.end())      // there isn't any open
logfile with logfile_path
      {
        int current_des;
        current_des=open((const char *) logfile_path.c_str(),
O_APPEND | O_CREAT | O_EXLOCK | O_NOFOLLOW);
        cout << "current_des " << current_des << " errno " << errno
<< " path " << logfile_path.c_str()<< endl;
        this->files[logfile_path]=current_des;
        cout << "write exit status: " << write(current_des,
message.data(), message.size()) << flush;
        cout << " errno: " << errno << " des " << current_des <<endl;
      }
    else
      {
        cout << "write exit status: " << write((*miter).second,
message.data(), message.size()) << flush;
        cout << " errno: " << errno << " des " <<
(*miter).second<<endl;
      }
     return 0;
  }

int main  ()
{
  class_logfiles logfiles;

  string path="/var/log/some_log";
  string message="hello world";
  logfiles.write_to_log((const string &) path, (const string &)
message);
  logfiles.write_to_log((const string &) path, (const string &)
message);
  return 0;
}

The above compiled with g++ and run under 4.4 RC will result in the
following output

current_des 3 errno 0 path /var/log/some_log
write exit status: -1 errno: 0 des 3
write exit status: -1 errno: 9 des 3


which I can't exactly understand as a failing write() should set
errno
but the first time errno remains zero, whereas the second time it
complains about an invalid file descriptor but open did return
a valid descriptor... What's going on here? Any comments would be
greatly appreciated.



TIA & best regards,
 Gabriel
ingx‰#x‰#@	

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5i

iQEVAwUBO3Ze88Za2WpymlDxAQExwAf+M0r6jjxcQLav2OaLnr06jf76wNM7O2NY
vcg5mU5RLsCZSIRw+oAucJpuH/8Vw+wOztUeFOoN3CxtmfsnKoWzjIaB0kqxnsS9
GYWTKvrqm4Eg88V+t/UHUP0eoPQ3d6SrsPYhSE3UnVye32giaZdV8lpiXZVoRKjM
FXEqmDiBW9O3WjB+aYnaQhoQrLdcs8TvKrQoyy7cYPtQiDAgAKg53xCHR8v63KJh
Me8kBtue+kwuKrev/9KC/aQqEu8hZaBTA/ye7EteNBFZ7nA52OXDD/fV12quXIIR
6nHxbCXl6HTUNhGTdYo9sPOA25y9wC+A4D0WkMCuW7qtCm7IPSDltQ==
=3Ggu
-----END PGP SIGNATURE-----


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?94356165699.20010812134815>