1 | /*
|
---|
2 | File: MoreUNIX.h
|
---|
3 |
|
---|
4 | Contains: Generic UNIX utilities.
|
---|
5 |
|
---|
6 | Written by: Quinn
|
---|
7 |
|
---|
8 | Copyright: Copyright (c) 2002 by Apple Computer, Inc., All Rights Reserved.
|
---|
9 |
|
---|
10 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
|
---|
11 | ("Apple") in consideration of your agreement to the following terms, and your
|
---|
12 | use, installation, modification or redistribution of this Apple software
|
---|
13 | constitutes acceptance of these terms. If you do not agree with these terms,
|
---|
14 | please do not use, install, modify or redistribute this Apple software.
|
---|
15 |
|
---|
16 | In consideration of your agreement to abide by the following terms, and subject
|
---|
17 | to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs
|
---|
18 | copyrights in this original Apple software (the "Apple Software"), to use,
|
---|
19 | reproduce, modify and redistribute the Apple Software, with or without
|
---|
20 | modifications, in source and/or binary forms; provided that if you redistribute
|
---|
21 | the Apple Software in its entirety and without modifications, you must retain
|
---|
22 | this notice and the following text and disclaimers in all such redistributions of
|
---|
23 | the Apple Software. Neither the name, trademarks, service marks or logos of
|
---|
24 | Apple Computer, Inc. may be used to endorse or promote products derived from the
|
---|
25 | Apple Software without specific prior written permission from Apple. Except as
|
---|
26 | expressly stated in this notice, no other rights or licenses, express or implied,
|
---|
27 | are granted by Apple herein, including but not limited to any patent rights that
|
---|
28 | may be infringed by your derivative works or by other works in which the Apple
|
---|
29 | Software may be incorporated.
|
---|
30 |
|
---|
31 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
|
---|
32 | WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
|
---|
33 | WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
34 | PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
|
---|
35 | COMBINATION WITH YOUR PRODUCTS.
|
---|
36 |
|
---|
37 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
|
---|
38 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
---|
39 | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
40 | ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
|
---|
41 | OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
|
---|
42 | (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
|
---|
43 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
44 |
|
---|
45 | Change History (most recent first):
|
---|
46 |
|
---|
47 | $Log: MoreUNIX.h,v $
|
---|
48 | Revision 1.2 2002/11/14 20:15:23 eskimo1
|
---|
49 | In MoreUNIXCopyFile, correctly set the modified and accessed times of the resulting file.
|
---|
50 |
|
---|
51 | Revision 1.1 2002/11/09 00:15:24 eskimo1
|
---|
52 | A collection of useful UNIX-level routines.
|
---|
53 |
|
---|
54 |
|
---|
55 | */
|
---|
56 |
|
---|
57 | #pragma once
|
---|
58 |
|
---|
59 | /////////////////////////////////////////////////////////////////
|
---|
60 |
|
---|
61 | // MoreIsBetter Setup
|
---|
62 |
|
---|
63 | #include "MoreSetup.h"
|
---|
64 |
|
---|
65 | #if !TARGET_RT_MAC_MACHO
|
---|
66 | #error MoreUNIX requires the use of Mach-O
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | // System prototypes
|
---|
70 |
|
---|
71 | #include <stdlib.h>
|
---|
72 |
|
---|
73 | /////////////////////////////////////////////////////////////////
|
---|
74 |
|
---|
75 | #ifdef __cplusplus
|
---|
76 | extern "C" {
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | // Macros that act like functions to convert OSStatus error codes to errno-style
|
---|
80 | // error codes, and vice versa. Right now these are just pass throughs because
|
---|
81 | // OSStatus errors are 32 bit signed values that are generally negative, and
|
---|
82 | // errno errors are 32 bit signed values that are small positive.
|
---|
83 |
|
---|
84 | #define OSStatusToEXXX(os) ((int) (os))
|
---|
85 | #define EXXXToOSStatus(ex) ((OSStatus) (ex))
|
---|
86 |
|
---|
87 | // A mechanism to extra errno if a function fails. You typically use this as
|
---|
88 | //
|
---|
89 | // fd = open(...);
|
---|
90 | // err = MoreUNIXErrno(fd);
|
---|
91 | //
|
---|
92 | // or
|
---|
93 | //
|
---|
94 | // err = setuid(0);
|
---|
95 | // err = MoreUNIXErrno(err);
|
---|
96 |
|
---|
97 | #if MORE_DEBUG
|
---|
98 |
|
---|
99 | extern int MoreUNIXErrno(int result);
|
---|
100 |
|
---|
101 | #else
|
---|
102 |
|
---|
103 | #define MoreUNIXErrno(err) (((err) < 0) ? errno : 0)
|
---|
104 |
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | /////////////////////////////////////////////////////////////////
|
---|
108 |
|
---|
109 | extern int MoreGetExecutablePath(char *execPath, size_t *execPathSize);
|
---|
110 | // Returns the path to the current executable in execPath.
|
---|
111 | // On input, execPathSize is the size of the execPath buffer.
|
---|
112 | // On output, execPathSize is the size of the path placed in
|
---|
113 | // that buffer.
|
---|
114 | // Result is an errno error code.
|
---|
115 |
|
---|
116 | /////////////////////////////////////////////////////////////////
|
---|
117 |
|
---|
118 | extern int MoreUNIXRead( int fd, void *buf, size_t bufSize, size_t *bytesRead );
|
---|
119 | // A wrapper around "read" that keeps reading until either
|
---|
120 | // bufSize bytes are read or until EOF is encountered,
|
---|
121 | // in which case you get EPIPE.
|
---|
122 | //
|
---|
123 | // If bytesRead is not NULL, *bytesRead will be set to the number
|
---|
124 | // of bytes successfully read.
|
---|
125 |
|
---|
126 | extern int MoreUNIXWrite(int fd, const void *buf, size_t bufSize, size_t *bytesWritten);
|
---|
127 | // A wrapper around "write" that keeps writing until either
|
---|
128 | // all the data is written or an error occurs, in which case
|
---|
129 | // you get EPIPE.
|
---|
130 | //
|
---|
131 | // If bytesWritten is not NULL, *bytesWritten will be set to the number
|
---|
132 | // of bytes successfully written.
|
---|
133 |
|
---|
134 | extern int MoreUNIXIgnoreSIGPIPE(void);
|
---|
135 | // Sets the handler for SIGPIPE to SIG_IGN. If you don't call
|
---|
136 | // this, writing to a broken pipe will cause SIGPIPE (rather
|
---|
137 | // than having "write" return EPIPE), which is hardly ever
|
---|
138 | // what you want.
|
---|
139 |
|
---|
140 | extern int MoreUNIXCopyDescriptorToDescriptor(int source, int dest);
|
---|
141 | // A naive copy engine, that copies from source to dest
|
---|
142 | // until EOF is encountered on source. Not meant for
|
---|
143 | // copying large amounts of data.
|
---|
144 |
|
---|
145 | extern int MoreUNIXCopyFile(const char *source, const char *dest);
|
---|
146 | // A very naive file copy implementation, that just opens
|
---|
147 | // up source and dest and copies the contents across
|
---|
148 | // using MoreUNIXCopyDescriptorToDescriptor.
|
---|
149 | // It does, however, handle setting the mode and access/modification
|
---|
150 | // times of dest properly.
|
---|
151 |
|
---|
152 | #ifdef __cplusplus
|
---|
153 | }
|
---|
154 | #endif
|
---|