Use stdbool in os module

This commit is contained in:
Hinidu 2014-04-07 00:24:21 +03:00 committed by Thiago de Arruda
parent c3cea30cb7
commit 8a2ffb2b01
10 changed files with 76 additions and 88 deletions

View File

@ -490,17 +490,13 @@ readfile (
}
if (fd < 0) { /* cannot open at all */
#ifndef UNIX
int isdir_f;
#endif
msg_scroll = msg_save;
#ifndef UNIX
/*
* On MSDOS and Amiga we can't open a directory, check here.
*/
isdir_f = (os_isdir(fname));
perm = os_getperm(fname); /* check if the file exists */
if (isdir_f) {
if (os_isdir(fname)) {
filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
curbuf->b_p_ro = TRUE; /* must use "w!" now */
} else
@ -2496,7 +2492,7 @@ buf_write (
int device = FALSE; /* writing to a device */
struct stat st_old;
int prev_got_int = got_int;
int file_readonly = FALSE; /* overwritten file is read-only */
bool file_readonly = false; /* overwritten file is read-only */
static char *err_readonly =
"is read-only (cannot override: \"W\" in 'cpoptions')";
#if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */
@ -5021,7 +5017,7 @@ int vim_rename(char_u *from, char_u *to)
STRCPY(tempname, from);
for (n = 123; n < 99999; ++n) {
sprintf((char *)path_tail(tempname), "%d", n);
if (os_file_exists(tempname) == FALSE) {
if (!os_file_exists(tempname)) {
if (os_rename(from, tempname) == OK) {
if (os_rename(tempname, to) == OK)
return 0;

View File

@ -141,24 +141,24 @@ int os_is_absolute_path(const char_u *fname)
return *fname == '/' || *fname == '~';
}
int os_isdir(const char_u *name)
bool os_isdir(const char_u *name)
{
int32_t mode = os_getperm(name);
if (mode < 0) {
return FALSE;
return false;
}
if (!S_ISDIR(mode)) {
return FALSE;
return false;
}
return TRUE;
return true;
}
static int is_executable(const char_u *name);
static int is_executable_in_path(const char_u *name);
static bool is_executable(const char_u *name);
static bool is_executable_in_path(const char_u *name);
int os_can_exe(const char_u *name)
bool os_can_exe(const char_u *name)
{
// If it's an absolute or relative path don't need to use $PATH.
if (os_is_absolute_path(name) ||
@ -170,32 +170,32 @@ int os_can_exe(const char_u *name)
return is_executable_in_path(name);
}
// Return TRUE if "name" is an executable file, FALSE if not or it doesn't
// Return true if "name" is an executable file, false if not or it doesn't
// exist.
static int is_executable(const char_u *name)
static bool is_executable(const char_u *name)
{
int32_t mode = os_getperm(name);
if (mode < 0) {
return FALSE;
return false;
}
if (S_ISREG(mode) && (S_IEXEC & mode)) {
return TRUE;
return true;
}
return FALSE;
return false;
}
/// Check if a file is inside the $PATH and is executable.
///
/// @return `TRUE` if `name` is an executable inside $PATH.
static int is_executable_in_path(const char_u *name)
/// @return `true` if `name` is an executable inside $PATH.
static bool is_executable_in_path(const char_u *name)
{
const char *path = getenv("PATH");
// PATH environment variable does not exist or is empty.
if (path == NULL || *path == NUL) {
return FALSE;
return false;
}
int buf_len = STRLEN(name) + STRLEN(path) + 2;
@ -217,13 +217,13 @@ static int is_executable_in_path(const char_u *name)
if (is_executable(buf)) {
// Found our executable. Free buf and return.
vim_free(buf);
return OK;
return true;
}
if (*e != ':') {
// End of $PATH without finding any executable called name.
vim_free(buf);
return FALSE;
return false;
}
path = e + 1;
@ -231,7 +231,7 @@ static int is_executable_in_path(const char_u *name)
// We should never get to this point.
assert(false);
return FALSE;
return false;
}
int os_stat(const char_u *name, uv_stat_t *statbuf)
@ -273,23 +273,19 @@ int os_setperm(const char_u *name, int perm)
return FAIL;
}
int os_file_exists(const char_u *name)
bool os_file_exists(const char_u *name)
{
uv_stat_t statbuf;
if (os_stat(name, &statbuf) == OK) {
return TRUE;
return true;
}
return FALSE;
return false;
}
int os_file_is_readonly(const char *name)
bool os_file_is_readonly(const char *name)
{
if (access(name, W_OK) == 0) {
return FALSE;
}
return TRUE;
return access(name, W_OK) != 0;
}
int os_file_is_writable(const char *name)

View File

@ -28,23 +28,23 @@ int os_get_absolute_path(char_u *fname, char_u *buf, int len, int force);
/// Check if the given file is absolute.
///
/// This just checks if the file name starts with '/' or '~'.
/// @return `TRUE` if "fname" is absolute.
/// @return `true` if "fname" is absolute.
int os_is_absolute_path(const char_u *fname);
/// Check if the given path is a directory or not.
///
/// @return `TRUE` if `fname` is a directory.
int os_isdir(const char_u *name);
/// @return `true` if `fname` is a directory.
bool os_isdir(const char_u *name);
/// Check if the given path represents an executable file.
///
/// @return `TRUE` if `name` is executable and
/// @return `true` if `name` is executable and
/// - can be found in $PATH,
/// - is relative to current dir or
/// - is absolute.
///
/// @return `FALSE` otherwise.
int os_can_exe(const char_u *name);
/// @return `false` otherwise.
bool os_can_exe(const char_u *name);
/// Get the file permissions for a given file.
///
@ -58,13 +58,13 @@ int os_setperm(const char_u *name, int perm);
/// Check if a file exists.
///
/// @return `TRUE` if `name` exists.
int os_file_exists(const char_u *name);
/// @return `true` if `name` exists.
bool os_file_exists(const char_u *name);
/// Check if a file is readonly.
///
/// @return `True` if `name` is readonly.
int os_file_is_readonly(const char *name);
/// @return `true` if `name` is readonly.
bool os_file_is_readonly(const char *name);
/// Check if a file is writable.
///

View File

@ -222,7 +222,7 @@ int os_call_shell(char_u *cmd, ShellOpts opts, char_u *extra_shell_arg)
// TODO for now this is only needed if the terminal is in raw mode, but
// when the UI is externalized we'll also need it, so leave it here
uv_process_kill(&proc, SIGINT);
got_int = FALSE;
got_int = false;
}
}

View File

@ -73,7 +73,7 @@ void signal_handle(Event event)
switch (signum) {
case SIGINT:
got_int = TRUE;
got_int = true;
break;
#ifdef SIGPWR
case SIGPWR:

View File

@ -14,7 +14,7 @@
int os_get_usernames(garray_T *users)
{
if (users == NULL) {
return FALSE;
return FAIL;
}
ga_init(users, sizeof(char *), 20);

View File

@ -1039,7 +1039,7 @@ int flags; /* EW_* flags */
int i;
size_t len;
char_u *p;
int dir;
bool dir;
char_u *extra_shell_arg = NULL;
ShellOpts shellopts = kShellOptExpand | kShellOptSilent;
/*

View File

@ -1208,7 +1208,7 @@ addfile (
)
{
char_u *p;
int isdir;
bool isdir;
/* if the file/dir doesn't exist, may not add it */
if (!(flags & EW_NOTFOUND) && !os_file_exists(f))

View File

@ -1174,7 +1174,7 @@ static char_u *qf_push_dir(char_u *dirbuf, struct dir_stack_T **stackptr)
vim_free((*stackptr)->dirname);
(*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
TRUE);
if (os_isdir((*stackptr)->dirname) == TRUE)
if (os_isdir((*stackptr)->dirname))
break;
ds_new = ds_new->next;

View File

@ -9,24 +9,20 @@ ffi.cdef [[
enum OKFAIL {
OK = 1, FAIL = 0
};
enum BOOLEAN {
TRUE = 1, FALSE = 0
};
int os_dirname(char_u *buf, int len);
int os_isdir(char_u * name);
int is_executable(char_u *name);
int os_can_exe(char_u *name);
bool os_isdir(char_u * name);
bool is_executable(char_u *name);
bool os_can_exe(char_u *name);
int32_t os_getperm(char_u *name);
int os_setperm(char_u *name, long perm);
int os_file_exists(const char_u *name);
int os_file_is_readonly(char *fname);
bool os_file_exists(const char_u *name);
bool os_file_is_readonly(char *fname);
int os_file_is_writable(const char *name);
int os_rename(const char_u *path, const char_u *new_path);
]]
-- import constants parsed by ffi
{:OK, :FAIL} = lib
{:TRUE, :FALSE} = lib
cppimport 'sys/stat.h'
@ -231,50 +227,50 @@ describe 'fs function', ->
fs.os_isdir (to_cstr name)
it 'returns false if an empty string is given', ->
eq FALSE, (os_isdir '')
eq false, (os_isdir '')
it 'returns false if a nonexisting directory is given', ->
eq FALSE, (os_isdir 'non-existing-directory')
eq false, (os_isdir 'non-existing-directory')
it 'returns false if a nonexisting absolute directory is given', ->
eq FALSE, (os_isdir '/non-existing-directory')
eq false, (os_isdir '/non-existing-directory')
it 'returns false if an existing file is given', ->
eq FALSE, (os_isdir 'unit-test-directory/test.file')
eq false, (os_isdir 'unit-test-directory/test.file')
it 'returns true if the current directory is given', ->
eq TRUE, (os_isdir '.')
eq true, (os_isdir '.')
it 'returns true if the parent directory is given', ->
eq TRUE, (os_isdir '..')
eq true, (os_isdir '..')
it 'returns true if an arbitrary directory is given', ->
eq TRUE, (os_isdir 'unit-test-directory')
eq true, (os_isdir 'unit-test-directory')
it 'returns true if an absolute directory is given', ->
eq TRUE, (os_isdir directory)
eq true, (os_isdir directory)
describe 'os_can_exe', ->
os_can_exe = (name) ->
fs.os_can_exe (to_cstr name)
it 'returns false when given a directory', ->
eq FALSE, (os_can_exe './unit-test-directory')
eq false, (os_can_exe './unit-test-directory')
it 'returns false when given a regular file without executable bit set', ->
eq FALSE, (os_can_exe 'unit-test-directory/test.file')
eq false, (os_can_exe 'unit-test-directory/test.file')
it 'returns false when the given file does not exists', ->
eq FALSE, (os_can_exe 'does-not-exist.file')
eq false, (os_can_exe 'does-not-exist.file')
it 'returns true when given an executable inside $PATH', ->
eq TRUE, (os_can_exe executable_name)
eq true, (os_can_exe executable_name)
it 'returns true when given an executable relative to the current dir', ->
old_dir = lfs.currentdir!
lfs.chdir directory
relative_executable = './' .. executable_name
eq TRUE, (os_can_exe relative_executable)
eq true, (os_can_exe relative_executable)
lfs.chdir old_dir
describe 'file permissions', ->
@ -332,32 +328,32 @@ describe 'fs function', ->
eq FAIL, (os_setperm 'non-existing-file', perm)
describe 'os_file_is_readonly', ->
it 'returns TRUE if the file is readonly', ->
it 'returns true if the file is readonly', ->
perm = os_getperm 'unit-test-directory/test.file'
perm_orig = perm
perm = unset_bit perm, ffi.C.kS_IWUSR
perm = unset_bit perm, ffi.C.kS_IWGRP
perm = unset_bit perm, ffi.C.kS_IWOTH
eq OK, (os_setperm 'unit-test-directory/test.file', perm)
eq TRUE, os_file_is_readonly 'unit-test-directory/test.file'
eq true, os_file_is_readonly 'unit-test-directory/test.file'
eq OK, (os_setperm 'unit-test-directory/test.file', perm_orig)
it 'returns FALSE if the file is writable', ->
eq FALSE, os_file_is_readonly 'unit-test-directory/test.file'
it 'returns false if the file is writable', ->
eq false, os_file_is_readonly 'unit-test-directory/test.file'
describe 'os_file_is_writable', ->
it 'returns FALSE if the file is readonly', ->
it 'returns 0 if the file is readonly', ->
perm = os_getperm 'unit-test-directory/test.file'
perm_orig = perm
perm = unset_bit perm, ffi.C.kS_IWUSR
perm = unset_bit perm, ffi.C.kS_IWGRP
perm = unset_bit perm, ffi.C.kS_IWOTH
eq OK, (os_setperm 'unit-test-directory/test.file', perm)
eq FALSE, os_file_is_writable 'unit-test-directory/test.file'
eq 0, os_file_is_writable 'unit-test-directory/test.file'
eq OK, (os_setperm 'unit-test-directory/test.file', perm_orig)
it 'returns TRUE if the file is writable', ->
eq TRUE, os_file_is_writable 'unit-test-directory/test.file'
it 'returns 1 if the file is writable', ->
eq 1, os_file_is_writable 'unit-test-directory/test.file'
it 'returns 2 when given a folder with rights to write into', ->
eq 2, os_file_is_writable 'unit-test-directory'
@ -370,11 +366,11 @@ describe 'fs function', ->
fs.os_rename (to_cstr path), (to_cstr new_path)
describe 'os_file_exists', ->
it 'returns FALSE when given a non-existing file', ->
eq FALSE, (os_file_exists 'non-existing-file')
it 'returns false when given a non-existing file', ->
eq false, (os_file_exists 'non-existing-file')
it 'returns TRUE when given an existing file', ->
eq TRUE, (os_file_exists 'unit-test-directory/test.file')
it 'returns true when given an existing file', ->
eq true, (os_file_exists 'unit-test-directory/test.file')
describe 'os_rename', ->
test = 'unit-test-directory/test.file'
@ -382,8 +378,8 @@ describe 'fs function', ->
it 'can rename file if destination file does not exist', ->
eq OK, (os_rename test, not_exist)
eq FALSE, (os_file_exists test)
eq TRUE, (os_file_exists not_exist)
eq false, (os_file_exists test)
eq true, (os_file_exists not_exist)
eq OK, (os_rename not_exist, test) -- restore test file
it 'fail if source file does not exist', ->
@ -397,8 +393,8 @@ describe 'fs function', ->
file\close!
eq OK, (os_rename other, test)
eq FALSE, (os_file_exists other)
eq TRUE, (os_file_exists test)
eq false, (os_file_exists other)
eq true, (os_file_exists test)
file = io.open test, 'r'
eq 'other', (file\read '*all')
file\close!