/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Diff of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.76 by sysadm, Thu Dec 18 03:23:48 2025 UTC Revision 1.79 by sysadm, Thu Dec 18 12:04:02 2025 UTC
# Line 88  int io_init(void) Line 88  int io_init(void)
88                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
89                          if (close(stdin_epollfd) < 0)                          if (close(stdin_epollfd) < 0)
90                          {                          {
91                                  log_error("close(stdin_epollfd) error (%d)\n");                                  log_error("close(stdin_epollfd) error (%d)\n", errno);
92                          }                          }
93                          stdin_epollfd = -1;                          stdin_epollfd = -1;
94                          return -1;                          return -1;
95                  }                  }
96    
97                  stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
98                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);                  {
99                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
100                            if (close(stdin_epollfd) < 0)
101                            {
102                                    log_error("close(stdin_epollfd) error (%d)\n", errno);
103                            }
104                            stdin_epollfd = -1;
105                            return -1;
106                    }
107                    if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1)
108                    {
109                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
110                            if (close(stdin_epollfd) < 0)
111                            {
112                                    log_error("close(stdin_epollfd) error (%d)\n", errno);
113                            }
114                            stdin_epollfd = -1;
115                            return -1;
116                    }
117          }          }
118    
119          if (stdout_epollfd == -1)          if (stdout_epollfd == -1)
# Line 114  int io_init(void) Line 132  int io_init(void)
132                          log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);                          log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
133                          if (close(stdout_epollfd) < 0)                          if (close(stdout_epollfd) < 0)
134                          {                          {
135                                  log_error("close(stdout_epollfd) error (%d)\n");                                  log_error("close(stdout_epollfd) error (%d)\n", errno);
136                          }                          }
137                          stdout_epollfd = -1;                          stdout_epollfd = -1;
138                          return -1;                          return -1;
139                  }                  }
140    
141                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
142                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  {
143                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
144                            if (close(stdout_epollfd) < 0)
145                            {
146                                    log_error("close(stdout_epollfd) error (%d)\n", errno);
147                            }
148                            stdout_epollfd = -1;
149                            return -1;
150                    }
151                    if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1)
152                    {
153                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
154                            if (close(stdout_epollfd) < 0)
155                            {
156                                    log_error("close(stdout_epollfd) error (%d)\n", errno);
157                            }
158                            stdout_epollfd = -1;
159                            return -1;
160                    }
161          }          }
162  #else  #else
163          if (stdin_flags == 0)          if (stdin_flags == 0)
164          {          {
165                  stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
166                  fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);                  {
167                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
168                            return -1;
169                    }
170                    if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1)
171                    {
172                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
173                            return -1;
174                    }
175          }          }
176    
177          if (stdout_flags == 0)          if (stdout_flags == 0)
178          {          {
179                  stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);                  if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
180                  fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);                  {
181                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
182                            return -1;
183                    }
184                    if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1)
185                    {
186                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
187                            return -1;
188                    }
189          }          }
190  #endif  #endif
191    
# Line 150  void io_cleanup(void) Line 202  void io_cleanup(void)
202    
203                  if (close(stdin_epollfd) < 0)                  if (close(stdin_epollfd) < 0)
204                  {                  {
205                          log_error("close(stdin_epollfd) error (%d)\n");                          log_error("close(stdin_epollfd) error (%d)\n", errno);
206                  }                  }
207                  stdin_epollfd = -1;                  stdin_epollfd = -1;
208          }          }
# Line 162  void io_cleanup(void) Line 214  void io_cleanup(void)
214    
215                  if (close(stdout_epollfd) < 0)                  if (close(stdout_epollfd) < 0)
216                  {                  {
217                          log_error("close(stdout_epollfd) error (%d)\n");                          log_error("close(stdout_epollfd) error (%d)\n", errno);
218                  }                  }
219                  stdout_epollfd = -1;                  stdout_epollfd = -1;
220          }          }
# Line 191  int prints(const char *format, ...) Line 243  int prints(const char *format, ...)
243          ret = vsnprintf(buf, sizeof(buf), format, args);          ret = vsnprintf(buf, sizeof(buf), format, args);
244          va_end(args);          va_end(args);
245    
246          if (ret > 0)          if (ret >= 0)
247          {          {
248                  if (stdout_buf_len + ret > OUTPUT_BUF_SIZE)                  int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret);
249    
250                    if (stdout_buf_len + written > OUTPUT_BUF_SIZE)
251                  {                  {
252                          iflush();                          iflush();
253                  }                  }
254    
255                  if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE)                  if (stdout_buf_len + written <= OUTPUT_BUF_SIZE)
256                  {                  {
257                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written);
258                          stdout_buf_len += ret;                          stdout_buf_len += written;
259                            ret = written;
260                  }                  }
261                  else                  else
262                  {                  {
263                          errno = EAGAIN;                          errno = EAGAIN;
264                          ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret);                          int need = stdout_buf_len + written - OUTPUT_BUF_SIZE;
265                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d bytes are required\n", need);
266                            ret = -need;
267                  }                  }
268          }          }
269    
# Line 216  int prints(const char *format, ...) Line 272  int prints(const char *format, ...)
272    
273  int outc(char c)  int outc(char c)
274  {  {
275          int ret;          int ret = 0;
276    
277          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
278          {          {
# Line 1058  int io_buf_conv(iconv_t cd, char *p_buf, Line 1114  int io_buf_conv(iconv_t cd, char *p_buf,
1114          size_t i = 0;          size_t i = 0;
1115          int skip_current = 0;          int skip_current = 0;
1116    
1117          if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)          if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1118          {          {
1119                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
1120                  return -1;                  return -1;
# Line 1112  int io_buf_conv(iconv_t cd, char *p_buf, Line 1168  int io_buf_conv(iconv_t cd, char *p_buf,
1168                  {                  {
1169                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
1170                          {                          {
1171                                  log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);                                  log_debug("iconv(inbytes=%zu, outbytes=%zu) error: EINVAL, in_buf[0]=%d\n",
1172                                                      in_bytes, out_bytes, (unsigned char)in_buf[0]);
1173                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1174                                  {                                  {
1175                                          *p_buf_len -= (int)(in_buf - p_buf);                                          *p_buf_len -= (int)(in_buf - p_buf);
# Line 1125  int io_buf_conv(iconv_t cd, char *p_buf, Line 1182  int io_buf_conv(iconv_t cd, char *p_buf,
1182                          }                          }
1183                          else if (errno == E2BIG)                          else if (errno == E2BIG)
1184                          {                          {
1185                                  log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%zu, outbytes=%zu) error: E2BIG\n", in_bytes, out_bytes);
1186                                  return -1;                                  return -1;
1187                          }                          }
1188                          else if (errno == EILSEQ)                          else if (errno == EILSEQ)
1189                          {                          {
1190                                  if (in_bytes > out_bytes || out_bytes <= 0)                                  if (in_bytes > out_bytes || out_bytes <= 0)
1191                                  {                                  {
1192                                          log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                          log_error("iconv(inbytes=%zu, outbytes=%zu) error: EILSEQ\n", in_bytes, out_bytes);
1193                                          return -2;                                          return -2;
1194                                  }                                  }
1195    
# Line 1140  int io_buf_conv(iconv_t cd, char *p_buf, Line 1197  int io_buf_conv(iconv_t cd, char *p_buf,
1197                                  if (in_bytes == 0)                                  if (in_bytes == 0)
1198                                  {                                  {
1199                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1200                                          log_debug("Reset in_bytes from 0 to %d\n", in_bytes);                                          log_debug("Reset in_bytes from 0 to %zu\n", in_bytes);
1201                                  }                                  }
1202    
1203                                  log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n",                                  log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: EILSEQ, in_buf[0]=%d\n",
1204                                                    in_bytes, out_bytes, in_buf[0]);                                                    in_bytes, out_bytes, (unsigned char)in_buf[0]);
1205                                  skip_current = 1;                                  skip_current = 1;
1206                          }                          }
1207                          else // something strange                          else // something strange
1208                          {                          {
1209                                  log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",                                  log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: %d, in_buf[0]=%d\n",
1210                                                    in_bytes, out_bytes, errno, in_buf[0]);                                                    in_bytes, out_bytes, errno, (unsigned char)in_buf[0]);
1211                                  *p_buf_offset = (int)(in_buf - p_buf);                                  *p_buf_offset = (int)(in_buf - p_buf);
1212                                  *p_conv_len = (int)(conv_size - out_bytes);                                  *p_conv_len = (int)(conv_size - out_bytes);
1213                                  skip_current = 1;                                  skip_current = 1;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1