Angband
Data Structures | Macros | Functions | Variables
z-file.c File Reference

Low-level file (and directory) handling. More...

#include "h-basic.h"
#include "z-file.h"
#include "z-form.h"
#include "z-util.h"
#include "z-virt.h"
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>

Data Structures

struct  ang_file
 
struct  ang_dir
 

Directory scanning API

More...
 

Macros

#define my_mkdir(path, perms)   false
 
#define S_IRUSR   S_IREAD
 

File-handling API

More...
 
#define S_IWUSR   S_IWRITE
 
#define O_BINARY   0
 
#define TAB_COLUMNS   4
 Line-based IO. More...
 

Functions

void safe_setuid_drop (void)
 Drop permissions. More...
 
void safe_setuid_grab (void)
 Grab permissions. More...
 
static void path_parse (char *buf, size_t max, const char *file)
 Apply special system-specific processing before dealing with a filename. More...
 
static void path_process (char *buf, size_t len, size_t *cur_len, const char *path)
 
size_t path_build (char *buf, size_t len, const char *base, const char *leaf)
 Create a new path string by appending a 'leaf' to 'base'. More...
 
size_t path_filename_index (const char *path)
 Return the index of the filename in a path, using PATH_SEPC. More...
 
bool file_delete (const char *fname)
 Utility functions. More...
 
bool file_move (const char *fname, const char *newname)
 Move file 'fname' to 'newname'. More...
 
bool file_exists (const char *fname)
 Decide whether a file exists or not. More...
 
bool file_newer (const char *first, const char *second)
 Return true if first is newer than second, false otherwise. More...
 
ang_filefile_open (const char *fname, file_mode mode, file_type ftype)
 Open file 'fname', in mode 'mode', with filetype 'ftype'. More...
 
bool file_close (ang_file *f)
 Close file handle 'f'. More...
 
void file_lock (ang_file *f)
 Locking functions. More...
 
void file_unlock (ang_file *f)
 Unlock a file locked using file_lock(). More...
 
bool file_skip (ang_file *f, int bytes)
 Byte-based IO and functions. More...
 
bool file_readc (ang_file *f, byte *b)
 Read a single, 8-bit character from file 'f'. More...
 
bool file_writec (ang_file *f, byte b)
 Write a single, 8-bit character 'b' to file 'f'. More...
 
int file_read (ang_file *f, char *buf, size_t n)
 Read 'n' bytes from file 'f' into array 'buf'. More...
 
bool file_write (ang_file *f, const char *buf, size_t n)
 Append 'n' bytes of array 'buf' to file 'f'. More...
 
bool file_getl (ang_file *f, char *buf, size_t len)
 Line-based IO. More...
 
bool file_put (ang_file *f, const char *buf)
 Append a line of text 'buf' to the end of file 'f', using system-dependent line ending. More...
 
bool file_putf (ang_file *f, const char *fmt,...)
 Append a formatted line of text to the end of file 'f'. More...
 
bool file_vputf (ang_file *f, const char *fmt, va_list vp)
 Append a formatted line of text to the end of file 'f'. More...
 
bool dir_exists (const char *path)
 

Directory code

More...
 
bool dir_create (const char *path)
 Create's the given directory, creating intermediate directories if needed and possible. More...
 
ang_dirmy_dopen (const char *dirname)
 Opens a directory handle. More...
 
bool my_dread (ang_dir *dir, char *fname, size_t len)
 Reads a directory entry. More...
 
void my_dclose (ang_dir *dir)
 Close a directory handle. More...
 

Variables

int player_uid
 Player info. More...
 
int player_egid
 
void(* file_open_hook )(const char *path, file_type ftype)
 File-handle functions. More...
 

Detailed Description

Low-level file (and directory) handling.

Copyright (c) 1997-2007 Ben Harrison, pelpel, Andi Sidwell, Matthew Jones

This work is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation, version 2, or

b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.

Macro Definition Documentation

◆ my_mkdir

#define my_mkdir (   path,
  perms 
)    false

Referenced by dir_create().

◆ O_BINARY

#define O_BINARY   0

