]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/unix++.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // unix++ - C++ layer for basic UNIX facilities
23 #include <Security/debugging.h>
27 namespace UnixPlusPlus
{
31 // Generic UNIX file descriptors
33 void FileDesc::open(const char *path
, int flags
, mode_t mode
)
35 checkSetFd(::open(path
, flags
, mode
));
37 debug("unixio", "open(%s,0x%x,0x%x) = %d", path
, flags
, mode
, mFd
);
40 void FileDesc::close()
43 checkError(::close(mFd
));
44 debug("unixio", "close(%d)", mFd
);
51 // Filedescoid operations
53 size_t FileDesc::read(void *addr
, size_t length
)
55 switch (ssize_t rc
= ::read(mFd
, addr
, length
)) {
56 case 0: // end-of-source
57 if (length
== 0) { // check for errors, but don't set mAtEnd unless we have to
58 debug("unixio", "%d zero read (ignored)", mFd
);
62 debug("unixio", "%d end of data", mFd
);
66 return 0; // no data, unknown end-of-source status
67 UnixError::throwMe(); // throw error
73 size_t FileDesc::write(const void *addr
, size_t length
)
75 ssize_t rc
= ::write(mFd
, addr
, length
);
88 off_t
FileDesc::seek(off_t position
, int whence
)
90 off_t rc
= ::lseek(mFd
, position
, whence
);
100 void *FileDesc::mmap(int prot
, size_t length
, int flags
, off_t offset
, void *addr
)
102 void *result
= ::mmap(addr
, length
? length
: fileSize(), prot
, flags
, mFd
, offset
);
103 if (result
== MAP_FAILED
)
104 UnixError::throwMe();
109 int FileDesc::fcntl(int cmd
, int arg
) const
111 int rc
= ::fcntl(mFd
, cmd
, arg
);
112 debug("unixio", "%d fcntl(%d,%d) = %d", mFd
, cmd
, arg
, rc
);
114 UnixError::throwMe();
118 int FileDesc::fcntl(int cmd
, void *arg
) const
120 // The BSD UNIX headers require an int argument to fcntl.
121 // This will fail miserably if sizeof(void *) > sizeof(int). For such ports,
122 // fix the problem here.
123 assert(sizeof(void *) <= sizeof(int));
124 return fcntl(cmd
, reinterpret_cast<int>(arg
));
127 int FileDesc::flags() const
129 int flags
= fcntl(F_GETFL
);
131 UnixError::throwMe();
135 void FileDesc::flags(int flags
) const
137 if (fcntl(F_SETFL
, flags
) == -1)
138 UnixError::throwMe();
141 void FileDesc::setFlag(int flag
, bool on
) const
143 if (flag
) { // if there's anything at all to do...
144 int oldFlags
= flags();
145 flags(on
? (oldFlags
| flag
) : (oldFlags
& ~flag
));
149 int FileDesc::ioctl(int cmd
, void *arg
) const
151 int rc
= ::ioctl(mFd
, cmd
, arg
);
153 UnixError::throwMe();
158 void FileDesc::fstat(UnixStat
&st
) const
160 if (::fstat(mFd
, &st
))
161 UnixError::throwMe();
164 size_t FileDesc::fileSize() const
172 FILE *FileDesc::fdopen(const char *form
)
174 return ::fdopen(mFd
, form
);
178 } // end namespace IPPlusPlus
179 } // end namespace Security