Fix bug: Using unsigned to detect errno<0

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@791 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2008-07-31 00:28:24 +00:00
parent 378b050ffa
commit faf168d93c
8 changed files with 228 additions and 93 deletions
+91 -39
View File
@@ -44,6 +44,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <semaphore.h>
#include <errno.h>
#include "pipe.h"
@@ -62,6 +63,8 @@
* Private Data
****************************************************************************/
static sem_t g_rddone;
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -73,37 +76,49 @@
static int redirect_reader(int argc, char *argv[])
{
char buffer[READ_SIZE];
int fd = (int)argv[0];
int fdin;
int fdout;
int ret;
int nbytes = 0;
printf("redirect_reader: started with fd=%s\n", argv[1]);
printf("redirect_reader: started with fdin=%s\n", argv[1]);
/* Convert the fd to binary */
/* Convert the fdin to binary */
fd = atoi(argv[1]);
fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
/* Re-direct the fd to stdin */
/* Close fdout -- we don't need it */
ret = dup2(fd, 0);
ret = close(fdout);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n", fdout);
return 1;
}
/* Re-direct the fdin to stdin */
ret = dup2(fdin, 0);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: dup2 failed: %d\n", errno);
close(fd);
return 1;
close(fdin);
return 2;
}
/* Close the original file descriptor */
ret = close(fd);
ret = close(fdin);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd);
return 2;
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 3;
}
/* Then read from stdin until we hit the end of file */
fflush(stdout);
for (;;)
{
/* Read from stdin */
@@ -112,7 +127,7 @@ static int redirect_reader(int argc, char *argv[])
if (ret < 0 )
{
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno);
return 3;
return 4;
}
else if (ret == 0)
{
@@ -126,7 +141,7 @@ static int redirect_reader(int argc, char *argv[])
if (ret < 0)
{
fprintf(stderr, "redirect_reader: read failed, errno=%d\n", errno);
return 4;
return 5;
}
}
@@ -134,9 +149,11 @@ static int redirect_reader(int argc, char *argv[])
ret = close(0);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd);
return 5;
fprintf(stderr, "redirect_reader: failed to close fd=0\n");
return 6;
}
sem_post(&g_rddone);
printf("redirect_reader: Returning success\n");
return 0;
}
@@ -147,36 +164,48 @@ static int redirect_reader(int argc, char *argv[])
static int redirect_writer(int argc, char *argv[])
{
int fd;
int fdin;
int fdout;
int nbytes = 0;
int ret;
printf("redirect_writer: started with fd=%s\n", argv[1]);
fprintf(stderr, "redirect_writer: started with fdout=%s\n", argv[2]);
/* Convert the fdout to binary */
/* Convert the fd to binary */
fdin = atoi(argv[1]);
fdout = atoi(argv[2]);
fd = atoi(argv[1]);
/* Close fdin -- we don't need it */
/* Re-direct the fd to stdout */
ret = close(fdin);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fdin=%d\n", fdin);
return 1;
}
ret = dup2(fd, 1);
/* Re-direct the fdout to stdout */
ret = dup2(fdout, 1);
if (ret != 0)
{
fprintf(stderr, "redirect_writer: dup2 failed: %d\n", errno);
return 1;
return 2;
}
/* Close the original file descriptor */
ret = close(fd);
ret = close(fdout);
if (ret != 0)
{
fprintf(stderr, "redirect_reader: failed to close fd=%d\n", fd);
return 2;
fprintf(stderr, "redirect_reader: failed to close fdout=%d\n", fdout);
return 3;
}
/* Then write a bunch of stuff to stdout */
fflush(stderr);
nbytes += printf("\nFour score and seven years ago our fathers brought forth on this continent a new nation,\n");
nbytes += printf("conceived in Liberty, and dedicated to the proposition that all men are created equal.\n");
nbytes += printf("\nNow we are engaged in a great civil war, testing whether that nation, or any nation, so\n");
@@ -194,15 +223,18 @@ static int redirect_writer(int argc, char *argv[])
nbytes += printf("here highly resolve that these dead shall not have died in vain—that this nation, under God,\n");
nbytes += printf("shall have a new birth of freedom—and that government of the people, by the people, for the\n");
nbytes += printf("people, shall not perish from the earth.\n\n");
printf("redirect_writer: %d bytes read\n", nbytes);
fflush(stdout);
fprintf(stderr, "redirect_writer: %d bytes written\n", nbytes);
ret = close(1);
if (ret != 0)
{
fprintf(stderr, "redirect_writer: failed to close fd=%d\n", fd);
return 3;
fprintf(stderr, "redirect_writer: failed to close fd=1\n");
return 4;
}
printf("redirect_writer: Returning success\n");
fprintf(stderr, "redirect_writer: Returning success\n");
return 0;
}
@@ -216,14 +248,17 @@ static int redirect_writer(int argc, char *argv[])
int redirection_test(void)
{
const char *argv[2];
char buffer[8];
const char *argv[3];
char buffer1[8];
char buffer2[8];
int readerid;
int writerid;
int filedes[2];
int ret;
/* Create the pipe */
sem_init(&g_rddone, 0, 0);
/* Create the pipe */
ret = pipe(filedes);
if (ret < 0)
@@ -232,12 +267,15 @@ int redirection_test(void)
return 5;
}
sprintf(buffer1, "%d", filedes[0]);
argv[0] = buffer1;
sprintf(buffer2, "%d", filedes[1]);
argv[1] = buffer2;
argv[2] = NULL;
/* Start redirect_reader thread */
printf("redirection_test: Starting redirect_reader task with fd=%d\n", filedes[0]);
sprintf(buffer, "%d", filedes[0]);
argv[0] = buffer;
argv[1] = NULL;
readerid = task_create("redirect_reader", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_reader, argv);
if (readerid < 0)
{
@@ -248,9 +286,6 @@ int redirection_test(void)
/* Start redirect_writer task */
printf("redirection_test: Starting redirect_writer task with fd=%d\n", filedes[1]);
sprintf(buffer, "%d", filedes[1]);
argv[0] = buffer;
argv[1] = NULL;
writerid = task_create("redirect_writer", 50, CONFIG_EXAMPLES_PIPE_STACKSIZE, redirect_writer, argv);
if (writerid < 0)
{
@@ -263,10 +298,27 @@ int redirection_test(void)
return 2;
}
/* We should be able to close the pipe file descriptors now. */
if (close(filedes[0]) != 0)
{
fprintf(stderr, "user_start: close failed: %d\n", errno);
}
if (close(filedes[1]) != 0)
{
fprintf(stderr, "user_start: close failed: %d\n", errno);
}
if (ret != 0)
{
fprintf(stderr, "user_start: PIPE test FAILED (%d)\n", ret);
return 6;
}
/* Wait for redirect_writer thread to complete */
printf("redirection_test: Waiting...\n");
sleep(10);
sem_wait(&g_rddone);
printf("redirection_test: returning %d\n", ret);
return ret;