2 * Chat -- a program for automatic session establishment (i.e. dial
3 * the phone and log in).
5 * Standard termination codes:
6 * 0 - successful completion of the script
7 * 1 - invalid argument, expect string too large, etc.
8 * 2 - error on an I/O operation or fatal error condition.
9 * 3 - timeout waiting for a simple string.
10 * 4 - the first string declared as "ABORT"
11 * 5 - the second string declared as "ABORT"
12 * 6 - ... and so on for successive ABORT strings.
14 * This software is in the public domain.
17 * added -T and -U option and \T and \U substitution to pass a phone
18 * number into chat script. Two are needed for some ISDN TA applications.
19 * Keith Dart <kdart@cisco.com>
22 * Added SAY keyword to send output to stderr.
23 * This allows to turn ECHO OFF and to output specific, user selected,
24 * text to give progress messages. This best works when stderr
25 * exists (i.e.: pppd in nodetach mode).
27 * Added HANGUP directives to allow for us to be called
28 * back. When HANGUP is set to NO, chat will not hangup at HUP signal.
29 * We rely on timeouts in that case.
31 * Added CLR_ABORT to clear previously set ABORT string. This has been
32 * dictated by the HANGUP above as "NO CARRIER" (for example) must be
33 * an ABORT condition until we know the other host is going to close
34 * the connection for call back. As soon as we have completed the
35 * first stage of the call back sequence, "NO CARRIER" is a valid, non
36 * fatal string. As soon as we got called back (probably get "CONNECT"),
37 * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
38 * Note that CLR_ABORT packs the abort_strings[] array so that we do not
39 * have unused entries not being reclaimed.
41 * In the same vein as above, added CLR_REPORT keyword.
43 * Allow for comments. Line starting with '#' are comments and are
44 * ignored. If a '#' is to be expected as the first character, the
45 * expect string must be quoted.
48 * Francis Demierre <Francis@SwissMail.Com>
49 * Thu May 15 17:15:40 MET DST 1997
52 * Added -r "report file" switch & REPORT keyword.
53 * Robert Geer <bgeer@xmission.com>
55 * Added -s "use stderr" and -S "don't use syslog" switches.
57 * Karl O. Pinc <kop@meme.com>
60 * Added -e "echo" switch & ECHO keyword
61 * Dick Streefland <dicks@tasking.nl>
64 * Considerable updates and modifications by
65 * Al Longyear <longyear@pobox.com>
66 * Paul Mackerras <paulus@cs.anu.edu.au>
69 * The original author is:
71 * Karl Fox <karl@MorningStar.Com>
72 * Morning Star Technologies, Inc.
78 * $FreeBSD: src/usr.bin/chat/chat.c,v 1.15 1999/11/25 07:28:54 kris Exp $
79 * $DragonFly: src/usr.bin/chat/chat.c,v 1.4 2003/11/03 19:31:28 eirikn Exp $
91 #include <sys/types.h>
121 #define __V(x) (va_alist) va_dcl
126 #define O_NONBLOCK O_NDELAY
131 extern char *sys_errlist[];
132 #define memmove(to, from, n) bcopy(from, to, n)
133 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
137 /*************** Micro getopt() *********************************************/
138 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
139 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
140 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
141 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
142 (_O=4,(char*)0):(char*)0)
143 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
144 #define ARG(c,v) (c?(--c,*v++):(char*)0)
146 static int _O = 0; /* Internal state */
147 /*************** Micro getopt() *********************************************/
149 #define MAX_ABORTS 50
150 #define MAX_REPORTS 50
151 #define DEFAULT_CHAT_TIMEOUT 45
161 FILE* report_fp = (FILE *) 0;
162 char *report_file = (char *) 0;
163 char *chat_file = (char *) 0;
164 char *phone_num = (char *) 0;
165 char *phone_num2 = (char *) 0;
166 int timeout = DEFAULT_CHAT_TIMEOUT;
168 int have_tty_parameters = 0;
171 #define term_parms struct termio
172 #define get_term_param(param) ioctl(0, TCGETA, param)
173 #define set_term_param(param) ioctl(0, TCSETA, param)
174 struct termio saved_tty_parameters;
178 #define term_parms struct termios
179 #define get_term_param(param) tcgetattr(0, param)
180 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
181 struct termios saved_tty_parameters;
184 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
186 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
187 int clear_abort_next = 0;
189 char *report_string[MAX_REPORTS] ;
190 char report_buffer[50] ;
191 int n_reports = 0, report_next = 0, report_gathering = 0 ;
192 int clear_report_next = 0;
194 int say_next = 0, hup_next = 0;
196 void *dup_mem(void *b, size_t c);
197 void *copy_of(char *s);
198 static void usage(void);
199 void logf(const char *fmt, ...);
200 void fatal(int code, const char *fmt, ...);
201 SIGTYPE sigalrm(int signo);
202 SIGTYPE sigint(int signo);
203 SIGTYPE sigterm(int signo);
204 SIGTYPE sighup(int signo);
207 void set_tty_parameters(void);
208 void echo_stderr(int);
209 void break_sequence(void);
210 void terminate(int status);
211 void do_file(char *chat_file);
212 int get_string(register char *string);
213 int put_string(register char *s);
214 int write_char(int c);
217 void chat_send(register char *s);
218 char *character(int c);
219 void chat_expect(register char *s);
220 char *clean(register char *s, int sending);
221 void break_sequence(void);
222 void terminate(int status);
223 void pack_array(char **array, int end);
224 char *expect_strtok(char *, char *);
225 int vfmtmsg(char *, int, const char *, va_list); /* vsprintf++ */
227 int main(int, char *[]);
233 void *ans = malloc (c);
235 fatal(2, "memory error!");
241 void *copy_of (char *s)
243 return dup_mem (s, strlen (s) + 1);
247 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
248 * [ -r report-file ] \
249 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
251 * Perform a UUCP-dialer-like chat script on stdin and stdout.
254 main(int argc, char **argv)
261 while ((option = OPTION(argc, argv)) != 0) {
284 if ((arg = OPTARG(argc, argv)) != NULL)
285 chat_file = copy_of(arg);
291 if ((arg = OPTARG(argc, argv)) != NULL)
298 arg = OPTARG (argc, argv);
300 if (report_fp != NULL)
302 report_file = copy_of (arg);
303 report_fp = fopen (report_file, "a");
304 if (report_fp != NULL) {
306 fprintf (report_fp, "Opening \"%s\"...\n",
314 if ((arg = OPTARG(argc, argv)) != NULL)
315 phone_num = copy_of(arg);
321 if ((arg = OPTARG(argc, argv)) != NULL)
322 phone_num2 = copy_of(arg);
333 * Default the report file to the stderr location
335 if (report_fp == NULL)
340 openlog("chat", LOG_PID);
342 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
345 setlogmask(LOG_UPTO(LOG_INFO));
347 setlogmask(LOG_UPTO(LOG_WARNING));
353 if (chat_file != NULL) {
354 arg = ARG(argc, argv);
360 while ((arg = ARG(argc, argv)) != NULL) {
363 if ((arg = ARG(argc, argv)) != NULL)
373 * Process a chat script when read from a file.
376 void do_file (char *chat_file)
379 char *sp, *arg, quote;
383 cfp = fopen (chat_file, "r");
385 fatal(1, "%s -- open failed: %m", chat_file);
390 while (fgets(buf, STR_LEN, cfp) != NULL) {
391 sp = strchr (buf, '\n');
398 /* lines starting with '#' are comments. If a real '#'
399 is to be expected, it should be quoted .... */
403 while (*sp != '\0') {
404 if (*sp == ' ' || *sp == '\t') {
409 if (*sp == '"' || *sp == '\'') {
412 while (*sp != quote) {
414 fatal(1, "unterminated quote (line %d)", linect);
424 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
442 * We got an error parsing the command line.
448 Usage: chat [-e] [-v] [-V] [-t timeout] [-r report-file] [-T phone-number]\n\
449 [-U phone-number2] {-f chat-file | chat-script}\n");
456 * Send a message to syslog and/or stderr.
458 void logf __V((const char *fmt, ...))
464 vfmtmsg(line, sizeof(line), fmt, args);
466 syslog(LOG_INFO, "%s", line);
468 fprintf(stderr, "%s\n", line);
472 * Print an error message and terminate.
475 void fatal __V((int code, const char *fmt, ...))
481 vfmtmsg(line, sizeof(line), fmt, args);
483 syslog(LOG_ERR, "%s", line);
485 fprintf(stderr, "%s\n", line);
491 SIGTYPE sigalrm(int signo)
496 alarmed = 1; /* Reset alarm to avoid race window */
497 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
499 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
500 fatal(2, "Can't get file mode flags on stdin: %m");
502 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
503 fatal(2, "Can't set file mode flags on stdin: %m");
513 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
514 fatal(2, "Can't get file mode flags on stdin: %m");
516 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
517 fatal(2, "Can't set file mode flags on stdin: %m");
520 SIGTYPE sigint(int signo)
525 SIGTYPE sigterm(int signo)
530 SIGTYPE sighup(int signo)
537 signal(SIGINT, sigint);
538 signal(SIGTERM, sigterm);
539 signal(SIGHUP, sighup);
541 set_tty_parameters();
542 signal(SIGALRM, sigalrm);
547 void set_tty_parameters(void)
549 #if defined(get_term_param)
552 if (get_term_param (&t) < 0)
553 fatal(2, "Can't get terminal parameters: %m");
555 saved_tty_parameters = t;
556 have_tty_parameters = 1;
558 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
566 if (set_term_param (&t) < 0)
567 fatal(2, "Can't set terminal parameters: %m");
571 void break_sequence(void)
578 void terminate(int status)
581 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
583 * Allow the last of the report string to be gathered before we terminate.
585 if (report_gathering) {
588 rep_len = strlen(report_buffer);
589 while (rep_len + 1 <= sizeof(report_buffer)) {
593 if (c < 0 || iscntrl(c))
595 report_buffer[rep_len] = c;
598 report_buffer[rep_len] = 0;
599 fprintf (report_fp, "chat: %s\n", report_buffer);
602 fprintf (report_fp, "Closing \"%s\".\n", report_file);
604 report_fp = (FILE *) NULL;
607 #if defined(get_term_param)
608 if (have_tty_parameters) {
609 if (set_term_param (&saved_tty_parameters) < 0)
610 fatal(2, "Can't restore terminal parameters: %m");
618 * 'Clean up' this string.
620 /* sending: set to 1 when sending (putting) this string. */
621 char *clean(register char *s, int sending)
623 char temp[STR_LEN], cur_chr;
624 register char *s1, *phchar;
625 int add_return = sending;
626 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
629 /* Don't overflow buffer, leave room for chars we append later */
630 while (*s && s1 - temp < sizeof(temp) - 2 - add_return) {
632 if (cur_chr == '^') {
634 if (cur_chr == '\0') {
645 if (cur_chr != '\\') {
651 if (cur_chr == '\0') {
665 if (sending && *s == '\0')
682 if (sending && phone_num) {
683 for ( phchar = phone_num; *phchar != '\0'; phchar++)
693 if (sending && phone_num2) {
694 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
733 if (isoctal (cur_chr)) {
737 cur_chr |= *s++ - '0';
740 cur_chr |= *s++ - '0';
744 if (cur_chr != 0 || sending) {
745 if (sending && (cur_chr == '\\' || cur_chr == 0))
762 *s1++ = '\0'; /* guarantee closure */
763 *s1++ = '\0'; /* terminate the string */
764 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
768 * A modified version of 'strtok'. This version skips \ sequences.
771 char *expect_strtok (char *s, char *term)
773 static char *str = "";
778 * If a string was specified then do initial processing.
784 * If this is the escape flag then reset it and ignore the character.
805 * If this is not in the termination string, continue.
807 if (strchr (term, *str) == (char *) 0) {
813 * This is the terminator. Mark the end of the string and stop.
822 * Process the expect string
825 void chat_expect (char *s)
830 if (strcmp(s, "HANGUP") == 0) {
835 if (strcmp(s, "ABORT") == 0) {
840 if (strcmp(s, "CLR_ABORT") == 0) {
845 if (strcmp(s, "REPORT") == 0) {
850 if (strcmp(s, "CLR_REPORT") == 0) {
855 if (strcmp(s, "TIMEOUT") == 0) {
860 if (strcmp(s, "ECHO") == 0) {
865 if (strcmp(s, "SAY") == 0) {
871 * Fetch the expect and reply string.
874 expect = expect_strtok (s, "-");
877 if (expect == (char *) 0)
880 reply = expect_strtok (s, "-");
883 * Handle the expect string. If successful then exit.
885 if (get_string (expect))
889 * If there is a sub-reply string then send it. Otherwise any condition
892 if (reply == (char *) 0 || exit_code != 3)
899 * The expectation did not occur. This is terminal.
902 logf("Failed (%s)", fail_reason);
905 terminate(exit_code);
909 * Translate the input character to the appropriate string for printing
913 char *character(int c)
915 static char string[10];
918 meta = (c & 0x80) ? "M-" : "";
922 sprintf(string, "%s^%c", meta, (int)c + '@');
924 sprintf(string, "%s^?", meta);
926 sprintf(string, "%s%c", meta, c);
932 * process the reply string
934 void chat_send (register char *s)
939 write(2, s, strlen(s));
946 if (strcmp(s, "OFF") == 0)
947 signal(SIGHUP, SIG_IGN);
949 signal(SIGHUP, sighup);
955 echo = (strcmp(s, "ON") == 0);
964 if (n_aborts >= MAX_ABORTS)
965 fatal(2, "Too many ABORT strings");
969 if (strlen(s1) > strlen(s)
970 || strlen(s1) + 1 > sizeof(fail_buffer))
971 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
973 abort_string[n_aborts++] = s1;
976 logf("abort on (%v)", s);
980 if (clear_abort_next) {
986 clear_abort_next = 0;
990 if (strlen(s1) > strlen(s)
991 || strlen(s1) + 1 > sizeof(fail_buffer))
992 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
995 for (i=0; i < n_aborts; i++) {
996 if ( strcmp(s1,abort_string[i]) == 0 ) {
997 free(abort_string[i]);
998 abort_string[i] = NULL;
1002 logf("clear abort on (%v)", s);
1007 pack_array(abort_string,old_max);
1015 if (n_reports >= MAX_REPORTS)
1016 fatal(2, "Too many REPORT strings");
1020 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1021 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1023 report_string[n_reports++] = s1;
1026 logf("report (%v)", s);
1030 if (clear_report_next) {
1036 clear_report_next = 0;
1040 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1041 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1043 old_max = n_reports;
1044 for (i=0; i < n_reports; i++) {
1045 if ( strcmp(s1,report_string[i]) == 0 ) {
1046 free(report_string[i]);
1047 report_string[i] = NULL;
1051 logf("clear report (%v)", s);
1056 pack_array(report_string,old_max);
1066 timeout = DEFAULT_CHAT_TIMEOUT;
1069 logf("timeout set to %d seconds", timeout);
1074 if (strcmp(s, "EOT") == 0)
1076 else if (strcmp(s, "BREAK") == 0)
1088 status = read(0, &c, 1);
1092 return ((int)c & 0x7F);
1095 logf("warning: read() on stdin returned %d", status);
1098 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1099 fatal(2, "Can't get file mode flags on stdin: %m");
1101 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1102 fatal(2, "Can't set file mode flags on stdin: %m");
1113 usleep(10000); /* inter-character typing delay (?) */
1115 status = write(1, &ch, 1);
1122 logf("warning: write() on stdout returned %d", status);
1125 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1126 fatal(2, "Can't get file mode flags on stdin, %m");
1128 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1129 fatal(2, "Can't set file mode flags on stdin: %m");
1135 int write_char (int c)
1137 if (alarmed || put_char(c) < 0) {
1142 if (errno == EINTR || errno == EWOULDBLOCK)
1143 logf(" -- write timed out");
1145 logf(" -- write failed: %m");
1152 int put_string (register char *s)
1159 logf("send (??????)");
1161 logf("send (%v)", s);
1164 alarm(timeout); alarmed = 0;
1167 register char c = *s++;
1170 if (!write_char (c))
1186 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1190 if (!write_char (c))
1202 * Echo a character to stderr.
1203 * When called with -1, a '\n' character is generated when
1204 * the cursor is not at the beginning of a line.
1206 void echo_stderr(int n)
1212 case '\r': /* ignore '\r' */
1224 write(2, s, strlen(s));
1231 * 'Wait for' this string to appear on this file descriptor.
1233 int get_string(register char *string)
1236 int c, printed = 0, len, minlen;
1237 register char *s = temp, *end = s + STR_LEN;
1238 char *logged = temp;
1240 fail_reason = (char *)0;
1242 if (strlen(string) > STR_LEN) {
1243 logf("expect string is too long");
1248 string = clean(string, 0);
1249 len = strlen(string);
1250 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1253 logf("expect (%v)", string);
1264 while ( ! alarmed && (c = get_char()) >= 0) {
1265 int n, abort_len, report_len;
1269 if (verbose && c == '\n') {
1271 logf(""); /* blank line */
1273 logf("%0.*v", s - logged, logged);
1279 if (verbose && s >= logged + 80) {
1280 logf("%0.*v", s - logged, logged);
1286 fputc( '\n', stderr );
1288 fprintf( stderr, "%s", character(c) );
1291 if (!report_gathering) {
1292 for (n = 0; n < n_reports; ++n) {
1293 if ((report_string[n] != (char*) NULL) &&
1294 s - temp >= (report_len = strlen(report_string[n])) &&
1295 strncmp(s - report_len, report_string[n], report_len) == 0) {
1296 time_t time_now = time ((time_t*) NULL);
1297 struct tm* tm_now = localtime (&time_now);
1299 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1300 strcat (report_buffer, report_string[n]);
1302 report_string[n] = (char *) NULL;
1303 report_gathering = 1;
1310 int rep_len = strlen (report_buffer);
1311 report_buffer[rep_len] = c;
1312 report_buffer[rep_len + 1] = '\0';
1315 report_gathering = 0;
1316 fprintf (report_fp, "chat: %s\n", report_buffer);
1320 if (s - temp >= len &&
1321 c == string[len - 1] &&
1322 strncmp(s - len, string, len) == 0) {
1325 logf("%0.*v", s - logged, logged);
1326 logf(" -- got it\n");
1334 for (n = 0; n < n_aborts; ++n) {
1335 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1336 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1339 logf("%0.*v", s - logged, logged);
1346 strcpy(fail_reason = fail_buffer, abort_string[n]);
1352 if (logged < s - minlen) {
1353 logf("%0.*v", s - logged, logged);
1357 memmove(temp, s, minlen);
1358 logged = temp + (logged - s);
1362 if (alarmed && verbose)
1363 logf("warning: alarm synchronization problem");
1368 if (verbose && printed) {
1370 logf(" -- read timed out");
1372 logf(" -- read failed: %m");
1381 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1384 #include <sys/param.h>
1385 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1391 #include <sys/types.h>
1392 #include <sys/time.h>
1395 usleep -- support routine for 4.2BSD system call emulations
1396 last edit: 29-Oct-1984 D A Gwyn
1399 extern int select();
1401 /* usec: delay in microseconds */
1403 usleep( long usec ) /* returns 0 if ok, else -1 */
1405 static struct { /* `timeval' */
1406 long tv_sec; /* seconds */
1407 long tv_usec; /* microsecs */
1408 } delay; /* _select() timeout */
1410 delay.tv_sec = usec / 1000000L;
1411 delay.tv_usec = usec % 1000000L;
1413 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1418 pack_array (char **array, int end)
1422 for (i = 0; i < end; i++) {
1423 if (array[i] == NULL) {
1424 for (j = i+1; j < end; ++j)
1425 if (array[j] != NULL)
1426 array[i++] = array[j];
1427 for (; i < end; ++i)
1435 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1436 * also specify the length of the output buffer, and we handle the
1437 * %m (error message) format.
1438 * Doesn't do floating-point formats.
1439 * Returns the number of chars put into buf.
1441 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1444 vfmtmsg(char *buf, int buflen, const char *fmt, va_list args)
1447 int width, prec, fillch;
1448 int base, len, neg, quoted;
1449 unsigned long val = 0;
1454 static char hexchars[] = "0123456789abcdef";
1458 while (buflen > 0) {
1459 for (f = fmt; *f != '%' && *f != 0; ++f)
1465 memcpy(buf, fmt, len);
1480 width = va_arg(args, int);
1483 while (isdigit(c)) {
1484 width = width * 10 + c - '0';
1491 prec = va_arg(args, int);
1494 while (isdigit(c)) {
1495 prec = prec * 10 + c - '0';
1506 i = va_arg(args, int);
1515 val = va_arg(args, unsigned int);
1519 val = va_arg(args, unsigned int);
1523 val = (unsigned long) va_arg(args, void *);
1528 str = va_arg(args, char *);
1531 num[0] = va_arg(args, int);
1536 str = strerror(errno);
1538 case 'v': /* "visible" string */
1539 case 'q': /* quoted string */
1541 p = va_arg(args, unsigned char *);
1542 if (fillch == '0' && prec > 0) {
1545 n = strlen((char *)p);
1546 if (prec > 0 && prec < n)
1549 while (n > 0 && buflen > 0) {
1552 if (!quoted && c >= 0x80) {
1557 if (quoted && (c == '"' || c == '\\'))
1559 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1563 case '\t': OUTCHAR('t'); break;
1564 case '\n': OUTCHAR('n'); break;
1565 case '\b': OUTCHAR('b'); break;
1566 case '\f': OUTCHAR('f'); break;
1569 OUTCHAR(hexchars[c >> 4]);
1570 OUTCHAR(hexchars[c & 0xf]);
1587 --fmt; /* so %z outputs %z etc. */
1592 str = num + sizeof(num);
1594 while (str > num + neg) {
1595 *--str = hexchars[val % base];
1597 if (--prec <= 0 && val == 0)
1609 len = num + sizeof(num) - 1 - str;
1612 if (prec > 0 && len > prec)
1618 if ((n = width - len) > 0) {
1626 memcpy(buf, str, len);