source: code/icb.c@ 65b48ee

Last change on this file since 65b48ee was 65b48ee, checked in by Mike Belopuhov <mike@…>, 10 years ago

Improve [v]snprintf return value handling

  • Property mode set to 100644
File size: 17.7 KB
Line 
1/*
2 * Copyright (c) 2009, 2010, 2013, 2014 Mike Belopuhov
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/types.h>
18#include <sys/queue.h>
19#include <netdb.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <syslog.h>
25#include <unistd.h>
26#include <ctype.h>
27#include <event.h>
28#include <vis.h>
29
30#include "icb.h"
31#include "icbd.h"
32
33extern int creategroups;
34extern char srvname[NI_MAXHOST];
35
36void icb_command(struct icb_session *, char *, char *);
37void icb_groupmsg(struct icb_session *, char *);
38int icb_login(struct icb_session *, char *, char *, char *);
39int icb_dowho(struct icb_session *, struct icb_group *);
40
41/*
42 * icb_init: initializes pointers to callbacks
43 */
44void
45icb_init(void)
46{
47 LIST_INIT(&groups);
48
49 if (strlen(srvname) == 0)
50 (void)gethostname(srvname, sizeof srvname);
51 /*
52 * NI_MAXHOST is usually greater than what we
53 * can actually send, hence truncation:
54 */
55 if (strlen(srvname) > 200)
56 srvname[200] = '\0';
57}
58
59/*
60 * icb_start: called upon accepting a new connection, greets new client
61 */
62void
63icb_start(struct icb_session *is)
64{
65 icb_sendfmt(is, "%c%c%c%s%c%s", ICB_M_PROTO, '1', ICB_M_SEP, srvname,
66 ICB_M_SEP, "icbd");
67 SETF(is->flags, ICB_SF_PROTOSENT);
68}
69
70/*
71 * icb_input: main input processing routine
72 */
73int
74icb_input(struct icb_session *is)
75{
76 char *msg = is->buffer;
77 int msglen = is->length;
78 unsigned char type;
79 char *wptr = NULL;
80 int res = 0;
81
82 is->last = getmonotime();
83 type = msg[0];
84 msg++;
85 if (!ISSETF(is->flags, ICB_SF_LOGGEDIN) && type != ICB_M_LOGIN) {
86 icb_error(is, "Not logged in");
87 return (0);
88 }
89 switch (type) {
90 case ICB_M_LOGIN: {
91 char client[ICB_MAXNICKLEN];
92 char nick[ICB_MAXNICKLEN];
93 char group[ICB_MAXGRPLEN];
94 char cmd[ICB_MAXCMDLEN];
95
96 if (icb_token(msg, msglen, &wptr, client, ICB_MAXNICKLEN,
97 ICB_M_SEP, 1) < 0) {
98 icb_error(is, "Invalid client");
99 icbd_drop(is, NULL);
100 return (1);
101 }
102 if (icb_token(msg, msglen, &wptr, nick, ICB_MAXNICKLEN,
103 ICB_M_SEP, 1) <= 0) {
104 icb_error(is, "Invalid nick");
105 icbd_drop(is, NULL);
106 return (1);
107 }
108 if (icb_token(msg, msglen, &wptr, group, ICB_MAXGRPLEN,
109 ICB_M_SEP, 1) < 0) {
110 icb_error(is, "Invalid login group");
111 icbd_drop(is, NULL);
112 return (1);
113 }
114 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
115 ICB_M_SEP, 1) < 0) {
116 icb_error(is, "Invalid command");
117 icbd_drop(is, NULL);
118 return (1);
119 }
120 if (strlen(cmd) > 0 && cmd[0] == 'w') {
121 icb_error(is, "Command not implemented");
122 icbd_drop(is, NULL);
123 return (1);
124 }
125 if (strlen(cmd) == 0 || strcmp(cmd, "login") != 0) {
126 icb_error(is, "Malformed login packet");
127 icbd_drop(is, NULL);
128 return (1);
129 }
130 res = icb_login(is, group, nick, client);
131 break;
132 }
133 case ICB_M_OPEN: {
134 icb_groupmsg(is, msg);
135 break;
136 }
137 case ICB_M_COMMAND: {
138 char cmd[ICB_MAXCMDLEN];
139 char arg[ICB_MAXTOPICLEN];
140
141 if (icb_token(msg, msglen, &wptr, cmd, ICB_MAXCMDLEN,
142 ICB_M_SEP, 1) <= 0) {
143 icb_error(is, "Invalid command");
144 icbd_drop(is, NULL);
145 return (1);
146 }
147 if (icb_token(msg, msglen, &wptr, arg, ICB_MAXTOPICLEN,
148 ICB_M_SEP, 1) < 0) {
149 icb_error(is, "Invalid argument");
150 icbd_drop(is, NULL);
151 return (1);
152 }
153 icb_command(is, cmd, arg);
154 break;
155 }
156 case ICB_M_PONG: {
157 icb_sendfmt(is, "%c", ICB_M_PING);
158 break;
159 }
160 case ICB_M_PROTO:
161 case ICB_M_NOOP:
162 /* ignore */
163 break;
164 default:
165 /* everything else is not valid */
166 icb_error(is, "Undefined message type %u", type);
167 }
168 return (res);
169}
170
171/*
172 * icb_login: handles login ('a') packets
173 */
174int
175icb_login(struct icb_session *is, char *grp, char *nick, char *client)
176{
177 const char *defgrp = "1";
178 struct icb_group *ig;
179 struct icb_session *s;
180 char group[ICB_MAXGRPLEN];
181
182 if (!nick || strlen(nick) == 0 ||
183 icb_vis(is->nick, nick, ICB_MAXNICKLEN, VIS_SP)) {
184 icb_error(is, "Invalid nick");
185 icbd_drop(is, NULL);
186 return (1);
187 }
188 if (!grp || strlen(grp) == 0)
189 strlcpy(group, defgrp, ICB_MAXGRPLEN);
190 else
191 icb_vis(group, grp, ICB_MAXGRPLEN, VIS_SP);
192 LIST_FOREACH(ig, &groups, entry) {
193 if (strcmp(ig->name, group) == 0)
194 break;
195 }
196 if (ig == NULL) {
197 if (!creategroups) {
198 icb_error(is, "Can't create new groups", group);
199 icbd_drop(is, NULL);
200 return (1);
201 } else {
202 if ((ig = icb_addgroup(is, group)) == NULL) {
203 icb_error(is, "Can't create group %s", group);
204 return (0);
205 }
206 icbd_log(NULL, LOG_DEBUG, "%s created group %s",
207 is->nick, group);
208 }
209 }
210 LIST_FOREACH(s, &ig->sess, entry) {
211 if (strcmp(s->nick, is->nick) == 0) {
212 icb_error(is, "Nick is already in use");
213 icbd_drop(is, NULL);
214 return (1);
215 }
216 }
217
218 if (client && strlen(client) > 0)
219 icb_vis(is->client, client, sizeof is->client, VIS_SP);
220 else
221 strlcpy(is->client, is->nick, sizeof is->client);
222 is->group = ig;
223 is->login = time(NULL);
224 is->last = getmonotime();
225
226 /* notify group */
227 icb_status_group(ig, NULL, STATUS_SIGNON, "%s (%s@%s) entered group",
228 is->nick, is->client, is->host);
229
230 CLRF(is->flags, ICB_SF_PROTOSENT);
231 SETF(is->flags, ICB_SF_LOGGEDIN);
232
233 LIST_INSERT_HEAD(&ig->sess, is, entry);
234
235 /* acknowledge successful login */
236 icb_sendfmt(is, "%c", ICB_M_LOGIN);
237
238 /* notify user */
239 icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
240 icb_ismod(ig, is) ? " as moderator" : "");
241
242 /* send user a topic name */
243 if (strlen(ig->topic) > 0)
244 icb_status(is, STATUS_TOPIC, "Topic for %s is \"%s\"",
245 ig->name, ig->topic);
246 return (0);
247}
248
249/*
250 * icb_groupmsg: handles open message ('b') packets
251 */
252void
253icb_groupmsg(struct icb_session *is, char *msg)
254{
255 char buf[ICB_MSGSIZE];
256 struct icb_group *ig = is->group;
257 struct icb_session *s;
258 int res, buflen = 1;
259
260 if (strlen(msg) == 0) {
261 icb_error(is, "Empty message");
262 return;
263 }
264
265 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
266 is->nick, ICB_M_SEP, msg);
267 if (res < 0) {
268 icb_error(is, "Format error");
269 return;
270 }
271 buflen += MIN((size_t)res, sizeof buf - 1);
272 buf[0] = buflen;
273
274 logger(ig->name, is->nick, msg);
275
276 LIST_FOREACH(s, &ig->sess, entry) {
277 if (s == is)
278 continue;
279 icbd_send(s, buf, buflen + 1);
280 }
281}
282
283/*
284 * icb_privmsg: handles personal message ('c') packets
285 */
286void
287icb_privmsg(struct icb_session *is, char *to, char *msg)
288{
289 struct icb_group *ig = is->group;
290 struct icb_session *s;
291 char whom[ICB_MAXNICKLEN];
292
293 icb_vis(whom, to, ICB_MAXNICKLEN, VIS_SP);
294
295 /* try home group first */
296 LIST_FOREACH(s, &ig->sess, entry) {
297 if (strcmp(s->nick, whom) == 0)
298 break;
299 }
300 if (!s) {
301 /* try all groups until the first match */
302 LIST_FOREACH(ig, &groups, entry) {
303 LIST_FOREACH(s, &ig->sess, entry) {
304 if (strcmp(s->nick, whom) == 0)
305 break;
306 }
307 if (s)
308 break;
309 }
310 if (!s) {
311 icb_error(is, "No such user %s", whom);
312 return;
313 }
314 }
315 icb_sendfmt(s, "%c%s%c%s", ICB_M_PERSONAL, is->nick, ICB_M_SEP, msg);
316}
317
318/*
319 * icb_command: handles command ('h') packets
320 */
321void
322icb_command(struct icb_session *is, char *cmd, char *arg)
323{
324 void (*handler)(struct icb_session *, char *);
325 char command[ICB_MAXCMDLEN];
326
327 icb_vis(command, cmd, sizeof command, VIS_SP);
328
329 if ((handler = icb_cmd_lookup(command)) == NULL) {
330 icb_error(is, "Unsupported command: %s", command);
331 return;
332 }
333 handler(is, arg);
334}
335
336/*
337 * icb_cmdout: sends out command output ('i') packets, called from the
338 * command handlers
339 */
340void
341icb_cmdout(struct icb_session *is, int type, char *outmsg)
342{
343 char *otype = NULL;
344
345 switch (type) {
346 case CMDOUT_CO:
347 otype = "co";
348 break;
349 case CMDOUT_EC:
350 otype = "ec";
351 break;
352 case CMDOUT_WG:
353 otype = "wg";
354 break;
355 case CMDOUT_WH:
356 otype = "wh";
357 break;
358 case CMDOUT_WL:
359 otype = "wl";
360 break;
361 default:
362 icbd_log(is, LOG_ERR, "unknown cmdout type %d", type);
363 return;
364 }
365 if (outmsg)
366 icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
367 outmsg);
368 else
369 icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
370}
371
372/*
373 * icb_status: sends a status message ('d') to the client
374 */
375void
376icb_status(struct icb_session *is, int type, const char *fmt, ...)
377{
378 va_list ap;
379 char buf[ICB_MSGSIZE];
380 int res, buflen = 1;
381 static const struct {
382 int type;
383 const char *msg;
384 } msgtab[] = {
385 { STATUS_ARRIVE, "Arrive" },
386 { STATUS_BOOT, "Boot" },
387 { STATUS_DEPART, "Depart" },
388 { STATUS_HELP, "Help" },
389 { STATUS_NAME, "Name" },
390 { STATUS_NOBEEP, "No-Beep" },
391 { STATUS_NOTIFY, "Notify" },
392 { STATUS_SIGNON, "Sign-on" },
393 { STATUS_SIGNOFF, "Sign-off" },
394 { STATUS_STATUS, "Status" },
395 { STATUS_TOPIC, "Topic" },
396 { STATUS_WARNING, "Warning" },
397 { 0, NULL }
398 };
399
400 if (type < 0 || type > (int)nitems(msgtab) - 1)
401 return;
402 res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
403 msgtab[type].msg, ICB_M_SEP);
404 if (res < 0) {
405 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
406 return;
407 }
408 buflen += MIN((size_t)res, sizeof buf - 1);
409 if ((size_t)buflen >= sizeof buf) {
410 icbd_log(NULL, LOG_ERR, "Status buffer too small");
411 return;
412 }
413 va_start(ap, fmt);
414 res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
415 va_end(ap);
416 if (res < 0) {
417 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
418 return;
419 }
420 buflen += MIN((size_t)res, sizeof buf - buflen);
421 buf[0] = buflen;
422 icbd_send(is, buf, buflen + 1);
423}
424
425/*
426 * icb_status: sends a status message ('d') to the group except of the
427 * "ex" if it's not NULL
428 */
429void
430icb_status_group(struct icb_group *ig, struct icb_session *ex, int type,
431 const char *fmt, ...)
432{
433 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
434 va_list ap;
435 struct icb_session *s;
436
437 va_start(ap, fmt);
438 (void)vsnprintf(buf, sizeof buf, fmt, ap);
439 LIST_FOREACH(s, &ig->sess, entry) {
440 if (ex && s == ex)
441 continue;
442 icb_status(s, type, buf);
443 }
444 logger(ig->name, "", buf);
445 icbd_log(NULL, LOG_DEBUG, "%s", buf);
446 va_end(ap);
447}
448
449/*
450 * icb_error: sends an error message ('e') to the client
451 */
452void
453icb_error(struct icb_session *is, const char *fmt, ...)
454{
455 char buf[ICB_MSGSIZE];
456 va_list ap;
457 int res, buflen = 1;
458
459 va_start(ap, fmt);
460 res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
461 va_end(ap);
462 if (res < 0) {
463 icbd_log(NULL, LOG_ERR, "Format error");
464 return;
465 }
466 buflen += MIN((size_t)res, sizeof buf - 2);
467 buf[0] = ++buflen; /* account for ICB_M_ERROR */
468 buf[1] = ICB_M_ERROR;
469 icbd_send(is, buf, buflen + 1);
470 icbd_log(is, LOG_DEBUG, "%s", buf + 2);
471}
472
473/*
474 * icb_remove: removes a session from the associated group
475 */
476void
477icb_remove(struct icb_session *is, char *reason)
478{
479 if (is->group) {
480 if (icb_ismod(is->group, is))
481 (void)icb_pass(is->group, is, NULL);
482 LIST_REMOVE(is, entry);
483 if (reason)
484 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
485 "%s (%s@%s) just left: %s", is->nick, is->client,
486 is->host, reason);
487 else
488 icb_status_group(is->group, NULL, STATUS_SIGNOFF,
489 "%s (%s@%s) just left", is->nick, is->client,
490 is->host);
491 is->group = NULL;
492 }
493}
494
495/*
496 * icb_addgroup: adds a new group to the list
497 */
498struct icb_group *
499icb_addgroup(struct icb_session *is, char *name)
500{
501 struct icb_group *ig;
502
503 if ((ig = calloc(1, sizeof *ig)) == NULL)
504 return (NULL);
505 strlcpy(ig->name, name, sizeof ig->name);
506 if (is)
507 ig->mod = is;
508 LIST_INIT(&ig->sess);
509 LIST_INSERT_HEAD(&groups, ig, entry);
510 return (ig);
511}
512
513#ifdef notused
514/*
515 * icb_delgroup: removes a group from the list
516 */
517void
518icb_delgroup(struct icb_group *ig)
519{
520 struct icb_session *s;
521
522 /* well, i guess we should kick out participants! ;-) */
523 LIST_FOREACH(s, &ig->sess, entry) {
524 icb_status(s, STATUS_WARNING, "Group dismissed");
525 s->group = NULL;
526 }
527 LIST_REMOVE(ig, entry);
528 bzero(ig, sizeof ig); /* paranoic thing, obviously */
529 free(ig);
530}
531#endif
532
533/*
534 * icb_dowho: a helper function that sends out a group header as a command
535 * output and user information in the "wl" format
536 */
537int
538icb_dowho(struct icb_session *is, struct icb_group *ig)
539{
540 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
541 struct icb_session *s;
542 time_t now;
543 int nusers = 0;
544
545 now = getmonotime();
546 icb_cmdout(is, CMDOUT_CO, " ");
547 snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
548 ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
549 strlen(ig->topic) > 0 ? ig->topic : "(None)");
550 icb_cmdout(is, CMDOUT_CO, buf);
551 LIST_FOREACH(s, &ig->sess, entry) {
552 (void)snprintf(buf, sizeof buf,
553 "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
554 icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
555 s->nick, ICB_M_SEP, now - s->last,
556 ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
557 s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
558 icb_cmdout(is, CMDOUT_WL, buf);
559 nusers++;
560 }
561 return (nusers);
562}
563
564/*
565 * icb_who: sends a list of users of either the specified group or all
566 * groups found on the server
567 */
568void
569icb_who(struct icb_session *is, struct icb_group *ig)
570{
571 char buf[ICB_MSGSIZE - 10]; /* truncate to make sure all fits */
572 struct icb_group *g;
573
574 if (!ig) {
575 int nusers = 0, ngroups = 0;
576
577 LIST_FOREACH(g, &groups, entry) {
578 nusers += icb_dowho(is, g);
579 ngroups++;
580 }
581 if (nusers > 0) {
582 (void)snprintf(buf, sizeof buf,
583 "Total: %d %s in %d %s",
584 nusers, nusers > 1 ? "users" : "user",
585 ngroups, ngroups > 1 ? "groups" : "group");
586 } else
587 (void)snprintf(buf, sizeof buf, "No users found.");
588 icb_cmdout(is, CMDOUT_CO, buf);
589 } else
590 (void)icb_dowho(is, ig);
591}
592
593/*
594 * icb_ismod: checks whether group is moderated by "is"
595 */
596inline int
597icb_ismod(struct icb_group *ig, struct icb_session *is)
598{
599 return (ig->mod == is);
600}
601
602/*
603 * icb_modpermit: checks user against the moderators table if it has
604 * been populated
605 */
606int
607icb_modpermit(struct icb_session *is, int enforce)
608{
609 extern char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
610 extern int modtabcnt;
611
612 icbd_modupdate();
613 if ((enforce ? 0 : modtabcnt == 0) ||
614 bsearch(is->nick, modtab, modtabcnt, ICB_MAXNICKLEN,
615 (int (*)(const void *, const void *))strcmp))
616 return (1);
617 return (0);
618}
619
620/*
621 * icb_pass: passes moderation of group "ig" from "from" to "to",
622 * returns -1 if "from" is not a moderator, 1 if passed
623 * to "to" and 0 otherwise (no moderator or passed to the
624 * internal bot)
625 */
626int
627icb_pass(struct icb_group *ig, struct icb_session *from,
628 struct icb_session *to)
629{
630 if (ig->mod && ig->mod != from)
631 return (-1);
632 if (!from && !to)
633 return (-1);
634 ig->mod = to;
635 if (to)
636 icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
637 " of %s", from ? from->nick : "server", ig->name);
638 icb_status_group(ig, to, STATUS_NOTIFY, "%s has passed moderation "
639 "to %s", from ? from->nick : "server", to ? to->nick : "server");
640 return (1);
641}
642
643/*
644 * icb_sendfmt: formats a string and sends it over
645 */
646void
647icb_sendfmt(struct icb_session *is, const char *fmt, ...)
648{
649 char buf[ICB_MSGSIZE];
650 va_list ap;
651 int res, buflen = 1;
652
653 va_start(ap, fmt);
654 res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
655 va_end(ap);
656 if (res < 0) {
657 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
658 return;
659 }
660 buflen += MIN((size_t)res, sizeof buf - 1);
661 buf[0] = buflen;
662 icbd_send(is, buf, buflen + 1);
663}
664
665/*
666 * icb_token: copies a sequence of characters delimited by the 'sep' character
667 * from the source buffer 'buf' at offset indicated by 'bufptr' to
668 * the destination buffer 'dst' and sets 'bufptr' to the next byte
669 * after 'sep'.
670 */
671int
672icb_token(char *buf, int len, char **bufptr, char *dst, int dstlen, int sep,
673 int trim)
674{
675 char *start;
676 int i, ret;
677
678 if (buf == NULL || len <= 0 || dst == NULL || dstlen <= 0)
679 return (0);
680 if (*bufptr == NULL)
681 *bufptr = buf;
682 start = *bufptr;
683 for (i = *bufptr - buf; i < len; i++, (*bufptr)++) {
684 if (**bufptr == sep || **bufptr == '\0') {
685 /* copy and null terminate the token */
686 ret = strlcpy(dst, start,
687 MIN(*bufptr - start + 1, dstlen));
688 if (**bufptr != '\0')
689 (*bufptr)++;
690 if (ret > 0 && trim)
691 ret = icb_trim(dst, dstlen);
692 return (ret);
693 }
694 }
695 /*
696 * Reached the end of the buffer without finding a field separator
697 * nor the end of line character. If we have advanced our pointer
698 * we should copy the resulting single field out.
699 */
700 if (*bufptr - start > 0) {
701 ret = strlcpy(dst, start, MIN(*bufptr - start + 1, dstlen));
702 if (ret > 0 && trim)
703 ret = icb_trim(dst, dstlen);
704 return (ret);
705 }
706 return (0);
707}
708
709/*
710 * icb_trim: trims trailing whitespace
711 */
712int
713icb_trim(char *buf, int len)
714{
715 char *p = buf;
716 int i;
717
718 for (i = 0; i < len && *p != '\0'; i++)
719 p++;
720 if (*p == '\0' && p - buf > 0)
721 p--;
722 while (p >= buf && isspace(*p)) {
723 *p = '\0';
724 i--;
725 if (p > buf)
726 p--;
727 }
728 return (i);
729}
730
731/*
732 * icb_vis: strnvis-like function that escapes percentages as well
733 */
734int
735icb_vis(char *dst, const char *src, size_t dstsize, int flags)
736{
737 int si = 0, di = 0, td;
738
739 while ((size_t)di < dstsize - 1 && src[si] != '\0') {
740 if (src[si] == '%') {
741 if ((size_t)di + 1 >= dstsize - 1)
742 break;
743 dst[di++] = '%', dst[di] = '%';
744 } else if (src[si] == ' ' && flags & VIS_SP)
745 dst[di] = '_';
746 else if (isgraph(src[si]) || src[si] == ' ')
747 dst[di] = src[si];
748 else {
749 td = snprintf(&dst[di], dstsize - di,
750 "\\%03o", (unsigned char)src[si]);
751 if (td == -1 || (size_t)td >= dstsize - di)
752 break;
753 di += td - 1;
754 }
755 si++, di++;
756 }
757 dst[MIN((size_t)di, dstsize - 1)] = '\0';
758 return (0);
759}
Note: See TracBrowser for help on using the repository browser.