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.21 2003/10/31 06:22:03 kientzle Exp $
81 #include <sys/types.h>
103 #define O_NONBLOCK O_NDELAY
106 #define MAX_ABORTS 50
107 #define MAX_REPORTS 50
108 #define DEFAULT_CHAT_TIMEOUT 45
111 static int verbose
= 0;
112 static int to_log
= 1;
113 static int to_stderr
= 0;
114 static int Verbose
= 0;
115 static int quiet
= 0;
116 static int exit_code
= 0;
117 static FILE* report_fp
= NULL
;
118 static char *report_file
= NULL
;
119 static char *chat_file
= NULL
;
120 static char *phone_num
= NULL
;
121 static char *phone_num2
= NULL
;
122 static int timeout
= DEFAULT_CHAT_TIMEOUT
;
124 static char blank
[] = "";
126 static int have_tty_parameters
= 0;
128 #define term_parms struct termios
129 #define get_term_param(param) tcgetattr(0, param)
130 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
131 static struct termios saved_tty_parameters
;
133 static char *abort_string
[MAX_ABORTS
], *fail_reason
= NULL
,
135 static int n_aborts
= 0, abort_next
= 0, timeout_next
= 0, echo_next
= 0;
136 static int clear_abort_next
= 0;
138 static char *report_string
[MAX_REPORTS
] ;
139 static char report_buffer
[50] ;
140 static int n_reports
= 0, report_next
= 0, report_gathering
= 0 ;
141 static int clear_report_next
= 0;
143 static int say_next
= 0, hup_next
= 0;
145 static void *dup_mem(void *b
, size_t c
);
146 static void *copy_of(char *s
);
147 static void usage(void);
148 static void chat_logf(const char *fmt
, ...);
149 static void fatal(int code
, const char *fmt
, ...) __dead2
;
150 static SIGTYPE
sigalrm(int signo
);
151 static SIGTYPE
sigint(int signo
) __dead2
;
152 static SIGTYPE
sigterm(int signo
) __dead2
;
153 static SIGTYPE
sighup(int signo
) __dead2
;
154 static void init(void);
155 static void set_tty_parameters(void);
156 static void echo_stderr(int);
157 static void break_sequence(void);
158 static void terminate(int status
) __dead2
;
159 static void do_file(char *chatfile
);
160 static int get_string(char *string
);
161 static int put_string(char *s
);
162 static int write_char(int c
);
163 static int put_char(int c
);
164 static int get_char(void);
165 static void chat_send(char *s
);
166 static char *character(int c
);
167 static void chat_expect(char *s
);
168 static char *clean(char *s
, int sending
);
169 static void pack_array(char **array
, int end
);
170 static char *expect_strtok(char *, const char *);
171 static int vfmtmsg(char *, int, const char *, va_list); /* vsprintf++ */
174 dup_mem(void *b
, size_t c
)
176 void *ans
= malloc (c
);
178 fatal(2, "memory error!");
187 return dup_mem (s
, strlen (s
) + 1);
191 * chat [-esSvV] [-f chat-file] [-r report-file] [-t timeout]
192 * [-T phone-number] [-U phone-number2] [chat-script]
193 * where chat-script has the form:
194 * [...[[expect[-send[-expect...]] send expect[-send[-expect]] ...]]]
196 * Perform a UUCP-dialer-like chat script on stdin and stdout.
199 main(int argc
, char *argv
[])
205 while ((option
= getopt(argc
, argv
, "ef:r:sSt:T:U:vV")) != -1) {
212 if (chat_file
!= NULL
)
214 chat_file
= copy_of(optarg
);
218 if (report_fp
!= NULL
)
220 if (report_file
!= NULL
)
222 report_file
= copy_of(optarg
);
223 report_fp
= fopen(report_file
, "a");
224 if (report_fp
!= NULL
) {
226 fprintf(report_fp
, "Opening \"%s\"...\n", report_file
);
228 fatal(2, "cannot open \"%s\" for appending", report_file
);
240 timeout
= atoi(optarg
);
244 if (phone_num
!= NULL
)
246 phone_num
= copy_of(optarg
);
250 if (phone_num2
!= NULL
)
252 phone_num2
= copy_of(optarg
);
273 * Default the report file to the stderr location
275 if (report_fp
== NULL
)
279 openlog("chat", LOG_PID
| LOG_NDELAY
, LOG_LOCAL2
);
282 setlogmask(LOG_UPTO(LOG_INFO
));
284 setlogmask(LOG_UPTO(LOG_WARNING
));
287 if (chat_file
!= NULL
) {
296 while (*argv
!= NULL
&& argc
> 0) {
301 if (*argv
!= NULL
&& argc
> 0) {
314 * Process a chat script when read from a file.
318 do_file(char *chatfile
)
321 char *sp
, *arg
, quote
;
325 cfp
= fopen (chatfile
, "r");
327 fatal(1, "%s -- open failed: %m", chatfile
);
332 while (fgets(buf
, STR_LEN
, cfp
) != NULL
) {
333 sp
= strchr (buf
, '\n');
340 /* lines starting with '#' are comments. If a real '#'
341 is to be expected, it should be quoted .... */
345 while (*sp
!= '\0') {
346 if (*sp
== ' ' || *sp
== '\t') {
351 if (*sp
== '"' || *sp
== '\'') {
354 while (*sp
!= quote
) {
356 fatal(1, "unterminated quote (line %d)", linect
);
366 while (*sp
!= '\0' && *sp
!= ' ' && *sp
!= '\t')
384 * We got an error parsing the command line.
390 "Usage: chat [-esSvV] [-f chat-file] [-r report-file] [-t timeout]\n"
391 " [-T phone-number] [-U phone-number2] [chat-script]\n"
392 "where chat-script has the form:\n"
393 " [...[[expect[-send[-expect...]] send expect[-send[-expect]] ...]]]\n");
397 static char line
[1024];
400 * Send a message to syslog and/or stderr.
403 chat_logf(const char *fmt
, ...)
408 vfmtmsg(line
, sizeof(line
), fmt
, args
);
410 syslog(LOG_INFO
, "%s", line
);
412 fprintf(stderr
, "%s\n", line
);
416 * Print an error message and terminate.
420 fatal(int code
, const char *fmt
, ...)
425 vfmtmsg(line
, sizeof(line
), fmt
, args
);
427 syslog(LOG_ERR
, "%s", line
);
429 fprintf(stderr
, "%s\n", line
);
433 static int alarmed
= 0;
435 static SIGTYPE
sigalrm(int signo __unused
)
440 alarmed
= 1; /* Reset alarm to avoid race window */
441 signal(SIGALRM
, sigalrm
); /* that can cause hanging in read() */
443 if ((flags
= fcntl(0, F_GETFL
, 0)) == -1)
444 fatal(2, "Can't get file mode flags on stdin: %m");
446 if (fcntl(0, F_SETFL
, flags
| O_NONBLOCK
) == -1)
447 fatal(2, "Can't set file mode flags on stdin: %m");
453 static SIGTYPE
sigint(int signo __unused
)
458 static SIGTYPE
sigterm(int signo __unused
)
463 static SIGTYPE
sighup(int signo __unused
)
468 static void init(void)
470 signal(SIGINT
, sigint
);
471 signal(SIGTERM
, sigterm
);
472 signal(SIGHUP
, sighup
);
474 set_tty_parameters();
475 signal(SIGALRM
, sigalrm
);
480 static void set_tty_parameters(void)
482 #if defined(get_term_param)
485 if (get_term_param (&t
) < 0)
486 fatal(2, "Can't get terminal parameters: %m");
488 saved_tty_parameters
= t
;
489 have_tty_parameters
= 1;
491 t
.c_iflag
|= IGNBRK
| ISTRIP
| IGNPAR
;
499 if (set_term_param (&t
) < 0)
500 fatal(2, "Can't set terminal parameters: %m");
504 static void break_sequence(void)
509 static void terminate(int status
)
512 if (report_file
!= NULL
&& report_fp
!= NULL
) {
514 * Allow the last of the report string to be gathered before we terminate.
516 if (report_gathering
) {
520 rep_len
= strlen(report_buffer
);
521 while (rep_len
+ 1 <= sizeof(report_buffer
)) {
525 if (c
< 0 || iscntrl(c
))
527 report_buffer
[rep_len
] = c
;
530 report_buffer
[rep_len
] = 0;
531 fprintf (report_fp
, "chat: %s\n", report_buffer
);
534 fprintf (report_fp
, "Closing \"%s\".\n", report_file
);
539 #if defined(get_term_param)
540 if (have_tty_parameters
) {
541 if (set_term_param (&saved_tty_parameters
) < 0)
542 fatal(2, "Can't restore terminal parameters: %m");
550 * 'Clean up' this string.
553 clean(char *s
, int sending
)
555 char temp
[STR_LEN
], cur_chr
;
557 int add_return
= sending
;
558 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
561 /* Don't overflow buffer, leave room for chars we append later */
562 while (*s
&& s1
- temp
< (off_t
)(sizeof(temp
) - 2 - add_return
)) {
564 if (cur_chr
== '^') {
566 if (cur_chr
== '\0') {
577 if (cur_chr
!= '\\') {
583 if (cur_chr
== '\0') {
597 if (sending
&& *s
== '\0')
614 if (sending
&& phone_num
) {
615 for ( phchar
= phone_num
; *phchar
!= '\0'; phchar
++)
625 if (sending
&& phone_num2
) {
626 for ( phchar
= phone_num2
; *phchar
!= '\0'; phchar
++)
665 if (isoctal (cur_chr
)) {
669 cur_chr
|= *s
++ - '0';
672 cur_chr
|= *s
++ - '0';
676 if (cur_chr
!= 0 || sending
) {
677 if (sending
&& (cur_chr
== '\\' || cur_chr
== 0))
694 *s1
++ = '\0'; /* guarantee closure */
695 *s1
++ = '\0'; /* terminate the string */
696 return dup_mem (temp
, (size_t) (s1
- temp
)); /* may have embedded nuls */
700 * A modified version of 'strtok'. This version skips \ sequences.
704 expect_strtok (char *s
, const char *term
)
706 static char *str
= blank
;
711 * If a string was specified then do initial processing.
717 * If this is the escape flag then reset it and ignore the character.
738 * If this is not in the termination string, continue.
740 if (strchr (term
, *str
) == NULL
) {
746 * This is the terminator. Mark the end of the string and stop.
755 * Process the expect string
764 if (strcmp(s
, "HANGUP") == 0) {
769 if (strcmp(s
, "ABORT") == 0) {
774 if (strcmp(s
, "CLR_ABORT") == 0) {
779 if (strcmp(s
, "REPORT") == 0) {
784 if (strcmp(s
, "CLR_REPORT") == 0) {
789 if (strcmp(s
, "TIMEOUT") == 0) {
794 if (strcmp(s
, "ECHO") == 0) {
799 if (strcmp(s
, "SAY") == 0) {
805 * Fetch the expect and reply string.
808 expect
= expect_strtok (s
, "-");
814 reply
= expect_strtok (s
, "-");
817 * Handle the expect string. If successful then exit.
819 if (get_string (expect
))
823 * If there is a sub-reply string then send it. Otherwise any condition
826 if (reply
== NULL
|| exit_code
!= 3)
833 * The expectation did not occur. This is terminal.
836 chat_logf("Failed (%s)", fail_reason
);
839 terminate(exit_code
);
843 * Translate the input character to the appropriate string for printing
850 static char string
[10];
853 meta
= (c
& 0x80) ? "M-" : "";
857 sprintf(string
, "%s^%c", meta
, (int)c
+ '@');
859 sprintf(string
, "%s^?", meta
);
861 sprintf(string
, "%s%c", meta
, c
);
867 * process the reply string
875 write(STDERR_FILENO
, s
, strlen(s
));
882 if (strcmp(s
, "OFF") == 0)
883 signal(SIGHUP
, SIG_IGN
);
885 signal(SIGHUP
, sighup
);
891 echo
= (strcmp(s
, "ON") == 0);
900 if (n_aborts
>= MAX_ABORTS
)
901 fatal(2, "Too many ABORT strings");
905 if (strlen(s1
) > strlen(s
)
906 || strlen(s1
) + 1 > sizeof(fail_buffer
))
907 fatal(1, "Illegal or too-long ABORT string ('%v')", s
);
909 abort_string
[n_aborts
++] = s1
;
912 chat_logf("abort on (%v)", s
);
916 if (clear_abort_next
) {
922 clear_abort_next
= 0;
926 if (strlen(s1
) > strlen(s
)
927 || strlen(s1
) + 1 > sizeof(fail_buffer
))
928 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s
);
931 for (i
=0; i
< n_aborts
; i
++) {
932 if ( strcmp(s1
,abort_string
[i
]) == 0 ) {
933 free(abort_string
[i
]);
934 abort_string
[i
] = NULL
;
938 chat_logf("clear abort on (%v)", s
);
943 pack_array(abort_string
,old_max
);
951 if (n_reports
>= MAX_REPORTS
)
952 fatal(2, "Too many REPORT strings");
956 if (strlen(s1
) > strlen(s
) || strlen(s1
) > sizeof fail_buffer
- 1)
957 fatal(1, "Illegal or too-long REPORT string ('%v')", s
);
959 report_string
[n_reports
++] = s1
;
962 chat_logf("report (%v)", s
);
966 if (clear_report_next
) {
972 clear_report_next
= 0;
976 if (strlen(s1
) > strlen(s
) || strlen(s1
) > sizeof fail_buffer
- 1)
977 fatal(1, "Illegal or too-long REPORT string ('%v')", s
);
980 for (i
=0; i
< n_reports
; i
++) {
981 if ( strcmp(s1
,report_string
[i
]) == 0 ) {
982 free(report_string
[i
]);
983 report_string
[i
] = NULL
;
987 chat_logf("clear report (%v)", s
);
992 pack_array(report_string
,old_max
);
1002 timeout
= DEFAULT_CHAT_TIMEOUT
;
1005 chat_logf("timeout set to %d seconds", timeout
);
1010 if (strcmp(s
, "EOT") == 0)
1011 s
= strdup("^D\\c");
1012 else if (strcmp(s
, "BREAK") == 0)
1013 s
= strdup("\\K\\c");
1025 status
= read(STDIN_FILENO
, &c
, 1);
1029 return ((int)c
& 0x7F);
1032 chat_logf("warning: read() on stdin returned %d", status
);
1035 if ((status
= fcntl(0, F_GETFL
, 0)) == -1)
1036 fatal(2, "Can't get file mode flags on stdin: %m");
1038 if (fcntl(0, F_SETFL
, status
& ~O_NONBLOCK
) == -1)
1039 fatal(2, "Can't set file mode flags on stdin: %m");
1045 static int put_char(int c
)
1050 usleep(10000); /* inter-character typing delay (?) */
1052 status
= write(STDOUT_FILENO
, &ch
, 1);
1059 chat_logf("warning: write() on stdout returned %d", status
);
1062 if ((status
= fcntl(0, F_GETFL
, 0)) == -1)
1063 fatal(2, "Can't get file mode flags on stdin, %m");
1065 if (fcntl(0, F_SETFL
, status
& ~O_NONBLOCK
) == -1)
1066 fatal(2, "Can't set file mode flags on stdin: %m");
1075 if (alarmed
|| put_char(c
) < 0) {
1080 if (errno
== EINTR
|| errno
== EWOULDBLOCK
)
1081 chat_logf(" -- write timed out");
1083 chat_logf(" -- write failed: %m");
1097 chat_logf("send (%v)", quiet
? "??????" : s
);
1099 alarm(timeout
); alarmed
= 0;
1105 if (!write_char (c
))
1121 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1125 if (!write_char (c
))
1137 * Echo a character to stderr.
1138 * When called with -1, a '\n' character is generated when
1139 * the cursor is not at the beginning of a line.
1148 case '\r': /* ignore '\r' */
1155 write(STDERR_FILENO
, "\n", 1);
1160 write(STDERR_FILENO
, s
, strlen(s
));
1167 * 'Wait for' this string to appear on this file descriptor.
1170 get_string(char *string
)
1175 char *s
= temp
, *end
= s
+ STR_LEN
;
1176 char *logged
= temp
;
1180 if (strlen(string
) > STR_LEN
) {
1181 chat_logf("expect string is too long");
1186 string
= clean(string
, 0);
1187 len
= strlen(string
);
1188 minlen
= (len
> sizeof(fail_buffer
)? len
: sizeof(fail_buffer
)) - 1;
1191 chat_logf("expect (%v)", string
);
1195 chat_logf("got it");
1202 while ( ! alarmed
&& (c
= get_char()) >= 0) {
1203 int n
, abort_len
, report_len
;
1207 if (verbose
&& c
== '\n') {
1209 chat_logf(""); /* blank line */
1211 chat_logf("%0.*v", s
- logged
, logged
);
1217 if (verbose
&& s
>= logged
+ 80) {
1218 chat_logf("%0.*v", s
- logged
, logged
);
1224 fputc( '\n', stderr
);
1226 fprintf( stderr
, "%s", character(c
) );
1229 if (!report_gathering
) {
1230 for (n
= 0; n
< n_reports
; ++n
) {
1231 if ((report_string
[n
] != NULL
) &&
1232 s
- temp
>= (report_len
= strlen(report_string
[n
])) &&
1233 strncmp(s
- report_len
, report_string
[n
], report_len
) == 0) {
1234 time_t time_now
= time (NULL
);
1235 struct tm
* tm_now
= localtime (&time_now
);
1237 strftime (report_buffer
, 20, "%b %d %H:%M:%S ", tm_now
);
1238 strcat (report_buffer
, report_string
[n
]);
1240 report_string
[n
] = NULL
;
1241 report_gathering
= 1;
1248 int rep_len
= strlen (report_buffer
);
1249 report_buffer
[rep_len
] = c
;
1250 report_buffer
[rep_len
+ 1] = '\0';
1253 report_gathering
= 0;
1254 fprintf (report_fp
, "chat: %s\n", report_buffer
);
1258 if ((size_t)(s
- temp
) >= len
&&
1259 c
== string
[len
- 1] &&
1260 strncmp(s
- len
, string
, len
) == 0) {
1263 chat_logf("%0.*v", s
- logged
, logged
);
1264 chat_logf(" -- got it\n");
1272 for (n
= 0; n
< n_aborts
; ++n
) {
1273 if (s
- temp
>= (abort_len
= strlen(abort_string
[n
])) &&
1274 strncmp(s
- abort_len
, abort_string
[n
], abort_len
) == 0) {
1277 chat_logf("%0.*v", s
- logged
, logged
);
1278 chat_logf(" -- failed");
1284 strcpy(fail_reason
= fail_buffer
, abort_string
[n
]);
1290 if (logged
< s
- minlen
) {
1291 chat_logf("%0.*v", s
- logged
, logged
);
1295 memmove(temp
, s
, minlen
);
1296 logged
= temp
+ (logged
- s
);
1300 if (alarmed
&& verbose
)
1301 chat_logf("warning: alarm synchronization problem");
1306 if (verbose
&& printed
) {
1308 chat_logf(" -- read timed out");
1310 chat_logf(" -- read failed: %m");
1319 pack_array(char **array
, int end
)
1323 for (i
= 0; i
< end
; i
++) {
1324 if (array
[i
] == NULL
) {
1325 for (j
= i
+1; j
< end
; ++j
)
1326 if (array
[j
] != NULL
)
1327 array
[i
++] = array
[j
];
1328 for (; i
< end
; ++i
)
1336 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1337 * also specify the length of the output buffer, and we handle the
1338 * %m (error message) format.
1339 * Doesn't do floating-point formats.
1340 * Returns the number of chars put into buf.
1342 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1345 vfmtmsg(char *buf
, int buflen
, const char *fmt
, va_list args
)
1348 int width
, prec
, fillch
;
1349 int base
, len
, neg
, quoted
;
1350 unsigned long val
= 0;
1355 static char hexchars
[] = "0123456789abcdef";
1359 while (buflen
> 0) {
1360 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
1366 memcpy(buf
, fmt
, len
);
1381 width
= va_arg(args
, int);
1384 while (isdigit(c
)) {
1385 width
= width
* 10 + c
- '0';
1392 prec
= va_arg(args
, int);
1395 while (isdigit(c
)) {
1396 prec
= prec
* 10 + c
- '0';
1407 i
= va_arg(args
, int);
1416 val
= va_arg(args
, unsigned int);
1420 val
= va_arg(args
, unsigned int);
1424 val
= (unsigned long) va_arg(args
, void *);
1429 str
= va_arg(args
, char *);
1432 num
[0] = va_arg(args
, int);
1437 str
= strerror(errno
);
1439 case 'v': /* "visible" string */
1440 case 'q': /* quoted string */
1442 p
= va_arg(args
, unsigned char *);
1443 if (fillch
== '0' && prec
> 0) {
1446 n
= strlen((char *)p
);
1447 if (prec
> 0 && prec
< n
)
1450 while (n
> 0 && buflen
> 0) {
1453 if (!quoted
&& c
>= 0x80) {
1458 if (quoted
&& (c
== '"' || c
== '\\'))
1460 if (c
< 0x20 || (0x7f <= c
&& c
< 0xa0)) {
1464 case '\t': OUTCHAR('t'); break;
1465 case '\n': OUTCHAR('n'); break;
1466 case '\b': OUTCHAR('b'); break;
1467 case '\f': OUTCHAR('f'); break;
1470 OUTCHAR(hexchars
[c
>> 4]);
1471 OUTCHAR(hexchars
[c
& 0xf]);
1488 --fmt
; /* so %z outputs %z etc. */
1493 str
= num
+ sizeof(num
);
1495 while (str
> num
+ neg
) {
1496 *--str
= hexchars
[val
% base
];
1498 if (--prec
<= 0 && val
== 0)
1510 len
= num
+ sizeof(num
) - 1 - str
;
1513 if (prec
> 0 && len
> prec
)
1519 if ((n
= width
- len
) > 0) {
1527 memcpy(buf
, str
, len
);