Referenced by file_open().

◆ S_IRUSR

#define S_IRUSR   S_IREAD


File-handling API

Referenced by file_open().

◆ S_IWUSR

#define S_IWUSR   S_IWRITE

Referenced by file_open().

◆ TAB_COLUMNS

#define TAB_COLUMNS   4

Line-based IO.

Read a line of text from file 'f' into buffer 'buf' of size 'n' bytes.

Support both
and
as line endings, but not the outdated that used to be used on Macs. Replace non-printables with '?', and with ' '.

Referenced by file_getl().

Function Documentation

◆ dir_create()

bool dir_create ( const char *  dirname)

Create's the given directory, creating intermediate directories if needed and possible.

Returns whether or not the directory was created successfully.

References buf, dir_create(), dir_exists(), ang_file::fname, format(), h, mem_free(), mem_zalloc(), my_dclose(), my_dopen(), my_dread(), my_mkdir, my_strcpy(), NULL, ok, path, PATH_SEPC, ptr, string_make(), and true.

Referenced by change_path(), create_needed_dirs(), dir_create(), and stats_make_output_dir().

◆ dir_exists()

bool dir_exists ( const char *  dirname)


Directory code

Return whether or not a directory exists.

Referenced by dir_create().

◆ file_close()

bool file_close ( ang_file f)

◆ file_delete()

bool file_delete ( const char *  fname)

Utility functions.

Tries to delete fname.

Delete file 'fname'.

References buf, and path_parse().

Referenced by do_cmd_save_screen_html(), highscore_write(), remove_old_dump(), savefile_save(), teardown_tests(), and text_lines_to_file().

◆ file_exists()

bool file_exists ( const char *  fname)

◆ file_getl()

bool file_getl ( ang_file f,
char *  buf,
size_t  n 
)

Line-based IO.

Get a line of text from the file represented by f, placing it into buf to a maximum length of n.

This expands tabs, replaces non-printables with '?', and deals with differing line endings.

Returns true when data is returned; false otherwise.

References false, ang_file::fh, file_readc(), i, max_len, SEEK_CUR, and TAB_COLUMNS.

Referenced by display_winner(), init_graphics_modes(), parse_file(), print_tomb(), process_pref_file_named(), remove_old_dump(), show_file(), and show_splashscreen().

◆ file_lock()

void file_lock ( ang_file f)

Locking functions.

File locking.

Lock a file using POSIX locks, on platforms where this is supported.

References ang_file::fh, ang_file::mode, MODE_READ, and SEEK_SET.

Referenced by highscore_write().

◆ file_move()

bool file_move ( const char *  fname,
const char *  newname 
)

Move file 'fname' to 'newname'.

Moves the file fname to newname.

References buf, and path_parse().

Referenced by activate_randart_file(), file_archive(), highscore_write(), remove_old_dump(), savefile_save(), and text_lines_to_file().

◆ file_newer()

bool file_newer ( const char *  first,
const char *  second 
)

Return true if first is newer than second, false otherwise.

Returns true if the file first is newer than second.

References true.

◆ file_open()

ang_file* file_open ( const char *  fname,
file_mode  mode,
file_type  ftype 
)

◆ file_put()

bool file_put ( ang_file f,
const char *  buf 
)

Append a line of text 'buf' to the end of file 'f', using system-dependent line ending.

Write the string pointed to by buf to the file represented by f.

References file_write().

Referenced by dump_history(), and file_vputf().

◆ file_putf()

bool file_putf ( ang_file f,
const char *  fmt,
  ... 
)

◆ file_read()

int file_read ( ang_file f,
char *  buf,
size_t  n 
)

Read 'n' bytes from file 'f' into array 'buf'.

Reads n bytes from file 'f' into buffer 'buf'.

References ang_file::fh, and read.

Referenced by check_header(), highscore_read(), load_block(), and next_blockheader().

◆ file_readc()

bool file_readc ( ang_file f,
byte b 
)

Read a single, 8-bit character from file 'f'.

Read a byte from the file represented by f and place it at the location specified by 'b'.

References ang_file::fh, and i.

