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.
84 static const char rcsid[] = "$Id: chat.c,v 1.22 1999/08/12 03:56:05 paulus Exp $";
96 #include <sys/types.h>
127 #define __V(x) (va_alist) va_dcl
133 #define O_NONBLOCK O_NDELAY
138 extern char *sys_errlist[];
139 #define memmove(to, from, n) bcopy(from, to, n)
140 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
144 /*************** Micro getopt() *********************************************/
145 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
146 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
147 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
148 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
149 (_O=4,(char*)0):(char*)0)
150 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
151 #define ARG(c,v) (c?(--c,*v++):(char*)0)
153 static int _O = 0; /* Internal state */
154 /*************** Micro getopt() *********************************************/
158 #define MAX_ABORTS 50
159 #define MAX_REPORTS 50
160 #define DEFAULT_CHAT_TIMEOUT 45
170 FILE* report_fp = (FILE *) 0;
171 char *report_file = (char *) 0;
172 char *chat_file = (char *) 0;
173 char *phone_num = (char *) 0;
174 char *phone_num2 = (char *) 0;
175 int timeout = DEFAULT_CHAT_TIMEOUT;
177 int have_tty_parameters = 0;
180 #define term_parms struct termio
181 #define get_term_param(param) ioctl(0, TCGETA, param)
182 #define set_term_param(param) ioctl(0, TCSETA, param)
183 struct termio saved_tty_parameters;
187 #define term_parms struct termios
188 #define get_term_param(param) tcgetattr(0, param)
189 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
190 struct termios saved_tty_parameters;
193 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
195 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
196 int clear_abort_next = 0;
198 char *report_string[MAX_REPORTS] ;
199 char report_buffer[50] ;
200 int n_reports = 0, report_next = 0, report_gathering = 0 ;
201 int clear_report_next = 0;
203 int say_next = 0, hup_next = 0;
205 void *dup_mem __P((void *b, size_t c));
206 void *copy_of __P((char *s));
207 void usage __P((void));
208 void logf __P((const char *fmt, ...));
209 void fatal __P((int code, const char *fmt, ...));
210 SIGTYPE sigalrm __P((int signo));
211 SIGTYPE sigint __P((int signo));
212 SIGTYPE sigterm __P((int signo));
213 SIGTYPE sighup __P((int signo));
214 void unalarm __P((void));
215 void init __P((void));
216 void set_tty_parameters __P((void));
217 void echo_stderr __P((int));
218 void break_sequence __P((void));
219 void terminate __P((int status));
220 void do_file __P((char *chat_file));
221 int get_string __P((register char *string));
222 int put_string __P((register char *s));
223 int write_char __P((int c));
224 int put_char __P((int c));
225 int get_char __P((void));
226 void chat_send __P((register char *s));
227 char *character __P((int c));
228 void chat_expect __P((register char *s));
229 char *clean __P((register char *s, int sending));
230 void break_sequence __P((void));
231 void terminate __P((int status));
232 void pack_array __P((char **array, int end));
233 char *expect_strtok __P((char *, char *));
234 int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
236 int main __P((int, char *[]));
242 void *ans = malloc (c);
244 fatal(2, "memory error!");
253 return dup_mem (s, strlen (s) + 1);
257 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
258 * [ -r report-file ] \
259 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
261 * Perform a UUCP-dialer-like chat script on stdin and stdout.
271 program_name = *argv;
274 while ((option = OPTION(argc, argv)) != 0) {
297 if ((arg = OPTARG(argc, argv)) != NULL)
298 chat_file = copy_of(arg);
304 if ((arg = OPTARG(argc, argv)) != NULL)
311 arg = OPTARG (argc, argv);
313 if (report_fp != NULL)
315 report_file = copy_of (arg);
316 report_fp = fopen (report_file, "a");
317 if (report_fp != NULL) {
319 fprintf (report_fp, "Opening \"%s\"...\n",
327 if ((arg = OPTARG(argc, argv)) != NULL)
328 phone_num = copy_of(arg);
334 if ((arg = OPTARG(argc, argv)) != NULL)
335 phone_num2 = copy_of(arg);
346 * Default the report file to the stderr location
348 if (report_fp == NULL)
353 openlog("chat", LOG_PID);
355 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
358 setlogmask(LOG_UPTO(LOG_INFO));
360 setlogmask(LOG_UPTO(LOG_WARNING));
366 if (chat_file != NULL) {
367 arg = ARG(argc, argv);
373 while ((arg = ARG(argc, argv)) != NULL) {
376 if ((arg = ARG(argc, argv)) != NULL)
386 * Process a chat script when read from a file.
389 void do_file (chat_file)
393 char *sp, *arg, quote;
397 cfp = fopen (chat_file, "r");
399 fatal(1, "%s -- open failed: %m", chat_file);
404 while (fgets(buf, STR_LEN, cfp) != NULL) {
405 sp = strchr (buf, '\n');
412 /* lines starting with '#' are comments. If a real '#'
413 is to be expected, it should be quoted .... */
417 while (*sp != '\0') {
418 if (*sp == ' ' || *sp == '\t') {
423 if (*sp == '"' || *sp == '\'') {
426 while (*sp != quote) {
428 fatal(1, "unterminated quote (line %d)", linect);
438 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
456 * We got an error parsing the command line.
461 Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
462 [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
469 * Send a message to syslog and/or stderr.
471 void logf __V((const char *fmt, ...))
480 fmt = va_arg(args, char *);
483 vfmtmsg(line, sizeof(line), fmt, args);
485 syslog(LOG_INFO, "%s", line);
487 fprintf(stderr, "%s\n", line);
491 * Print an error message and terminate.
494 void fatal __V((int code, const char *fmt, ...))
504 code = va_arg(args, int);
505 fmt = va_arg(args, char *);
508 vfmtmsg(line, sizeof(line), fmt, args);
510 syslog(LOG_ERR, "%s", line);
512 fprintf(stderr, "%s\n", line);
518 SIGTYPE sigalrm(signo)
524 alarmed = 1; /* Reset alarm to avoid race window */
525 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
527 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
528 fatal(2, "Can't get file mode flags on stdin: %m");
530 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
531 fatal(2, "Can't set file mode flags on stdin: %m");
541 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
542 fatal(2, "Can't get file mode flags on stdin: %m");
544 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
545 fatal(2, "Can't set file mode flags on stdin: %m");
548 SIGTYPE sigint(signo)
554 SIGTYPE sigterm(signo)
560 SIGTYPE sighup(signo)
568 signal(SIGINT, sigint);
569 signal(SIGTERM, sigterm);
570 signal(SIGHUP, sighup);
572 set_tty_parameters();
573 signal(SIGALRM, sigalrm);
578 void set_tty_parameters()
580 #if defined(get_term_param)
583 if (get_term_param (&t) < 0)
584 fatal(2, "Can't get terminal parameters: %m");
586 saved_tty_parameters = t;
587 have_tty_parameters = 1;
589 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
597 if (set_term_param (&t) < 0)
598 fatal(2, "Can't set terminal parameters: %m");
602 void break_sequence()
609 void terminate(status)
612 static int terminating = 0;
618 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
620 * Allow the last of the report string to be gathered before we terminate.
622 if (report_gathering) {
625 rep_len = strlen(report_buffer);
626 while (rep_len + 1 <= sizeof(report_buffer)) {
630 if (c < 0 || iscntrl(c))
632 report_buffer[rep_len] = c;
635 report_buffer[rep_len] = 0;
636 fprintf (report_fp, "chat: %s\n", report_buffer);
639 fprintf (report_fp, "Closing \"%s\".\n", report_file);
641 report_fp = (FILE *) NULL;
644 #if defined(get_term_param)
645 if (have_tty_parameters) {
646 if (set_term_param (&saved_tty_parameters) < 0)
647 fatal(2, "Can't restore terminal parameters: %m");
655 * 'Clean up' this string.
657 char *clean(s, sending)
659 int sending; /* set to 1 when sending (putting) this string. */
661 char temp[STR_LEN], cur_chr;
662 register char *s1, *phchar;
663 int add_return = sending;
664 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
669 if (cur_chr == '^') {
671 if (cur_chr == '\0') {
682 if (cur_chr != '\\') {
688 if (cur_chr == '\0') {
702 if (sending && *s == '\0')
719 if (sending && phone_num) {
720 for ( phchar = phone_num; *phchar != '\0'; phchar++)
730 if (sending && phone_num2) {
731 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
770 if (isoctal (cur_chr)) {
774 cur_chr |= *s++ - '0';
777 cur_chr |= *s++ - '0';
781 if (cur_chr != 0 || sending) {
782 if (sending && (cur_chr == '\\' || cur_chr == 0))
799 *s1++ = '\0'; /* guarantee closure */
800 *s1++ = '\0'; /* terminate the string */
801 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
805 * A modified version of 'strtok'. This version skips \ sequences.
808 char *expect_strtok (s, term)
811 static char *str = "";
816 * If a string was specified then do initial processing.
822 * If this is the escape flag then reset it and ignore the character.
843 * If this is not in the termination string, continue.
845 if (strchr (term, *str) == (char *) 0) {
851 * This is the terminator. Mark the end of the string and stop.
860 * Process the expect string
869 if (strcmp(s, "HANGUP") == 0) {
874 if (strcmp(s, "ABORT") == 0) {
879 if (strcmp(s, "CLR_ABORT") == 0) {
884 if (strcmp(s, "REPORT") == 0) {
889 if (strcmp(s, "CLR_REPORT") == 0) {
894 if (strcmp(s, "TIMEOUT") == 0) {
899 if (strcmp(s, "ECHO") == 0) {
904 if (strcmp(s, "SAY") == 0) {
910 * Fetch the expect and reply string.
913 expect = expect_strtok (s, "-");
916 if (expect == (char *) 0)
919 reply = expect_strtok (s, "-");
922 * Handle the expect string. If successful then exit.
924 if (get_string (expect))
928 * If there is a sub-reply string then send it. Otherwise any condition
931 if (reply == (char *) 0 || exit_code != 3)
938 * The expectation did not occur. This is terminal.
941 logf("Failed (%s)", fail_reason);
944 terminate(exit_code);
948 * Translate the input character to the appropriate string for printing
955 static char string[10];
958 meta = (c & 0x80) ? "M-" : "";
962 sprintf(string, "%s^%c", meta, (int)c + '@');
964 sprintf(string, "%s^?", meta);
966 sprintf(string, "%s%c", meta, c);
972 * process the reply string
977 char file_data[STR_LEN];
982 write(2, s, strlen(s));
989 if (strcmp(s, "OFF") == 0)
990 signal(SIGHUP, SIG_IGN);
992 signal(SIGHUP, sighup);
998 echo = (strcmp(s, "ON") == 0);
1007 if (n_aborts >= MAX_ABORTS)
1008 fatal(2, "Too many ABORT strings");
1012 if (strlen(s1) > strlen(s)
1013 || strlen(s1) + 1 > sizeof(fail_buffer))
1014 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
1016 abort_string[n_aborts++] = s1;
1019 logf("abort on (%v)", s);
1023 if (clear_abort_next) {
1029 clear_abort_next = 0;
1033 if (strlen(s1) > strlen(s)
1034 || strlen(s1) + 1 > sizeof(fail_buffer))
1035 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1038 for (i=0; i < n_aborts; i++) {
1039 if ( strcmp(s1,abort_string[i]) == 0 ) {
1040 free(abort_string[i]);
1041 abort_string[i] = NULL;
1045 logf("clear abort on (%v)", s);
1050 pack_array(abort_string,old_max);
1058 if (n_reports >= MAX_REPORTS)
1059 fatal(2, "Too many REPORT strings");
1063 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1064 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1066 report_string[n_reports++] = s1;
1069 logf("report (%v)", s);
1073 if (clear_report_next) {
1079 clear_report_next = 0;
1083 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1084 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1086 old_max = n_reports;
1087 for (i=0; i < n_reports; i++) {
1088 if ( strcmp(s1,report_string[i]) == 0 ) {
1089 free(report_string[i]);
1090 report_string[i] = NULL;
1094 logf("clear report (%v)", s);
1099 pack_array(report_string,old_max);
1109 timeout = DEFAULT_CHAT_TIMEOUT;
1112 logf("timeout set to %d seconds", timeout);
1118 * The syntax @filename means read the string to send from the
1122 /* skip the @ and any following white-space */
1124 while (*++fn == ' ' || *fn == '\t')
1131 /* open the file and read until STR_LEN-1 bytes or end-of-file */
1134 fatal(1, "%s -- open failed: %m", fn);
1135 while (n < STR_LEN - 1) {
1136 int nr = fread(&file_data[n], 1, STR_LEN - 1 - n, f);
1138 fatal(1, "%s -- read error", fn);
1145 /* use the string we got as the string to send,
1146 but trim off the final newline if any. */
1147 if (n > 0 && file_data[n-1] == '\n')
1154 if (strcmp(s, "EOT") == 0)
1156 else if (strcmp(s, "BREAK") == 0)
1168 status = read(0, &c, 1);
1172 return ((int)c & 0x7F);
1175 logf("warning: read() on stdin returned %d", status);
1178 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1179 fatal(2, "Can't get file mode flags on stdin: %m");
1181 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1182 fatal(2, "Can't set file mode flags on stdin: %m");
1194 usleep(10000); /* inter-character typing delay (?) */
1196 status = write(1, &ch, 1);
1203 logf("warning: write() on stdout returned %d", status);
1206 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1207 fatal(2, "Can't get file mode flags on stdin, %m");
1209 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1210 fatal(2, "Can't set file mode flags on stdin: %m");
1219 if (alarmed || put_char(c) < 0) {
1224 if (errno == EINTR || errno == EWOULDBLOCK)
1225 logf(" -- write timed out");
1227 logf(" -- write failed: %m");
1242 logf("send (??????)");
1244 logf("send (%v)", s);
1247 alarm(timeout); alarmed = 0;
1250 register char c = *s++;
1253 if (!write_char (c))
1269 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1273 if (!write_char (c))
1285 * Echo a character to stderr.
1286 * When called with -1, a '\n' character is generated when
1287 * the cursor is not at the beginning of a line.
1296 case '\r': /* ignore '\r' */
1308 write(2, s, strlen(s));
1315 * 'Wait for' this string to appear on this file descriptor.
1317 int get_string(string)
1318 register char *string;
1321 int c, printed = 0, len, minlen;
1322 register char *s = temp, *end = s + STR_LEN;
1323 char *logged = temp;
1325 fail_reason = (char *)0;
1326 string = clean(string, 0);
1327 len = strlen(string);
1328 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1331 logf("expect (%v)", string);
1333 if (len > STR_LEN) {
1334 logf("expect string is too long");
1348 while ( ! alarmed && (c = get_char()) >= 0) {
1349 int n, abort_len, report_len;
1353 if (verbose && c == '\n') {
1355 logf(""); /* blank line */
1357 logf("%0.*v", s - logged, logged);
1363 if (verbose && s >= logged + 80) {
1364 logf("%0.*v", s - logged, logged);
1370 fputc( '\n', stderr );
1372 fprintf( stderr, "%s", character(c) );
1375 if (!report_gathering) {
1376 for (n = 0; n < n_reports; ++n) {
1377 if ((report_string[n] != (char*) NULL) &&
1378 s - temp >= (report_len = strlen(report_string[n])) &&
1379 strncmp(s - report_len, report_string[n], report_len) == 0) {
1380 time_t time_now = time ((time_t*) NULL);
1381 struct tm* tm_now = localtime (&time_now);
1383 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1384 strcat (report_buffer, report_string[n]);
1386 report_string[n] = (char *) NULL;
1387 report_gathering = 1;
1394 int rep_len = strlen (report_buffer);
1395 report_buffer[rep_len] = c;
1396 report_buffer[rep_len + 1] = '\0';
1399 report_gathering = 0;
1400 fprintf (report_fp, "chat: %s\n", report_buffer);
1404 if (s - temp >= len &&
1405 c == string[len - 1] &&
1406 strncmp(s - len, string, len) == 0) {
1409 logf("%0.*v", s - logged, logged);
1410 logf(" -- got it\n");
1418 for (n = 0; n < n_aborts; ++n) {
1419 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1420 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1423 logf("%0.*v", s - logged, logged);
1430 strcpy(fail_reason = fail_buffer, abort_string[n]);
1436 if (logged < s - minlen) {
1437 logf("%0.*v", s - logged, logged);
1441 memmove(temp, s, minlen);
1442 logged = temp + (logged - s);
1446 if (alarmed && verbose)
1447 logf("warning: alarm synchronization problem");
1452 if (verbose && printed) {
1454 logf(" -- read timed out");
1456 logf(" -- read failed: %m");
1465 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1468 #include <sys/param.h>
1469 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1475 #include <sys/types.h>
1476 #include <sys/time.h>
1479 usleep -- support routine for 4.2BSD system call emulations
1480 last edit: 29-Oct-1984 D A Gwyn
1483 extern int select();
1486 usleep( usec ) /* returns 0 if ok, else -1 */
1487 long usec; /* delay in microseconds */
1489 static struct { /* `timeval' */
1490 long tv_sec; /* seconds */
1491 long tv_usec; /* microsecs */
1492 } delay; /* _select() timeout */
1494 delay.tv_sec = usec / 1000000L;
1495 delay.tv_usec = usec % 1000000L;
1497 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1502 pack_array (array, end)
1503 char **array; /* The address of the array of string pointers */
1504 int end; /* The index of the next free entry before CLR_ */
1508 for (i = 0; i < end; i++) {
1509 if (array[i] == NULL) {
1510 for (j = i+1; j < end; ++j)
1511 if (array[j] != NULL)
1512 array[i++] = array[j];
1513 for (; i < end; ++i)
1521 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1522 * also specify the length of the output buffer, and we handle the
1523 * %m (error message) format.
1524 * Doesn't do floating-point formats.
1525 * Returns the number of chars put into buf.
1527 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1530 vfmtmsg(buf, buflen, fmt, args)
1537 int width, prec, fillch;
1538 int base, len, neg, quoted;
1539 unsigned long val = 0;
1544 static char hexchars[] = "0123456789abcdef";
1548 while (buflen > 0) {
1549 for (f = fmt; *f != '%' && *f != 0; ++f)
1555 memcpy(buf, fmt, len);
1570 width = va_arg(args, int);
1573 while (isdigit(c)) {
1574 width = width * 10 + c - '0';
1581 prec = va_arg(args, int);
1584 while (isdigit(c)) {
1585 prec = prec * 10 + c - '0';
1596 i = va_arg(args, int);
1605 val = va_arg(args, unsigned int);
1609 val = va_arg(args, unsigned int);
1613 val = (unsigned long) va_arg(args, void *);
1618 str = va_arg(args, char *);
1621 num[0] = va_arg(args, int);
1626 str = strerror(errno);
1628 case 'v': /* "visible" string */
1629 case 'q': /* quoted string */
1631 p = va_arg(args, unsigned char *);
1632 if (fillch == '0' && prec > 0) {
1635 n = strlen((char *)p);
1636 if (prec > 0 && prec < n)
1639 while (n > 0 && buflen > 0) {
1642 if (!quoted && c >= 0x80) {
1647 if (quoted && (c == '"' || c == '\\'))
1649 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1653 case '\t': OUTCHAR('t'); break;
1654 case '\n': OUTCHAR('n'); break;
1655 case '\b': OUTCHAR('b'); break;
1656 case '\f': OUTCHAR('f'); break;
1659 OUTCHAR(hexchars[c >> 4]);
1660 OUTCHAR(hexchars[c & 0xf]);
1677 --fmt; /* so %z outputs %z etc. */
1682 str = num + sizeof(num);
1684 while (str > num + neg) {
1685 *--str = hexchars[val % base];
1687 if (--prec <= 0 && val == 0)
1699 len = num + sizeof(num) - 1 - str;
1702 if (prec > 0 && len > prec)
1708 if ((n = width - len) > 0) {
1716 memcpy(buf, str, len);