]>
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
80 off_t
seek(off_t position
, int whence
= SEEK_SET
);
83 void *mmap(int prot
= PROT_READ
, size_t length
= 0, int flags
= MAP_FILE
,
84 off_t offset
= 0, void *addr
= NULL
);
87 int fcntl(int cmd
, int arg
= 0) const;
88 int fcntl(int cmd
, void *arg
) const;
90 void flags(int flags
) const;
91 void setFlag(int flag
, bool on
= true) const;
92 void clearFlag(int flag
) const { setFlag(flag
, false); }
95 int ioctl(int cmd
, void *arg
) const;
96 template <class Arg
> Arg
iocget(int cmd
) const
97 { Arg arg
; ioctl(cmd
, &arg
); return arg
; }
98 template <class Arg
> void iocget(int cmd
, Arg
&arg
) const
100 template <class Arg
> void iocset(int cmd
, const Arg
&arg
)
101 { ioctl(cmd
, const_cast<Arg
*>(&arg
)); }
103 // stat-related utilities. @@@ should cache??
104 typedef struct stat UnixStat
;
105 void fstat(UnixStat
&st
) const;
106 size_t fileSize() const;
108 // stdio interactions
109 FILE *fdopen(const char *mode
= NULL
); // fdopen(3)
112 int mFd
; // UNIX file descriptor
115 bool mAtEnd
; // end-of-data indicator (after zero read)
119 } // end namespace UnixPlusPlus
120 } // end namespace Security
123 #endif //_H_UNIXPLUSPLUS