/* file_operations.c -- control multiple file operations */
/*
* This file is part of CliFM
*
* Copyright (C) 2016-2021, L. Abramovich <johndoe.arch@outlook.com>
* All rights reserved.
* CliFM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* CliFM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "helpers.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <readline/readline.h>
#include <limits.h>
#include <fcntl.h>
#ifndef _NO_ARCHIVING
#include "archives.h"
#endif
#include "aux.h"
#include "checks.h"
#include "colors.h"
#include "exec.h"
#include "file_operations.h"
#include "history.h"
#include "listing.h"
#include "mime.h"
#include "misc.h"
#include "navigation.h"
#include "readline.h"
#include "selection.h"
#include "messages.h"
void
clear_selbox(void)
{
size_t i;
for (i = 0; i < sel_n; i++)
free(sel_elements[i]);
sel_n = 0;
save_sel();
}
/* Open a file via OPENER, if set, or via LIRA. If not compiled with
* Lira support, fallback to open (Haiku), or xdg-open. Returns zero
* on success and one on failure */
int
open_file(char *file)
{
if (!file || !*file)
return EXIT_FAILURE;
int exit_status = EXIT_SUCCESS;
if (opener) {
char *cmd[] = {opener, file, NULL};
if (launch_execve(cmd, FOREGROUND, E_NOSTDERR) != EXIT_SUCCESS)
exit_status = EXIT_FAILURE;
} else {
#ifndef _NO_LIRA
char *cmd[] = {"mm", file, NULL};
exit_status = mime_open(cmd);
#else
/* Fallback to (xdg-)open */
#ifdef __HAIKU__
char *cmd[] = {"open", file, NULL};
#else
char *cmd[] = {"xdg-open", file, NULL};
#endif /* __HAIKU__ */
if (launch_execve(cmd, FOREGROUND, E_NOSTDERR) != EXIT_SUCCESS)
exit_status = EXIT_FAILURE;
#endif /* _NO_LIRA */
}
return exit_status;
}
/* Toggle executable bit on file */
int
xchmod(const char *file, mode_t mode)
{
/* Set or unset S_IXUSR, S_IXGRP, and S_IXOTH */
(0100 & mode) ? (mode &= (mode_t)~0111) : (mode |= 0111);
log_function(NULL);
int fd = open(file, O_WRONLY);
if (fd == -1) {
fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, file, strerror(errno));
return EXIT_FAILURE;
}
if (fchmod(fd, mode) == -1) {
close(fd);
fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, file, strerror(errno));
return EXIT_FAILURE;
}
close(fd);