minishell/stdme/src/fs/open.c
2024-07-10 18:06:10 +02:00

60 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:29:38 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:43:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/fs/open.h"
#include <fcntl.h>
t_error me_open(t_const_str path, bool read, bool write, int *file_out)
{
int out;
int flags;
flags = 0;
if (read && write)
flags = O_RDWR;
else if (read)
flags = O_RDONLY;
else if (write)
flags = O_WRONLY;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}
t_error me_open_truncate(t_const_str path, int *file_out)
{
int out;
int flags;
unlink(path);
flags = O_WRONLY | O_CREAT | O_TRUNC;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}
t_error me_open_create(t_const_str path, int *file_out)
{
int out;
int flags;
flags = O_WRONLY | O_CREAT | O_APPEND;
out = open(path, flags, 0666);
if (out < 0)
return (ERROR);
*file_out = out;
return (NO_ERROR);
}