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.17 1997/11/27 06:37:15 paulus Exp $";
93 #include <sys/types.h>
124 #define __V(x) (va_alist) va_dcl
130 #define O_NONBLOCK O_NDELAY
133 /*************** Micro getopt() *********************************************/
134 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
135 ((--c,++v),_O=4,c)&&**v=='-'&&v[0][1]?*++*v=='-'\
136 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
137 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
138 (_O=4,(char*)0):(char*)0)
139 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
140 #define ARG(c,v) (c?(--c,*v++):(char*)0)
142 static int _O = 0; /* Internal state */
143 /*************** Micro getopt() *********************************************/
147 #define MAX_ABORTS 50
148 #define MAX_REPORTS 50
149 #define DEFAULT_CHAT_TIMEOUT 45
159 FILE* report_fp = (FILE *) 0;
160 char *report_file = (char *) 0;
161 char *chat_file = (char *) 0;
162 char *phone_num = (char *) 0;
163 char *phone_num2 = (char *) 0;
164 int timeout = DEFAULT_CHAT_TIMEOUT;
166 int have_tty_parameters = 0;
169 #define term_parms struct termio
170 #define get_term_param(param) ioctl(0, TCGETA, param)
171 #define set_term_param(param) ioctl(0, TCSETA, param)
172 struct termio saved_tty_parameters;
176 #define term_parms struct termios
177 #define get_term_param(param) tcgetattr(0, param)
178 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
179 struct termios saved_tty_parameters;
182 char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
184 int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
185 int clear_abort_next = 0;
187 char *report_string[MAX_REPORTS] ;
188 char report_buffer[50] ;
189 int n_reports = 0, report_next = 0, report_gathering = 0 ;
190 int clear_report_next = 0;
192 int say_next = 0, hup_next = 0;
194 void *dup_mem __P((void *b, size_t c));
195 void *copy_of __P((char *s));
196 void usage __P((void));
197 void logf __P((const char *fmt, ...));
198 void fatal __P((int code, const char *fmt, ...));
199 SIGTYPE sigalrm __P((int signo));
200 SIGTYPE sigint __P((int signo));
201 SIGTYPE sigterm __P((int signo));
202 SIGTYPE sighup __P((int signo));
203 void unalarm __P((void));
204 void init __P((void));
205 void set_tty_parameters __P((void));
206 void echo_stderr __P((int));
207 void break_sequence __P((void));
208 void terminate __P((int status));
209 void do_file __P((char *chat_file));
210 int get_string __P((register char *string));
211 int put_string __P((register char *s));
212 int write_char __P((int c));
213 int put_char __P((int c));
214 int get_char __P((void));
215 void chat_send __P((register char *s));
216 char *character __P((int c));
217 void chat_expect __P((register char *s));
218 char *clean __P((register char *s, int sending));
219 void break_sequence __P((void));
220 void terminate __P((int status));
221 void pack_array __P((char **array, int end));
222 char *expect_strtok __P((char *, char *));
223 int vfmtmsg __P((char *, int, const char *, va_list)); /* vsprintf++ */
225 int main __P((int, char *[]));
231 void *ans = malloc (c);
233 fatal(2, "memory error!");
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.
260 program_name = *argv;
263 while ((option = OPTION(argc, argv)) != 0) {
286 if ((arg = OPTARG(argc, argv)) != NULL)
287 chat_file = copy_of(arg);
293 if ((arg = OPTARG(argc, argv)) != NULL)
300 arg = OPTARG (argc, argv);
302 if (report_fp != NULL)
304 report_file = copy_of (arg);
305 report_fp = fopen (report_file, "a");
306 if (report_fp != NULL) {
308 fprintf (report_fp, "Opening \"%s\"...\n",
316 if ((arg = OPTARG(argc, argv)) != NULL)
317 phone_num = copy_of(arg);
323 if ((arg = OPTARG(argc, argv)) != NULL)
324 phone_num2 = copy_of(arg);
335 * Default the report file to the stderr location
337 if (report_fp == NULL)
342 openlog("chat", LOG_PID);
344 openlog("chat", LOG_PID | LOG_NDELAY, LOG_LOCAL2);
347 setlogmask(LOG_UPTO(LOG_INFO));
349 setlogmask(LOG_UPTO(LOG_WARNING));
355 if (chat_file != NULL) {
356 arg = ARG(argc, argv);
362 while ((arg = ARG(argc, argv)) != NULL) {
365 if ((arg = ARG(argc, argv)) != NULL)
375 * Process a chat script when read from a file.
378 void do_file (chat_file)
382 char *sp, *arg, quote;
386 cfp = fopen (chat_file, "r");
388 fatal(1, "%s -- open failed: %m", chat_file);
393 while (fgets(buf, STR_LEN, cfp) != NULL) {
394 sp = strchr (buf, '\n');
401 /* lines starting with '#' are comments. If a real '#'
402 is to be expected, it should be quoted .... */
406 while (*sp != '\0') {
407 if (*sp == ' ' || *sp == '\t') {
412 if (*sp == '"' || *sp == '\'') {
415 while (*sp != quote) {
417 fatal(1, "unterminated quote (line %d)", linect);
427 while (*sp != '\0' && *sp != ' ' && *sp != '\t')
445 * We got an error parsing the command line.
450 Usage: %s [-e] [-v] [-t timeout] [-r report-file] [-T phone-number]\n\
451 [-U phone-number2] {-f chat-file | chat-script}\n", program_name);
458 * Send a message to syslog and/or stderr.
460 void logf __V((const char *fmt, ...))
469 fmt = va_arg(args, char *);
472 vfmtmsg(line, sizeof(line), fmt, args);
474 syslog(LOG_INFO, "%s", line);
476 fprintf(stderr, "%s\n", line);
480 * Print an error message and terminate.
483 void fatal __V((int code, const char *fmt, ...))
493 code = va_arg(args, int);
494 fmt = va_arg(args, char *);
497 vfmtmsg(line, sizeof(line), fmt, args);
499 syslog(LOG_ERR, "%s", line);
501 fprintf(stderr, "%s\n", line);
507 SIGTYPE sigalrm(signo)
513 alarmed = 1; /* Reset alarm to avoid race window */
514 signal(SIGALRM, sigalrm); /* that can cause hanging in read() */
516 if ((flags = fcntl(0, F_GETFL, 0)) == -1)
517 fatal(2, "Can't get file mode flags on stdin: %m");
519 if (fcntl(0, F_SETFL, flags | O_NONBLOCK) == -1)
520 fatal(2, "Can't set file mode flags on stdin: %m");
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");
537 SIGTYPE sigint(signo)
543 SIGTYPE sigterm(signo)
549 SIGTYPE sighup(signo)
557 signal(SIGINT, sigint);
558 signal(SIGTERM, sigterm);
559 signal(SIGHUP, sighup);
561 set_tty_parameters();
562 signal(SIGALRM, sigalrm);
567 void set_tty_parameters()
569 #if defined(get_term_param)
572 if (get_term_param (&t) < 0)
573 fatal(2, "Can't get terminal parameters: %m");
575 saved_tty_parameters = t;
576 have_tty_parameters = 1;
578 t.c_iflag |= IGNBRK | ISTRIP | IGNPAR;
586 if (set_term_param (&t) < 0)
587 fatal(2, "Can't set terminal parameters: %m");
591 void break_sequence()
598 void terminate(status)
602 if (report_file != (char *) 0 && report_fp != (FILE *) NULL) {
604 * Allow the last of the report string to be gathered before we terminate.
606 if (report_gathering) {
609 rep_len = strlen(report_buffer);
610 while (rep_len + 1 <= sizeof(report_buffer)) {
614 if (c < 0 || iscntrl(c))
616 report_buffer[rep_len] = c;
619 report_buffer[rep_len] = 0;
620 fprintf (report_fp, "chat: %s\n", report_buffer);
623 fprintf (report_fp, "Closing \"%s\".\n", report_file);
625 report_fp = (FILE *) NULL;
628 #if defined(get_term_param)
629 if (have_tty_parameters) {
630 if (set_term_param (&saved_tty_parameters) < 0)
631 fatal(2, "Can't restore terminal parameters: %m");
639 * 'Clean up' this string.
641 char *clean(s, sending)
643 int sending; /* set to 1 when sending (putting) this string. */
645 char temp[STR_LEN], cur_chr;
646 register char *s1, *phchar;
647 int add_return = sending;
648 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
653 if (cur_chr == '^') {
655 if (cur_chr == '\0') {
666 if (cur_chr != '\\') {
672 if (cur_chr == '\0') {
686 if (sending && *s == '\0')
703 if (sending && phone_num) {
704 for ( phchar = phone_num; *phchar != '\0'; phchar++)
714 if (sending && phone_num2) {
715 for ( phchar = phone_num2; *phchar != '\0'; phchar++)
754 if (isoctal (cur_chr)) {
758 cur_chr |= *s++ - '0';
761 cur_chr |= *s++ - '0';
765 if (cur_chr != 0 || sending) {
766 if (sending && (cur_chr == '\\' || cur_chr == 0))
783 *s1++ = '\0'; /* guarantee closure */
784 *s1++ = '\0'; /* terminate the string */
785 return dup_mem (temp, (size_t) (s1 - temp)); /* may have embedded nuls */
789 * A modified version of 'strtok'. This version skips \ sequences.
792 char *expect_strtok (s, term)
795 static char *str = "";
800 * If a string was specified then do initial processing.
806 * If this is the escape flag then reset it and ignore the character.
827 * If this is not in the termination string, continue.
829 if (strchr (term, *str) == (char *) 0) {
835 * This is the terminator. Mark the end of the string and stop.
844 * Process the expect string
853 if (strcmp(s, "HANGUP") == 0) {
858 if (strcmp(s, "ABORT") == 0) {
863 if (strcmp(s, "CLR_ABORT") == 0) {
868 if (strcmp(s, "REPORT") == 0) {
873 if (strcmp(s, "CLR_REPORT") == 0) {
878 if (strcmp(s, "TIMEOUT") == 0) {
883 if (strcmp(s, "ECHO") == 0) {
888 if (strcmp(s, "SAY") == 0) {
894 * Fetch the expect and reply string.
897 expect = expect_strtok (s, "-");
900 if (expect == (char *) 0)
903 reply = expect_strtok (s, "-");
906 * Handle the expect string. If successful then exit.
908 if (get_string (expect))
912 * If there is a sub-reply string then send it. Otherwise any condition
915 if (reply == (char *) 0 || exit_code != 3)
922 * The expectation did not occur. This is terminal.
925 logf("Failed (%s)", fail_reason);
928 terminate(exit_code);
932 * Translate the input character to the appropriate string for printing
939 static char string[10];
942 meta = (c & 0x80) ? "M-" : "";
946 sprintf(string, "%s^%c", meta, (int)c + '@');
948 sprintf(string, "%s^?", meta);
950 sprintf(string, "%s%c", meta, c);
956 * process the reply string
964 write(2, s, strlen(s));
971 if (strcmp(s, "OFF") == 0)
972 signal(SIGHUP, SIG_IGN);
974 signal(SIGHUP, sighup);
980 echo = (strcmp(s, "ON") == 0);
989 if (n_aborts >= MAX_ABORTS)
990 fatal(2, "Too many ABORT strings");
994 if (strlen(s1) > strlen(s)
995 || strlen(s1) + 1 > sizeof(fail_buffer))
996 fatal(1, "Illegal or too-long ABORT string ('%v')", s);
998 abort_string[n_aborts++] = s1;
1001 logf("abort on (%v)", s);
1005 if (clear_abort_next) {
1012 clear_abort_next = 0;
1016 if (strlen(s1) > strlen(s)
1017 || strlen(s1) + 1 > sizeof(fail_buffer))
1018 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s);
1021 for (i=0; i < n_aborts; i++) {
1022 if ( strcmp(s1,abort_string[i]) == 0 ) {
1023 free(abort_string[i]);
1024 abort_string[i] = NULL;
1028 logf("clear abort on (%v)", s);
1033 pack_array(abort_string,old_max);
1041 if (n_reports >= MAX_REPORTS)
1042 fatal(2, "Too many REPORT strings");
1046 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1047 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1049 report_string[n_reports++] = s1;
1052 logf("report (%v)", s);
1056 if (clear_report_next) {
1063 clear_report_next = 0;
1067 if (strlen(s1) > strlen(s) || strlen(s1) > sizeof fail_buffer - 1)
1068 fatal(1, "Illegal or too-long REPORT string ('%v')", s);
1070 old_max = n_reports;
1071 for (i=0; i < n_reports; i++) {
1072 if ( strcmp(s1,report_string[i]) == 0 ) {
1073 free(report_string[i]);
1074 report_string[i] = NULL;
1078 logf("clear report (%v)", s);
1083 pack_array(report_string,old_max);
1093 timeout = DEFAULT_CHAT_TIMEOUT;
1096 logf("timeout set to %d seconds", timeout);
1101 if (strcmp(s, "EOT") == 0)
1103 else if (strcmp(s, "BREAK") == 0)
1115 status = read(0, &c, 1);
1119 return ((int)c & 0x7F);
1122 logf("warning: read() on stdin 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");
1141 usleep(10000); /* inter-character typing delay (?) */
1143 status = write(1, &ch, 1);
1150 logf("warning: write() on stdout returned %d", status);
1153 if ((status = fcntl(0, F_GETFL, 0)) == -1)
1154 fatal(2, "Can't get file mode flags on stdin, %m");
1156 if (fcntl(0, F_SETFL, status & ~O_NONBLOCK) == -1)
1157 fatal(2, "Can't set file mode flags on stdin: %m");
1166 if (alarmed || put_char(c) < 0) {
1171 if (errno == EINTR || errno == EWOULDBLOCK)
1172 logf(" -- write timed out");
1174 logf(" -- write failed: %m");
1189 logf("send (??????)");
1191 logf("send (%v)", s);
1194 alarm(timeout); alarmed = 0;
1197 register char c = *s++;
1200 if (!write_char (c))
1216 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1220 if (!write_char (c))
1232 * Echo a character to stderr.
1233 * When called with -1, a '\n' character is generated when
1234 * the cursor is not at the beginning of a line.
1243 case '\r': /* ignore '\r' */
1255 write(2, s, strlen(s));
1262 * 'Wait for' this string to appear on this file descriptor.
1264 int get_string(string)
1265 register char *string;
1268 int c, printed = 0, len, minlen;
1269 register char *s = temp, *end = s + STR_LEN;
1270 char *logged = temp;
1272 fail_reason = (char *)0;
1273 string = clean(string, 0);
1274 len = strlen(string);
1275 minlen = (len > sizeof(fail_buffer)? len: sizeof(fail_buffer)) - 1;
1278 logf("expect (%v)", string);
1280 if (len > STR_LEN) {
1281 logf("expect string is too long");
1295 while ( ! alarmed && (c = get_char()) >= 0) {
1296 int n, abort_len, report_len;
1300 if (verbose && c == '\n') {
1302 logf(""); /* blank line */
1304 logf("%0.*v", s - logged, logged);
1310 if (verbose && s >= logged + 80) {
1311 logf("%0.*v", s - logged, logged);
1317 fputc( '\n', stderr );
1319 fprintf( stderr, "%s", character(c) );
1322 if (!report_gathering) {
1323 for (n = 0; n < n_reports; ++n) {
1324 if ((report_string[n] != (char*) NULL) &&
1325 s - temp >= (report_len = strlen(report_string[n])) &&
1326 strncmp(s - report_len, report_string[n], report_len) == 0) {
1327 time_t time_now = time ((time_t*) NULL);
1328 struct tm* tm_now = localtime (&time_now);
1330 strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now);
1331 strcat (report_buffer, report_string[n]);
1333 report_string[n] = (char *) NULL;
1334 report_gathering = 1;
1341 int rep_len = strlen (report_buffer);
1342 report_buffer[rep_len] = c;
1343 report_buffer[rep_len + 1] = '\0';
1346 report_gathering = 0;
1347 fprintf (report_fp, "chat: %s\n", report_buffer);
1351 if (s - temp >= len &&
1352 c == string[len - 1] &&
1353 strncmp(s - len, string, len) == 0) {
1356 logf("%0.*v", s - logged, logged);
1357 logf(" -- got it\n");
1365 for (n = 0; n < n_aborts; ++n) {
1366 if (s - temp >= (abort_len = strlen(abort_string[n])) &&
1367 strncmp(s - abort_len, abort_string[n], abort_len) == 0) {
1370 logf("%0.*v", s - logged, logged);
1377 strcpy(fail_reason = fail_buffer, abort_string[n]);
1383 if (logged < s - minlen) {
1384 logf("%0.*v", s - logged, logged);
1388 memmove(temp, s, minlen);
1389 logged = temp + (logged - s);
1393 if (alarmed && verbose)
1394 logf("warning: alarm synchronization problem");
1399 if (verbose && printed) {
1401 logf(" -- read timed out");
1403 logf(" -- read failed: %m");
1412 #include <sys/types.h>
1413 #include <sys/time.h>
1416 usleep -- support routine for 4.2BSD system call emulations
1417 last edit: 29-Oct-1984 D A Gwyn
1420 extern int select();
1423 usleep( usec ) /* returns 0 if ok, else -1 */
1424 long usec; /* delay in microseconds */
1426 static struct { /* `timeval' */
1427 long tv_sec; /* seconds */
1428 long tv_usec; /* microsecs */
1429 } delay; /* _select() timeout */
1431 delay.tv_sec = usec / 1000000L;
1432 delay.tv_usec = usec % 1000000L;
1434 return select(0, (long *)0, (long *)0, (long *)0, &delay);
1439 pack_array (array, end)
1440 char **array; /* The address of the array of string pointers */
1441 int end; /* The index of the next free entry before CLR_ */
1445 for (i = 0; i < end; i++) {
1446 if (array[i] == NULL) {
1447 for (j = i+1; j < end; ++j)
1448 if (array[j] != NULL)
1449 array[i++] = array[j];
1450 for (; i < end; ++i)
1458 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1459 * also specify the length of the output buffer, and we handle the
1460 * %m (error message) format.
1461 * Doesn't do floating-point formats.
1462 * Returns the number of chars put into buf.
1464 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1467 vfmtmsg(buf, buflen, fmt, args)
1474 int width, prec, fillch;
1475 int base, len, neg, quoted;
1476 unsigned long val = 0;
1482 static char hexchars[] = "0123456789abcdef";
1486 while (buflen > 0) {
1487 for (f = fmt; *f != '%' && *f != 0; ++f)
1493 memcpy(buf, fmt, len);
1508 width = va_arg(args, int);
1511 while (isdigit(c)) {
1512 width = width * 10 + c - '0';
1519 prec = va_arg(args, int);
1522 while (isdigit(c)) {
1523 prec = prec * 10 + c - '0';
1534 i = va_arg(args, int);
1543 val = va_arg(args, unsigned int);
1547 val = va_arg(args, unsigned int);
1551 val = (unsigned long) va_arg(args, void *);
1556 str = va_arg(args, char *);
1559 num[0] = va_arg(args, int);
1564 str = strerror(errno);
1566 case 'v': /* "visible" string */
1567 case 'q': /* quoted string */
1569 p = va_arg(args, unsigned char *);
1570 if (fillch == '0' && prec > 0) {
1573 n = strlen((char *)p);
1574 if (prec > 0 && prec < n)
1577 while (n > 0 && buflen > 0) {
1580 if (!quoted && c >= 0x80) {
1585 if (quoted && (c == '"' || c == '\\'))
1587 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
1591 case '\t': OUTCHAR('t'); break;
1592 case '\n': OUTCHAR('n'); break;
1593 case '\b': OUTCHAR('b'); break;
1594 case '\f': OUTCHAR('f'); break;
1597 OUTCHAR(hexchars[c >> 4]);
1598 OUTCHAR(hexchars[c & 0xf]);
1615 --fmt; /* so %z outputs %z etc. */
1620 str = num + sizeof(num);
1622 while (str > num + neg) {
1623 *--str = hexchars[val % base];
1625 if (--prec <= 0 && val == 0)
1637 len = num + sizeof(num) - 1 - str;
1640 if (prec > 0 && len > prec)
1646 if ((n = width - len) > 0) {
1654 memcpy(buf, str, len);