Add DIR_IS_DOT and DIR_IS_DOTDOT macros

...to detect "." and ".." directories, respectively.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-07-13 21:02:34 +04:00
parent 1d52364b52
commit ab3f53fbbe
14 changed files with 50 additions and 79 deletions

View File

@ -74,6 +74,9 @@
#define MC_MAXFILENAMELEN MAXNAMLEN
#define DIR_IS_DOT(x) ((x)[0] == '.' && (x)[1] == '\0')
#define DIR_IS_DOTDOT(x) ((x)[0] == '.' && (x)[1] == '.' && (x)[2] == '\0')
/*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/

View File

@ -800,7 +800,7 @@ vfs_parse_ls_lga (const char *p, struct stat * s, char **filename, char **linkna
if (num_spaces != NULL)
{
*num_spaces = column_ptr[idx] - column_ptr[idx - 1] - strlen (columns[idx - 1]);
if (strcmp (columns[idx], "..") == 0)
if (DIR_IS_DOTDOT (columns[idx]))
vfs_parce_ls_final_num_spaces = *num_spaces;
}

View File

@ -213,7 +213,7 @@ filename_completion_function (const char *text, int state, input_complete_t flag
All entries except "." and ".." match. */
if (filename_len == 0)
{
if (!strcmp (entry->d_name, ".") || !strcmp (entry->d_name, ".."))
if (DIR_IS_DOT (entry->d_name) || DIR_IS_DOTDOT (entry->d_name))
continue;
}
else

View File

