/[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.75 by sysadm, Thu Dec 18 02:56:00 2025 UTC Revision 1.77 by sysadm, Thu Dec 18 11:18:29 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 120  int io_init(void) Line 138  int io_init(void)
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 193  int prints(const char *format, ...) Line 245  int prints(const char *format, ...)
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                  }                  }
260                  else                  else
261                  {                  {
262                          errno = EAGAIN;                          errno = EAGAIN;
263                          ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret);                          int need = stdout_buf_len + ret - OUTPUT_BUF_SIZE;
264                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d is required\n", need);
265                  }                  }
266          }          }
267    
# Line 216  int prints(const char *format, ...) Line 270  int prints(const char *format, ...)
270    
271  int outc(char c)  int outc(char c)
272  {  {
273          int ret;          int ret = 0;
274    
275          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
276          {          {
# Line 282  int iflush(void) Line 336  int iflush(void)
336                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
337                  {                  {
338  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
339                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)))                          if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
340  #else  #else
341                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR)))                          if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
342  #endif  #endif
343                          {                          {
344  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
# Line 440  int igetch(int timeout) Line 494  int igetch(int timeout)
494                                  for (int i = 0; i < nfds; i++)                                  for (int i = 0; i < nfds; i++)
495                                  {                                  {
496  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
497                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)))                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
498  #else  #else
499                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR)))                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
500  #endif  #endif
501                                          {                                          {
502  #ifdef HAVE_SYS_EPOLL_H  #ifdef HAVE_SYS_EPOLL_H
# Line 1058  int io_buf_conv(iconv_t cd, char *p_buf, Line 1112  int io_buf_conv(iconv_t cd, char *p_buf,
1112          size_t i = 0;          size_t i = 0;
1113          int skip_current = 0;          int skip_current = 0;
1114    
1115          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)
1116          {          {
1117                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
1118                  return -1;                  return -1;
# Line 1112  int io_buf_conv(iconv_t cd, char *p_buf, Line 1166  int io_buf_conv(iconv_t cd, char *p_buf,
1166                  {                  {
1167                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
1168                          {                          {
1169                                  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=%ld, outbytes=%ld) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
1170                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1171                                  {                                  {
1172                                          *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 1179  int io_buf_conv(iconv_t cd, char *p_buf,
1179                          }                          }
1180                          else if (errno == E2BIG)                          else if (errno == E2BIG)
1181                          {                          {
1182                                  log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%ld, outbytes=%ld) error: E2BIG\n", in_bytes, out_bytes);
1183                                  return -1;                                  return -1;
1184                          }                          }
1185                          else if (errno == EILSEQ)                          else if (errno == EILSEQ)
1186                          {                          {
1187                                  if (in_bytes > out_bytes || out_bytes <= 0)                                  if (in_bytes > out_bytes || out_bytes <= 0)
1188                                  {                                  {
1189                                          log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                          log_error("iconv(inbytes=%ld, outbytes=%ld) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1190                                          return -2;                                          return -2;
1191                                  }                                  }
1192    
# Line 1140  int io_buf_conv(iconv_t cd, char *p_buf, Line 1194  int io_buf_conv(iconv_t cd, char *p_buf,
1194                                  if (in_bytes == 0)                                  if (in_bytes == 0)
1195                                  {                                  {
1196                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1197                                          log_debug("Reset in_bytes from 0 to %d\n", in_bytes);                                          log_debug("Reset in_bytes from 0 to %ld\n", in_bytes);
1198                                  }                                  }
1199    
1200                                  log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n",                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: EILSEQ, in_buf[0]=%d\n",
1201                                                    in_bytes, out_bytes, in_buf[0]);                                                    in_bytes, out_bytes, in_buf[0]);
1202                                  skip_current = 1;                                  skip_current = 1;
1203                          }                          }
1204                          else // something strange                          else // something strange
1205                          {                          {
1206                                  log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: %d, in_buf[0]=%d\n",
1207                                                    in_bytes, out_bytes, errno, in_buf[0]);                                                    in_bytes, out_bytes, errno, in_buf[0]);
1208                                  *p_buf_offset = (int)(in_buf - p_buf);                                  *p_buf_offset = (int)(in_buf - p_buf);
1209                                  *p_conv_len = (int)(conv_size - out_bytes);                                  *p_conv_len = (int)(conv_size - out_bytes);


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

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