2 * Copyright 2000 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 * Chat -- a program for automatic session establishment (i.e. dial
7 * the phone and log in).
9 * Standard termination codes:
10 * 0 - successful completion of the script
11 * 1 - invalid argument, expect string too large, etc.
12 * 2 - error on an I/O operation or fatal error condition.
13 * 3 - timeout waiting for a simple string.
14 * 4 - the first string declared as "ABORT"
15 * 5 - the second string declared as "ABORT"
16 * 6 - ... and so on for successive ABORT strings.
18 * This software is in the public domain.
21 * 22-May-99 added environment substitutuion, enabled with -E switch.
22 * Andreas Arens <andras@cityweb.de>.
24 * 12-May-99 added a feature to read data to be sent from a file,
25 * if the send string starts with @. Idea from gpk <gpk@onramp.net>.
27 * added -T and -U option and \T and \U substitution to pass a phone
28 * number into chat script. Two are needed for some ISDN TA applications.
29 * Keith Dart <kdart@cisco.com>
32 * Added SAY keyword to send output to stderr.
33 * This allows to turn ECHO OFF and to output specific, user selected,
34 * text to give progress messages. This best works when stderr
35 * exists (i.e.: pppd in nodetach mode).
37 * Added HANGUP directives to allow for us to be called
38 * back. When HANGUP is set to NO, chat will not hangup at HUP signal.
39 * We rely on timeouts in that case.
41 * Added CLR_ABORT to clear previously set ABORT string. This has been
42 * dictated by the HANGUP above as "NO CARRIER" (for example) must be
43 * an ABORT condition until we know the other host is going to close
44 * the connection for call back. As soon as we have completed the
45 * first stage of the call back sequence, "NO CARRIER" is a valid, non
46 * fatal string. As soon as we got called back (probably get "CONNECT"),
47 * we should re-arm the ABORT "NO CARRIER". Hence the CLR_ABORT command.
48 * Note that CLR_ABORT packs the abort_strings[] array so that we do not
49 * have unused entries not being reclaimed.
51 * In the same vein as above, added CLR_REPORT keyword.
53 * Allow for comments. Line starting with '#' are comments and are
54 * ignored. If a '#' is to be expected as the first character, the
55 * expect string must be quoted.
58 * Francis Demierre <Francis@SwissMail.Com>
59 * Thu May 15 17:15:40 MET DST 1997
62 * Added -r "report file" switch & REPORT keyword.
63 * Robert Geer <bgeer@xmission.com>
65 * Added -s "use stderr" and -S "don't use syslog" switches.
67 * Karl O. Pinc <kop@meme.com>
70 * Added -e "echo" switch & ECHO keyword
71 * Dick Streefland <dicks@tasking.nl>
74 * Considerable updates and modifications by
75 * Al Longyear <longyear@pobox.com>
76 * Paul Mackerras <paulus@cs.anu.edu.au>
79 * The original author is:
81 * Karl Fox <karl@MorningStar.Com>
82 * Morning Star Technologies, Inc.
93 #pragma ident "%Z%%M% %I% %E% SMI"
96 static const char rcsid
[] = "$Id: chat.c,v 1.26 1999/12/23 01:39:54 paulus Exp $";
108 #include <sys/types.h>
109 #include <sys/stat.h>
139 #define __V(x) (va_alist) va_dcl
145 #define O_NONBLOCK O_NDELAY
150 extern char *sys_errlist
[];
151 #define memmove(to, from, n) bcopy(from, to, n)
152 #define strerror(n) ((unsigned)(n) < sys_nerr? sys_errlist[(n)] :\
156 /*************** Micro getopt() *********************************************/
157 #define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \
158 (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\
159 &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0))
160 #define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \
161 (_O=4,(char*)0):(char*)0)
162 #define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0)
163 #define ARG(c,v) (c?(--c,*v++):(char*)0)
165 static int _O
= 0; /* Internal state */
166 /*************** Micro getopt() *********************************************/
170 #define MAX_ABORTS 50
171 #define MAX_REPORTS 50
172 #define DEFAULT_CHAT_TIMEOUT 45
183 FILE* report_fp
= (FILE *) 0;
184 char *report_file
= (char *) 0;
185 char *chat_file
= (char *) 0;
186 char *phone_num
= (char *) 0;
187 char *phone_num2
= (char *) 0;
188 int timeout
= DEFAULT_CHAT_TIMEOUT
;
190 int have_tty_parameters
= 0;
193 #define term_parms struct termio
194 #define get_term_param(param) ioctl(0, TCGETA, param)
195 #define set_term_param(param) ioctl(0, TCSETA, param)
196 struct termio saved_tty_parameters
;
200 #define term_parms struct termios
201 #define get_term_param(param) tcgetattr(0, param)
202 #define set_term_param(param) tcsetattr(0, TCSANOW, param)
203 struct termios saved_tty_parameters
;
206 char *abort_string
[MAX_ABORTS
], *fail_reason
= (char *)0,
208 int n_aborts
= 0, abort_next
= 0, timeout_next
= 0, echo_next
= 0;
209 int clear_abort_next
= 0;
211 char *report_string
[MAX_REPORTS
] ;
212 char report_buffer
[50] ;
213 int n_reports
= 0, report_next
= 0, report_gathering
= 0 ;
214 int clear_report_next
= 0;
216 int say_next
= 0, hup_next
= 0;
218 void *dup_mem
__P((void *b
, size_t c
));
219 void *copy_of
__P((char *s
));
220 void usage
__P((void));
221 void logf
__P((const char *fmt
, ...));
222 void fatal
__P((int code
, const char *fmt
, ...));
223 SIGTYPE sigalrm
__P((int signo
));
224 SIGTYPE sigint
__P((int signo
));
225 SIGTYPE sigterm
__P((int signo
));
226 SIGTYPE sighup
__P((int signo
));
227 void unalarm
__P((void));
228 void init
__P((void));
229 void set_tty_parameters
__P((void));
230 void echo_stderr
__P((int));
231 void break_sequence
__P((void));
232 void terminate
__P((int status
));
233 void do_file
__P((char *chat_file
));
234 int get_string
__P((register char *string
));
235 int put_string
__P((register char *s
));
236 int write_char
__P((int c
));
237 int put_char
__P((int c
));
238 int get_char
__P((void));
239 void chat_send
__P((register char *s
));
240 char *character
__P((int c
));
241 void chat_expect
__P((register char *s
));
242 char *clean
__P((register char *s
, int sending
));
243 void break_sequence
__P((void));
244 void terminate
__P((int status
));
245 void pack_array
__P((char **array
, int end
));
246 char *expect_strtok
__P((char *, char *));
247 int vfmtmsg
__P((char *, int, const char *, va_list)); /* vsprintf++ */
249 int main
__P((int, char *[]));
255 void *ans
= malloc (c
);
257 fatal(2, "memory error!");
266 return dup_mem (s
, strlen (s
) + 1);
270 * chat [ -v ] [ -E ] [ -T number ] [ -U number ] [ -t timeout ] [ -f chat-file ] \
271 * [ -r report-file ] \
272 * [...[[expect[-say[-expect...]] say expect[-say[-expect]] ...]]]
274 * Perform a UUCP-dialer-like chat script on stdin and stdout.
284 program_name
= *argv
;
287 while ((option
= OPTION(argc
, argv
)) != 0) {
314 if ((arg
= OPTARG(argc
, argv
)) != NULL
)
315 chat_file
= copy_of(arg
);
321 if ((arg
= OPTARG(argc
, argv
)) != NULL
)
328 arg
= OPTARG (argc
, argv
);
330 if (report_fp
!= NULL
)
332 report_file
= copy_of (arg
);
333 report_fp
= fopen (report_file
, "a");
334 if (report_fp
!= NULL
) {
336 fprintf (report_fp
, "Opening \"%s\"...\n",
344 if ((arg
= OPTARG(argc
, argv
)) != NULL
)
345 phone_num
= copy_of(arg
);
351 if ((arg
= OPTARG(argc
, argv
)) != NULL
)
352 phone_num2
= copy_of(arg
);
363 * Default the report file to the stderr location
365 if (report_fp
== NULL
)
370 openlog("chat", LOG_PID
);
372 openlog("chat", LOG_PID
| LOG_NDELAY
, LOG_LOCAL2
);
375 setlogmask(LOG_UPTO(LOG_INFO
));
377 setlogmask(LOG_UPTO(LOG_WARNING
));
383 if (chat_file
!= NULL
) {
384 arg
= ARG(argc
, argv
);
390 while ((arg
= ARG(argc
, argv
)) != NULL
) {
393 if ((arg
= ARG(argc
, argv
)) != NULL
)
403 * Process a chat script when read from a file.
406 void do_file (chat_file
)
410 char *sp
, *arg
, quote
;
414 cfp
= fopen (chat_file
, "r");
416 fatal(1, "%s -- open failed: %m", chat_file
);
421 while (fgets(buf
, STR_LEN
, cfp
) != NULL
) {
422 sp
= strchr (buf
, '\n');
429 /* lines starting with '#' are comments. If a real '#'
430 is to be expected, it should be quoted .... */
434 while (*sp
!= '\0') {
435 if (*sp
== ' ' || *sp
== '\t') {
440 if (*sp
== '"' || *sp
== '\'') {
443 while (*sp
!= quote
) {
445 fatal(1, "unterminated quote (line %d)", linect
);
455 while (*sp
!= '\0' && *sp
!= ' ' && *sp
!= '\t')
473 * We got an error parsing the command line.
478 Usage: %s [-e] [-E] [-v] [-V] [-t timeout] [-r report-file]\n\
479 [-T phone-number] [-U phone-number2] {-f chat-file | chat-script}\n", program_name
);
486 * Send a message to syslog and/or stderr.
488 void logf
__V((const char *fmt
, ...))
497 fmt
= va_arg(args
, char *);
500 vfmtmsg(line
, sizeof(line
), fmt
, args
);
502 syslog(LOG_INFO
, "%s", line
);
504 fprintf(stderr
, "%s\n", line
);
508 * Print an error message and terminate.
511 void fatal
__V((int code
, const char *fmt
, ...))
521 code
= va_arg(args
, int);
522 fmt
= va_arg(args
, char *);
525 vfmtmsg(line
, sizeof(line
), fmt
, args
);
527 syslog(LOG_ERR
, "%s", line
);
529 fprintf(stderr
, "%s\n", line
);
535 SIGTYPE
sigalrm(signo
)
541 alarmed
= 1; /* Reset alarm to avoid race window */
542 signal(SIGALRM
, sigalrm
); /* that can cause hanging in read() */
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");
558 if ((flags
= fcntl(0, F_GETFL
, 0)) == -1)
559 fatal(2, "Can't get file mode flags on stdin: %m");
561 if (fcntl(0, F_SETFL
, flags
& ~O_NONBLOCK
) == -1)
562 fatal(2, "Can't set file mode flags on stdin: %m");
565 SIGTYPE
sigint(signo
)
571 SIGTYPE
sigterm(signo
)
577 SIGTYPE
sighup(signo
)
585 signal(SIGINT
, sigint
);
586 signal(SIGTERM
, sigterm
);
587 signal(SIGHUP
, sighup
);
589 set_tty_parameters();
590 signal(SIGALRM
, sigalrm
);
595 void set_tty_parameters()
597 #if defined(get_term_param)
600 if (get_term_param (&t
) < 0)
601 fatal(2, "Can't get terminal parameters: %m");
603 saved_tty_parameters
= t
;
604 have_tty_parameters
= 1;
606 t
.c_iflag
|= IGNBRK
| ISTRIP
| IGNPAR
;
614 if (set_term_param (&t
) < 0)
615 fatal(2, "Can't set terminal parameters: %m");
619 void break_sequence()
626 void terminate(status
)
629 static int terminating
= 0;
636 * Allow the last of the report string to be gathered before we terminate.
638 if (report_gathering
) {
641 rep_len
= strlen(report_buffer
);
642 while (rep_len
+ 1 <= sizeof(report_buffer
)) {
646 if (c
< 0 || iscntrl(c
))
648 report_buffer
[rep_len
] = c
;
651 report_buffer
[rep_len
] = 0;
652 fprintf (report_fp
, "chat: %s\n", report_buffer
);
654 if (report_file
!= (char *) 0 && report_fp
!= (FILE *) NULL
) {
656 fprintf (report_fp
, "Closing \"%s\".\n", report_file
);
658 report_fp
= (FILE *) NULL
;
661 #if defined(get_term_param)
662 if (have_tty_parameters
) {
663 if (set_term_param (&saved_tty_parameters
) < 0)
664 fatal(2, "Can't restore terminal parameters: %m");
672 * 'Clean up' this string.
674 char *clean(s
, sending
)
676 int sending
; /* set to 1 when sending (putting) this string. */
678 char temp
[STR_LEN
], env_str
[STR_LEN
], cur_chr
;
679 register char *s1
, *phchar
;
680 int add_return
= sending
;
681 #define isoctal(chr) (((chr) >= '0') && ((chr) <= '7'))
682 #define isalnumx(chr) ((((chr) >= '0') && ((chr) <= '9')) \
683 || (((chr) >= 'a') && ((chr) <= 'z')) \
684 || (((chr) >= 'A') && ((chr) <= 'Z')) \
690 if (cur_chr
== '^') {
692 if (cur_chr
== '\0') {
703 if (use_env
&& cur_chr
== '$') { /* ARI */
708 phchar
= getenv(env_str
);
715 if (cur_chr
!= '\\') {
721 if (cur_chr
== '\0') {
735 if (sending
&& *s
== '\0')
751 if (sending
&& phone_num
) {
752 for (phchar
= phone_num
; *phchar
!= '\0'; phchar
++)
762 if (sending
&& phone_num2
) {
763 for (phchar
= phone_num2
; *phchar
!= '\0'; phchar
++)
809 if (isoctal (cur_chr
)) {
813 cur_chr
|= *s
++ - '0';
816 cur_chr
|= *s
++ - '0';
820 if (cur_chr
!= 0 || sending
) {
821 if (sending
&& (cur_chr
== '\\' || cur_chr
== 0))
838 *s1
++ = '\0'; /* guarantee closure */
839 *s1
++ = '\0'; /* terminate the string */
840 return dup_mem (temp
, (size_t) (s1
- temp
)); /* may have embedded nuls */
844 * A modified version of 'strtok'. This version skips \ sequences.
847 char *expect_strtok (s
, term
)
850 static char *str
= "";
855 * If a string was specified then do initial processing.
861 * If this is the escape flag then reset it and ignore the character.
882 * If this is not in the termination string, continue.
884 if (strchr (term
, *str
) == (char *) 0) {
890 * This is the terminator. Mark the end of the string and stop.
899 * Process the expect string
908 if (strcmp(s
, "HANGUP") == 0) {
913 if (strcmp(s
, "ABORT") == 0) {
918 if (strcmp(s
, "CLR_ABORT") == 0) {
923 if (strcmp(s
, "REPORT") == 0) {
928 if (strcmp(s
, "CLR_REPORT") == 0) {
933 if (strcmp(s
, "TIMEOUT") == 0) {
938 if (strcmp(s
, "ECHO") == 0) {
943 if (strcmp(s
, "SAY") == 0) {
949 * Fetch the expect and reply string.
952 expect
= expect_strtok (s
, "-");
955 if (expect
== (char *) 0)
958 reply
= expect_strtok (s
, "-");
961 * Handle the expect string. If successful then exit.
963 if (get_string (expect
))
967 * If there is a sub-reply string then send it. Otherwise any condition
970 if (reply
== (char *) 0 || exit_code
!= 3)
977 * The expectation did not occur. This is terminal.
980 logf("Failed (%s)", fail_reason
);
983 terminate(exit_code
);
987 * Translate the input character to the appropriate string for printing
994 static char string
[10];
997 meta
= (c
& 0x80) ? "M-" : "";
1001 sprintf(string
, "%s^%c", meta
, (int)c
+ '@');
1003 sprintf(string
, "%s^?", meta
);
1005 sprintf(string
, "%s%c", meta
, c
);
1011 * process the reply string
1016 char file_data
[STR_LEN
];
1021 write(2, s
, strlen(s
));
1028 if (strcmp(s
, "OFF") == 0)
1029 signal(SIGHUP
, SIG_IGN
);
1031 signal(SIGHUP
, sighup
);
1037 echo
= (strcmp(s
, "ON") == 0);
1046 if (n_aborts
>= MAX_ABORTS
)
1047 fatal(2, "Too many ABORT strings");
1051 if (strlen(s1
) > strlen(s
)
1052 || strlen(s1
) + 1 > sizeof(fail_buffer
))
1053 fatal(1, "Illegal or too-long ABORT string ('%v')", s
);
1055 abort_string
[n_aborts
++] = s1
;
1058 logf("abort on (%v)", s
);
1062 if (clear_abort_next
) {
1068 clear_abort_next
= 0;
1072 if (strlen(s1
) > strlen(s
)
1073 || strlen(s1
) + 1 > sizeof(fail_buffer
))
1074 fatal(1, "Illegal or too-long CLR_ABORT string ('%v')", s
);
1077 for (i
=0; i
< n_aborts
; i
++) {
1078 if ( strcmp(s1
,abort_string
[i
]) == 0 ) {
1079 free(abort_string
[i
]);
1080 abort_string
[i
] = NULL
;
1084 logf("clear abort on (%v)", s
);
1089 pack_array(abort_string
,old_max
);
1097 if (n_reports
>= MAX_REPORTS
)
1098 fatal(2, "Too many REPORT strings");
1102 if (strlen(s1
) > strlen(s
) || strlen(s1
) > sizeof fail_buffer
- 1)
1103 fatal(1, "Illegal or too-long REPORT string ('%v')", s
);
1105 report_string
[n_reports
++] = s1
;
1108 logf("report (%v)", s
);
1112 if (clear_report_next
) {
1118 clear_report_next
= 0;
1122 if (strlen(s1
) > strlen(s
) || strlen(s1
) > sizeof fail_buffer
- 1)
1123 fatal(1, "Illegal or too-long REPORT string ('%v')", s
);
1125 old_max
= n_reports
;
1126 for (i
=0; i
< n_reports
; i
++) {
1127 if ( strcmp(s1
,report_string
[i
]) == 0 ) {
1128 free(report_string
[i
]);
1129 report_string
[i
] = NULL
;
1133 logf("clear report (%v)", s
);
1138 pack_array(report_string
,old_max
);
1148 timeout
= DEFAULT_CHAT_TIMEOUT
;
1151 logf("timeout set to %d seconds", timeout
);
1157 * The syntax @filename means read the string to send from the
1161 /* skip the @ and any following white-space */
1163 while (*++fn
== ' ' || *fn
== '\t')
1170 /* open the file and read until STR_LEN-1 bytes or end-of-file */
1173 fatal(1, "%s -- open failed: %m", fn
);
1174 while (n
< STR_LEN
- 1) {
1175 int nr
= fread(&file_data
[n
], 1, STR_LEN
- 1 - n
, f
);
1177 fatal(1, "%s -- read error", fn
);
1184 /* use the string we got as the string to send,
1185 but trim off the final newline if any. */
1186 if (n
> 0 && file_data
[n
-1] == '\n')
1193 if (strcmp(s
, "EOT") == 0)
1195 else if (strcmp(s
, "BREAK") == 0)
1207 status
= read(0, &c
, 1);
1211 return ((int)c
& 0x7F);
1214 logf("warning: read() on stdin returned %d", status
);
1217 if ((status
= fcntl(0, F_GETFL
, 0)) == -1)
1218 fatal(2, "Can't get file mode flags on stdin: %m");
1220 if (fcntl(0, F_SETFL
, status
& ~O_NONBLOCK
) == -1)
1221 fatal(2, "Can't set file mode flags on stdin: %m");
1233 usleep(10000); /* inter-character typing delay (?) */
1235 status
= write(1, &ch
, 1);
1242 logf("warning: write() on stdout returned %d", status
);
1245 if ((status
= fcntl(0, F_GETFL
, 0)) == -1)
1246 fatal(2, "Can't get file mode flags on stdin, %m");
1248 if (fcntl(0, F_SETFL
, status
& ~O_NONBLOCK
) == -1)
1249 fatal(2, "Can't set file mode flags on stdin: %m");
1258 if (alarmed
|| put_char(c
) < 0) {
1263 if (errno
== EINTR
|| errno
== EWOULDBLOCK
)
1264 logf(" -- write timed out");
1266 logf(" -- write failed: %m");
1281 logf("send (??????)");
1283 logf("send (%v)", s
);
1286 alarm(timeout
); alarmed
= 0;
1289 register char c
= *s
++;
1292 if (!write_char (c
))
1308 usleep(10000); /* 1/100th of a second (arg is microseconds) */
1312 if (!write_char (c
))
1324 * Echo a character to stderr.
1325 * When called with -1, a '\n' character is generated when
1326 * the cursor is not at the beginning of a line.
1335 case '\r': /* ignore '\r' */
1347 write(2, s
, strlen(s
));
1354 * 'Wait for' this string to appear on this file descriptor.
1356 int get_string(string
)
1357 register char *string
;
1360 int c
, printed
= 0, len
, minlen
;
1361 register char *s
= temp
, *end
= s
+ STR_LEN
;
1362 char *logged
= temp
;
1364 fail_reason
= (char *)0;
1365 string
= clean(string
, 0);
1366 len
= strlen(string
);
1367 minlen
= (len
> sizeof(fail_buffer
)? len
: sizeof(fail_buffer
)) - 1;
1370 logf("expect (%v)", string
);
1372 if (len
> STR_LEN
) {
1373 logf("expect string is too long");
1387 while ( ! alarmed
&& (c
= get_char()) >= 0) {
1388 int n
, abort_len
, report_len
;
1392 if (verbose
&& c
== '\n') {
1394 logf(""); /* blank line */
1396 logf("%0.*v", s
- logged
, logged
);
1402 if (verbose
&& s
>= logged
+ 80) {
1403 logf("%0.*v", s
- logged
, logged
);
1409 fputc( '\n', stderr
);
1411 fprintf( stderr
, "%s", character(c
) );
1414 if (!report_gathering
) {
1415 for (n
= 0; n
< n_reports
; ++n
) {
1416 if ((report_string
[n
] != (char*) NULL
) &&
1417 s
- temp
>= (report_len
= strlen(report_string
[n
])) &&
1418 strncmp(s
- report_len
, report_string
[n
], report_len
) == 0) {
1419 time_t time_now
= time ((time_t*) NULL
);
1420 struct tm
* tm_now
= localtime (&time_now
);
1422 strftime (report_buffer
, 20, "%b %d %H:%M:%S ", tm_now
);
1423 strcat (report_buffer
, report_string
[n
]);
1425 report_string
[n
] = (char *) NULL
;
1426 report_gathering
= 1;
1433 int rep_len
= strlen (report_buffer
);
1434 report_buffer
[rep_len
] = c
;
1435 report_buffer
[rep_len
+ 1] = '\0';
1438 report_gathering
= 0;
1439 fprintf (report_fp
, "chat: %s\n", report_buffer
);
1443 if (s
- temp
>= len
&&
1444 c
== string
[len
- 1] &&
1445 strncmp(s
- len
, string
, len
) == 0) {
1448 logf("%0.*v", s
- logged
, logged
);
1449 logf(" -- got it\n");
1457 for (n
= 0; n
< n_aborts
; ++n
) {
1458 if (s
- temp
>= (abort_len
= strlen(abort_string
[n
])) &&
1459 strncmp(s
- abort_len
, abort_string
[n
], abort_len
) == 0) {
1462 logf("%0.*v", s
- logged
, logged
);
1469 strcpy(fail_reason
= fail_buffer
, abort_string
[n
]);
1475 if (logged
< s
- minlen
) {
1477 logf("%0.*v", s
- logged
, logged
);
1481 memmove(temp
, s
, minlen
);
1482 logged
= temp
+ (logged
- s
);
1486 if (alarmed
&& verbose
)
1487 logf("warning: alarm synchronization problem");
1492 if (verbose
&& printed
) {
1494 logf(" -- read timed out");
1496 logf(" -- read failed: %m");
1505 * Gross kludge to handle Solaris versions >= 2.6 having usleep.
1508 #include <sys/param.h>
1509 #if MAXUID > 65536 /* then this is Solaris 2.6 or later */
1515 #include <sys/types.h>
1516 #include <sys/time.h>
1519 usleep -- support routine for 4.2BSD system call emulations
1520 last edit: 29-Oct-1984 D A Gwyn
1523 extern int select();
1526 usleep( usec
) /* returns 0 if ok, else -1 */
1527 long usec
; /* delay in microseconds */
1529 static struct { /* `timeval' */
1530 long tv_sec
; /* seconds */
1531 long tv_usec
; /* microsecs */
1532 } delay
; /* _select() timeout */
1534 delay
.tv_sec
= usec
/ 1000000L;
1535 delay
.tv_usec
= usec
% 1000000L;
1537 return select(0, (long *)0, (long *)0, (long *)0, &delay
);
1542 pack_array (array
, end
)
1543 char **array
; /* The address of the array of string pointers */
1544 int end
; /* The index of the next free entry before CLR_ */
1548 for (i
= 0; i
< end
; i
++) {
1549 if (array
[i
] == NULL
) {
1550 for (j
= i
+1; j
< end
; ++j
)
1551 if (array
[j
] != NULL
)
1552 array
[i
++] = array
[j
];
1553 for (; i
< end
; ++i
)
1561 * vfmtmsg - format a message into a buffer. Like vsprintf except we
1562 * also specify the length of the output buffer, and we handle the
1563 * %m (error message) format.
1564 * Doesn't do floating-point formats.
1565 * Returns the number of chars put into buf.
1567 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
1570 vfmtmsg(buf
, buflen
, fmt
, args
)
1577 int width
, prec
, fillch
;
1578 int base
, len
, neg
, quoted
;
1579 unsigned long val
= 0;
1584 static char hexchars
[] = "0123456789abcdef";
1588 while (buflen
> 0) {
1589 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
1595 memcpy(buf
, fmt
, len
);
1610 width
= va_arg(args
, int);
1613 while (isdigit(c
)) {
1614 width
= width
* 10 + c
- '0';
1621 prec
= va_arg(args
, int);
1624 while (isdigit(c
)) {
1625 prec
= prec
* 10 + c
- '0';
1636 i
= va_arg(args
, int);
1645 val
= va_arg(args
, unsigned int);
1649 val
= va_arg(args
, unsigned int);
1653 val
= (unsigned long) va_arg(args
, void *);
1658 str
= va_arg(args
, char *);
1661 num
[0] = va_arg(args
, int);
1666 str
= strerror(errno
);
1668 case 'v': /* "visible" string */
1669 case 'q': /* quoted string */
1671 p
= va_arg(args
, unsigned char *);
1672 if (fillch
== '0' && prec
> 0) {
1675 n
= strlen((char *)p
);
1676 if (prec
> 0 && prec
< n
)
1679 while (n
> 0 && buflen
> 0) {
1682 if (!quoted
&& c
>= 0x80) {
1687 if (quoted
&& (c
== '"' || c
== '\\'))
1689 if (c
< 0x20 || (0x7f <= c
&& c
< 0xa0)) {
1693 case '\t': OUTCHAR('t'); break;
1694 case '\n': OUTCHAR('n'); break;
1695 case '\b': OUTCHAR('b'); break;
1696 case '\f': OUTCHAR('f'); break;
1699 OUTCHAR(hexchars
[c
>> 4]);
1700 OUTCHAR(hexchars
[c
& 0xf]);
1717 --fmt
; /* so %z outputs %z etc. */
1722 str
= num
+ sizeof(num
);
1724 while (str
> num
+ neg
) {
1725 *--str
= hexchars
[val
% base
];
1727 if (--prec
<= 0 && val
== 0)
1739 len
= num
+ sizeof(num
) - 1 - str
;
1742 if (prec
> 0 && len
> prec
)
1748 if ((n
= width
- len
) > 0) {
1756 memcpy(buf
, str
, len
);