From: Dave Hart Date: Wed, 3 Feb 2010 04:58:33 +0000 (+0000) Subject: [Bug 1455] ntpd does not try /etc/ntp.audio as documented. X-Git-Tag: NTP_4_2_7P15~1^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a976a76f18246ee5a82c59a48ab30e35cea81dc;p=thirdparty%2Fntp.git [Bug 1455] ntpd does not try /etc/ntp.audio as documented. Convert many sprintf() calls to snprintf(), also strcpy(), strcat(). Fix widely cut-n-pasted bug in refclock shutdown after failed start. Remove some dead code checking for emalloc() returning NULL. bk: 4b690279XH7_NKsG8uPbWDVeQPvJ1Q --- diff --git a/ChangeLog b/ChangeLog index a004ccc24..7860c17ba 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,12 @@ --- +* [Bug 1455] ntpd does not try /etc/ntp.audio as documented. * [Bug 1467] Fix bogus rebuild of sntp/sntp.html * [Bug 1470] "make distdir" in $srcdir builds keyword-gen, libntp.a. * [Bug 1473] "make distcheck" before build can't make sntp/version.m4. +* Convert many sprintf() calls to snprintf(), also strcpy(), strcat(). +* Fix widely cut-n-pasted bug in refclock shutdown after failed start. +* Remove some dead code checking for emalloc() returning NULL. --- (4.2.6p1-RC3) 2010/01/24 Released by Harlan Stenn diff --git a/lib/isc/win32/interfaceiter.c b/lib/isc/win32/interfaceiter.c index 903fa213d..a6cd7229b 100644 --- a/lib/isc/win32/interfaceiter.c +++ b/lib/isc/win32/interfaceiter.c @@ -373,14 +373,15 @@ internal_current(isc_interfaceiter_t *iter) { if ((flags & IFF_POINTTOPOINT) != 0) { iter->current.flags |= INTERFACE_F_POINTTOPOINT; - sprintf(iter->current.name, "PPP %d", iter->numIF); + snprintf(iter->current.name, sizeof(iter->current.name), + "PPP %d", iter->numIF); ifNamed = TRUE; } if ((flags & IFF_LOOPBACK) != 0) { iter->current.flags |= INTERFACE_F_LOOPBACK; - sprintf(iter->current.name, "v4loop %d", - iter->numIF); + snprintf(iter->current.name, sizeof(iter->current.name), + "v4loop %d", iter->numIF); ifNamed = TRUE; } @@ -408,7 +409,7 @@ internal_current(isc_interfaceiter_t *iter) { &iter->current.netmask); if (ifNamed == FALSE) - sprintf(iter->current.name, + snprintf(iter->current.name, sizeof(iter->current.name), "IPv4 %d", iter->numIF); return (ISC_R_SUCCESS); @@ -468,14 +469,16 @@ internal_current6(isc_interfaceiter_t *iter) { sizeof(iter->current.address.type.in6))) { iter->current.flags |= INTERFACE_F_LOOPBACK; - sprintf(iter->current.name, "v6loop %d", - iter->buf6->iAddressCount - iter->pos6); + snprintf(iter->current.name, sizeof(iter->current.name), + "v6loop %d", + iter->buf6->iAddressCount - iter->pos6); ifNamed = TRUE; } if (ifNamed == FALSE) - sprintf(iter->current.name, "IPv6 %d", - iter->buf6->iAddressCount - iter->pos6); + snprintf(iter->current.name, sizeof(iter->current.name), + "IPv6 %d", + iter->buf6->iAddressCount - iter->pos6); memset(iter->current.netmask.type.in6.s6_addr, 0xff, sizeof(iter->current.netmask.type.in6.s6_addr)); diff --git a/libntp/audio.c b/libntp/audio.c index ab5723eac..a685fe952 100644 --- a/libntp/audio.c +++ b/libntp/audio.c @@ -121,15 +121,18 @@ audio_config_read( FILE *fd; char device[20], line[100], ab[100]; - sprintf(device, "%s%d", INIT_FILE, unit); + snprintf(device, sizeof(device), "%s%d", INIT_FILE, unit); if ((fd = fopen(device, "r")) == NULL) { printf("audio_config_read: <%s> NO\n", device); - sprintf(device, "%s.%d", INIT_FILE, unit); + snprintf(device, sizeof(device), "%s.%d", INIT_FILE, + unit); if ((fd = fopen(device, "r")) == NULL) { printf("audio_config_read: <%s> NO\n", device); - sprintf(device, "%s.%d", INIT_FILE, unit); + snprintf(device, sizeof(device), "%s", + INIT_FILE); if ((fd = fopen(device, "r")) == NULL) { - printf("audio_config_read: <%s> NO\n", device); + printf("audio_config_read: <%s> NO\n", + device); return; } } @@ -233,7 +236,7 @@ audio_init( ; #ifdef PCM_STYLE_SOUND - (void)sprintf(actl_dev, ACTL_DEV, unit); + snprintf(actl_dev, sizeof(actl_dev), ACTL_DEV, unit); audio_config_read(unit, &actl, &dname); /* If we have values for cf_c_dev or cf_i_dev, use them. */ diff --git a/libntp/findconfig.c b/libntp/findconfig.c index ecf6a4bc2..562c01764 100644 --- a/libntp/findconfig.c +++ b/libntp/findconfig.c @@ -28,7 +28,7 @@ FindConfig( /* First choice is my hostname */ if (gethostname(hostname, BUFSIZ) >= 0) { - (void) sprintf(result, "%s/%s", base, hostname); + snprintf(result, sizeof(result), "%s/%s", base, hostname); if (stat(result, &sbuf) == 0) { goto outahere; } else { @@ -39,13 +39,13 @@ FindConfig( cp = unamebuf.machine + 5; else cp = unamebuf.machine; - (void) sprintf(result, "%s/default.%s", base, cp); + snprintf(result, sizeof(result), "%s/default.%s", base, cp); if (stat(result, &sbuf) == 0) { goto outahere; } else { /* Last choice is just default */ - (void) sprintf(result, "%s/default", base); + snprintf(result, sizeof(result), "%s/default", base); if (stat(result, &sbuf) == 0) { goto outahere; } else { diff --git a/libntp/humandate.c b/libntp/humandate.c index e3614cf20..36510c529 100644 --- a/libntp/humandate.c +++ b/libntp/humandate.c @@ -15,19 +15,20 @@ extern const char *months[]; /* prettydate.c */ char * humanlogtime(void) { - char *bp; - time_t cursec = time((time_t *) 0); - struct tm *tm; + char * bp; + time_t cursec; + struct tm * tm; + cursec = time(NULL); tm = localtime(&cursec); if (!tm) return "-- --- --:--:--"; LIB_GETBUF(bp); - (void) sprintf(bp, "%2d %s %02d:%02d:%02d", - tm->tm_mday, months[tm->tm_mon], - tm->tm_hour, tm->tm_min, tm->tm_sec); + snprintf(bp, LIB_BUFLENGTH, "%2d %s %02d:%02d:%02d", + tm->tm_mday, months[tm->tm_mon], + tm->tm_hour, tm->tm_min, tm->tm_sec); return bp; } diff --git a/libntp/modetoa.c b/libntp/modetoa.c index 405aef855..9daf1bb12 100644 --- a/libntp/modetoa.c +++ b/libntp/modetoa.c @@ -24,9 +24,9 @@ modetoa( "bclient", }; - if (mode < 0 || mode >= (sizeof modestrings)/sizeof(char *)) { + if (mode < 0 || mode >= COUNTOF(modestrings)) { LIB_GETBUF(bp); - (void)sprintf(bp, "mode#%d", mode); + snprintf(bp, LIB_BUFLENGTH, "mode#%d", mode); return bp; } diff --git a/libntp/prettydate.c b/libntp/prettydate.c index 09f358fe5..1503a2ce8 100644 --- a/libntp/prettydate.c +++ b/libntp/prettydate.c @@ -170,16 +170,17 @@ common_prettydate( msec = ts->l_uf / 4294967; /* fract / (2 ** 32 / 1000) */ tm = ntp2unix_tm(sec, local); - if (!tm) { - (void) sprintf(bp, "%08lx.%08lx --- --- -- ---- --:--:--", - (u_long)ts->l_ui, (u_long)ts->l_uf); - } - else { - (void) sprintf(bp, "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03lu", - (u_long)ts->l_ui, (u_long)ts->l_uf, days[tm->tm_wday], - months[tm->tm_mon], tm->tm_mday, 1900 + tm->tm_year, - tm->tm_hour,tm->tm_min, tm->tm_sec, msec); - } + if (!tm) + snprintf(bp, LIB_BUFLENGTH, + "%08lx.%08lx --- --- -- ---- --:--:--", + (u_long)ts->l_ui, (u_long)ts->l_uf); + else + snprintf(bp, LIB_BUFLENGTH, + "%08lx.%08lx %s, %s %2d %4d %2d:%02d:%02d.%03lu", + (u_long)ts->l_ui, (u_long)ts->l_uf, + days[tm->tm_wday], months[tm->tm_mon], + tm->tm_mday, 1900 + tm->tm_year, tm->tm_hour, + tm->tm_min, tm->tm_sec, msec); return bp; } diff --git a/libntp/uglydate.c b/libntp/uglydate.c index 676a5fdde..b2da9c0a6 100644 --- a/libntp/uglydate.c +++ b/libntp/uglydate.c @@ -41,8 +41,10 @@ uglydate( while (year >= 100) year -= 100; } - (void) sprintf(bp, "%17s %02d:%03d:%02d:%02d:%02d.%03ld", - timep, year, tm->tm_yday, tm->tm_hour, tm->tm_min, - tm->tm_sec, msec); + snprintf(bp, LIB_BUFLENGTH, + "%17s %02d:%03d:%02d:%02d:%02d.%03ld", timep, year, + tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, + msec); + return bp; } diff --git a/libntp/uinttoa.c b/libntp/uinttoa.c index be48ea514..ab63926cd 100644 --- a/libntp/uinttoa.c +++ b/libntp/uinttoa.c @@ -14,7 +14,7 @@ uinttoa( register char *buf; LIB_GETBUF(buf); + snprintf(buf, LIB_BUFLENGTH, "%lu", uval); - (void) sprintf(buf, "%lu", (u_long)uval); return buf; } diff --git a/ntpd/ntp_control.c b/ntpd/ntp_control.c index 7a32ddc38..d87b5afbf 100644 --- a/ntpd/ntp_control.c +++ b/ntpd/ntp_control.c @@ -1080,9 +1080,9 @@ ctl_putdbl( while (*cq != '\0') *cp++ = *cq++; *cp++ = '='; - (void)sprintf(cp, "%.3f", ts); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "%.3f", ts); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)(cp - buffer), 0); } @@ -1105,9 +1105,9 @@ ctl_putuint( *cp++ = *cq++; *cp++ = '='; - (void) sprintf(cp, "%lu", uval); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "%lu", uval); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)( cp - buffer ), 0); } @@ -1134,13 +1134,13 @@ ctl_putfs( *cp++ = '='; fstamp = uval - JAN_1970; tm = gmtime(&fstamp); - if (tm == NULL) + if (NULL == tm) return; - - sprintf(cp, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, - tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), + "%04d%02d%02d%02d%02d", tm->tm_year + 1900, + tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)( cp - buffer ), 0); } @@ -1165,9 +1165,9 @@ ctl_puthex( *cp++ = *cq++; *cp++ = '='; - (void) sprintf(cp, "0x%lx", uval); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%lx", uval); + cp += strlen(cp); ctl_putdata(buffer,(unsigned)( cp - buffer ), 0); } @@ -1191,9 +1191,9 @@ ctl_putint( *cp++ = *cq++; *cp++ = '='; - (void) sprintf(cp, "%ld", ival); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "%ld", ival); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)( cp - buffer ), 0); } @@ -1217,10 +1217,10 @@ ctl_putts( *cp++ = *cq++; *cp++ = '='; - (void) sprintf(cp, "0x%08lx.%08lx", ts->l_ui & 0xffffffffUL, - ts->l_uf & 0xffffffffUL); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "0x%08lx.%08lx", + ts->l_ui & 0xffffffffUL, ts->l_uf & 0xffffffffUL); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)( cp - buffer ), 0); } @@ -1245,12 +1245,13 @@ ctl_putadr( *cp++ = *cq++; *cp++ = '='; - if (addr == NULL) + if (NULL == addr) cq = numtoa(addr32); else cq = stoa(addr); - while (*cq != '\0') - *cp++ = *cq++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "%s", cq); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)(cp - buffer), 0); } @@ -1273,9 +1274,9 @@ ctl_putid( *cp++ = *cq++; *cp++ = '='; - cq = id; - while (*cq != '\0' && (cq - id) < 4) - *cp++ = *cq++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), "%s", id); + cp += strlen(cp); ctl_putdata(buffer, (unsigned)( cp - buffer ), 0); } @@ -1303,9 +1304,10 @@ ctl_putarray( if (i == 0) i = NTP_SHIFT; i--; - (void)sprintf(cp, " %.2f", arr[i] * 1e3); - while (*cp != '\0') - cp++; + NTP_INSIST((cp - buffer) < sizeof(buffer)); + snprintf(cp, sizeof(buffer) - (cp - buffer), + " %.2f", arr[i] * 1e3); + cp += strlen(cp); } while(i != start); ctl_putdata(buffer, (unsigned)(cp - buffer), 0); } @@ -1410,7 +1412,8 @@ ctl_putsys( ctl_putstr(sys_var[CS_SYSTEM].text, str_system, sizeof(str_system) - 1); #else - sprintf(str, "%s/%s", utsnamebuf.sysname, utsnamebuf.release); + snprintf(str, sizeof(str), "%s/%s", utsnamebuf.sysname, + utsnamebuf.release); ctl_putstr(sys_var[CS_SYSTEM].text, str, strlen(str)); #endif /* HAVE_UNAME */ break; diff --git a/ntpd/refclock_arbiter.c b/ntpd/refclock_arbiter.c index f360d37fc..35bee70dd 100644 --- a/ntpd/refclock_arbiter.c +++ b/ntpd/refclock_arbiter.c @@ -147,25 +147,23 @@ arb_start( /* * Open serial port. Use CLK line discipline, if available. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if (!(fd = refclock_open(device, SPEED232, LDISC_CLK))) return (0); /* * Allocate and initialize unit structure */ - if (!(up = (struct arbunit *)emalloc(sizeof(struct arbunit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct arbunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = arb_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -196,8 +194,10 @@ arb_shutdown( pp = peer->procptr; up = (struct arbunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_arc.c b/ntpd/refclock_arc.c index 7f1fcbf50..e0a2afeb4 100644 --- a/ntpd/refclock_arc.c +++ b/ntpd/refclock_arc.c @@ -646,7 +646,7 @@ arc_start( /* * Open serial port. Use CLK line discipline, if available. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if (!(fd = refclock_open(device, SPEED, LDISC_CLK))) return(0); #ifdef DEBUG @@ -690,16 +690,20 @@ arc_start( #endif - up = (struct arcunit *) emalloc(sizeof(struct arcunit)); - if(!up) { (void) close(fd); return(0); } + up = emalloc(sizeof(*up)); /* Set structure to all zeros... */ - memset((char *)up, 0, sizeof(struct arcunit)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = arc_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; - if(!io_addclock(&pp->io)) { (void) close(fd); free(up); return(0); } + if (!io_addclock(&pp->io)) { + close(fd); + pp->io.fd = -1; + free(up); + return(0); + } pp->unitptr = (caddr_t)up; /* @@ -772,8 +776,10 @@ arc_shutdown( pp = peer->procptr; up = (struct arcunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } /* diff --git a/ntpd/refclock_as2201.c b/ntpd/refclock_as2201.c index a48d65319..163c2afee 100644 --- a/ntpd/refclock_as2201.c +++ b/ntpd/refclock_as2201.c @@ -165,26 +165,23 @@ as2201_start( /* * Open serial port. Use CLK line discipline, if available. */ - (void)sprintf(gpsdev, DEVICE, unit); + snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit); if (!(fd = refclock_open(gpsdev, SPEED232, LDISC_CLK))) return (0); /* * Allocate and initialize unit structure */ - if (!(up = (struct as2201unit *) - emalloc(sizeof(struct as2201unit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct as2201unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = as2201_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -217,8 +214,10 @@ as2201_shutdown( pp = peer->procptr; up = (struct as2201unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_atom.c b/ntpd/refclock_atom.c index 569c16a6e..d8a9d3e27 100644 --- a/ntpd/refclock_atom.c +++ b/ntpd/refclock_atom.c @@ -140,11 +140,11 @@ atom_start( * Open PPS device. This can be any serial or parallel port and * not necessarily the port used for the associated radio. */ - sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); up->fddev = tty_open(device, O_RDWR, 0777); if (up->fddev <= 0) { msyslog(LOG_ERR, - "refclock_atom: %s: %m", device); + "refclock_atom: %s: %m", device); return (0); } @@ -199,7 +199,8 @@ atom_timer( * That's so we can make awesome Allan deviation plots. */ if (pp->sloppyclockflag & CLK_FLAG4) { - sprintf(tbuf, "%.9f", pp->filter[pp->coderecv]); + snprintf(tbuf, sizeof(tbuf), "%.9f", + pp->filter[pp->coderecv]); record_clock_stats(&peer->srcadr, tbuf); } } diff --git a/ntpd/refclock_bancomm.c b/ntpd/refclock_bancomm.c index 0e9ceb002..3bb0ffea4 100644 --- a/ntpd/refclock_bancomm.c +++ b/ntpd/refclock_bancomm.c @@ -312,8 +312,10 @@ vme_shutdown( vme = (struct vmeunit *)pp->unitptr; io_closeclock(&pp->io); pp->unitptr = NULL; - free(vme); - if (tfp_type == 2) bcStopPci(stfp_handle); + if (NULL != vme) + free(vme); + if (tfp_type == 2) + bcStopPci(stfp_handle); } @@ -368,14 +370,15 @@ vme_poll( tadr = gmtime(&tloc); tptr->year = (unsigned short)(tadr->tm_year + 1900); - sprintf(pp->a_lastcode, - "%3.3d %2.2d:%2.2d:%2.2d.%.6ld %1d", - tptr->day, - tptr->hr, - tptr->mn, - tptr->sec, - tptr->frac, - tptr->status); + snprintf(pp->a_lastcode, + sizeof(pp->a_lastcode), + "%3.3d %2.2d:%2.2d:%2.2d.%.6ld %1d", + tptr->day, + tptr->hr, + tptr->mn, + tptr->sec, + tptr->frac, + tptr->status); pp->lencode = (u_short) strlen(pp->a_lastcode); @@ -465,37 +468,41 @@ get_datumtime(struct vmedate *time_vme) default: /* legacy bancomm card */ if (ioctl(fd_vme, READTIME, &vts)) { - msyslog(LOG_ERR, "get_datumtime error: %m"); + msyslog(LOG_ERR, + "get_datumtime error: %m"); return(NULL); } /* Get day */ - sprintf(cbuf,"%3.3x", ((vts.btfp_time[ 0 ] & 0x000f) <<8) + - ((vts.btfp_time[ 1 ] & 0xff00) >> 8)); + snprintf(cbuf, sizeof(cbuf), "%3.3x", + ((vts.btfp_time[ 0 ] & 0x000f) << 8) + + ((vts.btfp_time[ 1 ] & 0xff00) >> 8)); time_vme->day = (unsigned short)atoi(cbuf); /* Get hour */ - sprintf(cbuf,"%2.2x", vts.btfp_time[ 1 ] & 0x00ff); - + snprintf(cbuf, sizeof(cbuf), "%2.2x", + vts.btfp_time[ 1 ] & 0x00ff); time_vme->hr = (unsigned short)atoi(cbuf); /* Get minutes */ - sprintf(cbuf,"%2.2x", (vts.btfp_time[ 2 ] & 0xff00) >>8); + snprintf(cbuf, sizeof(cbuf), "%2.2x", + (vts.btfp_time[ 2 ] & 0xff00) >> 8); time_vme->mn = (unsigned short)atoi(cbuf); /* Get seconds */ - sprintf(cbuf,"%2.2x", vts.btfp_time[ 2 ] & 0x00ff); + snprintf(cbuf, sizeof(cbuf), "%2.2x", + vts.btfp_time[ 2 ] & 0x00ff); time_vme->sec = (unsigned short)atoi(cbuf); /* Get microseconds. Yes, we ignore the 0.1 microsecond digit so we can use the TVTOTSF function later on...*/ - sprintf(cbuf,"%4.4x%2.2x", vts.btfp_time[ 3 ], - vts.btfp_time[ 4 ]>>8); - + snprintf(cbuf, sizeof(cbuf), "%4.4x%2.2x", + vts.btfp_time[ 3 ], + vts.btfp_time[ 4 ] >> 8); time_vme->frac = (u_long) atoi(cbuf); /* Get status bit */ - time_vme->status = (vts.btfp_time[0] & 0x0010) >>4; + time_vme->status = (vts.btfp_time[0] & 0x0010) >> 4; break; } diff --git a/ntpd/refclock_chronolog.c b/ntpd/refclock_chronolog.c index d7dace058..ba4f60b4e 100644 --- a/ntpd/refclock_chronolog.c +++ b/ntpd/refclock_chronolog.c @@ -104,7 +104,7 @@ chronolog_start( * Open serial port. Don't bother with CLK line discipline, since * it's not available. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); #ifdef DEBUG if (debug) printf ("starting Chronolog with device %s\n",device); @@ -115,12 +115,8 @@ chronolog_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct chronolog_unit *) - emalloc(sizeof(struct chronolog_unit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct chronolog_unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = chronolog_receive; @@ -128,8 +124,10 @@ chronolog_start( pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -157,8 +155,10 @@ chronolog_shutdown( pp = peer->procptr; up = (struct chronolog_unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_chu.c b/ntpd/refclock_chu.c index cce406f72..de3488631 100644 --- a/ntpd/refclock_chu.c +++ b/ntpd/refclock_chu.c @@ -489,7 +489,7 @@ chu_start( if (fd_audio > 0) { fd = fd_audio; } else { - sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); fd = refclock_open(device, SPEED232, LDISC_RAW); } #else /* HAVE_AUDIO */ @@ -497,7 +497,7 @@ chu_start( /* * Open serial port in raw mode. */ - sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); fd = refclock_open(device, SPEED232, LDISC_RAW); #endif /* HAVE_AUDIO */ if (fd < 0) @@ -506,12 +506,8 @@ chu_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct chuunit *) - emalloc(sizeof(struct chuunit)))) { - close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct chuunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = chu_receive; @@ -520,7 +516,9 @@ chu_start( pp->io.fd = fd; if (!io_addclock(&pp->io)) { close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -1103,6 +1101,9 @@ chu_b( u_char code[11]; /* decoded timecode */ char tbuf[80]; /* trace buffer */ + char * p; + size_t chars; + size_t cb; int i; pp = peer->procptr; @@ -1115,10 +1116,20 @@ chu_b( * only if the distance is 40. Note that once a valid frame has * been found errors are ignored. */ - sprintf(tbuf, "chuB %04x %4.0f %2d %2d ", up->status, - up->maxsignal, nchar, -up->burdist); - for (i = 0; i < nchar; i++) - sprintf(&tbuf[strlen(tbuf)], "%02x", up->cbuf[i]); + snprintf(tbuf, sizeof(tbuf), "chuB %04x %4.0f %2d %2d ", + up->status, up->maxsignal, nchar, -up->burdist); + cb = sizeof(tbuf); + p = tbuf; + for (i = 0; i < nchar; i++) { + chars = strlen(p); + if (cb < chars + 1) { + msyslog(LOG_ERR, "chu_b() fatal out buffer"); + exit(1); + } + cb -= chars; + p += chars; + snprintf(p, cb, "%02x", up->cbuf[i]); + } if (pp->sloppyclockflag & CLK_FLAG4) record_clock_stats(&peer->srcadr, tbuf); #ifdef DEBUG @@ -1163,6 +1174,9 @@ chu_a( struct chuunit *up; char tbuf[80]; /* trace buffer */ + char * p; + size_t chars; + size_t cb; l_fp offset; /* timestamp offset */ int val; /* distance */ int temp; @@ -1205,12 +1219,22 @@ chu_a( if (temp < 2 || temp > 9 || k + 9 >= nchar || temp != ((up->cbuf[k + 9] >> 4) & 0xf)) temp = 0; - sprintf(tbuf, "chuA %04x %4.0f %2d %2d %2d %2d %1d ", - up->status, up->maxsignal, nchar, up->burdist, k, - up->syndist, temp); - for (i = 0; i < nchar; i++) - sprintf(&tbuf[strlen(tbuf)], "%02x", - up->cbuf[i]); + snprintf(tbuf, sizeof(tbuf), + "chuA %04x %4.0f %2d %2d %2d %2d %1d ", up->status, + up->maxsignal, nchar, up->burdist, k, up->syndist, + temp); + cb = sizeof(tbuf); + p = tbuf; + for (i = 0; i < nchar; i++) { + chars = strlen(p); + if (cb < chars + 1) { + msyslog(LOG_ERR, "chu_a() fatal out buffer"); + exit(1); + } + cb -= chars; + p += chars; + snprintf(p, cb, "%02x", up->cbuf[i]); + } if (pp->sloppyclockflag & CLK_FLAG4) record_clock_stats(&peer->srcadr, tbuf); #ifdef DEBUG @@ -1354,7 +1378,7 @@ chu_second( } else { pp->leap = LEAP_NOWARNING; } - sprintf(pp->a_lastcode, + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), "%c%1X %04d %03d %02d:%02d:%02d %c%x %+d %d %d %s %.0f %d", synchar, qual, pp->year, pp->day, pp->hour, pp->minute, pp->second, leapchar, up->dst, up->dut, minset, up->gain, @@ -1565,7 +1589,7 @@ chu_newchan( */ rval = icom_freq(up->fd_icom, peer->ttl & 0x7f, qsy[up->chan] + TUNE); - sprintf(up->ident, "CHU%d", up->chan); + snprintf(up->ident, sizeof(up->ident), "CHU%d", up->chan); memcpy(&pp->refid, up->ident, 4); memcpy(&peer->refid, up->ident, 4); if (metric == 0 && up->status & METRIC) { diff --git a/ntpd/refclock_dumbclock.c b/ntpd/refclock_dumbclock.c index d85849eac..32e56f83d 100644 --- a/ntpd/refclock_dumbclock.c +++ b/ntpd/refclock_dumbclock.c @@ -115,7 +115,7 @@ dumbclock_start( * Open serial port. Don't bother with CLK line discipline, since * it's not available. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); #ifdef DEBUG if (debug) printf ("starting Dumbclock with device %s\n",device); @@ -127,8 +127,8 @@ dumbclock_start( /* * Allocate and initialize unit structure */ - up = (struct dumbclock_unit *)emalloc(sizeof(struct dumbclock_unit)); - memset((char *)up, 0, sizeof(struct dumbclock_unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = dumbclock_receive; @@ -136,8 +136,10 @@ dumbclock_start( pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -177,8 +179,10 @@ dumbclock_shutdown( pp = peer->procptr; up = (struct dumbclock_unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_gpsvme.c b/ntpd/refclock_gpsvme.c index 9f9e85c9f..c3322fdeb 100644 --- a/ntpd/refclock_gpsvme.c +++ b/ntpd/refclock_gpsvme.c @@ -104,13 +104,9 @@ psc_start( return 0; } - if (!up) { - msyslog(LOG_ERR, "psc_start: unit: %d, emalloc: %m", unit); - return 0; - } memset(up, '\0', sizeof *up); - sprintf(buf, DEVICE, unit); /* dev file name */ + snprintf(buf, sizeof(buf), DEVICE, unit); /* dev file name */ fd[unit] = open(buf, O_RDONLY); /* open device file */ if (fd[unit] < 0) { msyslog(LOG_ERR, "psc_start: unit: %d, open failed. %m", unit); @@ -131,7 +127,7 @@ psc_start( pp->io.fd = -1; pp->unitptr = (caddr_t) up; get_systime(&pp->lastrec); - memcpy((char *)&pp->refid, REFID, 4); + memcpy(&pp->refid, REFID, 4); peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; up->unit = unit; @@ -149,8 +145,10 @@ psc_shutdown( struct peer *peer ) { - free(peer->procptr->unitptr); - close(fd[unit]); + if (NULL != peer->procptr->unitptr) + free(peer->procptr->unitptr); + if (fd[unit] > 0) + close(fd[unit]); } /* psc_poll: read, decode, and record device time */ @@ -196,9 +194,10 @@ psc_poll( pp->nsec = 1000000*BCD2INT3((tlo & 0x00FFF000) >> 12) + BCD2INT3(tlo & 0x00000FFF); - sprintf(pp->a_lastcode, "%3.3d %2.2d:%2.2d:%2.2d.%09ld %02x %08x %08x", - pp->day, pp->hour, pp->minute, pp->second, pp->nsec, status, thi, - tlo); + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), + "%3.3d %2.2d:%2.2d:%2.2d.%09ld %02x %08x %08x", pp->day, + pp->hour, pp->minute, pp->second, pp->nsec, status, thi, + tlo); pp->lencode = strlen(pp->a_lastcode); /* compute the timecode timestamp */ diff --git a/ntpd/refclock_heath.c b/ntpd/refclock_heath.c index f9b2b3e64..cd74eca31 100644 --- a/ntpd/refclock_heath.c +++ b/ntpd/refclock_heath.c @@ -221,7 +221,7 @@ heath_start( /* * Open serial port */ - sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if (!(fd = refclock_open(device, speed[peer->ttl & 0x3], LDISC_REMOTE))) return (0); @@ -231,7 +231,8 @@ heath_start( pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; return (0); } @@ -241,7 +242,7 @@ heath_start( peer->precision = PRECISION; peer->burst = NSTAGE; pp->clockdesc = DESCRIPTION; - memcpy((char *)&pp->refid, REFID, 4); + memcpy(&pp->refid, REFID, 4); return (1); } @@ -258,7 +259,8 @@ heath_shutdown( struct refclockproc *pp; pp = peer->procptr; - io_closeclock(&pp->io); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); } diff --git a/ntpd/refclock_hopfpci.c b/ntpd/refclock_hopfpci.c index 81c186d16..284e3011c 100644 --- a/ntpd/refclock_hopfpci.c +++ b/ntpd/refclock_hopfpci.c @@ -124,24 +124,17 @@ hopfpci_start( /* * Allocate and initialize unit structure */ - up = (struct hopfclock_unit *) emalloc(sizeof(struct hopfclock_unit)); - - if (!(up)) { - msyslog(LOG_ERR, "hopfPCIClock(%d) emalloc: %m",unit); -#ifdef DEBUG - printf("hopfPCIClock(%d) emalloc\n",unit); -#endif - return (0); - } - memset((char *)up, 0, sizeof(struct hopfclock_unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); #ifndef SYS_WINNT fd = open(DEVICE,O_RDWR); /* try to open hopf clock device */ #else - if (!OpenHopfDevice()){ - msyslog(LOG_ERR,"Start: %s unit: %d failed!",DEVICE,unit); + if (!OpenHopfDevice()) { + msyslog(LOG_ERR, "Start: %s unit: %d failed!", DEVICE, unit); + free(up); return (0); } #endif @@ -158,17 +151,12 @@ hopfpci_start( /* * Initialize miscellaneous peer variables */ - if (pp->unitptr!=0) { - memcpy((char *)&pp->refid, REFID, 4); - peer->precision = PRECISION; - pp->clockdesc = DESCRIPTION; - up->leap_status = 0; - up->unit = (short) unit; - return (1); - } - else { - return 0; - } + memcpy((char *)&pp->refid, REFID, 4); + peer->precision = PRECISION; + pp->clockdesc = DESCRIPTION; + up->leap_status = 0; + up->unit = (short) unit; + return (1); } @@ -187,6 +175,8 @@ hopfpci_shutdown( #else CloseHopfDevice(); #endif + if (NULL != peer->procptr->unitptr) + free(peer->procptr->unitptr); } @@ -221,9 +211,11 @@ hopfpci_poll( else pp->leap = LEAP_NOWARNING; - sprintf(pp->a_lastcode,"ST: %02X T: %02d:%02d:%02d.%03ld D: %02d.%02d.%04d", - m_time.wStatus, pp->hour, pp->minute, pp->second, - pp->nsec / 1000000, m_time.wDay, m_time.wMonth, m_time.wYear); + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), + "ST: %02X T: %02d:%02d:%02d.%03ld D: %02d.%02d.%04d", + m_time.wStatus, pp->hour, pp->minute, pp->second, + pp->nsec / 1000000, m_time.wDay, m_time.wMonth, + m_time.wYear); pp->lencode = (u_short)strlen(pp->a_lastcode); get_systime(&pp->lastrec); diff --git a/ntpd/refclock_hopfser.c b/ntpd/refclock_hopfser.c index a94402a37..48eea0022 100644 --- a/ntpd/refclock_hopfser.c +++ b/ntpd/refclock_hopfser.c @@ -127,7 +127,7 @@ hopfserial_start ( int fd; char gpsdev[20]; - (void) sprintf(gpsdev, DEVICE, unit); + snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit); /* LDISC_STD, LDISC_RAW * Open serial port. Use CLK line discipline, if available. @@ -146,18 +146,8 @@ hopfserial_start ( /* * Allocate and initialize unit structure */ - up = (struct hopfclock_unit *) emalloc(sizeof(struct hopfclock_unit)); - - if (!(up)) { - msyslog(LOG_ERR, "hopfSerialClock(%d) emalloc: %m",unit); -#ifdef DEBUG - printf("hopfSerialClock(%d) emalloc\n",unit); -#endif - (void) close(fd); - return (0); - } - - memset((char *)up, 0, sizeof(struct hopfclock_unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = hopfserial_receive; @@ -166,10 +156,12 @@ hopfserial_start ( pp->io.fd = fd; if (!io_addclock(&pp->io)) { #ifdef DEBUG - printf("hopfSerialClock(%d) io_addclock\n",unit); + printf("hopfSerialClock(%d) io_addclock\n", unit); #endif - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -202,8 +194,11 @@ hopfserial_shutdown ( pp = peer->procptr; up = (struct hopfclock_unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } @@ -289,7 +284,7 @@ hopfserial_receive ( /* preparation for timecode ntpq rl command ! */ #if 0 - wsprintf(pp->a_lastcode, + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), "STATUS: %1X%1X, DATE: %02d.%02d.%04d TIME: %02d:%02d:%02d", synch, DoW, diff --git a/ntpd/refclock_hpgps.c b/ntpd/refclock_hpgps.c index 08f709fd3..5867a1d7b 100644 --- a/ntpd/refclock_hpgps.c +++ b/ntpd/refclock_hpgps.c @@ -162,7 +162,7 @@ hpgps_start( * Open serial port. Use CLK line discipline, if available. * Default is HP 58503A, mode arg selects HP Z3801A */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); /* mode parameter to server config line shares ttl slot */ if ((peer->ttl == 1)) { if (!(fd = refclock_open(device, SPEED232Z, @@ -175,19 +175,16 @@ hpgps_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct hpgpsunit *) - emalloc(sizeof(struct hpgpsunit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct hpgpsunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = hpgps_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -232,8 +229,10 @@ hpgps_shutdown( pp = peer->procptr; up = (struct hpgpsunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_irig.c b/ntpd/refclock_irig.c index 0e6cfca9b..8ff17ccf9 100644 --- a/ntpd/refclock_irig.c +++ b/ntpd/refclock_irig.c @@ -332,12 +332,8 @@ irig_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct irigunit *) - emalloc(sizeof(struct irigunit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct irigunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = irig_receive; @@ -345,8 +341,10 @@ irig_start( pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void)close(fd); + close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -371,7 +369,7 @@ irig_start( for (i = 3; i < OFFSET; i++) { up->comp[i] = up->comp[i - 1] + step; up->comp[OFFSET + i] = -up->comp[i]; - if (i % 16 == 0) + if (i % 16 == 0) step *= 2.; } DTOLFP(1. / SECOND, &up->tick); @@ -393,8 +391,10 @@ irig_shutdown( pp = peer->procptr; up = (struct irigunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } @@ -943,7 +943,7 @@ irig_decode( refclock_report(peer, CEVNT_BADTIME); } - sprintf(pp->a_lastcode, + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), "%02x %02d %03d %02d:%02d:%02d %4.0f %3d %6.3f %2d %6.2f %6.1f %s", up->errflg, pp->year, pp->day, pp->hour, pp->minute, pp->second, diff --git a/ntpd/refclock_jjy.c b/ntpd/refclock_jjy.c index 1ac09d50f..13f56defd 100644 --- a/ntpd/refclock_jjy.c +++ b/ntpd/refclock_jjy.c @@ -250,10 +250,8 @@ jjy_start ( int unit, struct peer *peer ) /* * Open serial port */ - if ( ! ( pDeviceName = (char*) emalloc ( strlen(DEVICE) + 10 ) ) ) { - return RC_START_ERROR ; - } - sprintf ( pDeviceName, DEVICE, unit ) ; + pDeviceName = emalloc ( strlen(DEVICE) + 10 ); + snprintf ( pDeviceName, strlen(DEVICE) + 10, DEVICE, unit ) ; /* * peer->ttl is a mode number specified by "127.127.40.X mode N" in the ntp.conf @@ -292,12 +290,8 @@ jjy_start ( int unit, struct peer *peer ) /* * Allocate and initialize unit structure */ - if ( ! ( up = (struct jjyunit *) emalloc (sizeof(struct jjyunit)) ) ) { - close ( fd ) ; - return RC_START_ERROR ; - } - - memset ( (char*)up, 0, sizeof(struct jjyunit) ) ; + up = emalloc (sizeof(*up)); + memset ( up, 0, sizeof(*up) ) ; up->linediscipline = iDiscipline ; /* @@ -325,8 +319,8 @@ jjy_start ( int unit, struct peer *peer ) up->unittype = UNITTYPE_ECHOKEISOKUKI_LT2000 ; up->operationmode = 2 ; /* Mode 2 : Continuous mode */ up->lineexpect = 1 ; - switch ( up->operationmode ) { - case 1 : + switch ( up->operationmode ) { + case 1 : up->charexpect[0] = 15 ; /* YYMMDDWHHMMSS */ break ; case 2 : @@ -334,11 +328,11 @@ jjy_start ( int unit, struct peer *peer ) break ; } break ; - case 4 : - up->unittype = UNITTYPE_CITIZENTIC_JJY200 ; - up->lineexpect = 1 ; - up->charexpect[0] = 23 ; /* 'XX YY/MM/DD W HH:MM:SS */ - break ; + case 4 : + up->unittype = UNITTYPE_CITIZENTIC_JJY200 ; + up->lineexpect = 1 ; + up->charexpect[0] = 23 ; /* 'XX YY/MM/DD W HH:MM:SS */ + break ; default : msyslog ( LOG_ERR, "JJY receiver [ %s mode %d ] : Unsupported mode", ntoa(&peer->srcadr), peer->ttl ) ; @@ -355,7 +349,9 @@ jjy_start ( int unit, struct peer *peer ) pp->io.fd = fd ; if ( ! io_addclock(&pp->io) ) { close ( fd ) ; - free ( (void*) up ) ; + pp->io.fd = -1 ; + free ( up ) ; + pp->unitptr = NULL ; return RC_START_ERROR ; } @@ -384,8 +380,10 @@ jjy_shutdown ( int unit, struct peer *peer ) pp = peer->procptr ; up = (struct jjyunit *) pp->unitptr ; - io_closeclock ( &pp->io ) ; - free ( (void*) up ) ; + if ( -1 != pp->io.fd ) + io_closeclock ( &pp->io ) ; + if ( NULL != up ) + free ( up ) ; } @@ -546,8 +544,10 @@ jjy_receive ( struct recvbuf *rbufp ) * timecode timestamp. */ - sprintf ( sLogText, "%04d/%02d/%02d %02d:%02d:%02d.%1d JST", - up->year, up->month, up->day, up->hour, up->minute, up->second, up->msecond/100 ) ; + snprintf ( sLogText, sizeof(sLogText), + "%04d/%02d/%02d %02d:%02d:%02d.%1d JST", + up->year, up->month, up->day, + up->hour, up->minute, up->second, up->msecond/100 ) ; record_clock_stats ( &peer->srcadr, sLogText ) ; if ( ! refclock_process ( pp ) ) { diff --git a/ntpd/refclock_jupiter.c b/ntpd/refclock_jupiter.c index 583e31bc0..844d24f59 100644 --- a/ntpd/refclock_jupiter.c +++ b/ntpd/refclock_jupiter.c @@ -188,7 +188,7 @@ jupiter_start( /* * Open serial port */ - (void)sprintf(gpsdev, DEVICE, unit); + snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit); fd = refclock_open(gpsdev, SPEED232, LDISC_RAW); if (fd == 0) { jupiter_debug(peer, "jupiter_start", "open %s: %s", @@ -197,12 +197,8 @@ jupiter_start( } /* Allocate unit structure */ - if ((instance = (struct instance *) - emalloc(sizeof(struct instance))) == NULL) { - (void) close(fd); - return (0); - } - memset((char *)instance, 0, sizeof(struct instance)); + instance = emalloc(sizeof(*instance)); + memset(instance, 0, sizeof(*instance)); instance->peer = peer; pp = peer->procptr; pp->io.clock_recv = jupiter_receive; @@ -210,7 +206,7 @@ jupiter_start( pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); free(instance); return (0); } @@ -934,10 +930,10 @@ jupiter_send(struct instance *instance, struct jheader *hp) } if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) { - (void)sprintf(errstr, "write: %s", strerror(errno)); + snprintf(errstr, sizeof(errstr), "write: %s", strerror(errno)); return (errstr); } else if (cc != size) { - (void)sprintf(errstr, "short write (%d != %d)", cc, size); + snprintf(errstr, sizeof(errstr), "short write (%d != %d)", cc, size); return (errstr); } return (NULL); diff --git a/ntpd/refclock_leitch.c b/ntpd/refclock_leitch.c index fe6f7725d..52b65085c 100644 --- a/ntpd/refclock_leitch.c +++ b/ntpd/refclock_leitch.c @@ -150,9 +150,17 @@ leitch_shutdown( struct peer *peer ) { + struct leitchunit *leitch; + + if (unit >= MAXUNITS) { + return; + } + leitch = &leitchunits[unit]; + if (-1 != leitch->leitchio.fd) + io_closeclock(&leitch->leitchio); #ifdef DEBUG if (debug) - fprintf(stderr, "leitch_shutdown()\n"); + fprintf(stderr, "leitch_shutdown()\n"); #endif } @@ -258,7 +266,7 @@ leitch_start( /* * Open serial port. */ - (void) sprintf(leitchdev, LEITCH232, unit); + snprintf(leitchdev, sizeof(leitchdev), LEITCH232, unit); fd232 = open(leitchdev, O_RDWR, 0777); if (fd232 == -1) { msyslog(LOG_ERR, @@ -267,7 +275,7 @@ leitch_start( } leitch = &leitchunits[unit]; - memset((char*)leitch, 0, sizeof(*leitch)); + memset(leitch, 0, sizeof(*leitch)); #if defined(HAVE_SYSV_TTYS) /* @@ -387,6 +395,7 @@ leitch_start( leitch->leitchio.datalen = 0; leitch->leitchio.fd = fd232; if (!io_addclock(&leitch->leitchio)) { + leitch->leitchio.fd = -1; goto screwed; } diff --git a/ntpd/refclock_msfees.c b/ntpd/refclock_msfees.c index 98034b587..1b25c1c38 100644 --- a/ntpd/refclock_msfees.c +++ b/ntpd/refclock_msfees.c @@ -382,11 +382,15 @@ dump_buf( int i; register char *ptr = buff; - sprintf(ptr, text); - for (i=from; i DUMP_BUF_SIZE) msyslog(LOG_DEBUG, "D: %s", ptr=buff); - sprintf(ptr, " %06d", ((int)coffs[i].l_f) / 4295); + snprintf(buff, sizeof(buff), text); + for (i = from; i < to; i++) { + ptr += strlen(ptr); + if ((ptr - buff) > DUMP_BUF_SIZE) { + msyslog(LOG_DEBUG, "D: %s", buff); + ptr = buff; + } + snprintf(ptr, sizeof(buff) - (ptr - buff), + " %06d", ((int)coffs[i].l_f) / 4295); } msyslog(LOG_DEBUG, "D: %s", buff); } @@ -450,7 +454,7 @@ msfees_start( /* Unit okay, attempt to open the devices. We do them both at * once to make sure we can */ - (void) sprintf(eesdev, EES232, unit); + snprintf(eesdev, sizeof(eesdev), EES232, unit); fd232 = open(eesdev, O_RDWR, 0777); if (fd232 == -1) { @@ -987,17 +991,20 @@ ees_receive( /* Dump the deltas each minute */ if (dbg & DB_DUMP_DELTAS) - { if (/*0 <= ees->second && */ - ees->second < ((sizeof deltas) / (sizeof deltas[0]))) deltas[ees->second] = delta_sfsec; + { + if (/*0 <= ees->second && */ + ees->second < COUNTOF(deltas)) + deltas[ees->second] = delta_sfsec; /* Dump on second 1, as second 0 sometimes missed */ if (ees->second == 1) { - char text[16 * ((sizeof deltas) / (sizeof deltas[0]))]; + char text[16 * COUNTOF(deltas)]; char *cptr=text; int i; - for (i=0; i<((sizeof deltas) / (sizeof deltas[0])); i++) { - sprintf(cptr, " %d.%04d", - msec(deltas[i]), subms(deltas[i])); - while (*cptr) cptr++; + for (i = 0; i < COUNTOF(deltas); i++) { + snprintf(cptr, sizeof(text) / COUNTOF(deltas), + " %d.%04d", msec(deltas[i]), + subms(deltas[i])); + cptr += strlen(cptr); } msyslog(LOG_ERR, "Deltas: %d.%04d<->%d.%04d: %s", msec(EES_STEP_F - EES_STEP_F_GRACE), subms(EES_STEP_F - EES_STEP_F_GRACE), diff --git a/ntpd/refclock_mx4200.c b/ntpd/refclock_mx4200.c index 77ccedd6c..f863dca08 100644 --- a/ntpd/refclock_mx4200.c +++ b/ntpd/refclock_mx4200.c @@ -222,7 +222,7 @@ mx4200_start( /* * Open serial port */ - (void)sprintf(gpsdev, DEVICE, unit); + snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit); if (!(fd = refclock_open(gpsdev, SPEED232, LDISC_PPS))) { return (0); } @@ -230,19 +230,16 @@ mx4200_start( /* * Allocate unit structure */ - if (!(up = (struct mx4200unit *) emalloc(sizeof(struct mx4200unit)))) { - perror("emalloc"); - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct mx4200unit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = mx4200_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -274,8 +271,10 @@ mx4200_shutdown( pp = peer->procptr; up = (struct mx4200unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } @@ -576,9 +575,9 @@ mx4200_ref( } alt = up->avg_alt; minute = (lat - (double)(int)lat) * 60.0; - sprintf(lats,"%02d%02.4f", (int)lat, minute); + snprintf(lats, sizeof(lats), "%02d%02.4f", (int)lat, minute); minute = (lon - (double)(int)lon) * 60.0; - sprintf(lons,"%03d%02.4f", (int)lon, minute); + snprintf(lons, sizeof(lons), "%03d%02.4f", (int)lon, minute); mx4200_send(peer, "%s,%03d,,,,,%s,%c,%s,%c,%.2f,%d", pmvxg, PMVXG_S_INITMODEA, diff --git a/ntpd/refclock_palisade.c b/ntpd/refclock_palisade.c index ff7ab8bad..7e9f59fbc 100644 --- a/ntpd/refclock_palisade.c +++ b/ntpd/refclock_palisade.c @@ -273,7 +273,7 @@ palisade_start ( char gpsdev[20]; struct termios tio; - (void) sprintf(gpsdev, DEVICE, unit); + snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit); /* * Open serial port. @@ -350,7 +350,8 @@ palisade_start ( #ifdef DEBUG printf("Palisade(%d) io_addclock\n",unit); #endif - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -394,8 +395,10 @@ palisade_shutdown ( struct refclockproc *pp; pp = peer->procptr; up = (struct palisade_unit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } @@ -920,8 +923,10 @@ palisade_receive ( * report and process */ - (void) sprintf(pp->a_lastcode,"%4d %03d %02d:%02d:%02d.%06ld", - pp->year,pp->day,pp->hour,pp->minute, pp->second,pp->nsec); + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), + "%4d %03d %02d:%02d:%02d.%06ld", + pp->year, pp->day, + pp->hour,pp->minute, pp->second, pp->nsec); pp->lencode = 24; if (!refclock_process(pp)) { diff --git a/ntpd/refclock_parse.c b/ntpd/refclock_parse.c index b6470d651..24d27f9b1 100644 --- a/ntpd/refclock_parse.c +++ b/ntpd/refclock_parse.c @@ -1474,10 +1474,10 @@ mkreadable( ) { char *b = buffer; - char *endb = (char *)0; + char *endb = NULL; if (blen < 4) - return (char *)0; /* don't bother with mini buffers */ + return NULL; /* don't bother with mini buffers */ endb = buffer + blen - 4; @@ -1515,7 +1515,7 @@ mkreadable( } else { - sprintf(buffer, "\\x%02x", *src++); + snprintf(buffer, blen, "\\x%02x", *src++); blen -= 4; buffer += 4; } diff --git a/ntpd/refclock_pcf.c b/ntpd/refclock_pcf.c index 6f7d58e57..fcc12be07 100644 --- a/ntpd/refclock_pcf.c +++ b/ntpd/refclock_pcf.c @@ -72,10 +72,10 @@ pcf_start( /* * Open device file for reading. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); fd = open(device, O_RDONLY); if (fd == -1) { - (void)sprintf(device, OLDDEVICE, unit); + snprintf(device, sizeof(device), OLDDEVICE, unit); fd = open(device, O_RDONLY); } #ifdef DEBUG diff --git a/ntpd/refclock_pst.c b/ntpd/refclock_pst.c index 4eaa50166..bd208c725 100644 --- a/ntpd/refclock_pst.c +++ b/ntpd/refclock_pst.c @@ -127,25 +127,23 @@ pst_start( /* * Open serial port. Use CLK line discipline, if available. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if (!(fd = refclock_open(device, SPEED232, LDISC_CLK))) return (0); /* * Allocate and initialize unit structure */ - if (!(up = (struct pstunit *)emalloc(sizeof(struct pstunit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct pstunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = pst_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { - (void) close(fd); + close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -176,8 +174,10 @@ pst_shutdown( pp = peer->procptr; up = (struct pstunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_shm.c b/ntpd/refclock_shm.c index c110c3826..b73e89976 100644 --- a/ntpd/refclock_shm.c +++ b/ntpd/refclock_shm.c @@ -137,8 +137,8 @@ struct shmTime *getShmTime (int unit) { HANDLE shmid=0; SECURITY_DESCRIPTOR sd; SECURITY_ATTRIBUTES sa; - sprintf (buf,"NTP%d",unit); - if (unit>=2) { /* world access */ + snprintf(buf, sizeof(buf), "NTP%d", unit); + if (unit >= 2) { /* world access */ if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) { msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m",unit); return 0; @@ -193,10 +193,8 @@ shm_start( pp->io.datalen = 0; pp->io.fd = -1; - up = (struct shmunit *) emalloc(sizeof(*up)); - if (up == NULL) - return (FALSE); - memset((char *)up, 0, sizeof(*up)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp->unitptr = (caddr_t)up; up->shm = getShmTime(unit); @@ -233,12 +231,16 @@ shm_shutdown( pp = peer->procptr; up = (struct shmunit *)pp->unitptr; + + if (NULL == up) + return; #ifndef SYS_WINNT /* HMS: shmdt()wants char* or const void * */ (void) shmdt ((char *)up->shm); #else UnmapViewOfFile (up->shm); #endif + free(up); } diff --git a/ntpd/refclock_tpro.c b/ntpd/refclock_tpro.c index 860f8bab5..86764d3f7 100644 --- a/ntpd/refclock_tpro.c +++ b/ntpd/refclock_tpro.c @@ -77,7 +77,7 @@ tpro_start( /* * Open TPRO device */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); fd = open(device, O_RDONLY | O_NDELAY, 0777); if (fd == -1) { msyslog(LOG_ERR, "tpro_start: open of %s: %m", device); @@ -87,11 +87,8 @@ tpro_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct tprounit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = noentry; pp->io.srcclock = (caddr_t)peer; @@ -125,7 +122,8 @@ tpro_shutdown( pp = peer->procptr; up = (struct tprounit *)pp->unitptr; io_closeclock(&pp->io); - free(up); + if (NULL != up) + free(up); } @@ -170,13 +168,13 @@ tpro_poll( * we could pad the written string appropriately and read the * resulting value in already scaled. */ - sprintf(pp->a_lastcode, - "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x", - tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1, - tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100, - tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1, - tp->status); - pp->lencode = strlen(pp->a_lastcode); + snprintf(pp->a_lastcode, sizeof(pp->a_lastcode), + "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x", + tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1, + tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100, + tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1, + tp->status); + pp->lencode = strlen(pp->a_lastcode); #ifdef DEBUG if (debug) printf("tpro: time %s timecode %d %s\n", diff --git a/ntpd/refclock_trak.c b/ntpd/refclock_trak.c index 3a4a54e65..ddb86de84 100644 --- a/ntpd/refclock_trak.c +++ b/ntpd/refclock_trak.c @@ -147,7 +147,7 @@ trak_start( * timestamp following the "*" on-time character of the * timecode. */ - (void)sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if ( #ifdef PPS !(fd = refclock_open(device, SPEED232, LDISC_CLK)) @@ -160,12 +160,8 @@ trak_start( /* * Allocate and initialize unit structure */ - if (!(up = (struct trakunit *) - emalloc(sizeof(struct trakunit)))) { - (void) close(fd); - return (0); - } - memset((char *)up, 0, sizeof(struct trakunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->io.clock_recv = trak_receive; pp->io.srcclock = (caddr_t)peer; @@ -173,6 +169,7 @@ trak_start( pp->io.fd = fd; if (!io_addclock(&pp->io)) { (void) close(fd); + pp->io.fd = -1; free(up); return (0); } @@ -214,8 +211,10 @@ trak_shutdown( pp = peer->procptr; up = (struct trakunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpd/refclock_wwvb.c b/ntpd/refclock_wwvb.c index e65af9aff..e327fb16f 100644 --- a/ntpd/refclock_wwvb.c +++ b/ntpd/refclock_wwvb.c @@ -188,15 +188,15 @@ wwvb_start( /* * Open serial port. Use CLK line discipline, if available. */ - sprintf(device, DEVICE, unit); + snprintf(device, sizeof(device), DEVICE, unit); if (-1 == (fd = refclock_open(device, SPEED232, LDISC_CLK))) return (0); /* * Allocate and initialize unit structure */ - up = (struct wwvbunit *)emalloc(sizeof(struct wwvbunit)); - memset((char *)up, 0, sizeof(struct wwvbunit)); + up = emalloc(sizeof(*up)); + memset(up, 0, sizeof(*up)); pp = peer->procptr; pp->unitptr = (caddr_t)up; pp->io.clock_recv = wwvb_receive; @@ -205,7 +205,9 @@ wwvb_start( pp->io.fd = fd; if (!io_addclock(&pp->io)) { close(fd); + pp->io.fd = -1; free(up); + pp->unitptr = NULL; return (0); } @@ -214,7 +216,7 @@ wwvb_start( */ peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; - memcpy((char *)&pp->refid, REFID, 4); + memcpy(&pp->refid, REFID, 4); return (1); } @@ -233,8 +235,10 @@ wwvb_shutdown( pp = peer->procptr; up = (struct wwvbunit *)pp->unitptr; - io_closeclock(&pp->io); - free(up); + if (-1 != pp->io.fd) + io_closeclock(&pp->io); + if (NULL != up) + free(up); } diff --git a/ntpq/ntpq-subs.c b/ntpq/ntpq-subs.c index c016c79a7..9cad68895 100644 --- a/ntpq/ntpq-subs.c +++ b/ntpq/ntpq-subs.c @@ -56,7 +56,7 @@ static void radiostatus (struct parse *, FILE *); static void pstatus (struct parse *, FILE *); static long when (l_fp *, l_fp *, l_fp *); -static char * prettyinterval (char *, long); +static char * prettyinterval (char *, size_t, long); static int doprintpeers (struct varlist *, int, int, int, char *, FILE *, int); static int dogetpeers (struct varlist *, int, FILE *, int); static void dopeers (int, FILE *, int); @@ -1091,14 +1091,16 @@ printassoc( break; } cnt = uinttoa(event_count); - sprintf(buf, - "%3d %5u %04x %3.3s %4s %4.4s %9.9s %11s %2s", - i + 1, assoc_cache[i].assid, assoc_cache[i].status, - conf, reach, auth, condition, last_event, cnt); - bp = &buf[strlen(buf)]; - while (bp > buf && *(bp-1) == ' ') - *(--bp) = '\0'; - (void) fprintf(fp, "%s\n", buf); + snprintf(buf, sizeof(buf), + "%3d %5u %04x %3.3s %4s %4.4s %9.9s %11s %2s", + i + 1, assoc_cache[i].assid, + assoc_cache[i].status, conf, reach, auth, + condition, last_event, cnt); + bp = buf + strlen(buf); + while (bp > buf && ' ' == bp[-1]) + --bp; + bp[0] = '\0'; + fprintf(fp, "%s\n", buf); } } @@ -1296,6 +1298,7 @@ when( static char * prettyinterval( char *buf, + size_t cb, long diff ) { @@ -1306,24 +1309,24 @@ prettyinterval( } if (diff <= 2048) { - (void) sprintf(buf, "%ld", (long int)diff); + snprintf(buf, cb, "%ld", diff); return buf; } diff = (diff + 29) / 60; if (diff <= 300) { - (void) sprintf(buf, "%ldm", (long int)diff); + snprintf(buf, cb, "%ldm", diff); return buf; } diff = (diff + 29) / 60; if (diff <= 96) { - (void) sprintf(buf, "%ldh", (long int)diff); + snprintf(buf, cb, "%ldh", diff); return buf; } diff = (diff + 11) / 24; - (void) sprintf(buf, "%ldd", (long int)diff); + snprintf(buf, cb, "%ldd", diff); return buf; } @@ -1584,18 +1587,21 @@ doprintpeers( else c = flash2[CTL_PEER_STATVAL(rstatus) & 0x3]; if (numhosts > 1) - (void) fprintf(fp, "%-*s ", maxhostlen, currenthost); + fprintf(fp, "%-*s ", maxhostlen, currenthost); if (af == 0 || AF(&srcadr) == af) { - strcpy(clock_name, nntohost(&srcadr)); - - (void) fprintf(fp, + strncpy(clock_name, nntohost(&srcadr), sizeof(clock_name)); + fprintf(fp, "%c%-15.15s %-15.15s %2ld %c %4.4s %4.4s %3lo %7.7s %8.7s %7.7s\n", c, clock_name, dstadr_refid, stratum, type, - prettyinterval(whenbuf, when(&ts, &rec, &reftime)), - prettyinterval(pollbuf, (int)poll_sec), reach, - lfptoms(&estdelay, 3), lfptoms(&estoffset, 3), - havevar[HAVE_JITTER] ? lfptoms(&estjitter, 3) : - lfptoms(&estdisp, 3)); + prettyinterval(whenbuf, sizeof(whenbuf), + when(&ts, &rec, &reftime)), + prettyinterval(pollbuf, sizeof(pollbuf), + (int)poll_sec), + reach, lfptoms(&estdelay, 3), + lfptoms(&estoffset, 3), + (havevar[HAVE_JITTER]) + ? lfptoms(&estjitter, 3) + : lfptoms(&estdisp, 3)); return (1); } else diff --git a/ntpq/ntpq.c b/ntpq/ntpq.c index d1ebc414e..29380fc9e 100644 --- a/ntpq/ntpq.c +++ b/ntpq/ntpq.c @@ -3108,33 +3108,44 @@ tstflags( u_long val ) { - register char *cb, *s; + register char *cp, *s; + size_t cb; register int i; register const char *sep; sep = ""; i = 0; - s = cb = &circ_buf[nextcb][0]; + s = cp = circ_buf[nextcb]; if (++nextcb >= NUMCB) - nextcb = 0; + nextcb = 0; + cb = sizeof(circ_buf[0]); - sprintf(cb, "%02lx", val); - cb += strlen(cb); + snprintf(cp, cb, "%02lx", val); + cp += strlen(cp); + cb -= strlen(cp); if (!val) { - strcat(cb, " ok"); - cb += strlen(cb); + strncat(cp, " ok", cb); + cp += strlen(cp); + cb -= strlen(cp); } else { - *cb++ = ' '; - for (i = 0; i < 13; i++) { + if (cb) { + *cp++ = ' '; + cb--; + } + for (i = 0; i < COUNTOF(tstflagnames); i++) { if (val & 0x1) { - sprintf(cb, "%s%s", sep, tstflagnames[i]); + snprintf(cp, cb, "%s%s", sep, + tstflagnames[i]); sep = ", "; - cb += strlen(cb); + cp += strlen(cp); + cb -= strlen(cp); } val >>= 1; } } - *cb = '\0'; + if (cb) + *cp = '\0'; + return s; } diff --git a/ports/winnt/ntpd/hopf_PCI_io.c b/ports/winnt/ntpd/hopf_PCI_io.c index c50088b5c..880825ddd 100644 --- a/ports/winnt/ntpd/hopf_PCI_io.c +++ b/ports/winnt/ntpd/hopf_PCI_io.c @@ -12,21 +12,21 @@ * Ignore nonstandard extension warning. * This happens when including winioctl.h */ -#pragma warning( disable : 4201) +#pragma warning(disable: 4201) #define _FILESYSTEMFSCTL_ #include #include #include #include +#include #include #include "hopf_PCI_io.h" -#include -#define ATL_PASSTHROUGH_READ_TOSIZE 3*sizeof(ULONG) -#define ATL_PASSTHROUGH_READ_FROMSIZE 0 -#define IOCTL_ATLSOFT_PASSTHROUGH_READ CTL_CODE( \ +#define ATL_PASSTHROUGH_READ_TOSIZE (3 * sizeof(ULONG)) +#define ATL_PASSTHROUGH_READ_FROMSIZE 0 +#define IOCTL_ATLSOFT_PASSTHROUGH_READ CTL_CODE( \ FILE_DEVICE_UNKNOWN, \ 0x805, \ METHOD_BUFFERED, \ @@ -35,68 +35,70 @@ HANDLE hDevice = NULL; // this is the handle to the PCI Device -HANDLE hRdEvent; -OVERLAPPED Rdoverlapped; -OVERLAPPED *pRdOverlapped; +HANDLE hRdEvent; +OVERLAPPED Rdoverlapped; +OVERLAPPED * pRdOverlapped; -ULONG iobuffer[256]; -DWORD cbReturned; -BOOL HaveBoard = FALSE; -struct{ +ULONG iobuffer[256]; +DWORD cbReturned; +BOOL HaveBoard = FALSE; + +struct { ULONG region; ULONG offset; ULONG count; - }io_params; +} io_params; -BOOL OpenHopfDevice() +BOOL +OpenHopfDevice(void) { - OSVERSIONINFO VersionInfo; - ULONG deviceNumber; - CHAR deviceName[255]; - - VersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO); - GetVersionEx(&VersionInfo); - switch(VersionInfo.dwPlatformId) - { - case VER_PLATFORM_WIN32_WINDOWS: // Win95/98 - return(FALSE); // "NTP does not support Win 95-98." - break; - - case VER_PLATFORM_WIN32_NT: // WinNT - deviceNumber=0; - sprintf(deviceName,"\\\\.\\hclk6039%d",deviceNumber+1); - hDevice=CreateFile( - deviceName, - GENERIC_WRITE|GENERIC_READ, - FILE_SHARE_WRITE|FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, - NULL); - break; - - default: - hDevice=INVALID_HANDLE_VALUE; - break; - - } // end switch - - if ( hDevice == INVALID_HANDLE_VALUE ) // the system didn't return a handle - { - return(FALSE); //"A handle to the driver could not be obtained properly" - } - - hRdEvent=CreateEvent(NULL, // an event to be used for async transfers - TRUE, - FALSE, - NULL); - - if ( hRdEvent == INVALID_HANDLE_VALUE ) - return(FALSE); // the system didn't return a handle - - pRdOverlapped=&Rdoverlapped; - pRdOverlapped->hEvent=hRdEvent; + OSVERSIONINFO VersionInfo; + ULONG deviceNumber; + CHAR deviceName[255]; + + VersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetVersionEx(&VersionInfo); + switch (VersionInfo.dwPlatformId) { + + case VER_PLATFORM_WIN32_WINDOWS: // Win95/98 + return FALSE; // "NTP does not support Win 95-98." + break; + + case VER_PLATFORM_WIN32_NT: // WinNT + deviceNumber = 0; + snprintf(deviceName, sizeof(deviceName), + "\\\\.\\hclk6039%d", deviceNumber + 1); + hDevice = CreateFile( + deviceName, + GENERIC_WRITE | GENERIC_READ, + FILE_SHARE_WRITE | FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, + NULL); + break; + + default: + hDevice = INVALID_HANDLE_VALUE; + break; + } // end switch + + if (INVALID_HANDLE_VALUE == hDevice) // the system didn't return a handle + return FALSE; //"A handle to the driver could not be obtained properly" + + // an event to be used for async transfers + hRdEvent = CreateEvent( + NULL, + TRUE, + FALSE, + NULL); + + if (INVALID_HANDLE_VALUE == hRdEvent) + return FALSE; // the system didn't return a handle + + pRdOverlapped = &Rdoverlapped; + pRdOverlapped->hEvent = hRdEvent; HaveBoard = TRUE; // board installed and we have access @@ -104,63 +106,76 @@ BOOL OpenHopfDevice() } // end of OpenHopfDevice() -BOOL CloseHopfDevice(){ - +BOOL +CloseHopfDevice(void) +{ CloseHandle(hRdEvent);// When done, close the handle to the driver - if (CloseHandle(hDevice)==TRUE) // unlink the driver using a CloseHandle call - { - return TRUE; - } - return FALSE; // "The driver handle could not be unlinked properly" + return CloseHandle(hDevice); } // end of CloseHopfDevice() -void ReadHopfDevice(){ - - if (HaveBoard){ - DeviceIoControl(hDevice, IOCTL_ATLSOFT_PASSTHROUGH_READ, &io_params,ATL_PASSTHROUGH_READ_TOSIZE, - iobuffer,ATL_PASSTHROUGH_READ_FROMSIZE+io_params.count*sizeof(ULONG),&cbReturned, pRdOverlapped); - } +void +ReadHopfDevice(void) +{ + if (!HaveBoard) + return; + + DeviceIoControl( + hDevice, + IOCTL_ATLSOFT_PASSTHROUGH_READ, + &io_params, + ATL_PASSTHROUGH_READ_TOSIZE, + iobuffer, + ATL_PASSTHROUGH_READ_FROMSIZE + + io_params.count * sizeof(ULONG), + &cbReturned, + pRdOverlapped + ); } - - -void GetHardwareData(LPDWORD Data32,WORD Ofs) +void +GetHardwareData( + LPDWORD Data32, + WORD Ofs + ) { io_params.region = 1; io_params.offset = Ofs; - io_params.count =1; + io_params.count = 1; ReadHopfDevice(); *Data32 = iobuffer[0]; - return; } -void GetHopfTime(LPHOPFTIME Data,DWORD Offset) +void +GetHopfTime( + LPHOPFTIME Data, + DWORD Offset + ) { io_params.region = 1; io_params.offset = Offset; - io_params.count =4; + io_params.count = 4; ReadHopfDevice(); Data->wHour = 0; Data->wMinute = 0; Data->wSecond = 0; - while (iobuffer[0] >= (60 * 60 * 1000)) { - iobuffer[0] = iobuffer[0] - 60*60 *1000; + while (iobuffer[0] >= 60 * 60 * 1000) { + iobuffer[0] = iobuffer[0] - 60 * 60 * 1000; Data->wHour++; - } - while (iobuffer[0] >= (60 * 1000)) { - iobuffer[0] = iobuffer[0] - 60 *1000; + } + while (iobuffer[0] >= 60 * 1000) { + iobuffer[0] = iobuffer[0] - 60 * 1000; Data->wMinute++; - } - while (iobuffer[0] >= (1000)) { + } + while (iobuffer[0] >= 1000) { iobuffer[0] = iobuffer[0] - 1000; Data->wSecond++; - } + } Data->wMilliseconds = LOWORD(iobuffer[0]); Data->wDay = HIBYTE(HIWORD(iobuffer[1])); Data->wMonth = LOBYTE(HIWORD(iobuffer[1])); @@ -171,81 +186,95 @@ void GetHopfTime(LPHOPFTIME Data,DWORD Offset) io_params.region = 1; io_params.offset += 0x08; - io_params.count =1; + io_params.count = 1; ReadHopfDevice(); Data->wStatus = LOBYTE(HIWORD(iobuffer[0])); - return; } -void GetHopfLocalTime(LPHOPFTIME Data) +void +GetHopfLocalTime( + LPHOPFTIME Data + ) { DWORD Offset = 0; - GetHopfTime(Data,Offset); - return; + + GetHopfTime(Data, Offset); } -void GetHopfSystemTime(LPHOPFTIME Data) +void +GetHopfSystemTime( + LPHOPFTIME Data + ) { DWORD Offset = 0x10; - GetHopfTime(Data,Offset); - return; + + GetHopfTime(Data,Offset); } -void GetSatData(LPSATSTAT Data) +void +GetSatData( + LPSATSTAT Data + ) { io_params.region = 1; io_params.offset = 0xb0; - io_params.count =5; + io_params.count = 5; ReadHopfDevice(); - Data->wVisible= HIBYTE(HIWORD(iobuffer[0])); - Data->wMode = LOBYTE(LOWORD(iobuffer[0])); - Data->wSat0 = HIBYTE(HIWORD(iobuffer[1])); - Data->wRat0 = LOBYTE(HIWORD(iobuffer[1])); - Data->wSat1 = HIBYTE(LOWORD(iobuffer[1])); - Data->wRat1 = LOBYTE(LOWORD(iobuffer[1])); - Data->wSat2 = HIBYTE(HIWORD(iobuffer[2])); - Data->wRat2 = LOBYTE(HIWORD(iobuffer[2])); - Data->wSat3 = HIBYTE(LOWORD(iobuffer[2])); - Data->wRat3 = LOBYTE(LOWORD(iobuffer[2])); - Data->wSat4 = HIBYTE(HIWORD(iobuffer[3])); - Data->wRat4 = LOBYTE(HIWORD(iobuffer[3])); - Data->wSat5 = HIBYTE(LOWORD(iobuffer[3])); - Data->wRat5 = LOBYTE(LOWORD(iobuffer[3])); - Data->wSat6 = HIBYTE(HIWORD(iobuffer[4])); - Data->wRat6 = LOBYTE(HIWORD(iobuffer[4])); - Data->wSat7 = HIBYTE(LOWORD(iobuffer[4])); - Data->wRat7 = LOBYTE(LOWORD(iobuffer[4])); - - return; + Data->wVisible = HIBYTE(HIWORD(iobuffer[0])); + Data->wMode = LOBYTE(LOWORD(iobuffer[0])); + Data->wSat0 = HIBYTE(HIWORD(iobuffer[1])); + Data->wRat0 = LOBYTE(HIWORD(iobuffer[1])); + Data->wSat1 = HIBYTE(LOWORD(iobuffer[1])); + Data->wRat1 = LOBYTE(LOWORD(iobuffer[1])); + Data->wSat2 = HIBYTE(HIWORD(iobuffer[2])); + Data->wRat2 = LOBYTE(HIWORD(iobuffer[2])); + Data->wSat3 = HIBYTE(LOWORD(iobuffer[2])); + Data->wRat3 = LOBYTE(LOWORD(iobuffer[2])); + Data->wSat4 = HIBYTE(HIWORD(iobuffer[3])); + Data->wRat4 = LOBYTE(HIWORD(iobuffer[3])); + Data->wSat5 = HIBYTE(LOWORD(iobuffer[3])); + Data->wRat5 = LOBYTE(LOWORD(iobuffer[3])); + Data->wSat6 = HIBYTE(HIWORD(iobuffer[4])); + Data->wRat6 = LOBYTE(HIWORD(iobuffer[4])); + Data->wSat7 = HIBYTE(LOWORD(iobuffer[4])); + Data->wRat7 = LOBYTE(LOWORD(iobuffer[4])); } -void GetDiffTime(LPLONG Data) + +void +GetDiffTime( + LPLONG Data + ) { io_params.region = 1; io_params.offset = 0x0c; - io_params.count =1; + io_params.count = 1; ReadHopfDevice(); - *Data = iobuffer[0]; - return; + *Data = iobuffer[0]; } -void GetPosition(LPGPSPOS Data){ + +void +GetPosition( + LPGPSPOS Data + ) +{ io_params.region = 1; io_params.offset = 0x90; // Positionsdaten Länge io_params.count = 1; ReadHopfDevice(); - Data->wLongitude = iobuffer[0]; //in Millisekunden + Data->wLongitude = iobuffer[0]; //in Millisekunden io_params.region = 1; io_params.offset = 0xa0; // Positionsdaten Breite io_params.count = 1; @@ -254,20 +283,25 @@ void GetPosition(LPGPSPOS Data){ Data->wLatitude = iobuffer[0]; Data->wAltitude = 0; - return; } -void GetHardwareVersion(LPCLOCKVER Data){ -int i; + +void +GetHardwareVersion( + LPCLOCKVER Data + ) +{ + int i; + io_params.region = 1; io_params.offset = 0x50; - io_params.count =12; + io_params.count = 12; ReadHopfDevice(); - strcpy(Data->cVersion,""); - iobuffer[13] =0; - for (i=0; i < 13; i++){ + Data->cVersion[0] = '\0'; + iobuffer[13] = 0; + for (i = 0; i < 13; i++) { Data->cVersion[i * 4 ] = HIBYTE(HIWORD(iobuffer[i])); Data->cVersion[i * 4 + 1] = LOBYTE(HIWORD(iobuffer[i])); Data->cVersion[i * 4 + 2] = HIBYTE(LOWORD(iobuffer[i])); @@ -275,16 +309,19 @@ int i; } } -void GetDCFAntenne(LPDCFANTENNE Data){ + +void +GetDCFAntenne( + LPDCFANTENNE Data + ) +{ io_params.region = 1; - io_params.offset = 0xcc; // + io_params.offset = 0xcc; io_params.count = 1; ReadHopfDevice(); - Data->bStatus1 = HIBYTE(HIWORD(iobuffer[0])); // - Data->bStatus = LOBYTE(HIWORD(iobuffer[0])); // - Data->wAntValue = LOWORD(iobuffer[0]); - return; + Data->bStatus1 = HIBYTE(HIWORD(iobuffer[0])); + Data->bStatus = LOBYTE(HIWORD(iobuffer[0])); + Data->wAntValue = LOWORD(iobuffer[0]); } - diff --git a/ports/winnt/vs2008/libntp/libntp.vcproj b/ports/winnt/vs2008/libntp/libntp.vcproj index b80e53899..1fcdbeae8 100644 --- a/ports/winnt/vs2008/libntp/libntp.vcproj +++ b/ports/winnt/vs2008/libntp/libntp.vcproj @@ -207,6 +207,10 @@ RelativePath="..\..\..\..\libntp\atouint.c" > + + @@ -817,11 +821,11 @@ >