Beispiel #1
0
int		my_put_hist(char *cmd, char **env)
{
    int		fd;
    char		*path;

    path = get_history_path(env);
    if (path == NULL + 1)
        return (-1);
    if (path == NULL)
        return (0);
    if ((fd = open(path, O_WRONLY | O_APPEND | O_CREAT,
                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
    {
        warn("open error: %s", path);
        return (-1);
    }
    write(fd, cmd, my_strlen(cmd));
    write(fd, "\n", 1);
    if (close(fd) == -1)
    {
        warn("close error: %d", fd);
        return (-1);
    }
    return (0);
}
Beispiel #2
0
void	history_exit(t_data *data)
{
	char		*path;
	int			fd;
	t_history	*history;

	if (get_history_path(data, &path) == 1)
		return ;
	fd = open(path, O_RDWR | O_CREAT | O_APPEND, 0600);
	free(path);
	if (fd == -1 || (history = data->history) == NULL)
		return ;
	while (history->prec && history->prec->get_from_file == 0)
		history = history->prec;
	if (history->get_from_file == 1)
		return ;
	while (history)
	{
		path = ft_itoa_base(history->time, 10);
		path = ft_strjoinaf1(path, ";");
		path = ft_strjoinaf1(path, history->line);
		path = ft_strjoinaf1(path, "\n");
		write(fd, path, ft_strlen(path));
		free(path);
		history = history->next;
	}
	close(fd);
}
Beispiel #3
0
int		my_history(char *cmd, char **env, int action)
{
    char		*path;

    if (action == 0)
        return (my_put_hist(cmd, env));
    path = get_history_path(NULL);
    if (path == NULL + 1)
        return (-1);
    if (!path)
        return (0);
    return (my_remove(env, path));
}
Beispiel #4
0
int				get_history_fd(t_data *data)
{
	char	*path;

	path = NULL;
	data->history_fd = -1;
	if (get_history_path(data, &path) == 1)
		return (1);
	if (access(path, F_OK) == -1)
		data->history_fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
	else if (access(path, R_OK) == 0)
		data->history_fd = open(path, O_RDONLY);
	else
		return (1);
	free(path);
	return (0);
}