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.
81 static char rcsid[] = "$Id: chat.c,v 1.16 1997/11/27 06:00:06 paulus Exp $";
93 #include <sys/types.h>
114 #define __V(x) (va_alist) va_dcl
133 #define __V(x) (va_alist) va_dcl
139 #define O_NONBLOCK O_NDELAY
142 /*************** Micro getopt() *********************************************/
143 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
144 ((--c,++v),_O=4,c)&&**v=='-'&&v[0][1]?*++*v=='-'\
145 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
146 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
147 (_O=4,(char*)0):(char*)0)
148 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
149 #define ARG(c,v) (c?(--c,*v++):(char*)0)
151 static int _O = 0; /* Internal state */
152 /*************** Micro getopt() *********************************************/
156 #define MAX_ABORTS 50
157 #define MAX_REPORTS 50
158 #define DEFAULT_CHAT_TIMEOUT 45
168 FILE* report_fp = (FILE *) 0;
169 char *report_file = (char *) 0;
170 char *chat_file = (char *) 0;
171 char *phone_num = (char *) 0;
172 char *phone_num2 = (char *) 0;
173 int timeout = DEFAULT_CHAT_TIMEOUT;
175 int have_tty_parameters = 0;
178 #define term_parms struct termio
179 #define get_term_param(param) ioctl(0, TCGETA, param)
180 #define set_term_param(param) ioctl(0, TCSETA, param)
181 struct termio saved_tty_parameters;
185 #define term_parms struct termios
186 #define get_term_param(param) tcgetattr(0, param)
187 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
188 struct termios saved_tty_parameters;
191 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
193 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
194 int clear_abort_next = 0;
196 char *report_string[MAX_REPORTS] ;
197 char report_buffer[50] ;
198 int n_reports = 0, report_next = 0, report_gathering = 0 ;
199 int clear_report_next = 0;
201 int say_next = 0, hup_next = 0;
203 void *dup_mem __P((void *b, size_t c));
204 void *copy_of __P((char *s));
205 void usage __P((void));
206 void logf __P((const char *fmt, ...));
207 void fatal __P((int code, const char *fmt, ...));
208 SIGTYPE sigalrm __P((int signo));
209 SIGTYPE sigint __P((int signo));
210 SIGTYPE sigterm __P((int signo));
211 SIGTYPE sighup __P((int signo));
212 void unalarm __P((void));
213 void init __P((void));
214 void set_tty_parameters __P((void));
215 void echo_stderr __P((int));
216 void break_sequence __P((void));
217 void terminate __P((int status));
218 void do_file __P((char *chat_file));
219 int get_string __P((register char *string));
220 int put_string __P((register char *s));
221 int write_char __P((int c));
222 int put_char __P((int c));
223 int get_char __P((void));
224 void chat_send __P((register char *s));
225 char *character __P((int c));
226 void chat_expect __P((register char *s));
227 char *clean __P((register char *s, int sending));
228 void break_sequence __P((void));
229 void terminate __P((int status));
230 void pack_array __P((char **array, int end));
231 char *expect_strtok __P((char *, char *));
232 int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
234 int main __P((int, char *[]));
240 void *ans = malloc (c);
242 fatal(2, "memory error!");
251 return dup_mem (s, strlen (s) + 1);
255 * chat [ -v ] [-T number] [-U number] [ -t timeout ] [ -f chat-file ] \
256 * [ -r report-file ] \
257 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
259 * Perform a UUCP-dialer-like chat script on stdin and stdout.
269 program_name = *argv;
272 while ((option = OPTION(argc, argv)) != 0) {
295 if ((arg = OPTARG(argc, argv)) != NULL)
296 chat_file = copy_of(arg);
302 if ((arg = OPTARG(argc, argv)) != NULL)
309 arg = OPTARG (argc, argv);
311 if (report_fp != NULL)
313 report_file = copy_of (arg);
314 report_fp = fopen (report_file, "a");
315 if (report_fp != NULL) {
317 fprintf (report_fp, "Opening \"%s\"...\n",
325 if ((arg = OPTARG(argc, argv)) != NULL)
326 phone_num = copy_of(arg);
332 if ((arg = OPTARG(argc, argv)) != NULL)
333 phone_num2 = copy_of(arg);
344 * Default the report file to the stderr location
346 if (report_fp == NULL)
351 openlog("chat", LOG_PID);
353 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
356 setlogmask(LOG_UPTO(LOG_INFO));
358 setlogmask(LOG_UPTO(LOG_WARNING));
364 if (chat_file != NULL) {
365 arg = ARG(argc, argv);
371 while ((arg = ARG(argc, argv)) != NULL) {
374 if ((arg = ARG(argc, argv)) != NULL)
384 * Process a chat script when read from a file.
387 void do_file (chat_file)
391 char *sp, *arg, quote;
395 cfp = fopen (chat_file, "r");
397 fatal(1, "%s -- open failed: %m", chat_file);
402 while (fgets(buf, STR_LEN, cfp) != NULL) {
403 sp = strchr (buf, '\n');
410 /* lines starting with '#' are comments. If a real '#'
411 is to be expected, it should be quoted .... */
415 while (*sp != '\0') {
416 if (*sp == ' ' || *sp == '\t') {
421 if (*sp == '"' || *sp == '\'') {
424 while (*sp != quote) {
426 fatal(1, "unterminated quote (line %d)", linect);
436 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
454 * We got an error parsing the command line.
459 Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
460 [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
467 * Send a message to syslog and/or stderr.
469 void logf __V((const char *fmt, ...))
478 fmt = va_arg(args, char *);
481 vfmtmsg(line, sizeof(line), fmt, args);
483 syslog(LOG_INFO, "%s", line);
485 fprintf(stderr, "%s\n", line);
489 * Print an error message and terminate.
492 void fatal __V((int code, const char *fmt, ...))
502 code = va_arg(args, int);
503 fmt = va_arg(args, char *);
506 vfmtmsg(line, sizeof(line), fmt, args);
508 syslog(LOG_ERR, "%s", line);
510 fprintf(stderr, "%s\n", line);
516 SIGTYPE sigalrm(signo)
522 alarmed = 1; /* Reset alarm to avoid race window */
523 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
525 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
526 fatal(2, "Can't get file mode flags on stdin: %m");
528 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
529 fatal(2, "Can't set file mode flags on stdin: %m");
539 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
540 fatal(2, "Can't get file mode flags on stdin: %m");
542 if (fcntl(0, F_SETFL, flags & ~O_NONBLOCK) == -1)
543 fatal(2, "Can't set file mode flags on stdin: %m");
546 SIGTYPE sigint(signo)
552 SIGTYPE sigterm(signo)
558 SIGTYPE sighup(signo)
566 signal(SIGINT, sigint);
567 signal(SIGTERM, sigterm);
568 signal(SIGHUP, sighup);
570 set_tty_parameters();
571 signal(SIGALRM, sigalrm);
576 void set_tty_parameters()
578 #if defined(get_term_param)
581 if (get_term_param (&t) < 0)
582 fatal(2, "Can't get terminal parameters: %m");
584 saved_tty_parameters = t;
585 have_tty_parameters = 1;
587 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
595 if (set_term_param (&t) < 0)
596 fatal(2, "Can't set terminal parameters: %m");
600 void break_sequence()
607 void terminate(status)
611 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
613 * Allow the last of the report string to be gathered before we terminate.
615 if (report_gathering) {
618 rep_len = strlen(report_buffer);
619 while (rep_len + 1 <= sizeof(report_buffer)) {
623 if (c < 0 || iscntrl(c))
625 report_buffer[rep_len] = c;
628 report_buffer[rep_len] = 0;
629 fprintf (report_fp, "chat: %s\n", report_buffer);
632 fprintf (report_fp, "Closing \"%s\".\n", report_file);
634 report_fp = (FILE *) NULL;
637 #if defined(get_term_param)
638 if (have_tty_parameters) {
639 if (set_term_param (&saved_tty_parameters) < 0)
640 fatal(2, "Can't restore terminal parameters: %m");
648 * 'Clean up' this string.
650 char *clean(s, sending)
652 int sending; /* set to 1 when sending (putting) this string. */
654 char temp[STR_LEN], cur_chr;
655 register char *s1, *phchar;
656 int add_return = sending;
657 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
662 if (cur_chr == '^') {
664 if (cur_chr == '\0') {
675 if (cur_chr != '\\') {
681 if (cur_chr == '\0') {
695 if (sending && *s == '\0')
712 if (sending && phone_num) {
713 for ( phchar = phone_num; *phchar != '\0'; phchar++)
723 if (sending && phone_num2) {
724 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
763 if (isoctal (cur_chr)) {
767 cur_chr |= *s++ - '0';
770 cur_chr |= *s++ - '0';
774 if (cur_chr != 0 || sending) {
775 if (sending && (cur_chr == '\\' || cur_chr == 0))
792 *s1++ = '\0'; /* guarantee closure */
793 *s1++ = '\0'; /* terminate the string */
794 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
798 * A modified version of 'strtok'. This version skips \ sequences.
801 char *expect_strtok (s, term)
804 static char *str = "";
809 * If a string was specified then do initial processing.
815 * If this is the escape flag then reset it and ignore the character.
836 * If this is not in the termination string, continue.
838 if (strchr (term, *str) == (char *) 0) {
844 * This is the terminator. Mark the end of the string and stop.
853 * Process the expect string
862 if (strcmp(s, "HANGUP") == 0) {
867 if (strcmp(s, "ABORT") == 0) {
872 if (strcmp(s, "CLR_ABORT") == 0) {
877 if (strcmp(s, "REPORT") == 0) {
882 if (strcmp(s, "CLR_REPORT") == 0) {
887 if (strcmp(s, "TIMEOUT") == 0) {
892 if (strcmp(s, "ECHO") == 0) {
897 if (strcmp(s, "SAY") == 0) {
903 * Fetch the expect and reply string.
906 expect = expect_strtok (s, "-");
909 if (expect == (char *) 0)
912 reply = expect_strtok (s, "-");
915 * Handle the expect string. If successful then exit.
917 if (get_string (expect))
921 * If there is a sub-reply string then send it. Otherwise any condition
924 if (reply == (char *) 0 || exit_code != 3)
931 * The expectation did not occur. This is terminal.
934 logf("Failed (%s)", fail_reason);
937 terminate(exit_code);
941 * Translate the input character to the appropriate string for printing
948 static char string[10];
951 meta = (c & 0x80) ? "M-" : "";
955 sprintf(string, "%s^%c", meta, (int)c + '@');
957 sprintf(string, "%s^?", meta);
959 sprintf(string, "%s%c", meta, c);
965 * process the reply string
973 write(2, s, strlen(s));
980 if (strcmp(s, "OFF") == 0)
981 signal(SIGHUP, SIG_IGN);
983 signal(SIGHUP, sighup);
989 echo = (strcmp(s, "ON") == 0);
998 if (n_aborts >= MAX_ABORTS)
999 fatal(2, "Too many ABORT strings");
1003 if (strlen(s1) > strlen(s)
1004 || strlen(s1) + 1 > sizeof(fail_buffer))
1005 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
1007 abort_string[n_aborts++] = s1;
1010 logf("abort on (%v)", s);
1014 if (clear_abort_next) {
1021 clear_abort_next = 0;
1025 if (strlen(s1) > strlen(s)
1026 || strlen(s1) + 1 > sizeof(fail_buffer))
1027 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1030 for (i=0; i < n_aborts; i++) {
1031 if ( strcmp(s1,abort_string[i]) == 0 ) {
1032 free(abort_string[i]);
1033 abort_string[i] = NULL;
1037 logf("clear abort on (%v)", s);
1042 pack_array(abort_string,old_max);
1050 if (n_reports >= MAX_REPORTS)
1051 fatal(2, "Too many REPORT strings");
1055 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1056 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1058 report_string[n_reports++] = s1;
1061 logf("report (%v)", s);
1065 if (clear_report_next) {
1072 clear_report_next = 0;
1076 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1077 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1079 old_max = n_reports;
1080 for (i=0; i < n_reports; i++) {
1081 if ( strcmp(s1,report_string[i]) == 0 ) {
1082 free(report_string[i]);
1083 report_string[i] = NULL;
1087 logf("clear report (%v)", s);
1092 pack_array(report_string,old_max);
1102 timeout = DEFAULT_CHAT_TIMEOUT;
1105 logf("timeout set to %d seconds", timeout);
1110 if (strcmp(s, "EOT") == 0)
1112 else if (strcmp(s, "BREAK") == 0)
1124 status = read(0, &c, 1);
1128 return ((int)c & 0x7F);
1131 logf("warning: read() on stdin returned %d", status);
1134 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1135 fatal(2, "Can't get file mode flags on stdin: %m");
1137 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1138 fatal(2, "Can't set file mode flags on stdin: %m");
1150 usleep(10000); /* inter-character typing delay (?) */
1152 status = write(1, &ch, 1);
1159 logf("warning: write() on stdout returned %d", status);
1162 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1163 fatal(2, "Can't get file mode flags on stdin, %m");
1165 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1166 fatal(2, "Can't set file mode flags on stdin: %m");
1175 if (alarmed || put_char(c) < 0) {
1180 if (errno == EINTR || errno == EWOULDBLOCK)
1181 logf(" -- write timed out");
1183 logf(" -- write failed: %m");
1198 logf("send (??????)");
1200 logf("send (%v)", s);
1203 alarm(timeout); alarmed = 0;
1206 register char c = *s++;
1209 if (!write_char (c))
1225 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1229 if (!write_char (c))
1241 * Echo a character to stderr.
1242 * When called with -1, a '\n' character is generated when
1243 * the cursor is not at the beginning of a line.
1252 case '\r': /* ignore '\r' */
1264 write(2, s, strlen(s));
1271 * 'Wait for' this string to appear on this file descriptor.
1273 int get_string(string)
1274 register char *string;
1277 int c, printed = 0, len, minlen;
1278 register char *s = temp, *end = s + STR_LEN;
1279 char *logged = temp;
1281 fail_reason = (char *)0;
1282 string = clean(string, 0);
1283 len = strlen(string);
1284 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1287 logf("expect (%v)", string);
1289 if (len > STR_LEN) {
1290 logf("expect string is too long");
1304 while ( ! alarmed && (c = get_char()) >= 0) {
1305 int n, abort_len, report_len;
1309 if (verbose && c == '\n') {
1311 logf(""); /* blank line */
1313 logf("%0.*v", s - logged, logged);
1319 if (verbose && s >= logged + 80) {
1320 logf("%0.*v", s - logged, logged);
1326 fputc( '\n', stderr );
1328 fprintf( stderr, "%s", character(c) );
1331 if (!report_gathering) {
1332 for (n = 0; n < n_reports; ++n) {
1333 if ((report_string[n] != (char*) NULL) &&
1334 s - temp >= (report_len = strlen(report_string[n])) &&
1335 strncmp(s - report_len, report_string[n], report_len) == 0) {
1336 time_t time_now = time ((time_t*) NULL);
1337 struct tm* tm_now = localtime (&time_now);
1339 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1340 strcat (report_buffer, report_string[n]);
1342 report_string[n] = (char *) NULL;
1343 report_gathering = 1;
1350 int rep_len = strlen (report_buffer);
1351 report_buffer[rep_len] = c;
1352 report_buffer[rep_len + 1] = '\0';
1355 report_gathering = 0;
1356 fprintf (report_fp, "chat: %s\n", report_buffer);
1360 if (s - temp >= len &&
1361 c == string[len - 1] &&
1362 strncmp(s - len, string, len) == 0) {
1365 logf("%0.*v", s - logged, logged);
1366 logf(" -- got it\n");
1374 for (n = 0; n < n_aborts; ++n) {
1375 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1376 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1379 logf("%0.*v", s - logged, logged);
1386 strcpy(fail_reason = fail_buffer, abort_string[n]);
1392 if (logged < s - minlen) {
1393 logf("%0.*v", s - logged, logged);
1397 memmove(temp, s, minlen);
1398 logged = temp + (logged - s);
1402 if (alarmed && verbose)
1403 logf("warning: alarm synchronization problem");
1408 if (verbose && printed) {
1410 logf(" -- read timed out");
1412 logf(" -- read failed: %m");
1421 #include <sys/types.h>
1422 #include <sys/time.h>
1425 usleep -- support routine for 4.2BSD system call emulations
1426 last edit: 29-Oct-1984 D A Gwyn
1429 extern int select();
1432 usleep( usec ) /* returns 0 if ok, else -1 */
1433 long usec; /* delay in microseconds */
1435 static struct { /* `timeval' */
1436 long tv_sec; /* seconds */
1437 long tv_usec; /* microsecs */
1438 } delay; /* _select() timeout */
1440 delay.tv_sec = usec / 1000000L;
1441 delay.tv_usec = usec % 1000000L;
1443 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1448 pack_array (array, end)
1449 char **array; /* The address of the array of string pointers */
1450 int end; /* The index of the next free entry before CLR_ */
1454 for (i = 0; i < end; i++) {
1455 if (array[i] == NULL) {
1456 for (j = i+1; j < end; ++j)
1457 if (array[j] != NULL)
1458 array[i++] = array[j];
1459 for (; i < end; ++i)
1467 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1468 * also specify the length of the output buffer, and we handle the
1469 * %m (error message) format.
1470 * Doesn't do floating-point formats.
1471 * Returns the number of chars put into buf.
1473 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1476 vfmtmsg(buf, buflen, fmt, args)
1483 int width, prec, fillch;
1484 int base, len, neg, quoted;
1485 unsigned long val = 0;
1491 static char hexchars[] = "0123456789abcdef";
1495 while (buflen > 0) {
1496 for (f = fmt; *f != '%' && *f != 0; ++f)
1502 memcpy(buf, fmt, len);
1517 width = va_arg(args, int);
1520 while (isdigit(c)) {
1521 width = width * 10 + c - '0';
1528 prec = va_arg(args, int);
1531 while (isdigit(c)) {
1532 prec = prec * 10 + c - '0';
1543 i = va_arg(args, int);
1552 val = va_arg(args, unsigned int);
1556 val = va_arg(args, unsigned int);
1560 val = (unsigned long) va_arg(args, void *);
1565 str = va_arg(args, char *);
1568 num[0] = va_arg(args, int);
1573 str = strerror(errno);
1575 case 'v': /* "visible" string */
1576 case 'q': /* quoted string */
1578 p = va_arg(args, unsigned char *);
1579 if (fillch == '0' && prec > 0) {
1582 n = strlen((char *)p);
1583 if (prec > 0 && prec < n)
1586 while (n > 0 && buflen > 0) {
1589 if (!quoted && c >= 0x80) {
1594 if (quoted && (c == '"' || c == '\\'))
1596 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1600 case '\t': OUTCHAR('t'); break;
1601 case '\n': OUTCHAR('n'); break;
1602 case '\b': OUTCHAR('b'); break;
1603 case '\f': OUTCHAR('f'); break;
1606 OUTCHAR(hexchars[c >> 4]);
1607 OUTCHAR(hexchars[c & 0xf]);
1624 --fmt; /* so %z outputs %z etc. */
1629 str = num + sizeof(num);
1631 while (str > num + neg) {
1632 *--str = hexchars[val % base];
1634 if (--prec <= 0 && val == 0)
1646 len = num + sizeof(num) - 1 - str;
1649 if (prec > 0 && len > prec)
1655 if ((n = width - len) > 0) {
1663 memcpy(buf, str, len);