Line data Source code
1 : #include "testitall.h"
2 :
3 : /**
4 : * @brief Writes content of a buffer to a temporary file
5 : * @param buffer Pointer to the null-terminated string to be written
6 : * @return Return enum value indicating execution status
7 : * @note Creates a temporary file using mkstemp with template /tmp/testitall_XXXXXX
8 : */
9 0 : Return write_to_temp_file(const char *buffer)
10 : {
11 : /// The status that will be passed to return() before exiting.
12 : /// By default, the function worked without errors.
13 0 : Return status = SUCCESS;
14 :
15 0 : int fd = -1;
16 0 : FILE *temp_file = NULL;
17 0 : char template[] = "/tmp/testitall_XXXXXX";
18 : int write_result;
19 :
20 0 : if(SUCCESS == status)
21 : {
22 0 : if(buffer == NULL)
23 : {
24 0 : status = FAILURE;
25 : }
26 : }
27 :
28 0 : if(SUCCESS == status)
29 : {
30 0 : fd = mkstemp(template);
31 :
32 0 : if(fd == -1)
33 : {
34 0 : status = FAILURE;
35 : }
36 : }
37 :
38 0 : if(SUCCESS == status)
39 : {
40 0 : temp_file = fdopen(fd,"w");
41 :
42 0 : if(temp_file == NULL)
43 : {
44 0 : status = FAILURE;
45 : }
46 : }
47 :
48 0 : if(SUCCESS == status)
49 : {
50 0 : write_result = fprintf(temp_file,"%s",buffer);
51 :
52 0 : if(write_result < 0)
53 : {
54 0 : status = FAILURE;
55 : }
56 : }
57 :
58 : // Cleanup
59 0 : if(temp_file != NULL)
60 : {
61 0 : fclose(temp_file); // This also closes fd
62 0 : } else if(fd != -1){
63 0 : close(fd);
64 : }
65 :
66 0 : return(status);
67 : }
|