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

View File

@ -141,24 +141,24 @@ int os_is_absolute_path(const char_u *fname)
return *fname == '/' || *fname == '~'; return *fname == '/' || *fname == '~';
} }
int os_isdir(const char_u *name) bool os_isdir(const char_u *name)
{ {
int32_t mode = os_getperm(name); int32_t mode = os_getperm(name);
if (mode < 0) { if (mode < 0) {
return FALSE; return false;
} }
if (!S_ISDIR(mode)) { if (!S_ISDIR(mode)) {
return FALSE; return false;
} }
return TRUE; return true;
} }
static int is_executable(const char_u *name); static bool is_executable(const char_u *name);
static int is_executable_in_path(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 it's an absolute or relative path don't need to use $PATH.
if (os_is_absolute_path(name) || 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 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. // exist.
static int is_executable(const char_u *name) static bool is_executable(const char_u *name)
{ {
int32_t mode = os_getperm(name); int32_t mode = os_getperm(name);
if (mode < 0) { if (mode < 0) {
return FALSE; return false;
} }
if (S_ISREG(mode) && (S_IEXEC & mode)) { 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. /// Check if a file is inside the $PATH and is executable.
/// ///
/// @return `TRUE` if `name` is an executable inside $PATH. /// @return `true` if `name` is an executable inside $PATH.
static int is_executable_in_path(const char_u *name) static bool is_executable_in_path(const char_u *name)
{ {
const char *path = getenv("PATH"); const char *path = getenv("PATH");
// PATH environment variable does not exist or is empty. // PATH environment variable does not exist or is empty.
if (path == NULL || *path == NUL) { if (path == NULL || *path == NUL) {
return FALSE; return false;
} }
int buf_len = STRLEN(name) + STRLEN(path) + 2; 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)) { if (is_executable(buf)) {
// Found our executable. Free buf and return. // Found our executable. Free buf and return.
vim_free(buf); vim_free(buf);
return OK; return true;
} }
if (*e != ':') { if (*e != ':') {
// End of $PATH without finding any executable called name. // End of $PATH without finding any executable called name.
vim_free(buf); vim_free(buf);
return FALSE; return false;
} }
path = e + 1; path = e + 1;
@ -231,7 +231,7 @@ static int is_executable_in_path(const char_u *name)
// We should never get to this point. // We should never get to this point.
assert(false); assert(false);
return FALSE; return false;
} }
int os_stat(const char_u *name, uv_stat_t *statbuf) 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; return FAIL;
} }
int os_file_exists(const char_u *name) bool os_file_exists(const char_u *name)
{ {
uv_stat_t statbuf; uv_stat_t statbuf;
if (os_stat(name, &statbuf) == OK) { 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 access(name, W_OK) != 0;
return FALSE;
}
return TRUE;
} }
int os_file_is_writable(const char *name) 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. /// Check if the given file is absolute.
/// ///
/// This just checks if the file name starts with '/' or '~'. /// 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); int os_is_absolute_path(const char_u *fname);
/// Check if the given path is a directory or not. /// Check if the given path is a directory or not.
/// ///
/// @return `TRUE` if `fname` is a directory. /// @return `true` if `fname` is a directory.
int os_isdir(const char_u *name); bool os_isdir(const char_u *name);
/// Check if the given path represents an executable file. /// 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, /// - can be found in $PATH,
/// - is relative to current dir or /// - is relative to current dir or
/// - is absolute. /// - is absolute.
/// ///
/// @return `FALSE` otherwise. /// @return `false` otherwise.
int os_can_exe(const char_u *name); bool os_can_exe(const char_u *name);
/// Get the file permissions for a given file. /// 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. /// Check if a file exists.
/// ///
/// @return `TRUE` if `name` exists. /// @return `true` if `name` exists.
int os_file_exists(const char_u *name); bool os_file_exists(const char_u *name);
/// Check if a file is readonly. /// Check if a file is readonly.
/// ///
/// @return `True` if `name` is readonly. /// @return `true` if `name` is readonly.
int os_file_is_readonly(const char *name); bool os_file_is_readonly(const char *name);
/// Check if a file is writable. /// 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 // 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 // when the UI is externalized we'll also need it, so leave it here
uv_process_kill(&proc, SIGINT); uv_process_kill(&proc, SIGINT);
got_int = FALSE; got_int = false;
} }
} }

View File

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

View File

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

View File

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

View File

@ -1208,7 +1208,7 @@ addfile (
) )
{ {
char_u *p; char_u *p;
int isdir; bool isdir;
/* if the file/dir doesn't exist, may not add it */ /* if the file/dir doesn't exist, may not add it */
if (!(flags & EW_NOTFOUND) && !os_file_exists(f)) 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); vim_free((*stackptr)->dirname);
(*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf, (*stackptr)->dirname = concat_fnames(ds_new->dirname, dirbuf,
TRUE); TRUE);
if (os_isdir((*stackptr)->dirname) == TRUE) if (os_isdir((*stackptr)->dirname))
break; break;
ds_new = ds_new->next; ds_new = ds_new->next;

View File

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