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 * 12-May-99 added a feature to read data to be sent from a file,
18 * if the send string starts with @. Idea from gpk <gpk@onramp.net>.
20 * added -T and -U option and \T and \U substitution to pass a phone
21 * number into chat script. Two are needed for some ISDN TA applications.
22 * Keith Dart <kdart@cisco.com>
25 * Added SAY keyword to send output to stderr.
26 * This allows to turn ECHO OFF and to output specific, user selected,
27 * text to give progress messages. This best works when stderr
28 * exists (i.e.: pppd in nodetach mode).
30 * Added HANGUP directives to allow for us to be called
31 * back. When HANGUP is set to NO, chat will not hangup at HUP signal.
32 * We rely on timeouts in that case.
34 * Added CLR_ABORT to clear previously set ABORT string. This has been
35 * dictated by the HANGUP above as "NO CARRIER" (for example) must be
36 * an ABORT condition until we know the other host is going to close
37 * the connection for call back. As soon as we have completed the
38 * first stage of the call back sequence, "NO CARRIER" is a valid, non
39 * fatal string. As soon as we got called back (probably get "CONNECT"),
40 * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
41 * Note that CLR_ABORT packs the abort_strings[] array so that we do not
42 * have unused entries not being reclaimed.
44 * In the same vein as above, added CLR_REPORT keyword.
46 * Allow for comments. Line starting with '#' are comments and are
47 * ignored. If a '#' is to be expected as the first character, the
48 * expect string must be quoted.
51 * Francis Demierre <Francis@SwissMail.Com>
52 * Thu May 15 17:15:40 MET DST 1997
55 * Added -r "report file" switch & REPORT keyword.
56 * Robert Geer <bgeer@xmission.com>
58 * Added -s "use stderr" and -S "don't use syslog" switches.
60 * Karl O. Pinc <kop@meme.com>
63 * Added -e "echo" switch & ECHO keyword
64 * Dick Streefland <dicks@tasking.nl>
67 * Considerable updates and modifications by
68 * Al Longyear <longyear@pobox.com>
69 * Paul Mackerras <paulus@cs.anu.edu.au>
72 * The original author is:
74 * Karl Fox <karl@MorningStar.Com>
75 * Morning Star Technologies, Inc.
87 static const char rcsid[] = "$Id: chat.c,v 1.24 1999/08/13 06:46:09 paulus Exp $";
99 #include <sys/types.h>
100 #include <sys/stat.h>
130 #define __V(x) (va_alist) va_dcl
136 #define O_NONBLOCK O_NDELAY
141 extern char *sys_errlist[];
142 #define memmove(to, from, n) bcopy(from, to, n)
143 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
147 /*************** Micro getopt() *********************************************/
148 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
149 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
150 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
151 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
152 (_O=4,(char*)0):(char*)0)
153 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
154 #define ARG(c,v) (c?(--c,*v++):(char*)0)
156 static int _O = 0; /* Internal state */
157 /*************** Micro getopt() *********************************************/
161 #define MAX_ABORTS 50
162 #define MAX_REPORTS 50
163 #define DEFAULT_CHAT_TIMEOUT 45
173 FILE* report_fp = (FILE *) 0;
174 char *report_file = (char *) 0;
175 char *chat_file = (char *) 0;
176 char *phone_num = (char *) 0;
177 char *phone_num2 = (char *) 0;
178 int timeout = DEFAULT_CHAT_TIMEOUT;
180 int have_tty_parameters = 0;
183 #define term_parms struct termio
184 #define get_term_param(param) ioctl(0, TCGETA, param)
185 #define set_term_param(param) ioctl(0, TCSETA, param)
186 struct termio saved_tty_parameters;
190 #define term_parms struct termios
191 #define get_term_param(param) tcgetattr(0, param)
192 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
193 struct termios saved_tty_parameters;
196 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
198 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
199 int clear_abort_next = 0;
201 char *report_string[MAX_REPORTS] ;
202 char report_buffer[50] ;
203 int n_reports = 0, report_next = 0, report_gathering = 0 ;
204 int clear_report_next = 0;
206 int say_next = 0, hup_next = 0;
208 void *dup_mem __P((void *b, size_t c));
209 void *copy_of __P((char *s));
210 void usage __P((void));
211 void logf __P((const char *fmt, ...));
212 void fatal __P((int code, const char *fmt, ...));
213 SIGTYPE sigalrm __P((int signo));
214 SIGTYPE sigint __P((int signo));
215 SIGTYPE sigterm __P((int signo));
216 SIGTYPE sighup __P((int signo));
217 void unalarm __P((void));
218 void init __P((void));
219 void set_tty_parameters __P((void));
220 void echo_stderr __P((int));
221 void break_sequence __P((void));
222 void terminate __P((int status));
223 void do_file __P((char *chat_file));
224 int get_string __P((register char *string));
225 int put_string __P((register char *s));
226 int write_char __P((int c));
227 int put_char __P((int c));
228 int get_char __P((void));
229 void chat_send __P((register char *s));
230 char *character __P((int c));
231 void chat_expect __P((register char *s));
232 char *clean __P((register char *s, int sending));
233 void break_sequence __P((void));
234 void terminate __P((int status));
235 void pack_array __P((char **array, int end));
236 char *expect_strtok __P((char *, char *));
237 int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
239 int main __P((int, char *[]));
245 void *ans = malloc (c);
247 fatal(2, "memory error!");
256 return dup_mem (s, strlen (s) + 1);
260 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
261 * [ -r report-file ] \
262 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
264 * Perform a UUCP-dialer-like chat script on stdin and stdout.
274 program_name = *argv;
277 while ((option = OPTION(argc, argv)) != 0) {
300 if ((arg = OPTARG(argc, argv)) != NULL)
301 chat_file = copy_of(arg);
307 if ((arg = OPTARG(argc, argv)) != NULL)
314 arg = OPTARG (argc, argv);
316 if (report_fp != NULL)
318 report_file = copy_of (arg);
319 report_fp = fopen (report_file, "a");
320 if (report_fp != NULL) {
322 fprintf (report_fp, "Opening \"%s\"...\n",
330 if ((arg = OPTARG(argc, argv)) != NULL)
331 phone_num = copy_of(arg);
337 if ((arg = OPTARG(argc, argv)) != NULL)
338 phone_num2 = copy_of(arg);
349 * Default the report file to the stderr location
351 if (report_fp == NULL)
356 openlog("chat", LOG_PID);
358 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
361 setlogmask(LOG_UPTO(LOG_INFO));
363 setlogmask(LOG_UPTO(LOG_WARNING));
369 if (chat_file != NULL) {
370 arg = ARG(argc, argv);
376 while ((arg = ARG(argc, argv)) != NULL) {
379 if ((arg = ARG(argc, argv)) != NULL)
389 * Process a chat script when read from a file.
392 void do_file (chat_file)
396 char *sp, *arg, quote;
400 cfp = fopen (chat_file, "r");
402 fatal(1, "%s -- open failed: %m", chat_file);
407 while (fgets(buf, STR_LEN, cfp) != NULL) {
408 sp = strchr (buf, '\n');
415 /* lines starting with '#' are comments. If a real '#'
416 is to be expected, it should be quoted .... */
420 while (*sp != '\0') {
421 if (*sp == ' ' || *sp == '\t') {
426 if (*sp == '"' || *sp == '\'') {
429 while (*sp != quote) {
431 fatal(1, "unterminated quote (line %d)", linect);
441 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
459 * We got an error parsing the command line.
464 Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
465 [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
472 * Send a message to syslog and/or stderr.
474 void logf __V((const char *fmt, ...))
483 fmt = va_arg(args, char *);
486 vfmtmsg(line, sizeof(line), fmt, args);
488 syslog(LOG_INFO, "%s", line);
490 fprintf(stderr, "%s\n", line);
494 * Print an error message and terminate.
497 void fatal __V((int code, const char *fmt, ...))
507 code = va_arg(args, int);
508 fmt = va_arg(args, char *);
511 vfmtmsg(line, sizeof(line), fmt, args);
513 syslog(LOG_ERR, "%s", line);
515 fprintf(stderr, "%s\n", line);
521 SIGTYPE sigalrm(signo)
527 alarmed = 1; /* Reset alarm to avoid race window */
528 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
530 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
531 fatal(2, "Can't get file mode flags on stdin: %m");
533 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
534 fatal(2, "Can't set file mode flags on stdin: %m");
544 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
545 fatal(2, "Can't get file mode flags on stdin: %m");
547 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
548 fatal(2, "Can't set file mode flags on stdin: %m");
551 SIGTYPE sigint(signo)
557 SIGTYPE sigterm(signo)
563 SIGTYPE sighup(signo)
571 signal(SIGINT, sigint);
572 signal(SIGTERM, sigterm);
573 signal(SIGHUP, sighup);
575 set_tty_parameters();
576 signal(SIGALRM, sigalrm);
581 void set_tty_parameters()
583 #if defined(get_term_param)
586 if (get_term_param (&t) < 0)
587 fatal(2, "Can't get terminal parameters: %m");
589 saved_tty_parameters = t;
590 have_tty_parameters = 1;
592 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
600 if (set_term_param (&t) < 0)
601 fatal(2, "Can't set terminal parameters: %m");
605 void break_sequence()
612 void terminate(status)
615 static int terminating = 0;
622 * Allow the last of the report string to be gathered before we terminate.
624 if (report_gathering) {
627 rep_len = strlen(report_buffer);
628 while (rep_len + 1 <= sizeof(report_buffer)) {
632 if (c < 0 || iscntrl(c))
634 report_buffer[rep_len] = c;
637 report_buffer[rep_len] = 0;
638 fprintf (report_fp, "chat: %s\n", report_buffer);
640 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
642 fprintf (report_fp, "Closing \"%s\".\n", report_file);
644 report_fp = (FILE *) NULL;
647 #if defined(get_term_param)
648 if (have_tty_parameters) {
649 if (set_term_param (&saved_tty_parameters) < 0)
650 fatal(2, "Can't restore terminal parameters: %m");
658 * 'Clean up' this string.
660 char *clean(s, sending)
662 int sending; /* set to 1 when sending (putting) this string. */
664 char temp[STR_LEN], cur_chr;
665 register char *s1, *phchar;
666 int add_return = sending;
667 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
672 if (cur_chr == '^') {
674 if (cur_chr == '\0') {
685 if (cur_chr != '\\') {
691 if (cur_chr == '\0') {
705 if (sending && *s == '\0')
722 if (sending && phone_num) {
723 for ( phchar = phone_num; *phchar != '\0'; phchar++)
733 if (sending && phone_num2) {
734 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
773 if (isoctal (cur_chr)) {
777 cur_chr |= *s++ - '0';
780 cur_chr |= *s++ - '0';
784 if (cur_chr != 0 || sending) {
785 if (sending && (cur_chr == '\\' || cur_chr == 0))
802 *s1++ = '\0'; /* guarantee closure */
803 *s1++ = '\0'; /* terminate the string */
804 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
808 * A modified version of 'strtok'. This version skips \ sequences.
811 char *expect_strtok (s, term)
814 static char *str = "";
819 * If a string was specified then do initial processing.
825 * If this is the escape flag then reset it and ignore the character.
846 * If this is not in the termination string, continue.
848 if (strchr (term, *str) == (char *) 0) {
854 * This is the terminator. Mark the end of the string and stop.
863 * Process the expect string
872 if (strcmp(s, "HANGUP") == 0) {
877 if (strcmp(s, "ABORT") == 0) {
882 if (strcmp(s, "CLR_ABORT") == 0) {
887 if (strcmp(s, "REPORT") == 0) {
892 if (strcmp(s, "CLR_REPORT") == 0) {
897 if (strcmp(s, "TIMEOUT") == 0) {
902 if (strcmp(s, "ECHO") == 0) {
907 if (strcmp(s, "SAY") == 0) {
913 * Fetch the expect and reply string.
916 expect = expect_strtok (s, "-");
919 if (expect == (char *) 0)
922 reply = expect_strtok (s, "-");
925 * Handle the expect string. If successful then exit.
927 if (get_string (expect))
931 * If there is a sub-reply string then send it. Otherwise any condition
934 if (reply == (char *) 0 || exit_code != 3)
941 * The expectation did not occur. This is terminal.
944 logf("Failed (%s)", fail_reason);
947 terminate(exit_code);
951 * Translate the input character to the appropriate string for printing
958 static char string[10];
961 meta = (c & 0x80) ? "M-" : "";
965 sprintf(string, "%s^%c", meta, (int)c + '@');
967 sprintf(string, "%s^?", meta);
969 sprintf(string, "%s%c", meta, c);
975 * process the reply string
980 char file_data[STR_LEN];
985 write(2, s, strlen(s));
992 if (strcmp(s, "OFF") == 0)
993 signal(SIGHUP, SIG_IGN);
995 signal(SIGHUP, sighup);
1001 echo = (strcmp(s, "ON") == 0);
1010 if (n_aborts >= MAX_ABORTS)
1011 fatal(2, "Too many ABORT strings");
1015 if (strlen(s1) > strlen(s)
1016 || strlen(s1) + 1 > sizeof(fail_buffer))
1017 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
1019 abort_string[n_aborts++] = s1;
1022 logf("abort on (%v)", s);
1026 if (clear_abort_next) {
1032 clear_abort_next = 0;
1036 if (strlen(s1) > strlen(s)
1037 || strlen(s1) + 1 > sizeof(fail_buffer))
1038 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1041 for (i=0; i < n_aborts; i++) {
1042 if ( strcmp(s1,abort_string[i]) == 0 ) {
1043 free(abort_string[i]);
1044 abort_string[i] = NULL;
1048 logf("clear abort on (%v)", s);
1053 pack_array(abort_string,old_max);
1061 if (n_reports >= MAX_REPORTS)
1062 fatal(2, "Too many REPORT strings");
1066 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1067 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1069 report_string[n_reports++] = s1;
1072 logf("report (%v)", s);
1076 if (clear_report_next) {
1082 clear_report_next = 0;
1086 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1087 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1089 old_max = n_reports;
1090 for (i=0; i < n_reports; i++) {
1091 if ( strcmp(s1,report_string[i]) == 0 ) {
1092 free(report_string[i]);
1093 report_string[i] = NULL;
1097 logf("clear report (%v)", s);
1102 pack_array(report_string,old_max);
1112 timeout = DEFAULT_CHAT_TIMEOUT;
1115 logf("timeout set to %d seconds", timeout);
1121 * The syntax @filename means read the string to send from the
1125 /* skip the @ and any following white-space */
1127 while (*++fn == ' ' || *fn == '\t')
1134 /* open the file and read until STR_LEN-1 bytes or end-of-file */
1137 fatal(1, "%s -- open failed: %m", fn);
1138 while (n < STR_LEN - 1) {
1139 int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f);
1141 fatal(1, "%s -- read error", fn);
1148 /* use the string we got as the string to send,
1149 but trim off the final newline if any. */
1150 if (n > 0 && file_data[n-1] == '\n')
1157 if (strcmp(s, "EOT") == 0)
1159 else if (strcmp(s, "BREAK") == 0)
1171 status = read(0, &c, 1);
1175 return ((int)c & 0x7F);
1178 logf("warning: read() on stdin returned %d", status);
1181 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1182 fatal(2, "Can't get file mode flags on stdin: %m");
1184 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1185 fatal(2, "Can't set file mode flags on stdin: %m");
1197 usleep(10000); /* inter-character typing delay (?) */
1199 status = write(1, &ch, 1);
1206 logf("warning: write() on stdout returned %d", status);
1209 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1210 fatal(2, "Can't get file mode flags on stdin, %m");
1212 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1213 fatal(2, "Can't set file mode flags on stdin: %m");
1222 if (alarmed || put_char(c) < 0) {
1227 if (errno == EINTR || errno == EWOULDBLOCK)
1228 logf(" -- write timed out");
1230 logf(" -- write failed: %m");
1245 logf("send (??????)");
1247 logf("send (%v)", s);
1250 alarm(timeout); alarmed = 0;
1253 register char c = *s++;
1256 if (!write_char (c))
1272 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1276 if (!write_char (c))
1288 * Echo a character to stderr.
1289 * When called with -1, a '\n' character is generated when
1290 * the cursor is not at the beginning of a line.
1299 case '\r': /* ignore '\r' */
1311 write(2, s, strlen(s));
1318 * 'Wait for' this string to appear on this file descriptor.
1320 int get_string(string)
1321 register char *string;
1324 int c, printed = 0, len, minlen;
1325 register char *s = temp, *end = s + STR_LEN;
1326 char *logged = temp;
1328 fail_reason = (char *)0;
1329 string = clean(string, 0);
1330 len = strlen(string);
1331 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1334 logf("expect (%v)", string);
1336 if (len > STR_LEN) {
1337 logf("expect string is too long");
1351 while ( ! alarmed && (c = get_char()) >= 0) {
1352 int n, abort_len, report_len;
1356 if (verbose && c == '\n') {
1358 logf(""); /* blank line */
1360 logf("%0.*v", s - logged, logged);
1366 if (verbose && s >= logged + 80) {
1367 logf("%0.*v", s - logged, logged);
1373 fputc( '\n', stderr );
1375 fprintf( stderr, "%s", character(c) );
1378 if (!report_gathering) {
1379 for (n = 0; n < n_reports; ++n) {
1380 if ((report_string[n] != (char*) NULL) &&
1381 s - temp >= (report_len = strlen(report_string[n])) &&
1382 strncmp(s - report_len, report_string[n], report_len) == 0) {
1383 time_t time_now = time ((time_t*) NULL);
1384 struct tm* tm_now = localtime (&time_now);
1386 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1387 strcat (report_buffer, report_string[n]);
1389 report_string[n] = (char *) NULL;
1390 report_gathering = 1;
1397 int rep_len = strlen (report_buffer);
1398 report_buffer[rep_len] = c;
1399 report_buffer[rep_len + 1] = '\0';
1402 report_gathering = 0;
1403 fprintf (report_fp, "chat: %s\n", report_buffer);
1407 if (s - temp >= len &&
1408 c == string[len - 1] &&
1409 strncmp(s - len, string, len) == 0) {
1412 logf("%0.*v", s - logged, logged);
1413 logf(" -- got it\n");
1421 for (n = 0; n < n_aborts; ++n) {
1422 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1423 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1426 logf("%0.*v", s - logged, logged);
1433 strcpy(fail_reason = fail_buffer, abort_string[n]);
1439 if (logged < s - minlen) {
1440 logf("%0.*v", s - logged, logged);
1444 memmove(temp, s, minlen);
1445 logged = temp + (logged - s);
1449 if (alarmed && verbose)
1450 logf("warning: alarm synchronization problem");
1455 if (verbose && printed) {
1457 logf(" -- read timed out");
1459 logf(" -- read failed: %m");
1468 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1471 #include <sys/param.h>
1472 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1478 #include <sys/types.h>
1479 #include <sys/time.h>
1482 usleep -- support routine for 4.2BSD system call emulations
1483 last edit: 29-Oct-1984 D A Gwyn
1486 extern int select();
1489 usleep( usec ) /* returns 0 if ok, else -1 */
1490 long usec; /* delay in microseconds */
1492 static struct { /* `timeval' */
1493 long tv_sec; /* seconds */
1494 long tv_usec; /* microsecs */
1495 } delay; /* _select() timeout */
1497 delay.tv_sec = usec / 1000000L;
1498 delay.tv_usec = usec % 1000000L;
1500 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1505 pack_array (array, end)
1506 char **array; /* The address of the array of string pointers */
1507 int end; /* The index of the next free entry before CLR_ */
1511 for (i = 0; i < end; i++) {
1512 if (array[i] == NULL) {
1513 for (j = i+1; j < end; ++j)
1514 if (array[j] != NULL)
1515 array[i++] = array[j];
1516 for (; i < end; ++i)
1524 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1525 * also specify the length of the output buffer, and we handle the
1526 * %m (error message) format.
1527 * Doesn't do floating-point formats.
1528 * Returns the number of chars put into buf.
1530 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1533 vfmtmsg(buf, buflen, fmt, args)
1540 int width, prec, fillch;
1541 int base, len, neg, quoted;
1542 unsigned long val = 0;
1547 static char hexchars[] = "0123456789abcdef";
1551 while (buflen > 0) {
1552 for (f = fmt; *f != '%' && *f != 0; ++f)
1558 memcpy(buf, fmt, len);
1573 width = va_arg(args, int);
1576 while (isdigit(c)) {
1577 width = width * 10 + c - '0';
1584 prec = va_arg(args, int);
1587 while (isdigit(c)) {
1588 prec = prec * 10 + c - '0';
1599 i = va_arg(args, int);
1608 val = va_arg(args, unsigned int);
1612 val = va_arg(args, unsigned int);
1616 val = (unsigned long) va_arg(args, void *);
1621 str = va_arg(args, char *);
1624 num[0] = va_arg(args, int);
1629 str = strerror(errno);
1631 case 'v': /* "visible" string */
1632 case 'q': /* quoted string */
1634 p = va_arg(args, unsigned char *);
1635 if (fillch == '0' && prec > 0) {
1638 n = strlen((char *)p);
1639 if (prec > 0 && prec < n)
1642 while (n > 0 && buflen > 0) {
1645 if (!quoted && c >= 0x80) {
1650 if (quoted && (c == '"' || c == '\\'))
1652 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1656 case '\t': OUTCHAR('t'); break;
1657 case '\n': OUTCHAR('n'); break;
1658 case '\b': OUTCHAR('b'); break;
1659 case '\f': OUTCHAR('f'); break;
1662 OUTCHAR(hexchars[c >> 4]);
1663 OUTCHAR(hexchars[c & 0xf]);
1680 --fmt; /* so %z outputs %z etc. */
1685 str = num + sizeof(num);
1687 while (str > num + neg) {
1688 *--str = hexchars[val % base];
1690 if (--prec <= 0 && val == 0)
1702 len = num + sizeof(num) - 1 - str;
1705 if (prec > 0 && len > prec)
1711 if ((n = width - len) > 0) {
1719 memcpy(buf, str, len);