Change example code to use fgets instead of gets

Signed-off-by: Angus Salkeld <asalkeld@redhat.com>
This commit is contained in:
Angus Salkeld 2012-02-03 14:19:26 +11:00
parent ad4efc2005
commit 0710b40cde
2 changed files with 18 additions and 12 deletions

View File

@ -44,6 +44,7 @@ main(int argc, char *argv[])
int32_t rc;
struct my_req req;
struct my_res res;
char *newline;
conn = qb_ipcc_connect("ipcserver", MAX_MSG_SIZE);
if (conn == NULL) {
@ -53,20 +54,23 @@ main(int argc, char *argv[])
while(1) {
printf("SEND (q or Q to quit) : ");
if (gets(req.message) == NULL) {
if (fgets(req.message, 256, stdin) == NULL) {
continue;
}
newline = strrchr(req.message, '\n');
if (newline) {
*newline = '\0';
}
if (strcmp(req.message, "q") != 0 &&
strcmp(req.message, "Q") != 0) {
if (strcasecmp(req.message, "q") == 0) {
break;
} else {
req.hdr.id = QB_IPC_MSG_USER_START + 3;
req.hdr.size = sizeof(struct my_req);
rc = qb_ipcc_send(conn, &req, req.hdr.size);
if (rc < 0) {
perror("qb_ipcc_send");
}
} else {
break;
}
if (rc > 0) {

View File

@ -29,6 +29,7 @@ main(int argc, char *argv[])
int32_t res;
char send_data[1024];
char recv_data[1024];
char *newline;
struct sockaddr_in server_addr;
struct hostent *host = gethostbyname("127.0.0.1");
@ -50,16 +51,17 @@ main(int argc, char *argv[])
while(1) {
printf("\nSEND (q or Q to quit) : ");
if (gets(send_data) == NULL) {
if (fgets(send_data, 1024, stdin) == NULL) {
continue;
}
if (strcmp(send_data , "q") != 0 &&
strcmp(send_data , "Q") != 0) {
res = send(sock, send_data, strlen(send_data), 0);
} else {
send(sock,send_data, strlen(send_data), 0);
newline = strrchr(send_data, '\n');
if (newline) {
*newline = '\0';
}
res = send(sock, send_data, strlen(send_data), 0);
if (strcasecmp(send_data, "q") == 0) {
close(sock);
printf("you typed QUIT\n");
break;
}