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.5 2003/11/06 19:30:04 eirikn Exp $
91 #include <sys/types.h>
120 #define __V(x) (va_alist) va_dcl
125 #define O_NONBLOCK O_NDELAY
130 extern char *sys_errlist[];
131 #define memmove(to, from, n) bcopy(from, to, n)
132 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
136 /*************** Micro getopt() *********************************************/
137 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
138 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
139 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
140 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
141 (_O=4,(char*)0):(char*)0)
142 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
143 #define ARG(c,v) (c?(--c,*v++):(char*)0)
145 static int _O = 0; /* Internal state */
146 /*************** Micro getopt() *********************************************/
148 #define MAX_ABORTS 50
149 #define MAX_REPORTS 50
150 #define DEFAULT_CHAT_TIMEOUT 45
160 FILE* report_fp = (FILE *) 0;
161 char *report_file = (char *) 0;
162 char *chat_file = (char *) 0;
163 char *phone_num = (char *) 0;
164 char *phone_num2 = (char *) 0;
165 int timeout = DEFAULT_CHAT_TIMEOUT;
167 int have_tty_parameters = 0;
170 #define term_parms struct termio
171 #define get_term_param(param) ioctl(0, TCGETA, param)
172 #define set_term_param(param) ioctl(0, TCSETA, param)
173 struct termio saved_tty_parameters;
177 #define term_parms struct termios
178 #define get_term_param(param) tcgetattr(0, param)
179 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
180 struct termios saved_tty_parameters;
183 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
185 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
186 int clear_abort_next = 0;
188 char *report_string[MAX_REPORTS] ;
189 char report_buffer[50] ;
190 int n_reports = 0, report_next = 0, report_gathering = 0 ;
191 int clear_report_next = 0;
193 int say_next = 0, hup_next = 0;
195 void *dup_mem(void *b, size_t c);
196 void *copy_of(char *s);
197 static void usage(void);
198 void logf(const char *fmt, ...);
199 void fatal(int code, const char *fmt, ...);
200 SIGTYPE sigalrm(int signo);
201 SIGTYPE sigint(int signo);
202 SIGTYPE sigterm(int signo);
203 SIGTYPE sighup(int signo);
206 void set_tty_parameters(void);
207 void echo_stderr(int);
208 void break_sequence(void);
209 void terminate(int status);
210 void do_file(char *chat_file);
211 int get_string(register char *string);
212 int put_string(register char *s);
213 int write_char(int c);
216 void chat_send(register char *s);
217 char *character(int c);
218 void chat_expect(register char *s);
219 char *clean(register char *s, int sending);
220 void break_sequence(void);
221 void terminate(int status);
222 void pack_array(char **array, int end);
223 char *expect_strtok(char *, char *);
224 int vfmtmsg(char *, int, const char *, va_list); /* vsprintf++ */
226 int main(int, char *[]);
232 void *ans = malloc (c);
234 fatal(2, "memory error!");
240 void *copy_of (char *s)
242 return dup_mem (s, strlen (s) + 1);
246 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
247 * [ -r report-file ] \
248 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
250 * Perform a UUCP-dialer-like chat script on stdin and stdout.
253 main(int argc, char **argv)
260 while ((option = OPTION(argc, argv)) != 0) {
283 if ((arg = OPTARG(argc, argv)) != NULL)
284 chat_file = copy_of(arg);
290 if ((arg = OPTARG(argc, argv)) != NULL)
297 arg = OPTARG (argc, argv);
299 if (report_fp != NULL)
301 report_file = copy_of (arg);
302 report_fp = fopen (report_file, "a");
303 if (report_fp != NULL) {
305 fprintf (report_fp, "Opening \"%s\"...\n",
313 if ((arg = OPTARG(argc, argv)) != NULL)
314 phone_num = copy_of(arg);
320 if ((arg = OPTARG(argc, argv)) != NULL)
321 phone_num2 = copy_of(arg);
332 * Default the report file to the stderr location
334 if (report_fp == NULL)
339 openlog("chat", LOG_PID);
341 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
344 setlogmask(LOG_UPTO(LOG_INFO));
346 setlogmask(LOG_UPTO(LOG_WARNING));
352 if (chat_file != NULL) {
353 arg = ARG(argc, argv);
359 while ((arg = ARG(argc, argv)) != NULL) {
362 if ((arg = ARG(argc, argv)) != NULL)
372 * Process a chat script when read from a file.
375 void do_file (char *chat_file)
378 char *sp, *arg, quote;
382 cfp = fopen (chat_file, "r");
384 fatal(1, "%s -- open failed: %m", chat_file);
389 while (fgets(buf, STR_LEN, cfp) != NULL) {
390 sp = strchr (buf, '\n');
397 /* lines starting with '#' are comments. If a real '#'
398 is to be expected, it should be quoted .... */
402 while (*sp != '\0') {
403 if (*sp == ' ' || *sp == '\t') {
408 if (*sp == '"' || *sp == '\'') {
411 while (*sp != quote) {
413 fatal(1, "unterminated quote (line %d)", linect);
423 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
441 * We got an error parsing the command line.
447 Usage: chat [-e] [-v] [-V] [-t timeout] [-r report-file] [-T phone-number]\n\
448 [-U phone-number2] {-f chat-file | chat-script}\n");
455 * Send a message to syslog and/or stderr.
457 void logf __V((const char *fmt, ...))
463 vfmtmsg(line, sizeof(line), fmt, args);
465 syslog(LOG_INFO, "%s", line);
467 fprintf(stderr, "%s\n", line);
471 * Print an error message and terminate.
474 void fatal __V((int code, const char *fmt, ...))
480 vfmtmsg(line, sizeof(line), fmt, args);
482 syslog(LOG_ERR, "%s", line);
484 fprintf(stderr, "%s\n", line);
490 SIGTYPE sigalrm(int signo)
495 alarmed = 1; /* Reset alarm to avoid race window */
496 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
498 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
499 fatal(2, "Can't get file mode flags on stdin: %m");
501 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
502 fatal(2, "Can't set file mode flags on stdin: %m");
512 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
513 fatal(2, "Can't get file mode flags on stdin: %m");
515 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
516 fatal(2, "Can't set file mode flags on stdin: %m");
519 SIGTYPE sigint(int signo)
524 SIGTYPE sigterm(int signo)
529 SIGTYPE sighup(int signo)
536 signal(SIGINT, sigint);
537 signal(SIGTERM, sigterm);
538 signal(SIGHUP, sighup);
540 set_tty_parameters();
541 signal(SIGALRM, sigalrm);
546 void set_tty_parameters(void)
548 #if defined(get_term_param)
551 if (get_term_param (&t) < 0)
552 fatal(2, "Can't get terminal parameters: %m");
554 saved_tty_parameters = t;
555 have_tty_parameters = 1;
557 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
565 if (set_term_param (&t) < 0)
566 fatal(2, "Can't set terminal parameters: %m");
570 void break_sequence(void)
577 void terminate(int status)
580 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
582 * Allow the last of the report string to be gathered before we terminate.
584 if (report_gathering) {
587 rep_len = strlen(report_buffer);
588 while (rep_len + 1 <= sizeof(report_buffer)) {
592 if (c < 0 || iscntrl(c))
594 report_buffer[rep_len] = c;
597 report_buffer[rep_len] = 0;
598 fprintf (report_fp, "chat: %s\n", report_buffer);
601 fprintf (report_fp, "Closing \"%s\".\n", report_file);
603 report_fp = (FILE *) NULL;
606 #if defined(get_term_param)
607 if (have_tty_parameters) {
608 if (set_term_param (&saved_tty_parameters) < 0)
609 fatal(2, "Can't restore terminal parameters: %m");
617 * 'Clean up' this string.
619 /* sending: set to 1 when sending (putting) this string. */
620 char *clean(register char *s, int sending)
622 char temp[STR_LEN], cur_chr;
623 register char *s1, *phchar;
624 int add_return = sending;
625 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
628 /* Don't overflow buffer, leave room for chars we append later */
629 while (*s && s1 - temp < sizeof(temp) - 2 - add_return) {
631 if (cur_chr == '^') {
633 if (cur_chr == '\0') {
644 if (cur_chr != '\\') {
650 if (cur_chr == '\0') {
664 if (sending && *s == '\0')
681 if (sending && phone_num) {
682 for ( phchar = phone_num; *phchar != '\0'; phchar++)
692 if (sending && phone_num2) {
693 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
732 if (isoctal (cur_chr)) {
736 cur_chr |= *s++ - '0';
739 cur_chr |= *s++ - '0';
743 if (cur_chr != 0 || sending) {
744 if (sending && (cur_chr == '\\' || cur_chr == 0))
761 *s1++ = '\0'; /* guarantee closure */
762 *s1++ = '\0'; /* terminate the string */
763 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
767 * A modified version of 'strtok'. This version skips \ sequences.
770 char *expect_strtok (char *s, char *term)
772 static char *str = "";
777 * If a string was specified then do initial processing.
783 * If this is the escape flag then reset it and ignore the character.
804 * If this is not in the termination string, continue.
806 if (strchr (term, *str) == (char *) 0) {
812 * This is the terminator. Mark the end of the string and stop.
821 * Process the expect string
824 void chat_expect (char *s)
829 if (strcmp(s, "HANGUP") == 0) {
834 if (strcmp(s, "ABORT") == 0) {
839 if (strcmp(s, "CLR_ABORT") == 0) {
844 if (strcmp(s, "REPORT") == 0) {
849 if (strcmp(s, "CLR_REPORT") == 0) {
854 if (strcmp(s, "TIMEOUT") == 0) {
859 if (strcmp(s, "ECHO") == 0) {
864 if (strcmp(s, "SAY") == 0) {
870 * Fetch the expect and reply string.
873 expect = expect_strtok (s, "-");
876 if (expect == (char *) 0)
879 reply = expect_strtok (s, "-");
882 * Handle the expect string. If successful then exit.
884 if (get_string (expect))
888 * If there is a sub-reply string then send it. Otherwise any condition
891 if (reply == (char *) 0 || exit_code != 3)
898 * The expectation did not occur. This is terminal.
901 logf("Failed (%s)", fail_reason);
904 terminate(exit_code);
908 * Translate the input character to the appropriate string for printing
912 char *character(int c)
914 static char string[10];
917 meta = (c & 0x80) ? "M-" : "";
921 sprintf(string, "%s^%c", meta, (int)c + '@');
923 sprintf(string, "%s^?", meta);
925 sprintf(string, "%s%c", meta, c);
931 * process the reply string
933 void chat_send (register char *s)
938 write(2, s, strlen(s));
945 if (strcmp(s, "OFF") == 0)
946 signal(SIGHUP, SIG_IGN);
948 signal(SIGHUP, sighup);
954 echo = (strcmp(s, "ON") == 0);
963 if (n_aborts >= MAX_ABORTS)
964 fatal(2, "Too many ABORT strings");
968 if (strlen(s1) > strlen(s)
969 || strlen(s1) + 1 > sizeof(fail_buffer))
970 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
972 abort_string[n_aborts++] = s1;
975 logf("abort on (%v)", s);
979 if (clear_abort_next) {
985 clear_abort_next = 0;
989 if (strlen(s1) > strlen(s)
990 || strlen(s1) + 1 > sizeof(fail_buffer))
991 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
994 for (i=0; i < n_aborts; i++) {
995 if ( strcmp(s1,abort_string[i]) == 0 ) {
996 free(abort_string[i]);
997 abort_string[i] = NULL;
1001 logf("clear abort on (%v)", s);
1006 pack_array(abort_string,old_max);
1014 if (n_reports >= MAX_REPORTS)
1015 fatal(2, "Too many REPORT strings");
1019 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1020 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1022 report_string[n_reports++] = s1;
1025 logf("report (%v)", s);
1029 if (clear_report_next) {
1035 clear_report_next = 0;
1039 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1040 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1042 old_max = n_reports;
1043 for (i=0; i < n_reports; i++) {
1044 if ( strcmp(s1,report_string[i]) == 0 ) {
1045 free(report_string[i]);
1046 report_string[i] = NULL;
1050 logf("clear report (%v)", s);
1055 pack_array(report_string,old_max);
1065 timeout = DEFAULT_CHAT_TIMEOUT;
1068 logf("timeout set to %d seconds", timeout);
1073 if (strcmp(s, "EOT") == 0)
1075 else if (strcmp(s, "BREAK") == 0)
1087 status = read(0, &c, 1);
1091 return ((int)c & 0x7F);
1094 logf("warning: read() on stdin returned %d", status);
1097 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1098 fatal(2, "Can't get file mode flags on stdin: %m");
1100 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1101 fatal(2, "Can't set file mode flags on stdin: %m");
1112 usleep(10000); /* inter-character typing delay (?) */
1114 status = write(1, &ch, 1);
1121 logf("warning: write() on stdout returned %d", status);
1124 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1125 fatal(2, "Can't get file mode flags on stdin, %m");
1127 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1128 fatal(2, "Can't set file mode flags on stdin: %m");
1134 int write_char (int c)
1136 if (alarmed || put_char(c) < 0) {
1141 if (errno == EINTR || errno == EWOULDBLOCK)
1142 logf(" -- write timed out");
1144 logf(" -- write failed: %m");
1151 int put_string (register char *s)
1158 logf("send (??????)");
1160 logf("send (%v)", s);
1163 alarm(timeout); alarmed = 0;
1166 register char c = *s++;
1169 if (!write_char (c))
1185 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1189 if (!write_char (c))
1201 * Echo a character to stderr.
1202 * When called with -1, a '\n' character is generated when
1203 * the cursor is not at the beginning of a line.
1205 void echo_stderr(int n)
1211 case '\r': /* ignore '\r' */
1223 write(2, s, strlen(s));
1230 * 'Wait for' this string to appear on this file descriptor.
1232 int get_string(register char *string)
1235 int c, printed = 0, len, minlen;
1236 register char *s = temp, *end = s + STR_LEN;
1237 char *logged = temp;
1239 fail_reason = (char *)0;
1241 if (strlen(string) > STR_LEN) {
1242 logf("expect string is too long");
1247 string = clean(string, 0);
1248 len = strlen(string);
1249 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1252 logf("expect (%v)", string);
1263 while ( ! alarmed && (c = get_char()) >= 0) {
1264 int n, abort_len, report_len;
1268 if (verbose && c == '\n') {
1270 logf(""); /* blank line */
1272 logf("%0.*v", s - logged, logged);
1278 if (verbose && s >= logged + 80) {
1279 logf("%0.*v", s - logged, logged);
1285 fputc( '\n', stderr );
1287 fprintf( stderr, "%s", character(c) );
1290 if (!report_gathering) {
1291 for (n = 0; n < n_reports; ++n) {
1292 if ((report_string[n] != (char*) NULL) &&
1293 s - temp >= (report_len = strlen(report_string[n])) &&
1294 strncmp(s - report_len, report_string[n], report_len) == 0) {
1295 time_t time_now = time ((time_t*) NULL);
1296 struct tm* tm_now = localtime (&time_now);
1298 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1299 strcat (report_buffer, report_string[n]);
1301 report_string[n] = (char *) NULL;
1302 report_gathering = 1;
1309 int rep_len = strlen (report_buffer);
1310 report_buffer[rep_len] = c;
1311 report_buffer[rep_len + 1] = '\0';
1314 report_gathering = 0;
1315 fprintf (report_fp, "chat: %s\n", report_buffer);
1319 if (s - temp >= len &&
1320 c == string[len - 1] &&
1321 strncmp(s - len, string, len) == 0) {
1324 logf("%0.*v", s - logged, logged);
1325 logf(" -- got it\n");
1333 for (n = 0; n < n_aborts; ++n) {
1334 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1335 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1338 logf("%0.*v", s - logged, logged);
1345 strcpy(fail_reason = fail_buffer, abort_string[n]);
1351 if (logged < s - minlen) {
1352 logf("%0.*v", s - logged, logged);
1356 memmove(temp, s, minlen);
1357 logged = temp + (logged - s);
1361 if (alarmed && verbose)
1362 logf("warning: alarm synchronization problem");
1367 if (verbose && printed) {
1369 logf(" -- read timed out");
1371 logf(" -- read failed: %m");
1380 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1383 #include <sys/param.h>
1384 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1390 #include <sys/types.h>
1391 #include <sys/time.h>
1394 usleep -- support routine for 4.2BSD system call emulations
1395 last edit: 29-Oct-1984 D A Gwyn
1398 extern int select();
1400 /* usec: delay in microseconds */
1402 usleep( long usec ) /* returns 0 if ok, else -1 */
1404 static struct { /* `timeval' */
1405 long tv_sec; /* seconds */
1406 long tv_usec; /* microsecs */
1407 } delay; /* _select() timeout */
1409 delay.tv_sec = usec / 1000000L;
1410 delay.tv_usec = usec % 1000000L;
1412 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1417 pack_array (char **array, int end)
1421 for (i = 0; i < end; i++) {
1422 if (array[i] == NULL) {
1423 for (j = i+1; j < end; ++j)
1424 if (array[j] != NULL)
1425 array[i++] = array[j];
1426 for (; i < end; ++i)
1434 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1435 * also specify the length of the output buffer, and we handle the
1436 * %m (error message) format.
1437 * Doesn't do floating-point formats.
1438 * Returns the number of chars put into buf.
1440 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1443 vfmtmsg(char *buf, int buflen, const char *fmt, va_list args)
1446 int width, prec, fillch;
1447 int base, len, neg, quoted;
1448 unsigned long val = 0;
1453 static char hexchars[] = "0123456789abcdef";
1457 while (buflen > 0) {
1458 for (f = fmt; *f != '%' && *f != 0; ++f)
1464 memcpy(buf, fmt, len);
1479 width = va_arg(args, int);
1482 while (isdigit(c)) {
1483 width = width * 10 + c - '0';
1490 prec = va_arg(args, int);
1493 while (isdigit(c)) {
1494 prec = prec * 10 + c - '0';
1505 i = va_arg(args, int);
1514 val = va_arg(args, unsigned int);
1518 val = va_arg(args, unsigned int);
1522 val = (unsigned long) va_arg(args, void *);
1527 str = va_arg(args, char *);
1530 num[0] = va_arg(args, int);
1535 str = strerror(errno);
1537 case 'v': /* "visible" string */
1538 case 'q': /* quoted string */
1540 p = va_arg(args, unsigned char *);
1541 if (fillch == '0' && prec > 0) {
1544 n = strlen((char *)p);
1545 if (prec > 0 && prec < n)
1548 while (n > 0 && buflen > 0) {
1551 if (!quoted && c >= 0x80) {
1556 if (quoted && (c == '"' || c == '\\'))
1558 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1562 case '\t': OUTCHAR('t'); break;
1563 case '\n': OUTCHAR('n'); break;
1564 case '\b': OUTCHAR('b'); break;
1565 case '\f': OUTCHAR('f'); break;
1568 OUTCHAR(hexchars[c >> 4]);
1569 OUTCHAR(hexchars[c & 0xf]);
1586 --fmt; /* so %z outputs %z etc. */
1591 str = num + sizeof(num);
1593 while (str > num + neg) {
1594 *--str = hexchars[val % base];
1596 if (--prec <= 0 && val == 0)
1608 len = num + sizeof(num) - 1 - str;
1611 if (prec > 0 && len > prec)
1617 if ((n = width - len) > 0) {
1625 memcpy(buf, str, len);