- use strncmp instead of strstr

This commit is contained in:
zolliker
2011-04-29 14:07:28 +00:00
parent 446b05d6a2
commit f24ed4e5b6

48
ascon.c
View File

@ -218,22 +218,25 @@ int AsconConnectSuccess(int fd)
FD_SET(fd, &rmask); FD_SET(fd, &rmask);
ret = uselect(fd + 1, &rmask, &wmask, NULL, &tmo); ret = uselect(fd + 1, &rmask, &wmask, NULL, &tmo);
if (ret > 0) { if (ret > 0) {
/** /**
* This assertion triggered for some reason: as writing is only done later * MK:
* I moved the test for ISSET(wmask) down there * This assertion triggered for some reason: as writing is only done later
* assert(FD_ISSET(fd, &wmask)); * I moved the test for ISSET(wmask) down there
*/ * assert(FD_ISSET(fd, &wmask));
* MZ:
* I remember having found this logic on the www. Anyway, your way must be o.k. too
*/
if (FD_ISSET(fd, &rmask)) { /* there may already be data for read */ if (FD_ISSET(fd, &rmask)) { /* there may already be data for read */
if (recv(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */ if (recv(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */
ret = ASCON_RECV_ERROR; /* first recv failed */ ret = ASCON_RECV_ERROR; /* first recv failed */
} }
} else { } else {
if(FD_ISSET(fd,&wmask)){ if (FD_ISSET(fd,&wmask)) {
if (send(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */ if (send(fd, NULL, 0, 0) < 0) { /* zero length, check only return value */
ret = ASCON_SEND_ERROR; /* first send failed */ ret = ASCON_SEND_ERROR; /* first send failed */
} }
} }
} }
} }
fcntl(fd, F_SETFL, oldopts & ~O_NONBLOCK); /* reset to blocking mode */ fcntl(fd, F_SETFL, oldopts & ~O_NONBLOCK); /* reset to blocking mode */
@ -498,21 +501,22 @@ int AsconStdHandler(Ascon * a)
/** /**
* Treat hex strings as terminators right. Note that this * Treat hex strings as terminators right. Note that this
* is limited to single character terminators. * is limited to single character terminators.
* M.Z. changed strstr to strncmp (more precise)
*/ */
static void AsconCheckTerminators(Ascon *a) static void AsconCheckTerminators(Ascon *a)
{ {
int c; int c;
if(a->sendTerminator != NULL && strstr(a->sendTerminator,"0x") != NULL){ if (a->sendTerminator != NULL && strncmp(a->sendTerminator,"0x",2) == 0) {
sscanf(a->sendTerminator,"%x",&c); sscanf(a->sendTerminator,"%x",&c);
a->sendTerminator[0] = (char)c; a->sendTerminator[0] = (char)c;
a->sendTerminator[1] = '\0'; a->sendTerminator[1] = '\0';
} }
if(a->replyTerminator != NULL && strstr(a->replyTerminator,"0x") != NULL){ if (a->replyTerminator != NULL && strncmp(a->replyTerminator,"0x",2) == 0) {
sscanf(a->replyTerminator,"%x",&c); sscanf(a->replyTerminator,"%x",&c);
a->replyTerminator[0] = (char)c; a->replyTerminator[0] = (char)c;
a->replyTerminator[1] = '\0'; a->replyTerminator[1] = '\0';
} }
} }
int AsconInterpreteArgs(int argc, char *argv[], int AsconInterpreteArgs(int argc, char *argv[],
@ -579,7 +583,7 @@ int AsconStdInit(Ascon *a, SConnection *con, int argc, char *argv[])
} else { } else {
a->sendTerminator = strdup("\n"); a->sendTerminator = strdup("\n");
} }
if (pars[1] && pars[2] != '\0') { if (pars[1] && pars[1][0] != '\0') {
a->timeout = atof(pars[1]); a->timeout = atof(pars[1]);
} else { } else {
a->timeout = 2.0; /* sec */ a->timeout = 2.0; /* sec */