]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/unix++.h
Security-54.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / unix++.h
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 #ifndef _H_UNIXPLUSPLUS
23 #define _H_UNIXPLUSPLUS
24
25 #include <Security/utilities.h>
26 #include "timeflow.h"
27 #include <sys/types.h>
28 #include <sys/ioctl.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <fcntl.h>
32 #include <cstdio>
33 #include <cstdarg>
34 #include <map>
35
36
37 namespace Security {
38 namespace UnixPlusPlus {
39
40
41 //
42 // Generic file descriptors
43 //
44 class FileDesc {
45 protected:
46 static const int invalidFd = -1;
47
48 void setFd(int fd) { mFd = fd; mAtEnd = false; }
49 static void checkError(int result) { if (result == -1) UnixError::throwMe(); }
50 void checkSetFd(int fd) { checkError(fd); mFd = fd; mAtEnd = false; }
51
52 public:
53 FileDesc() : mFd(invalidFd), mAtEnd(false) { }
54 FileDesc(int fd) : mFd(fd), mAtEnd(false) { }
55
56 // implicit file system open() construction
57 FileDesc(const char *path, int flag = O_RDONLY, mode_t mode = 0666) : mFd(invalidFd)
58 { open(path, flag, mode); }
59
60 // assignment
61 FileDesc &operator = (int fd) { mFd = fd; mAtEnd = false; return *this; }
62 FileDesc &operator = (const FileDesc &fd) { mFd = fd.mFd; mAtEnd = fd.mAtEnd; return *this; }
63
64 bool isOpen() const { return mFd != invalidFd; }
65 operator bool() const { return isOpen(); }
66 int fd() const { return mFd; }
67 operator int() const { return fd(); }
68
69 void clear() { mFd = invalidFd; }
70 void close(); // close and clear
71
72 void open(const char *path, int flag = O_RDONLY, mode_t mode = 0666);
73
74 // basic I/O: this defines the "Filedescoid" pseudo-type
75 size_t read(void *addr, size_t length);
76 size_t write(const void *addr, size_t length);
77 bool atEnd() const { return mAtEnd; } // valid after zero-length read only
78
79 // more convenient I/O
80 template <class T> size_t read(T &obj) { return read(&obj, sizeof(obj)); }
81 template <class T> size_t write(const T &obj) { return write(&obj, sizeof(obj)); }
82
83 // seeking
84 off_t seek(off_t position, int whence = SEEK_SET);
85
86 // mapping support
87 void *mmap(int prot = PROT_READ, size_t length = 0, int flags = MAP_FILE,
88 off_t offset = 0, void *addr = NULL);
89
90 // fcntl support
91 int fcntl(int cmd, int arg = 0) const;
92 int fcntl(int cmd, void *arg) const;
93 int flags() const;
94 void flags(int flags) const;
95 void setFlag(int flag, bool on = true) const;
96 void clearFlag(int flag) const { setFlag(flag, false); }
97
98 int openMode() const { return flags() & O_ACCMODE; }
99 bool isWritable() const { return openMode() != O_RDONLY; }
100 bool isReadable() const { return openMode() != O_WRONLY; }
101
102 // ioctl support
103 int ioctl(int cmd, void *arg) const;
104 template <class Arg> Arg iocget(int cmd) const
105 { Arg arg; ioctl(cmd, &arg); return arg; }
106 template <class Arg> void iocget(int cmd, Arg &arg) const
107 { ioctl(cmd, &arg); }
108 template <class Arg> void iocset(int cmd, const Arg &arg)
109 { ioctl(cmd, const_cast<Arg *>(&arg)); }
110
111 // stat-related utilities. @@@ should cache??
112 typedef struct stat UnixStat;
113 void fstat(UnixStat &st) const;
114 size_t fileSize() const;
115
116 // stdio interactions
117 FILE *fdopen(const char *mode = NULL); // fdopen(3)
118
119 private:
120 int mFd; // UNIX file descriptor
121
122 protected:
123 bool mAtEnd; // end-of-data indicator (after zero read)
124 };
125
126
127 //
128 // A (plain) FileDesc that auto-closes
129 //
130 class AutoFileDesc : public FileDesc {
131 public:
132 AutoFileDesc() { }
133 AutoFileDesc(int fd) : FileDesc(fd) { }
134
135 AutoFileDesc(const char *path, int flag = O_RDONLY, mode_t mode = 0666)
136 : FileDesc(path, flag, mode) { }
137
138 ~AutoFileDesc() { close(); }
139 };
140
141
142 } // end namespace UnixPlusPlus
143 } // end namespace Security
144
145
146 #endif //_H_UNIXPLUSPLUS