]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/unix++.h
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
22 #ifndef _H_UNIXPLUSPLUS
23 #define _H_UNIXPLUSPLUS
25 #include <Security/utilities.h>
27 #include <sys/types.h>
28 #include <sys/ioctl.h>
38 namespace UnixPlusPlus
{
42 // Generic file descriptors
46 static const int invalidFd
= -1;
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; }
53 FileDesc() : mFd(invalidFd
), mAtEnd(false) { }
54 FileDesc(int fd
) : mFd(fd
), mAtEnd(false) { }
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
); }
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; }
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(); }
69 void clear() { mFd
= invalidFd
; }
70 void close(); // close and clear
72 void open(const char *path
, int flag
= O_RDONLY
, mode_t mode
= 0666);
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
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
)); }
84 off_t
seek(off_t position
, int whence
= SEEK_SET
);
87 void *mmap(int prot
= PROT_READ
, size_t length
= 0, int flags
= MAP_FILE
,
88 off_t offset
= 0, void *addr
= NULL
);
91 int fcntl(int cmd
, int arg
= 0) const;
92 int fcntl(int cmd
, void *arg
) 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); }
98 int openMode() const { return flags() & O_ACCMODE
; }
99 bool isWritable() const { return openMode() != O_RDONLY
; }
100 bool isReadable() const { return openMode() != O_WRONLY
; }
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
)); }
111 // stat-related utilities. @@@ should cache??
112 typedef struct stat UnixStat
;
113 void fstat(UnixStat
&st
) const;
114 size_t fileSize() const;
116 // stdio interactions
117 FILE *fdopen(const char *mode
= NULL
); // fdopen(3)
120 int mFd
; // UNIX file descriptor
123 bool mAtEnd
; // end-of-data indicator (after zero read)
128 // A (plain) FileDesc that auto-closes
130 class AutoFileDesc
: public FileDesc
{
133 AutoFileDesc(int fd
) : FileDesc(fd
) { }
135 AutoFileDesc(const char *path
, int flag
= O_RDONLY
, mode_t mode
= 0666)
136 : FileDesc(path
, flag
, mode
) { }
138 ~AutoFileDesc() { close(); }
142 } // end namespace UnixPlusPlus
143 } // end namespace Security
146 #endif //_H_UNIXPLUSPLUS