mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
here_doc: generate random filenames
This commit is contained in:
parent
0486368a07
commit
99babbf6d2
3 changed files with 69 additions and 4 deletions
|
|
@ -6,8 +6,52 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 11:42:29 by khais #+# #+# */
|
/* Created: 2025/03/07 11:42:29 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/07 11:42:36 by khais ### ########.fr */
|
/* Updated: 2025/03/11 11:22:54 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "here_doc.h"
|
#include "here_doc.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include "../../ft_errno.h"
|
||||||
|
#include "../../buffer/buffer.h"
|
||||||
|
#include "libft/libft.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Genereate a random filename for use by here_doc
|
||||||
|
**
|
||||||
|
** The filename will be of the form
|
||||||
|
** /tmp/minishell_here_doc_XXXXXXXXXXX
|
||||||
|
** where each X is a random alphanumerical char
|
||||||
|
**
|
||||||
|
** Random bytes are generated by reading from /dev/urandom
|
||||||
|
**
|
||||||
|
** Return NULL and sets ft_errno on error
|
||||||
|
*/
|
||||||
|
char *here_doc_random_filename(void)
|
||||||
|
{
|
||||||
|
int randomfd;
|
||||||
|
char buf[1];
|
||||||
|
t_buffer *filename;
|
||||||
|
|
||||||
|
randomfd = open("/dev/urandom", O_RDONLY);
|
||||||
|
if (randomfd < 0)
|
||||||
|
return (ft_errno(FT_EERRNO), NULL);
|
||||||
|
filename = ft_buffer_new();
|
||||||
|
if (filename == NULL)
|
||||||
|
return (close(randomfd), ft_errno(FT_EERRNO), NULL);
|
||||||
|
filename = ft_buffer_push_buf(filename, "/tmp/minishell_here_doc_", 24);
|
||||||
|
while (filename->length < 35)
|
||||||
|
{
|
||||||
|
if (read(randomfd, buf, 1) != 1)
|
||||||
|
{
|
||||||
|
ft_buffer_free(filename);
|
||||||
|
return (close(randomfd), ft_errno(FT_EERRNO), NULL);
|
||||||
|
}
|
||||||
|
if (ft_isalnum(buf[0]))
|
||||||
|
filename = ft_buffer_pushchar(filename, buf[0]);
|
||||||
|
}
|
||||||
|
close(randomfd);
|
||||||
|
return (ft_buffer_to_charptr(filename));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 11:41:27 by khais #+# #+# */
|
/* Created: 2025/03/07 11:41:27 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/07 11:42:19 by khais ### ########.fr */
|
/* Updated: 2025/03/11 10:58:19 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef HERE_DOC_H
|
#ifndef HERE_DOC_H
|
||||||
# define HERE_DOC_H
|
# define HERE_DOC_H
|
||||||
|
|
||||||
# include "../../parser/worddesc/worddesc.h"
|
char *here_doc_random_filename(void);
|
||||||
|
|
||||||
#endif // HERE_DOC_H
|
#endif // HERE_DOC_H
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
|
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/10 18:21:43 by khais ### ########.fr */
|
/* Updated: 2025/03/10 18:23:36 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,27 @@
|
||||||
#include "testutil.h"
|
#include "testutil.h"
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "../src/executing/here_doc/here_doc.h"
|
||||||
|
|
||||||
|
static void test_here_doc_filename_generation(void)
|
||||||
|
{
|
||||||
|
char *filename1;
|
||||||
|
char *filename2;
|
||||||
|
|
||||||
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
filename1 = here_doc_random_filename();
|
||||||
|
filename2 = here_doc_random_filename();
|
||||||
|
assert(filename1 != NULL);
|
||||||
|
assert(filename2 != NULL);
|
||||||
|
ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename1);
|
||||||
|
ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename2);
|
||||||
|
assert(ft_strcmp(filename1, filename2) != 0);
|
||||||
|
free(filename1);
|
||||||
|
free(filename2);
|
||||||
|
do_leak_check();
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
test_here_doc_filename_generation();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue