$NetBSD: patch-ad,v 1.2 2007/06/11 12:36:24 tron Exp $ --- chat/chat.c.orig 2004-02-02 03:36:46.000000000 +0000 +++ chat/chat.c 2007-06-11 13:07:08.000000000 +0100 @@ -147,20 +147,9 @@ "unknown error") #endif -/*************** Micro getopt() *********************************************/ -#define OPTION(c,v) (_O&2&&**v?*(*v)++:!c||_O&4?0:(!(_O&1)&& \ - (--c,++v),_O=4,c&&**v=='-'&&v[0][1]?*++*v=='-'\ - &&!v[0][1]?(--c,++v,0):(_O=2,*(*v)++):0)) -#define OPTARG(c,v) (_O&2?**v||(++v,--c)?(_O=1,--c,*v++): \ - (_O=4,(char*)0):(char*)0) -#define OPTONLYARG(c,v) (_O&2&&**v?(_O=1,--c,*v++):(char*)0) -#define ARG(c,v) (c?(--c,*v++):(char*)0) - -static int _O = 0; /* Internal state */ -/*************** Micro getopt() *********************************************/ - char *program_name; +#define BUFFER_SIZE 256 #define MAX_ABORTS 50 #define MAX_REPORTS 50 #define DEFAULT_CHAT_TIMEOUT 45 @@ -198,12 +187,12 @@ #endif char *abort_string[MAX_ABORTS], *fail_reason = (char *)0, - fail_buffer[50]; + fail_buffer[BUFFER_SIZE]; int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0; int clear_abort_next = 0; char *report_string[MAX_REPORTS] ; -char report_buffer[256] ; +char report_buffer[BUFFER_SIZE] ; int n_reports = 0, report_next = 0, report_gathering = 0 ; int clear_report_next = 0; @@ -289,12 +278,12 @@ char **argv; { int option; - char *arg; + int i; program_name = *argv; tzset(); - while ((option = OPTION(argc, argv)) != 0) { + while ((option = getopt(argc, argv, ":eEvVf:t:r:sST:U:")) != -1) { switch (option) { case 'e': ++echo; @@ -321,25 +310,24 @@ break; case 'f': - if ((arg = OPTARG(argc, argv)) != NULL) - chat_file = copy_of(arg); + if (optarg != NULL) + chat_file = copy_of(optarg); else usage(); break; case 't': - if ((arg = OPTARG(argc, argv)) != NULL) - timeout = atoi(arg); + if (optarg != NULL) + timeout = atoi(optarg); else usage(); break; case 'r': - arg = OPTARG (argc, argv); - if (arg) { + if (optarg) { if (report_fp != NULL) fclose (report_fp); - report_file = copy_of (arg); + report_file = copy_of (optarg); report_fp = fopen (report_file, "a"); if (report_fp != NULL) { if (verbose) @@ -351,15 +339,15 @@ break; case 'T': - if ((arg = OPTARG(argc, argv)) != NULL) - phone_num = copy_of(arg); + if (optarg != NULL) + phone_num = copy_of(optarg); else usage(); break; case 'U': - if ((arg = OPTARG(argc, argv)) != NULL) - phone_num2 = copy_of(arg); + if (optarg != NULL) + phone_num2 = copy_of(optarg); else usage(); break; @@ -369,6 +357,8 @@ break; } } + argc -= optind; + argv += optind; /* * Default the report file to the stderr location */ @@ -391,17 +381,15 @@ init(); if (chat_file != NULL) { - arg = ARG(argc, argv); - if (arg != NULL) + if (argc) usage(); else do_file (chat_file); } else { - while ((arg = ARG(argc, argv)) != NULL) { - chat_expect(arg); - - if ((arg = ARG(argc, argv)) != NULL) - chat_send(arg); + for (i = 0; i < argc; i++) { + chat_expect(argv[i]); + if (++i < argc) + chat_send(argv[i]); } } @@ -508,6 +496,7 @@ #endif vfmtmsg(line, sizeof(line), fmt, args); + va_end(args); if (to_log) syslog(LOG_INFO, "%s", line); if (to_stderr) @@ -533,6 +522,7 @@ #endif vfmtmsg(line, sizeof(line), fmt, args); + va_end(args); if (to_log) syslog(LOG_ERR, "%s", line); if (to_stderr) @@ -614,7 +604,7 @@ have_tty_parameters = 1; t.c_iflag |= IGNBRK | ISTRIP | IGNPAR; - t.c_oflag = 0; + t.c_oflag |= OPOST | ONLCR; t.c_lflag = 0; t.c_cc[VERASE] = t.c_cc[VKILL] = 0; @@ -1022,11 +1012,11 @@ c &= 0x7F; if (c < 32) - sprintf(string, "%s^%c", meta, (int)c + '@'); + snprintf(string, sizeof(string), "%s^%c", meta, (int)c + '@'); else if (c == 127) - sprintf(string, "%s^?", meta); + snprintf(string, sizeof(string), "%s^?", meta); else - sprintf(string, "%s%c", meta, c); + snprintf(string, sizeof(string), "%s%c", meta, c); return (string); } @@ -1303,7 +1293,7 @@ if (verbose) { if (quiet) - msgf("send (??????)"); + msgf("send (\?\?\?\?\?\?)"); else msgf("send (%v)", s); } @@ -1445,7 +1435,14 @@ struct tm* tm_now = localtime (&time_now); strftime (report_buffer, 20, "%b %d %H:%M:%S ", tm_now); - strcat (report_buffer, report_string[n]); +#ifdef HAS_STRLFUNCS + strlcat(report_buffer, report_string[n], + sizeof(report_buffer)); +#else + strncat(report_buffer, report_string[n], + sizeof(report_buffer)-1); + report_buffer[sizeof(report_buffer)-1] = '\0'; +#endif report_string[n] = (char *) NULL; report_gathering = 1; @@ -1491,7 +1488,13 @@ alarm(0); alarmed = 0; exit_code = n + 4; - strcpy(fail_reason = fail_buffer, abort_string[n]); +#ifdef HAS_STRLFUNCS + strlcpy(fail_buffer, abort_string[n], sizeof(fail_buffer)); +#else + strncpy(fail_buffer, abort_string[n], sizeof(fail_buffer)-1); + fail_buffer[sizeof(fail_buffer)-1] = '\0'; +#endif + fail_reason = fail_buffer; return (0); } }