1+ #include "fsio.h"
12#include "scriptexec.h"
23#include "string_buffer.h"
34#include <stddef.h>
78#include <unistd.h>
89
910// private functions
10- struct ScriptExecResult create_result ();
11- bool write_text_file (char * , const char * );
12- char * read_and_delete_text_file (char * , struct StringBuffer * );
11+ struct ScriptExecResult _create_result ();
12+ char * _read_and_delete_text_file (char * );
1313
1414struct ScriptExecOptions scriptexec_create_options ()
1515{
@@ -32,7 +32,7 @@ struct ScriptExecResult scriptexec_run(const char *script)
3232
3333struct ScriptExecResult scriptexec_run_with_options (const char * script , struct ScriptExecOptions options )
3434{
35- struct ScriptExecResult result = create_result ();
35+ struct ScriptExecResult result = _create_result ();
3636
3737 if (script == NULL )
3838 {
@@ -68,10 +68,10 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
6868 }
6969
7070 string_buffer_append_string (buffer , (char * )script );
71- const char * updated_script = string_buffer_to_string (buffer );
71+ char * updated_script = string_buffer_to_string (buffer );
7272
73- char template [] = "/tmp/scriptexec_XXXXXX" ;
74- char * dir_name = mkdtemp (template );
73+ char template [] = "/tmp/scriptexec_XXXXXX" ;
74+ char * dir_name = mkdtemp (template );
7575
7676 if (dir_name == NULL )
7777 {
@@ -95,7 +95,7 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
9595 char * err_file = string_buffer_to_string (buffer );
9696
9797 // write script file
98- if (!write_text_file (script_file , updated_script ))
98+ if (!fsio_write_text_file (script_file , updated_script ))
9999 {
100100 rmdir (dir_name );
101101 string_buffer_release (buffer );
@@ -119,71 +119,34 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
119119 string_buffer_append_string (buffer , " 1> " );
120120 string_buffer_append_string (buffer , out_file );
121121 const char * command = string_buffer_to_string (buffer );
122+ string_buffer_release (buffer );
122123
123124 result .code = system (command );
124125
125126 // read out/err
126- result .out = read_and_delete_text_file (out_file , buffer );
127- result .err = read_and_delete_text_file (err_file , buffer );
127+ result .out = _read_and_delete_text_file (out_file );
128+ result .err = _read_and_delete_text_file (err_file );
128129
129130 // delete files
130131 remove (script_file );
131132 rmdir (dir_name );
132133
133- string_buffer_release (buffer );
134-
135134 return (result );
136135} /* scriptexec_run_with_options */
137136
138- struct ScriptExecResult create_result ()
137+ struct ScriptExecResult _create_result ()
139138{
140139 struct ScriptExecResult result = { .code = 0 , .out = NULL , .err = NULL };
141140
142141 return (result );
143142}
144143
145144
146- bool write_text_file ( char * file , const char * text )
145+ char * _read_and_delete_text_file ( char * file )
147146{
148- FILE * fp = fopen (file , "w" );
149-
150- if (fp == NULL )
151- {
152- return (false);
153- }
154-
155- if (fputs (text , fp ) == EOF )
156- {
157- fclose (fp );
158- remove (file );
159-
160- return (false);
161- }
162-
163- fclose (fp );
164-
165- return (true);
166- }
167-
168-
169- char * read_and_delete_text_file (char * file , struct StringBuffer * buffer )
170- {
171- FILE * fp = fopen (file , "r" );
172-
173- if (fp == NULL )
174- {
175- return (NULL );
176- }
177-
178- int character ;
179- string_buffer_clear (buffer );
180- while ((character = getc (fp )) != EOF )
181- {
182- string_buffer_append (buffer , (char )character );
183- }
147+ char * text = fsio_read_text_file (file );
184148
185- fclose (fp );
186149 remove (file );
187150
188- return (string_buffer_to_string ( buffer ) );
151+ return (text );
189152}
0 commit comments