@ -266,7 +266,7 @@ select_unselect_cmd (const char *title, const char *history_name, gboolean do_se
for (i = 0; i < current_panel->count; i++)
{
if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
if (DIR_IS_DOTDOT (current_panel->dir.list[i].fname))
continue;
if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
continue;
@ -941,7 +941,7 @@ mkdir_cmd (void)
const char *name = "";
/* If 'on' then automatically fills name with current selected item name */
if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
if (auto_fill_mkdir_name && !DIR_IS_DOTDOT (selection (current_panel)->fname))
name = selection (current_panel)->fname;
dir =
@ -1607,7 +1607,7 @@ smart_dirsize_cmd (void)
file_entry *entry;
entry = &(panel->dir.list[panel->selected]);
if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
if ((S_ISDIR (entry->st.st_mode) && DIR_IS_DOTDOT (entry->fname)) || panel->dirs_marked)
dirsizes_cmd ();
else
single_dirsize_cmd ();
@ -1622,7 +1622,7 @@ single_dirsize_cmd (void)
file_entry *entry;
entry = &(panel->dir.list[panel->selected]);
if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
if (S_ISDIR (entry->st.st_mode) && !DIR_IS_DOTDOT (entry->fname))
{
size_t marked = 0;
uintmax_t total = 0;
@ -1668,7 +1668,7 @@ dirsizes_cmd (void)
for (i = 0; i < panel->count; i++)
if (S_ISDIR (panel->dir.list[i].st.st_mode)
&& ((panel->dirs_marked && panel->dir.list[i].f.marked)
|| !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
|| !panel->dirs_marked) && !DIR_IS_DOTDOT (panel->dir.list[i].fname))
{
vfs_path_t *p;
size_t marked = 0;

View File

@ -400,7 +400,7 @@ do_cd_command (char *orig_cmd)
{
sync_tree (mc_config_get_home_dir ());
}
else if (strcmp (cmd + operand_pos, "..") == 0)
else if (DIR_IS_DOTDOT (cmd + operand_pos))
{
if (vfs_path_elements_count (current_panel->cwd_vpath) != 1 ||
strlen (vfs_path_get_by_index (current_panel->cwd_vpath, 0)->path) > 1)

View File

@ -172,9 +172,7 @@ handle_dirent (dir_list * list, const char *fltr, struct dirent *dp,
{
vfs_path_t *vpath;
if (dp->d_name[0] == '.' && dp->d_name[1] == 0)
return 0;
if (dp->d_name[0] == '.' && dp->d_name[1] == '.' && dp->d_name[2] == 0)
if (DIR_IS_DOT (dp->d_name) || DIR_IS_DOTDOT (dp->d_name))
return 0;
if (!panels_options.show_dot_files && (dp->d_name[0] == '.'))
return 0;
@ -449,7 +447,7 @@ do_sort (dir_list * list, sortfn * sort, int top, gboolean reverse_f, gboolean c
/* If there is an ".." entry the caller must take care to
ensure that it occupies the first list element. */
if (strcmp (list->list[0].fname, "..") == 0)
if (DIR_IS_DOTDOT (list->list[0].fname))
dot_dot_found = 1;
reverse = reverse_f ? -1 : 1;
@ -510,9 +508,7 @@ handle_path (dir_list * list, const char *path,
{
vfs_path_t *vpath;
if (path[0] == '.' && path[1] == 0)
return 0;
if (path[0] == '.' && path[1] == '.' && path[2] == 0)
if (DIR_IS_DOT (path) || DIR_IS_DOTDOT (path))
return 0;
vpath = vfs_path_from_str (path);

View File

@ -524,9 +524,7 @@ do_compute_dir_size (const vfs_path_t * dirname_vpath, void *ui,
{
vfs_path_t *tmp_vpath;
if (strcmp (dirent->d_name, ".") == 0)
continue;
if (strcmp (dirent->d_name, "..") == 0)
if (DIR_IS_DOT (dirent->d_name) || DIR_IS_DOTDOT (dirent->d_name))
continue;
tmp_vpath = vfs_path_append_new (dirname_vpath, dirent->d_name, NULL);
@ -1076,7 +1074,7 @@ recursive_erase (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s)
FileProgressStatus return_status = FILE_CONT;
vfs_path_t *vpath;
if (strcmp (s, "..") == 0)
if (DIR_IS_DOTDOT (s))
return FILE_RETRY;
vpath = vfs_path_from_str (s);
@ -1092,10 +1090,9 @@ recursive_erase (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s)
{
vfs_path_t *tmp_vpath;
if (!strcmp (next->d_name, "."))
continue;
if (!strcmp (next->d_name, ".."))
if (DIR_IS_DOT (next->d_name) || DIR_IS_DOTDOT (next->d_name))
continue;
path = mc_build_filename (s, next->d_name, NULL);
tmp_vpath = vfs_path_from_str (path);
if (mc_lstat (tmp_vpath, &buf) != 0)
@ -1151,20 +1148,18 @@ check_dir_is_empty (const vfs_path_t * vpath)
{
DIR *dir;
struct dirent *d;
int i;
int i = 1;
dir = mc_opendir (vpath);
if (!dir)
if (dir == NULL)
return -1;
for (i = 1, d = mc_readdir (dir); d; d = mc_readdir (dir))
{
if (d->d_name[0] == '.' && (d->d_name[1] == '\0' ||
(d->d_name[1] == '.' && d->d_name[2] == '\0')))
continue; /* "." or ".." */
i = 0;
break;
}
for (d = mc_readdir (dir); d != NULL; d = mc_readdir (dir))
if (!DIR_IS_DOT (d->d_name) && !DIR_IS_DOTDOT (d->d_name))
{
i = 0;
break;
}
mc_closedir (dir);
return i;
@ -1178,10 +1173,7 @@ erase_dir_iff_empty (FileOpContext * ctx, const char *s)
FileProgressStatus error;
vfs_path_t *s_vpath;
if (strcmp (s, "..") == 0)
return FILE_SKIP;
if (strcmp (s, ".") == 0)
if (DIR_IS_DOT (s) || DIR_IS_DOTDOT (s))
return FILE_SKIP;
file_progress_show_deleting (ctx, s);
@ -2207,12 +2199,11 @@ copy_dir_dir (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s, con
{
char *path;
vfs_path_t *tmp_vpath;
/*
* Now, we don't want '.' and '..' to be created / copied at any time
*/
if (!strcmp (next->d_name, "."))
continue;
if (!strcmp (next->d_name, ".."))
if (DIR_IS_DOT (next->d_name) || DIR_IS_DOTDOT (next->d_name))
continue;
/* get the filename and add it to the src directory */
@ -2441,14 +2432,6 @@ erase_dir (FileOpTotalContext * tctx, FileOpContext * ctx, const vfs_path_t * s_
{
FileProgressStatus error;
/*
if (strcmp (s, "..") == 0)
return FILE_SKIP;
if (strcmp (s, ".") == 0)
return FILE_SKIP;
*/
file_progress_show_deleting (ctx, vfs_path_as_str (s_vpath));
if (check_progress_buttons (ctx) == FILE_ABORT)
return FILE_ABORT;
@ -2672,7 +2655,7 @@ panel_operate (void *source_panel, FileOperation operation, gboolean force_singl
else
source = panel_get_file (panel);
if (strcmp (source, "..") == 0)
if (DIR_IS_DOTDOT (source))
{
g_free (source);
message (D_ERROR, MSG_ERROR, _("Cannot operate on \"..\"!"));

View File

@ -717,7 +717,7 @@ find_parameters (char **start_dir, ssize_t * start_dir_len,
char *temp_dir;
temp_dir = in_start->buffer;
if ((temp_dir[0] == '\0') || ((temp_dir[0] == '.') && (temp_dir[1] == '\0')))
if (*temp_dir == '\0' || DIR_IS_DOT (temp_dir))
temp_dir = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
else
temp_dir = g_strdup (temp_dir);
@ -768,7 +768,7 @@ find_parameters (char **start_dir, ssize_t * start_dir_len,
s = tilde_expand (*start_dir);
canonicalize_pathname (s);
if (s[0] == '.' && s[1] == '\0')
if (DIR_IS_DOT (s))
{
*start_dir = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
/* FIXME: is current_panel->cwd_vpath canonicalized? */
@ -792,7 +792,7 @@ find_parameters (char **start_dir, ssize_t * start_dir_len,
}
if (!options.ignore_dirs_enable || in_ignore->buffer[0] == '\0'
|| (in_ignore->buffer[0] == '.' && in_ignore->buffer[1] == '\0'))
|| DIR_IS_DOT (in_ignore->buffer))
*ignore_dirs = NULL;
else
*ignore_dirs = g_strdup (in_ignore->buffer);
@ -1306,7 +1306,7 @@ do_search (WDialog * h)
;
} /* while (!dp) */
if (strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0)
if (DIR_IS_DOT (dp->d_name) || DIR_IS_DOTDOT (dp->d_name))
{
/* skip invalid filenames */
while ((dp = mc_readdir (dirp)) != NULL && !str_is_valid_string (dp->d_name))

View File

@ -779,10 +779,10 @@ read_file_system_list (int need_fs_type)
char *name;
struct stat statbuf;
if (strcmp (d->d_name, "..") == 0)
if (DIR_IS_DOT (d->d_name))
continue;
if (strcmp (d->d_name, ".") == 0)
if (DIR_IS_DOTDOT (d->d_name))
name = g_strdup ("/");
else
name = g_strconcat ("/", d->d_name, (char *) NULL);

View File

@ -501,10 +501,8 @@ string_file_size (file_entry * fe, int len)
static char buffer[BUF_TINY];
/* Don't ever show size of ".." since we don't calculate it */
if (!strcmp (fe->fname, ".."))
{
if (DIR_IS_DOTDOT (fe->fname))
return _("UP--DIR");
}
#ifdef HAVE_STRUCT_STAT_ST_RDEV
if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
@ -528,7 +526,7 @@ string_file_size_brief (file_entry * fe, int len)
return _("SYMLINK");
}
if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && !DIR_IS_DOTDOT (fe->fname))
{
return _("SUB-DIR");
}
@ -1007,7 +1005,7 @@ display_mini_info (WPanel * panel)
else
tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
}
else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
{
/* FIXME:
* while loading directory (do_load_dir() and do_reload_dir()),
@ -4377,7 +4375,7 @@ do_file_mark (WPanel * panel, int idx, int mark)
return;
/* Only '..' can't be marked, '.' isn't visible */
if (strcmp (panel->dir.list[idx].fname, "..") == 0)
if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
return;
file_mark (panel, idx, mark);

View File

@ -429,10 +429,7 @@ do_panelize_cd (struct WPanel *panel)
for (i = 0; i < panelized_panel.count; i++)
{
if (panelized_same
|| (panelized_panel.list.list[i].fname[0] == '.'
&& panelized_panel.list.list[i].fname[1] == '.'
&& panelized_panel.list.list[i].fname[2] == '\0'))
if (panelized_same || DIR_IS_DOTDOT (panelized_panel.list.list[i].fname))
{
list->list[i].fnamelen = panelized_panel.list.list[i].fnamelen;
list->list[i].fname = g_strndup (panelized_panel.list.list[i].fname,

View File

@ -748,7 +748,7 @@ tree_store_mark_checked (const char *subname)
return;
/* Calculate the full name of the subdirectory */
if (subname[0] == '.' && (subname[1] == 0 || (subname[1] == '.' && subname[2] == 0)))
if (DIR_IS_DOT (subname) || DIR_IS_DOTDOT (subname))
return;
cname = vfs_path_as_str (ts.check_name);
@ -927,18 +927,12 @@ tree_store_rescan (const vfs_path_t * vpath)
{
vfs_path_t *tmp_vpath;
if (dp->d_name[0] == '.')
{
if (dp->d_name[1] == 0 || (dp->d_name[1] == '.' && dp->d_name[2] == 0))
continue;
}
if (DIR_IS_DOT (dp->d_name) || DIR_IS_DOTDOT (dp->d_name))
continue;
tmp_vpath = vfs_path_append_new (vpath, dp->d_name, NULL);
if (mc_lstat (tmp_vpath, &buf) != -1)
{
if (S_ISDIR (buf.st_mode))
tree_store_mark_checked (dp->d_name);
}
if (mc_lstat (tmp_vpath, &buf) != -1 && S_ISDIR (buf.st_mode))
tree_store_mark_checked (dp->d_name);
vfs_path_free (tmp_vpath);
}
mc_closedir (dirp);

View File

@ -1221,7 +1221,7 @@ do_subshell_chdir (const vfs_path_t * vpath, gboolean update_prompt)
bPathNotEq = strcmp (p_subshell_cwd, p_current_panel_cwd);
}
if (bPathNotEq && strcmp (pcwd, ".") != 0)
if (bPathNotEq && !DIR_IS_DOT (pcwd))
{
char *cwd;

View File

@ -265,9 +265,9 @@ extfs_find_entry_int (struct entry *dir, const char *name, GSList * list,
c = *q;
*q = '\0';
if (strcmp (p, ".") != 0)
if (!DIR_IS_DOT (p))
{
if (strcmp (p, "..") == 0)
if (DIR_IS_DOTDOT (p))
pent = pent->dir;
else
{
@ -540,7 +540,7 @@ extfs_read_archive (int fstype, const char *name, struct archive **pparc)
*(p++) = '\0';
q = cfn;
}
if (S_ISDIR (hstat.st_mode) && (strcmp (p, ".") == 0 || strcmp (p, "..") == 0))
if (S_ISDIR (hstat.st_mode) && (DIR_IS_DOT (p) || DIR_IS_DOTDOT (p)))
goto read_extfs_continue;
pent = extfs_find_entry (current_archive->root_entry, q, TRUE, FALSE);
if (pent == NULL)