1 /*
2  * Part of Very Secure FTPd
3  * Licence: GPL v2
4  * Author: Chris Evans
5  * utility.c
6  */
7 
8 #include "utility.h"
9 #include "sysutil.h"
10 #include "str.h"
11 #include "defs.h"
12 
13 #define DIE_DEBUG
14 
15 void
die(const char * p_text)16 die(const char* p_text)
17 {
18 #ifdef DIE_DEBUG
19   bug(p_text);
20 #endif
21   vsf_sysutil_exit(2);
22 }
23 
24 void
die2(const char * p_text1,const char * p_text2)25 die2(const char* p_text1, const char* p_text2)
26 {
27   struct mystr die_str = INIT_MYSTR;
28   str_alloc_text(&die_str, p_text1);
29   if (p_text2)
30   {
31     str_append_text(&die_str, p_text2);
32   }
33   else
34   {
35     str_append_text(&die_str, "(null)");
36   }
37   die(str_getbuf(&die_str));
38 }
39 
40 void
bug(const char * p_text)41 bug(const char* p_text)
42 {
43   static int inside_bug = 0;
44   if (inside_bug)
45     vsf_sysutil_exit(1);
46   inside_bug++;
47   /* Rats. Try and write the reason to the network for diagnostics */
48   vsf_sysutil_activate_noblock(VSFTP_COMMAND_FD);
49   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "500 OOPS: ", 10);
50   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
51                                 vsf_sysutil_strlen(p_text));
52   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "\r\n", 2);
53   vsf_sysutil_exit(2);
54 }
55 
56 void
vsf_exit(const char * p_text)57 vsf_exit(const char* p_text)
58 {
59   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
60                                 vsf_sysutil_strlen(p_text));
61   vsf_sysutil_exit(0);
62 }
63 
64 void
vsf_print(const char * p_text)65 vsf_print(const char* p_text)
66 {
67   (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
68                                 vsf_sysutil_strlen(p_text));
69 }
70