Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
To avoid the situation where cat create the temp file but do no yet write in, and...
authorcherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 26 Jun 2008 11:46:56 +0000 (11:46 +0000)
committercherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 26 Jun 2008 11:46:56 +0000 (11:46 +0000)
Also ensure that what we read is written.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5831 48e7efb5-ca39-0410-a469-dd3cf9ba447f

tools/tesh2/examples/background.tesh

index 6838a01..4e663e7 100644 (file)
@@ -13,30 +13,47 @@ $ cd temp_testdir
 < #ifdef __MINGW32__
 < #define sleep        _sleep
 < #endif
-<                   
+<                   
 < int main() {
-<   char buff[2048];
-<   int got;
-<   int in;
+<      char buff[2048];
+<      char* p;
+<      int got;
+<      int in;
+<      int w;
+<      int tgot = 0;
+<      
+<      sleep(1);
+<      
+<      if((in = open("tmp_fich",O_RDONLY|O_CREAT)) == -1){
+<              perror("Cannot open tmp_fich");
+<              exit(1);
+<      }
+<      
+<      while (1) {
+<              if((got = read(in,&buff,2048)) == -1)
+<                      perror("Error while reading");
+<              else if(got == 0 && tgot != 0)
+<                      break; /* EOF */
+<              
+<              if(!got)
+<                      continue;                       
+<              
+<              p = buff;
+<
+<              tgot += got;
+<              
+<              while(got) {
+<                      if((w = write(1, p , got)) < 0) {
+<                              perror("Error while writing");
+<                              exit(1);
+<                      }       
+<                      
+<                      p += w;
+<                      got -= w;
+<              }
+<      }
 < 
-<   sleep(1);
-<   in = open("tmp_fich",O_RDONLY|O_CREAT);
-<   if (in == -1) {
-<     perror("Cannot open tmp_fich: ");
-<     exit(1);
-<   }
-<   while ((got = read(in,&buff,2048))>0) {
-<      int w = write(1,&buff,got);
-<      if (w<0) {
-<        perror("Error while writing:");
-<        exit(1);
-<      }
-<   }
-<   if (got < 0) {
-<     perror("Error while reading:");
-<     exit(1);
-<   }
-<   return 0;
+<      return 0;
 < }
 $ cat > delayed_cat.c