Commit 0749f184 0749f1847ba0c7fb4499239dbc7901071bcba2ae by Sergey Poznyakoff

(util_setio): Make sure both streams are line-buffered.

(util_get_input,util_get_output)
(util_set_input,util_set_output): New functions for manipulating
io streams.
(util_atexit): New function.
1 parent dbd7d206
...@@ -1087,12 +1087,42 @@ util_setio (FILE *in, FILE *out) ...@@ -1087,12 +1087,42 @@ util_setio (FILE *in, FILE *out)
1087 if (!out || !in) 1087 if (!out || !in)
1088 imap4d_bye (ERR_NO_OFILE); 1088 imap4d_bye (ERR_NO_OFILE);
1089 1089
1090 setvbuf (in, NULL, _IOLBF, 0);
1091 setvbuf (out, NULL, _IOLBF, 0);
1090 if (stdio_stream_create (&istream, in, MU_STREAM_NO_CLOSE) 1092 if (stdio_stream_create (&istream, in, MU_STREAM_NO_CLOSE)
1091 || stdio_stream_create (&ostream, out, MU_STREAM_NO_CLOSE)) 1093 || stdio_stream_create (&ostream, out, MU_STREAM_NO_CLOSE))
1092 imap4d_bye (ERR_NO_OFILE); 1094 imap4d_bye (ERR_NO_OFILE);
1093 } 1095 }
1094 1096
1095 void 1097 void
1098 util_get_input (stream_t *pstr)
1099 {
1100 *pstr = istream;
1101 }
1102
1103 void
1104 util_get_output (stream_t *pstr)
1105 {
1106 *pstr = ostream;
1107 }
1108
1109 void
1110 util_set_input (stream_t str)
1111 {
1112 if (istream)
1113 stream_destroy (istream, stream_get_owner (istream));
1114 istream = str;
1115 }
1116
1117 void
1118 util_set_output (stream_t str)
1119 {
1120 if (ostream)
1121 stream_destroy (ostream, stream_get_owner (ostream));
1122 ostream = str;
1123 }
1124
1125 void
1096 util_flush_output () 1126 util_flush_output ()
1097 { 1127 {
1098 stream_flush (ostream); 1128 stream_flush (ostream);
...@@ -1135,18 +1165,37 @@ imap4d_init_tls_server () ...@@ -1135,18 +1165,37 @@ imap4d_init_tls_server ()
1135 } 1165 }
1136 #endif /* WITH_TLS */ 1166 #endif /* WITH_TLS */
1137 1167
1168 static list_t atexit_list;
1169
1170 void
1171 util_atexit (void (*fp) (void))
1172 {
1173 if (!atexit_list)
1174 list_create (&atexit_list);
1175 list_append (atexit_list, (void*)fp);
1176 }
1177
1178 static int
1179 atexit_run (void *item, void *data)
1180 {
1181 ((void (*) (void)) item) ();
1182 return 0;
1183 }
1184
1138 void 1185 void
1139 util_bye () 1186 util_bye ()
1140 { 1187 {
1141 if (istream == ostream) 1188 int rc = istream != ostream;
1142 { 1189
1143 stream_close (istream); 1190 stream_close (istream);
1144 stream_destroy (&istream, stream_get_owner (istream)); 1191 stream_destroy (&istream, stream_get_owner (istream));
1192
1193 if (rc)
1194 {
1195 stream_close (ostream);
1196 stream_destroy (&ostream, stream_get_owner (ostream));
1145 } 1197 }
1146 /* There's no reason closing in/out streams otherwise */ 1198
1147 #ifdef WITH_TLS 1199 list_do (atexit_list, atexit_run, 0);
1148 if (tls_available)
1149 mu_deinit_tls_libs ();
1150 #endif /* WITH_TLS */
1151 } 1200 }
1152 1201
......