Referenced by file_getl().

◆ file_skip()

bool file_skip ( ang_file f,
int  bytes 
)

Byte-based IO and functions.

Byte-based IO.

Seek to location 'pos' in file 'f'.

References ang_file::fh, and SEEK_CUR.

Referenced by skip_block().

◆ file_unlock()

void file_unlock ( ang_file f)

Unlock a file locked using file_lock().

References ang_file::fh, and SEEK_SET.

◆ file_vputf()

bool file_vputf ( ang_file f,
const char *  fmt,
va_list  vp 
)

Append a formatted line of text to the end of file 'f'.

file_vputf() is the va_list version. It returns true if the write was successful and false otherwise.

References buf, file_put(), void(), and vstrnfmt().

Referenced by file_putf().

◆ file_write()

bool file_write ( ang_file f,
const char *  buf,
size_t  n 
)

Append 'n' bytes of array 'buf' to file 'f'.

Write the first n bytes following the pointer buf to the file represented by f.

References ang_file::fh.

Referenced by file_put(), file_writec(), highscore_write(), savefile_save(), text_out_to_file(), and try_save().

◆ file_writec()

bool file_writec ( ang_file f,
byte  b 
)

Write a single, 8-bit character 'b' to file 'f'.

Write the byte b to the file represented by f.

References file_write().

Referenced by spoiler_out_n_chars(), and text_out_to_file().

◆ my_dclose()

void my_dclose ( ang_dir dir)

Close a directory handle.

References ang_dir::d, ang_dir::dirname, and mem_free().

Referenced by dir_create(), and list_saves().

◆ my_dopen()

ang_dir* my_dopen ( const char *  dirname)

Opens a directory handle.

dirname must be a system-specific pathname to the directory you want scanned.

Returns a valid directory handle on success, NULL otherwise.

References ang_dir::d, ang_dir::dirname, mem_zalloc(), NULL, and string_make().

Referenced by dir_create(), and list_saves().

◆ my_dread()

bool my_dread ( ang_dir dir,
char *  fname,
size_t  len 
)

Reads a directory entry.

dir must point to a directory handle previously returned by my_dopen(). fname must be a pointer to a writeable chunk of memory len long.

Returns true on successful reading, false otherwise. (false generally indicates that there are no more files to be read.)

References ang_dir::d, ang_dir::dirname, my_strcpy(), NULL, path, and path_build().

Referenced by dir_create(), and list_saves().

◆ path_build()

size_t path_build ( char *  buf,
size_t  len,
const char *  base,
const char *  leaf 
)

◆ path_filename_index()

size_t path_filename_index ( const char *  path)

Return the index of the filename in a path, using PATH_SEPC.

If no path separator is found, return 0.

References i, and PATH_SEPC.

Referenced by process_character_pref_files().

◆ path_parse()

static void path_parse ( char *  buf,
size_t  max,
const char *  file 
)
static

Apply special system-specific processing before dealing with a filename.

References my_strcpy().

Referenced by file_delete(), file_move(), and file_open().

◆ path_process()

static void path_process ( char *  buf,
size_t  len,
size_t *  cur_len,
const char *  path 
)
static

References i, PATH_SEP, and strnfcat().

Referenced by path_build().

◆ safe_setuid_drop()

void safe_setuid_drop ( void  )

Drop permissions.

References quit().

Referenced by highscore_write(), main(), prefs_save(), savefile_save(), and text_lines_to_file().

◆ safe_setuid_grab()

void safe_setuid_grab ( void  )

Grab permissions.

Drop or grab privileges.

References player_egid, and quit().

Referenced by highscore_write(), prefs_save(), savefile_save(), and text_lines_to_file().

Variable Documentation

◆ file_open_hook

void(* file_open_hook) (const char *path, file_type ftype)

File-handle functions.

Platform hook for file_open.

Referenced by file_open().

◆ player_egid

int player_egid

Referenced by main(), and safe_setuid_grab().

◆ player_uid

int player_uid

Player info.


Permissions code

Referenced by build_score(), list_saves(), main(), and savefile_set_name().