]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/unix++.cpp
Security-54.1.3.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / unix++.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // unix++ - C++ layer for basic UNIX facilities
21 //
22 #include "unix++.h"
23 #include <Security/debugging.h>
24
25
26 namespace Security {
27 namespace UnixPlusPlus {
28
29
30 //
31 // Generic UNIX file descriptors
32 //
33 void FileDesc::open(const char *path, int flags, mode_t mode)
34 {
35 checkSetFd(::open(path, flags, mode));
36 mAtEnd = false;
37 debug("unixio", "open(%s,0x%x,0x%x) = %d", path, flags, mode, mFd);
38 }
39
40 void FileDesc::close()
41 {
42 if (mFd >= 0) {
43 checkError(::close(mFd));
44 debug("unixio", "close(%d)", mFd);
45 mFd = invalidFd;
46 }
47 }
48
49
50 //
51 // Filedescoid operations
52 //
53 size_t FileDesc::read(void *addr, size_t length)
54 {
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);
59 return 0;
60 }
61 mAtEnd = true;
62 debug("unixio", "%d end of data", mFd);
63 return 0;
64 case -1: // error
65 if (errno == EAGAIN)
66 return 0; // no data, unknown end-of-source status
67 UnixError::throwMe(); // throw error
68 default: // have data
69 return rc;
70 }
71 }
72
73 size_t FileDesc::write(const void *addr, size_t length)
74 {
75 ssize_t rc = ::write(mFd, addr, length);
76 if (rc == -1) {
77 if (errno == EAGAIN)
78 return 0;
79 UnixError::throwMe();
80 }
81 return rc;
82 }
83
84
85 //
86 // Seeking
87 //
88 off_t FileDesc::seek(off_t position, int whence)
89 {
90 off_t rc = ::lseek(mFd, position, whence);
91 if (rc == -1)
92 UnixError::throwMe();
93 return rc;
94 }
95
96
97 //
98 // Mmap support
99 //
100 void *FileDesc::mmap(int prot, size_t length, int flags, off_t offset, void *addr)
101 {
102 void *result = ::mmap(addr, length ? length : fileSize(), prot, flags, mFd, offset);
103 if (result == MAP_FAILED)
104 UnixError::throwMe();
105 return result;
106 }
107
108
109 int FileDesc::fcntl(int cmd, int arg) const
110 {
111 int rc = ::fcntl(mFd, cmd, arg);
112 debug("unixio", "%d fcntl(%d,%d) = %d", mFd, cmd, arg, rc);
113 if (rc == -1)
114 UnixError::throwMe();
115 return rc;
116 }
117
118 int FileDesc::fcntl(int cmd, void *arg) const
119 {
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));
125 }
126
127 int FileDesc::flags() const
128 {
129 int flags = fcntl(F_GETFL);
130 if (flags == -1)
131 UnixError::throwMe();
132 return flags;
133 }
134
135 void FileDesc::flags(int flags) const
136 {
137 if (fcntl(F_SETFL, flags) == -1)
138 UnixError::throwMe();
139 }
140
141 void FileDesc::setFlag(int flag, bool on) const
142 {
143 if (flag) { // if there's anything at all to do...
144 int oldFlags = flags();
145 flags(on ? (oldFlags | flag) : (oldFlags & ~flag));
146 }
147 }
148
149 int FileDesc::ioctl(int cmd, void *arg) const
150 {
151 int rc = ::ioctl(mFd, cmd, arg);
152 if (rc == -1)
153 UnixError::throwMe();
154 return rc;
155 }
156
157
158 void FileDesc::fstat(UnixStat &st) const
159 {
160 if (::fstat(mFd, &st))
161 UnixError::throwMe();
162 }
163
164 size_t FileDesc::fileSize() const
165 {
166 struct stat st;
167 fstat(st);
168 return st.st_size;
169 }
170
171
172 FILE *FileDesc::fdopen(const char *form)
173 {
174 return ::fdopen(mFd, form);
175 }
176
177
178 } // end namespace IPPlusPlus
179 } // end namespace Security