Changeset 474
- Timestamp:
- 08/09/07 10:53:42 (1 year ago)
- Files:
-
- trunk/src/z-file.c (modified) (5 diffs)
- trunk/src/z-file.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/z-file.c
r469 r474 698 698 699 699 /* 700 * Opens a directory handle. 701 * 702 * `dirname` must be a system-specific pathname to the directory 703 * you want scanned. 704 * 705 * Returns a valid directory handle on success, NULL otherwise. 706 */ 707 ang_dir *my_dopen(const char *dirname); 708 709 710 /* 711 * Reads a directory entry. 712 * 713 * `dir` must point to a directory handle previously returned by my_dopen(). 714 * `fname` must be a pointer to a writeable chunk of memory `len` long. 715 * 716 * Returns TRUE on successful reading, FALSE otherwise. 717 * (FALSE generally indicates that there are no more files to be read.) 718 */ 719 bool my_dread(ang_dir *dir, char *fname, size_t len); 720 721 722 /* 723 * Close a directory handle. 724 */ 725 void my_dclose(ang_dir *dir); 726 727 728 700 * For information on what these are meant to do, please read the header file. 701 */ 729 702 730 703 #ifdef WINDOWS … … 738 711 }; 739 712 740 /* Specified above */741 713 ang_dir *my_dopen(const char *dirname) 742 714 { … … 761 733 } 762 734 763 /* Specified above */764 735 bool my_dread(ang_dir *dir, char *fname, size_t len) 765 736 { … … 823 794 }; 824 795 825 /* Specified above */826 796 ang_dir *my_dopen(const char *dirname) 827 797 { … … 849 819 } 850 820 851 /* Specified above */852 821 bool my_dread(ang_dir *dir, char *fname, size_t len) 853 822 { trunk/src/z-file.h
r469 r474 4 4 #include "h-basic.h" 5 5 6 /*** Permissions code ***/ 7 8 /* 9 * Player's user ID and group ID, respectively. 10 * 11 * Only relevant to POSIX systems that use main.c, and set there. 12 */ 6 13 extern int player_uid; 7 14 extern int player_egid; 8 15 16 /* 17 * Drop or grab privileges. 18 * 19 * This is used on multiuser systems, where the game wants to gain access to 20 * system-wide files like the scores, raw files, or savefiles. Reading from 21 * these locations is permitted by anyone, but writing to them requires a call 22 * to safe_setuid_grab() before opening the file for writing. 23 * 24 * safe_setuid_drop() should be called immediately after the file has been 25 * opened, to prevent security risks, and restores the game's rights so that it 26 * cannot write to the system-wide files. 27 */ 28 void safe_setuid_grab(void); 9 29 void safe_setuid_drop(void); 10 void safe_setuid_grab(void); 11 30 31 32 /*** Path building code ***/ 33 34 /* 35 * Concatenates "leaf" onto the end of "base", using system-specific path 36 * separators, and places the result in buf[], truncated to "len" bytes. 37 * 38 * On Unixes, deals with the tilde as representing home directories. 39 */ 12 40 size_t path_build(char *buf, size_t len, const char *base, const char *leaf); 13 41 14 /* File code */ 42 43 44 /*** File access code ***/ 45 46 /** Data types **/ 47 48 /* 49 * An opaque file handle for Angband file handling. 50 */ 15 51 typedef struct ang_file ang_file; 16 52 53 /* 54 * Specifies what kind of access is required to a file. See file_open(). 55 */ 17 56 typedef enum 18 57 { … … 22 61 } file_mode; 23 62 63 /* 64 * Specified what kind of thing a file is, when writing. See file_open(). 65 */ 24 66 typedef enum 25 67 { 26 FTYPE_TEXT = 1, /* -> FILE_TYPE_TEXT */68 FTYPE_TEXT = 1, 27 69 FTYPE_SAVE, 28 FTYPE_RAW, /* -> FILE_TYPE_DATA */70 FTYPE_RAW, 29 71 FTYPE_HTML 30 72 } file_type; 31 73 74 75 /** Utility functions **/ 76 77 /* 78 * Returns TRUE if `fname` exists (and is a file), FALSE otherwise. 79 */ 32 80 bool file_exists(const char *fname); 81 82 /* 83 * Tries to delete `fname`. 84 * 85 * Returns TRUE if successful, FALSE otherwise. 86 */ 33 87 bool file_delete(const char *fname); 88 89 /* 90 * Moves the file `fname` to `newname`. 91 * 92 * Returns TRUE if successful, FALSE otherwise. 93 */ 34 94 bool file_move(const char *fname, const char *newname); 95 96 /* 97 * Returns TRUE if the file `first` is newer than `second`. 98 */ 35 99 bool file_newer(const char *first, const char *second); 36 100 101 102 /** File handle creation **/ 103 104 /* 105 * Open file `buf`, returning a a file handling representing that file. 106 * 107 * The file mode specifies what kind of access is required to the file: 108 * - MODE_WRITE will overwrite the current contents of the file 109 * - MODE_READ will allow read-only access to the file 110 * - MODE_APPEND will allow write-only access, but will not overwrite the 111 * current contents of the file. 112 * 113 * The file type is specified to allow systems which don't use file extensions 114 * to set the type of the file appropriately. When reading, pass -1 as ftype; 115 * when writing, use whichever filetype seems most appropriate. 116 * 117 * On any kind of error, this function returns NULL. 118 */ 37 119 ang_file *file_open(const char *buf, file_mode mode, file_type ftype); 38 ang_file *file_temp(); 120 121 /* 122 * Attempt to close the file handle `f`. 123 * 124 * Returns TRUE if successful, FALSE otherwise. 125 */ 39 126 bool file_close(ang_file *f); 40 127 128 129 /** File locking **/ 130 131 /* 132 * Lock or unlock the file represented by `f` for writing. 133 * If the file is not open for writing, this call will fail. 134 * 135 * If `f` is closed, the file is automatically unlocked. 136 */ 41 137 void file_lock(ang_file *f); 42 138 void file_unlock(ang_file *f); 43 139 140 141 /** Line-based IO **/ 142 143 /* 144 * Get a line of text from the file represented by `f`, placing it into `buf` 145 * to a maximum length of `n`. 146 * 147 * This expands tabs, replaces non-printables with '?', and deals with differing 148 * line endings. 149 * 150 * Returns TRUE when data is returned; FALSE otherwise. 151 */ 44 152 bool file_getl(ang_file *f, char *buf, size_t n); 153 154 /* 155 * Write the string pointed to by `buf` to the file represented by `f`. 156 * 157 * Returns TRUE if successful, FALSE otherwise. 158 */ 45 159 bool file_put(ang_file *f, const char *buf); 160 161 /* 162 * Format (using strnfmt) the given args, and then call file_put(). 163 */ 46 164 bool file_putf(ang_file *f, const char *fmt, ...); 47 165 166 167 /** Byte-based IO */ 168 169 /* 170 * Seek to position `pos` in the file represented by `f`. 171 * 172 * Returns TRUE if successful, FALSE otherwise. 173 */ 48 174 bool file_seek(ang_file *f, u32b pos); 175 176 /* 177 * Reads `n` bytes from the file represented by `f` into the buffer `buf`. 178 * Do not mix with calls to file_readc(). 179 * 180 * Returns the number of bytes read. 181 */ 49 182 size_t file_read(ang_file *f, char *buf, size_t n); 183 184 /* 185 * Write the first `n` bytes following the pointer `buf` to the file represented 186 * by `f`. Do not mix with calls to file_writec(). 187 * 188 * Returns TRUE if successful, FALSE otherwise. 189 */ 50 190 bool file_write(ang_file *f, const char *buf, size_t n); 191 192 /* 193 * Read a byte from the file represented by `f` and place it at the location 194 * specified by 'b'. 195 * 196 * Returns TRUE if successful, FALSE otherwise. 197 */ 51 198 bool file_readc(ang_file *f, byte *b); 199 200 /* 201 * Write the byte `b` to the file represented by `f`. 202 * 203 * Returns TRUE if successful, FALSE otherwise. 204 */ 52 205 bool file_writec(ang_file *f, byte b); 53 206 54 207 55 208 56 /* Directory code */ 209 /*** Directory code ***/ 210 211 /* 212 * An opaque file handle for Angband directory handling. 213 */ 57 214 typedef struct ang_dir ang_dir; 58 215 216 217 /* 218 * Opens a directory handle. 219 * 220 * `dirname` must be a system-specific pathname to the directory 221 * you want scanned. 222 * 223 * Returns a valid directory handle on success, NULL otherwise. 224 */ 59 225 ang_dir *my_dopen(const char *dirname); 226 227 /* 228 * Reads a directory entry. 229 * 230 * `dir` must point to a directory handle previously returned by my_dopen(). 231 * `fname` must be a pointer to a writeable chunk of memory `len` long. 232 * 233 * Returns TRUE on successful reading, FALSE otherwise. 234 * (FALSE generally indicates that there are no more files to be read.) 235 */ 60 236 bool my_dread(ang_dir *dir, char *fname, size_t len); 237 238 /* 239 * Close a directory handle. 240 */ 61 241 void my_dclose(ang_dir *dir); 62 242
