]> git.saurik.com Git - apple/security.git/blame - cdsa/cdsa_utilities/unix++.h
Security-30.1.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / unix++.h
CommitLineData
bac41a7b
A
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
37namespace Security {
38namespace UnixPlusPlus {
39
40
41//
42// Generic file descriptors
43//
44class FileDesc {
45protected:
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
52public:
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 // seeking
80 off_t seek(off_t position, int whence = SEEK_SET);
81
82 // mapping support
83 void *mmap(int prot = PROT_READ, size_t length = 0, int flags = MAP_FILE,
84 off_t offset = 0, void *addr = NULL);
85
86 // fcntl support
87 int fcntl(int cmd, int arg = 0) const;
88 int fcntl(int cmd, void *arg) const;
89 int flags() 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); }
93
94 // ioctl support
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
99 { ioctl(cmd, &arg); }
100 template <class Arg> void iocset(int cmd, const Arg &arg)
101 { ioctl(cmd, const_cast<Arg *>(&arg)); }
102
103 // stat-related utilities. @@@ should cache??
104 typedef struct stat UnixStat;
105 void fstat(UnixStat &st) const;
106 size_t fileSize() const;
107
108 // stdio interactions
109 FILE *fdopen(const char *mode = NULL); // fdopen(3)
110
111private:
112 int mFd; // UNIX file descriptor
113
114protected:
115 bool mAtEnd; // end-of-data indicator (after zero read)
116};
117
118
119} // end namespace UnixPlusPlus
120} // end namespace Security
121
122
123#endif //_H_UNIXPLUSPLUS