[ntp:hackers] /dev/ntpsymlink1 -> COM1: for ntpd on Windows

Terje Mathisen terje at tmsw.no
Sun May 31 19:21:24 UTC 2009


Dave Hart wrote:
> This is the code I currently have, complete with excessive diagnostic output:
> 
> 	DPRINTF(1, ("common_serial_open given %s\n", dev));
> 
> 	pch = NULL;
> 	if ('/' == dev[0]) {
> 		pch = dev + strlen(dev) - 1;
> 
> 		//DPRINTF(1, ("common_serial_open initial %s\n", pch));
> 		if (isdigit(pch[0])) {
> 			while (isdigit(pch[0])) {
> 				pch--;
> 				//DPRINTF(1, ("common_serial_open backed up to %s\n", pch));
> 			}
> 			pch++;
> 		}
> 		DPRINTF(1, ("common_serial_open skipped to ending digits leaving %s\n", pch));
[snip]

I just realized that the logic should be even simpler:

We are always looking for the _last_ numeric part of the device string, 
even if there's a previous directory name that also contains digits, right?

It also looks like you need to verify the given unit number?

How about:

   char *lch = dev + strlen(dev)-1; // Last char

// Skip any trailing non-digits, probably not needed?
   while (lch >= dev && !isdigit(*lch)) lch--;
   if (lch < dev) return NO_SERIAL_NR;

   char *fch = lch;
   while (fch >= dev && isdigit(*fch)) fch--;

// The device number is encoded from fch+1 to lch:
   fch++;
   *comp++ = *fch;
   int unit = *fch - '0';
   while (fch <= lch) {
     unit = unit*10 + (*fch - '0');
     *comp++ = *fch++;
   }
   *comp++ = ':'; *comp = '\0';

Terje

-- 
- <Terje at tmsw.no>
"almost all programming can be viewed as an exercise in caching"


More information about the hackers mailing list