genericopenlibs/openenvcore/libc/test/testmkfifo/src/tmkfifoblocks.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tmkfifo.h"
       
    20 #include <errno.h>
       
    21     
       
    22 
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 // GetFullPath
       
    26 // -----------------------------------------------------------------------------
       
    27 //
       
    28 TInt GetFullPath(TParse& aParse, const TText16* upath, RFs& aSession, TDes* aFileName)
       
    29 //
       
    30 // Parse a path of the form "[C:][\]AAA\..\.\BBB\xxx" where:
       
    31 // .  indicates the current directory
       
    32 // .. indicates move to the parent directory
       
    33 // An optional "\" at the start of the path indicates the path is not relative to the current path,
       
    34 // and is implied if the drive specifier is present
       
    35 // If aFileName is non-NULL then the final component is a filename and should be copied into 
       
    36 // the aFileName descriptor.
       
    37 //
       
    38 	{
       
    39 	TInt r;
       
    40 	TBuf<3> drive;
       
    41 	TFileName nextBit;
       
    42 	TText16 c=*upath;
       
    43 
       
    44 	if (c && upath[1]==KDriveDelimiter) 
       
    45 		{
       
    46 		// drive name specified
       
    47 		if (c==L'?')
       
    48 			drive.Zero();			// use "?:" to mean scan across drives
       
    49 		else
       
    50 			{
       
    51 			drive.Copy(TPtrC16(upath, 2));
       
    52 			drive.UpperCase();
       
    53 			}
       
    54 		upath+=2;
       
    55 		drive.Append(TChar(KPathDelimiter));	// enforce absoluteness
       
    56 		}
       
    57 	else
       
    58 		{
       
    59 		// no leading drive specifier
       
    60 		drive.Zero();
       
    61 		if (c==KPathDelimiter||c==L'/')
       
    62 			{
       
    63 			upath+=1;
       
    64 			drive.Append(TChar(KPathDelimiter));
       
    65 			}
       
    66 		}
       
    67 	r = aSession.Parse(drive, aParse);
       
    68 
       
    69 	// upath now looks like a relative pathname, to be added onto
       
    70 	// aParse a directory at a time. Note that '/' is not allowed in
       
    71 	// EPOC32 file or directory names, so treat it as an alternative separator
       
    72 
       
    73 	c=*upath;
       
    74 	while (c && (r==KErrNone))
       
    75 		{
       
    76 		const TText16* ustart=upath;
       
    77 		do 
       
    78 		{
       
    79 			c=*upath++;
       
    80 		}
       
    81 		while (c && c!=KPathDelimiter && c!=L'/');
       
    82 
       
    83 		TInt len=(upath-ustart)-1;		// excludes delimiter
       
    84 		if (len==0)
       
    85 			{
       
    86 			continue;
       
    87 			}
       
    88 		if (ustart[0]==L'.')
       
    89 			{
       
    90 			if (len==1)
       
    91 				{
       
    92 				continue;	// directory . ignored
       
    93 				}
       
    94 			if (len==2 && ustart[1]==L'.')
       
    95 				{
       
    96 				// directory ..
       
    97 				(void) aParse.PopDir();	// just stick at root dir, no errors
       
    98 				continue;
       
    99 				}
       
   100 			}
       
   101 		if (len>=KMaxFileName)
       
   102 			{
       
   103 			return ENAMETOOLONG;
       
   104 			}
       
   105 		if (c==L'\0' && aFileName!=NULL)
       
   106 			{
       
   107 			// it's the trailing filename
       
   108 			aFileName->Copy(TPtrC16(ustart, len));
       
   109 			break;
       
   110 			}
       
   111 		else	
       
   112 			{
       
   113 			// it's a component of the accumulating path
       
   114 			nextBit.Copy(TPtrC16(ustart, len));
       
   115 			r = aParse.AddDir(nextBit);
       
   116 			}
       
   117 		}
       
   118 	return(r);
       
   119 	}
       
   120 	
       
   121 // -----------------------------------------------------------------------------
       
   122 // GetFullFile
       
   123 // Use GetFullPath to establish the pathname, then add the filename onto the end
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 TInt GetFullFile(TFileName& aName, const TText16* upath, RFs& aSession)
       
   127 	{	
       
   128 	TParse path;
       
   129 	TInt err = GetFullPath(path,upath,aSession,&aName);
       
   130 	if (err!=KErrNone)
       
   131 		{
       
   132 		return err;
       
   133 		}
       
   134 	// Wildcard drive letter for searching across drives
       
   135 	if (upath[0]==L'?' && upath[1]==L':')
       
   136 		{
       
   137 		TFindFile search(aSession);
       
   138 		err=search.FindByDir(aName,path.Path());
       
   139 		if (!err)
       
   140 			{
       
   141 			aName=search.File();
       
   142 			return KErrNone;
       
   143 			}
       
   144 		}
       
   145 	err = path.SetNoWild(path.DriveAndPath(),NULL,&aName);
       
   146 	if (!err)
       
   147 		{
       
   148 		aName = path.FullName();
       
   149 		}
       
   150 	return err;	
       
   151 	}
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CTestMkfifo::DeleteFifo
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TInt CTestMkfifo::DeleteFifo(  )
       
   159 {
       
   160 
       
   161     _LIT(KFunc, "DeleteFifo");
       
   162 	const char* path = "C:\\mkfifo.test";
       
   163 	wchar_t _widename[KMaxFileName+1];
       
   164 	RFs iSession;
       
   165     TInt err;
       
   166     TFullName fullName;
       
   167     
       
   168     INFO_PRINTF1( KFunc);
       
   169     
       
   170 
       
   171 
       
   172     err = iSession.Connect(); // connect to server
       
   173     if (!err)
       
   174     {
       
   175         if((size_t)-1 != mbstowcs(_widename, path, KMaxFileName))
       
   176         {
       
   177     	    err = GetFullFile(fullName,(const TText16*)_widename,iSession);
       
   178     	    if(!err)
       
   179     	    {
       
   180     	        err = iSession.Delete(fullName);
       
   181     	        if(err < 0)
       
   182     	        {
       
   183     	    	    INFO_PRINTF2( KFunc, err);
       
   184     	    	    err  = KErrNone; // we do not care if this file was deleted
       
   185     	        }
       
   186     	    }
       
   187         }
       
   188     	
       
   189     }
       
   190     iSession.Close();
       
   191     //DebugLogPrintL ( KFunc(), ERROR, err);
       
   192     ////DebugLogPrintL( KFunc(), OUT);
       
   193     return err;    
       
   194 }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CTestMkfifo::FifoCreate
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 TInt CTestMkfifo::FifoCreate(  )
       
   201 {
       
   202 	_LIT(KFunc, "FifoCreate");
       
   203 	const char* path = "C:\\mkfifo.test";
       
   204     INFO_PRINTF1( KFunc);
       
   205     TInt errVal;
       
   206     _LIT( KerrVal, "Param%d" );
       
   207      TBuf<8> pNameBuf;
       
   208      pNameBuf.Format(KerrVal,++iParamCnt);
       
   209 	 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errVal);
       
   210 	 if(!res)
       
   211 	 {
       
   212 	 	_LIT(Kerr , "Unable to retrieve errVal") ;
       
   213 	 	INFO_PRINTF1(Kerr) ;
       
   214 		return KErrGeneral; 	
       
   215 	 }
       
   216     
       
   217     TInt err = mkfifo (path, 0666);
       
   218     
       
   219     //struct _reent *r = _REENT;
       
   220 	  //TInt err_no = r->_errno;
       
   221 	  
       
   222     if ( err < 0)
       
   223         {      
       
   224           if ( errVal && (errno == EEXIST))
       
   225             {
       
   226             err = KErrNone;
       
   227             }
       
   228           else
       
   229             {
       
   230             ERR_PRINTF2( _L("mkfifo error: %d"), err);
       
   231             }
       
   232         }
       
   233     ////DebugLogPrintL( KFunc(), OUT);
       
   234     return err;
       
   235 }
       
   236 
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CTestMkfifo::FifoCreateNULL
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 TInt CTestMkfifo::FifoCreateNULL(  )
       
   243 {
       
   244 	_LIT(KFunc, "FifoCreateNULL");
       
   245     INFO_PRINTF1( KFunc);
       
   246     TInt errVal;
       
   247     _LIT( KerrVal, "Param%d" );
       
   248      TBuf<8> pNameBuf;
       
   249      pNameBuf.Format(KerrVal,++iParamCnt);
       
   250 	 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, errVal);
       
   251 	 if(!res)
       
   252 	 {
       
   253 	 	_LIT(Kerr , "Unable to retrieve errVal") ;
       
   254 	 	INFO_PRINTF1(Kerr) ;
       
   255 		return KErrGeneral; 	
       
   256 	 }
       
   257     TInt err = mkfifo (NULL, 0666);
       
   258     if ( err )
       
   259         {
       
   260         	if ( errVal && (errno == ENOENT))
       
   261             {
       
   262             err = KErrNone;
       
   263             }
       
   264           else
       
   265             {
       
   266             ERR_PRINTF2( _L("mkfifo error: %d"), err);
       
   267             }
       
   268         }
       
   269     ////DebugLogPrintL( KFunc(), OUT);
       
   270     return err;
       
   271 }
       
   272 // -----------------------------------------------------------------------------
       
   273 // CTestMkfifo::FifoCreateInSystemDir
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 TInt CTestMkfifo::FifoCreateInSystemDir (  )
       
   277 {
       
   278 	_LIT(KFunc, "FifoCreateInSystemDir");
       
   279 	const char* path = "Z:\\mkfifo.test";
       
   280     INFO_PRINTF1( KFunc);
       
   281     
       
   282     TInt err = mkfifo (path, 0666);
       
   283     if ( err < 0 && errno == EACCES)
       
   284     {
       
   285          err = KErrNone;
       
   286     }
       
   287     else
       
   288     {
       
   289          ERR_PRINTF2( _L("mkfifo error: %d"), err);
       
   290          err = KErrGeneral;
       
   291     }
       
   292     ////DebugLogPrintL( KFunc(), OUT);
       
   293     return err;
       
   294 }
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // CTestMkfifo::FifoCreateNameTooLong
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 TInt CTestMkfifo::FifoCreateNameTooLong (  )
       
   301 {
       
   302 	_LIT(KFunc, "FifoCreateInSystemDir");
       
   303 	char path[258];
       
   304 	TInt i;
       
   305     INFO_PRINTF1( KFunc);
       
   306     
       
   307     for (i=0; i<258; i++)
       
   308     {
       
   309     	path[i] = 'a';
       
   310     }
       
   311     path[i] = '\0';
       
   312     
       
   313     TInt err = mkfifo (path, 0666);
       
   314     if ( err < 0 && errno == ENAMETOOLONG)
       
   315     {
       
   316          err = KErrNone;
       
   317     }
       
   318     else
       
   319     {
       
   320          ERR_PRINTF2( _L("mkfifo error:%d"), err);
       
   321          err = KErrGeneral;
       
   322     }
       
   323 	////DebugLogPrintL( KFunc(), OUT);
       
   324     return err;
       
   325 }
       
   326 
       
   327 // -----------------------------------------------------------------------------
       
   328 // CTestMkfifo::FifoCreateInNonDir
       
   329 // -----------------------------------------------------------------------------
       
   330 //
       
   331 TInt CTestMkfifo::FifoCreateInNonDir (  )
       
   332 {
       
   333 	_LIT(KFunc, "FifoCreateInNonDir");
       
   334 	const char* path = "C:\\mkfifo.test\\mkfifo.test";
       
   335     INFO_PRINTF1( KFunc);
       
   336     
       
   337     TInt err = mkfifo (path, 0666);
       
   338     if ( err < 0 && errno == ENOTDIR)
       
   339     {
       
   340          err = KErrNone;
       
   341     }
       
   342     else
       
   343     {
       
   344          ERR_PRINTF3( _L("mkfifo error :%d %d"), err,errno);
       
   345          err = KErrGeneral;
       
   346     }
       
   347 	unlink("C:\\mkfifo.test\\mkfifo.test");
       
   348     ////DebugLogPrintL( KFunc(), OUT);
       
   349     return err;
       
   350 }
       
   351 
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CTestMkfifo::FifoCreateNonExistantDir
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 TInt CTestMkfifo::FifoCreateNonExistantDir (  )
       
   358 {
       
   359 	_LIT(KFunc, "FifoCreateNonExistantDir");
       
   360 	const char* path = "C:\\XXX\\mkfifo.test";
       
   361     INFO_PRINTF1( KFunc);
       
   362     
       
   363     unlink("C:\\XXX");
       
   364     
       
   365     TInt err = mkfifo (path, 0666);
       
   366     if ( err < 0 && errno == ENOENT)
       
   367     {
       
   368          err = KErrNone;
       
   369     }
       
   370     else
       
   371     {
       
   372          ERR_PRINTF2( _L("mkfifo error :%d"), err);
       
   373          err = KErrGeneral;
       
   374     }
       
   375     ////DebugLogPrintL( KFunc(), OUT);
       
   376     return err;
       
   377 }
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CTestMkfifo::FifoOpen
       
   381 // -----------------------------------------------------------------------------
       
   382 //
       
   383 TInt CTestMkfifo::FifoOpen(  )
       
   384 {
       
   385 	  _LIT(KFunc, "FifoOpen");
       
   386 	  const char* path = "C:\\mkfifo.test";
       
   387     INFO_PRINTF1( KFunc);
       
   388     TInt err;
       
   389     //if ( err )
       
   390       //  {
       
   391        // DebugLogPrintL ( _L("aItem.GetNextInt"), ERROR, err);
       
   392        // return err;
       
   393        // }
       
   394     err = open (path, O_RDONLY);
       
   395     if ( err < 0)
       
   396         {
       
   397             ERR_PRINTF2 ( _L("open error :%d"), err);
       
   398         }
       
   399     //DebugLogPrintL ( KFunc(), OUT);
       
   400     return KErrNone;
       
   401 }
       
   402 
       
   403 
       
   404 // -----------------------------------------------------------------------------
       
   405 // CTestMkfifo::FifoOpenWNonBlock
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 TInt CTestMkfifo::FifoOpenWNonBlock(  )
       
   409 {
       
   410 	  _LIT(KFunc, "FifoOpenWNonBlock");
       
   411 	  const char* path = "C:\\mkfifo.test";
       
   412     INFO_PRINTF1( KFunc);
       
   413     TInt fd;
       
   414     fd = open (path, O_NONBLOCK|O_WRONLY);
       
   415 	TInt err_no =  errno;
       
   416     if ( fd < 0 && err_no == ENXIO)
       
   417         {
       
   418             ERR_PRINTF2 ( _L("open error :%d"), fd);
       
   419             return KErrNone;
       
   420         }
       
   421     if ( fd > 0 )    
       
   422         {
       
   423         close(fd);
       
   424         return KErrNone;
       
   425         }
       
   426     return fd;
       
   427 }
       
   428 
       
   429 // -----------------------------------------------------------------------------
       
   430 // CTestMkfifo::FifoOpenRNonBlock
       
   431 // -----------------------------------------------------------------------------
       
   432 //
       
   433 TInt CTestMkfifo::FifoOpenRNonBlock(  )
       
   434 {
       
   435 	  _LIT(KFunc, "FifoOpenRNonBlock");
       
   436 	  const char* path = "C:\\mkfifo.test";
       
   437     INFO_PRINTF1( KFunc);
       
   438     TInt fd;
       
   439     fd = open (path, O_NONBLOCK|O_RDONLY);
       
   440     if ( fd > 0 )
       
   441         {
       
   442             close(fd);
       
   443             return KErrNone;
       
   444         }
       
   445     return fd;
       
   446 }
       
   447 // -----------------------------------------------------------------------------
       
   448 // CTestMkfifo::FifoClose
       
   449 // -----------------------------------------------------------------------------
       
   450 //
       
   451 TInt CTestMkfifo::FifoClose(  )
       
   452 {
       
   453 	  _LIT(KFunc, "FifoClose");
       
   454 	  const char* path = "C:\\mkfifo.test";
       
   455     INFO_PRINTF1( KFunc);
       
   456     TInt err;
       
   457     TInt fd = open (path, O_NONBLOCK|O_RDONLY);
       
   458     if ( fd < 0)
       
   459         {
       
   460             ERR_PRINTF3 ( _L("Fifo open returned: %d [%d]"), fd, errno);
       
   461             return -1;
       
   462         }
       
   463     err = close(fd);  
       
   464     if ( err )
       
   465        {
       
   466            ERR_PRINTF2 ( _L("File close error: %d"), err);
       
   467            return -1;
       
   468        }  
       
   469     return err;
       
   470 }
       
   471 
       
   472 
       
   473 // -----------------------------------------------------------------------------
       
   474 // CTestMkfifo::FifoRead
       
   475 // -----------------------------------------------------------------------------
       
   476 //
       
   477 TInt CTestMkfifo::FifoRead(  )
       
   478 {
       
   479 	_LIT(KFunc, "FifoRead");
       
   480 	const char* path = "C:\\mkfifo.tst";
       
   481     INFO_PRINTF1( KFunc);
       
   482     TInt err;
       
   483     char buf[128];
       
   484 
       
   485     TInt fd = open (path, O_RDONLY|O_CREAT);
       
   486     if ( fd < 0 )
       
   487         {
       
   488             ERR_PRINTF2 ( _L("Fifo open %d"),  fd);
       
   489             return KErrGeneral;
       
   490         }
       
   491     User::After(500000);    
       
   492     err = read (fd, buf, 128);  
       
   493     if ( (err < 0) || (strcmp(buf, "somejunk") != 0))
       
   494        {
       
   495            ERR_PRINTF2 ( _L("Read error: %d"), err);
       
   496 		   close(fd);
       
   497            return KErrGeneral;
       
   498        }  
       
   499     User::After(100000);
       
   500     close(fd);   
       
   501     return KErrNone;
       
   502 }
       
   503 
       
   504 // -----------------------------------------------------------------------------
       
   505 // CTestMkfifo::FifoWrite
       
   506 // -----------------------------------------------------------------------------
       
   507 //
       
   508 TInt CTestMkfifo::FifoWrite(  )
       
   509 {
       
   510 	_LIT(KFunc, "FifoWrite");
       
   511 	const char* path = "C:\\mkfifo.test";
       
   512     INFO_PRINTF1( KFunc);
       
   513     TInt err;
       
   514     char buf[128];
       
   515     TInt fd = open (path, O_WRONLY);
       
   516     if ( fd < 0 )
       
   517         {
       
   518             ERR_PRINTF2 ( _L("Fifo open error: Returned %d"),  fd);
       
   519             return -1;
       
   520         }
       
   521     err = write (fd, buf, 128);  
       
   522     if ( err < 0 )
       
   523        {
       
   524            ERR_PRINTF2 ( _L("Write error : %d"), err);
       
   525            return -1;
       
   526        }  
       
   527     return err;
       
   528 }
       
   529 
       
   530 // -----------------------------------------------------------------------------
       
   531 // CTestMkfifo::FifoWriteRead
       
   532 // -----------------------------------------------------------------------------
       
   533 //
       
   534 TInt CTestMkfifo::FifoWriteRead(  )
       
   535 {
       
   536 	  _LIT(KFunc, "FifoWriteRead");
       
   537 	  const char* path = "C:\\mkfifo.test";
       
   538     INFO_PRINTF1( KFunc);
       
   539     TInt err;
       
   540     const char* wbuf = "testfifowrite";
       
   541     char rbuf[128];
       
   542     TInt fd = open (path, O_WRONLY);
       
   543     if ( fd < 0 )
       
   544         {
       
   545             ERR_PRINTF2( _L("Fifo open"),  fd);
       
   546             return -1;
       
   547         }
       
   548     err = write (fd, wbuf, (strlen(wbuf)+1));  
       
   549     if ( err < 0 )
       
   550        {
       
   551            ERR_PRINTF2( _L("write error :%d"), err);
       
   552            return -1;
       
   553        }  
       
   554     err = read (fd, rbuf, 128);  
       
   555     if ( err < 0 )
       
   556        {
       
   557            ERR_PRINTF2( _L("read error :%d"), err);
       
   558            return -1;
       
   559        }     
       
   560     return err;
       
   561 }
       
   562 
       
   563 // -----------------------------------------------------------------------------
       
   564 // CTestMkfifo::CreateThreadL
       
   565 // -----------------------------------------------------------------------------
       
   566 //
       
   567 TInt CTestMkfifo::CreateThreadL(  )
       
   568 {
       
   569     _LIT(KFunc, "CreateThreadL");
       
   570     INFO_PRINTF1( KFunc);
       
   571     TInt threadType;
       
   572     TInt err = KErrNone;
       
   573     _LIT( KthreadType, "Param%d" );
       
   574      TBuf<8> pNameBuf;
       
   575      pNameBuf.Format(KthreadType,++iParamCnt);
       
   576 	 TBool res = GetIntFromConfig(ConfigSection(), pNameBuf, threadType);
       
   577 	 if(!res)
       
   578 	 {
       
   579 	 	_LIT(Kerr , "Unable to retrieve threadType") ;
       
   580 	 	INFO_PRINTF1(Kerr) ;
       
   581 		return KErrGeneral; 	
       
   582 	 }
       
   583     switch(threadType)
       
   584         {
       
   585         case 1: 
       
   586             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionW,(void*)this);
       
   587             User::After(500000);
       
   588             
       
   589             err = OpenFifo(TREAD);
       
   590            
       
   591             break;
       
   592         case 2:
       
   593             err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionR,(void*)this);
       
   594             User::After(100000);
       
   595             err = OpenFifo(TWRITE);
       
   596             break;
       
   597         case 3:
       
   598         	err = pthread_create((unsigned int *)&iThreadId,(pthread_attr_t *)NULL,ThreadEntryFunctionWrite,(void*)this);
       
   599         	pthread_join(iThreadId, NULL);
       
   600 			INFO_PRINTF1( _L("case3 called***********"));
       
   601 			err = FifoRead();
       
   602 		    break;
       
   603         default:
       
   604             err = KErrNone;
       
   605             break;            
       
   606         }
       
   607     if ( err )
       
   608         {
       
   609         ERR_PRINTF3 ( _L("pthread_create: %d %d"), err, errno);
       
   610         }
       
   611     return err;
       
   612 }
       
   613 
       
   614 // -----------------------------------------------------------------------------
       
   615 // CTestMkfifo::ThreadEntryFunctionW
       
   616 // -----------------------------------------------------------------------------
       
   617 //    
       
   618 void* CTestMkfifo::ThreadEntryFunctionW(void* arg)
       
   619 {
       
   620     CTestMkfifo *self = static_cast<CTestMkfifo*> (arg);
       
   621     const char* path = "C:\\mkfifo.test";
       
   622       self->iBlocked = 1;
       
   623     TInt fd = open (path, O_WRONLY);
       
   624     if(fd > 0 )
       
   625     {
       
   626        self->iBlocked = 0;
       
   627        close(fd);
       
   628     }
       
   629     return NULL;
       
   630 }    
       
   631 
       
   632 // -----------------------------------------------------------------------------
       
   633 // CTestMkfifo::ThreadEntryFunctionR
       
   634 // -----------------------------------------------------------------------------
       
   635 //
       
   636 void* CTestMkfifo::ThreadEntryFunctionR(void* arg)
       
   637 	{
       
   638     CTestMkfifo *self = static_cast<CTestMkfifo*> (arg);
       
   639     const char* path = "C:\\mkfifo.test";
       
   640     self->iBlocked = 1;
       
   641     TInt fd = open (path, O_RDONLY);
       
   642     
       
   643     if(fd > 0 )
       
   644     	{
       
   645        	self->iBlocked = 0;
       
   646        	close(fd);
       
   647     	}
       
   648     return NULL;
       
   649 	}
       
   650 
       
   651 // -----------------------------------------------------------------------------
       
   652 // CTestMkfifo::ThreadEntryFunctionWrite
       
   653 // -----------------------------------------------------------------------------
       
   654 //
       
   655 void* CTestMkfifo::ThreadEntryFunctionWrite(void* arg)
       
   656 	{
       
   657 	CTestMkfifo *self = static_cast<CTestMkfifo*> (arg);
       
   658     const char* path = "C:\\mkfifo.tst";
       
   659     const char* buf = "somejunk";
       
   660     arg = NULL;
       
   661     self->iBlocked = 1;
       
   662     TInt fd = open (path, O_WRONLY|O_CREAT);
       
   663     if(fd > 0 )
       
   664     	{
       
   665     	self->iBlocked = 0;
       
   666        	write(fd, buf, 9);
       
   667        	close(fd);
       
   668     	}
       
   669      return NULL;
       
   670 	}    
       
   671 
       
   672 // -----------------------------------------------------------------------------
       
   673 // CTestMkfifo::OpenFifo
       
   674 // -----------------------------------------------------------------------------
       
   675 //
       
   676 TInt CTestMkfifo::OpenFifo(TMode mode)
       
   677 {
       
   678     _LIT(KFunc, "OpenFifo");
       
   679     INFO_PRINTF1(KFunc);
       
   680     CTestMkfifo *self = static_cast<CTestMkfifo*>(this);
       
   681     TInt err = KErrNone;
       
   682     TInt fd;
       
   683     const char* path = "C:\\mkfifo.test";
       
   684     
       
   685     
       
   686     if (mode == TREAD)
       
   687     {
       
   688     	fd = open(path, O_RDONLY|O_CREAT);
       
   689     }
       
   690     else // TWRITE
       
   691     {
       
   692     	fd = open(path, O_WRONLY);
       
   693     
       
   694     }
       
   695     User::After(500000);
       
   696     if(!self->iBlocked)
       
   697     {
       
   698         if(fd < 0)
       
   699     	   {
       
   700     	      self->ERR_PRINTF2 ( _L("open error :%d"), fd);
       
   701     	   }
       
   702     	   else
       
   703     	   {
       
   704     	    close(fd);
       
   705     		err = KErrNone;
       
   706     	   }
       
   707     }
       
   708     else
       
   709     {
       
   710     	close(fd);
       
   711    	    err = KErrGeneral;
       
   712     }
       
   713    
       
   714     return err;
       
   715 }
       
   716     
       
   717 
       
   718 TInt CTestMkfifo::IntgTest1()
       
   719 {
       
   720 	TInt err = DeleteFifo();
       
   721 	if(err)
       
   722 		{
       
   723 		return err;
       
   724 		}
       
   725 	err = FifoCreate();
       
   726 	return err;
       
   727 }
       
   728 
       
   729 TInt CTestMkfifo::IntgTest2()
       
   730 {
       
   731 	TInt err = DeleteFifo();
       
   732 	if(err)
       
   733 		{
       
   734 		return err;
       
   735 		}
       
   736 	err = FifoCreate();
       
   737 	if(err)
       
   738 		{
       
   739 		return err;
       
   740 		}
       
   741 	err = FifoCreate();
       
   742 	return err;
       
   743 }
       
   744 
       
   745 TInt CTestMkfifo::IntgTest3()
       
   746 {
       
   747 	TInt err = DeleteFifo();
       
   748 	if(err)
       
   749 		{
       
   750 		return err;
       
   751 		}
       
   752 	err = FifoCreate();
       
   753 	if(err)
       
   754 		{
       
   755 		return err;
       
   756 		}
       
   757 	err = FifoCreateInNonDir();
       
   758 	return err;
       
   759 }
       
   760 
       
   761 TInt CTestMkfifo::IntgTest4()
       
   762 {
       
   763 	TInt err = DeleteFifo();
       
   764 	if(err)
       
   765 		{
       
   766 		return err;
       
   767 		}
       
   768 	err = FifoCreate();
       
   769 	if(err)
       
   770 		{
       
   771 		return err;
       
   772 		}
       
   773 	err = CreateThreadL();
       
   774 	return err;
       
   775 }
       
   776 
       
   777 TInt CTestMkfifo::IntgTest5()
       
   778 {
       
   779 	TInt err = DeleteFifo();
       
   780 	if(err)
       
   781 		{
       
   782 		return err;
       
   783 		}
       
   784 	err = FifoCreate();
       
   785 	if(err)
       
   786 		{
       
   787 		return err;
       
   788 		}
       
   789 	err = FifoOpenWNonBlock();
       
   790 	return err;
       
   791 }
       
   792 
       
   793 TInt CTestMkfifo::IntgTest6()
       
   794 {
       
   795 	TInt err = DeleteFifo();
       
   796 	if(err)
       
   797 		{
       
   798 		return err;
       
   799 		}
       
   800 	err = FifoCreate();
       
   801 	if(err)
       
   802 		{
       
   803 		return err;
       
   804 		}
       
   805 	err = FifoOpenRNonBlock();
       
   806 	return err;
       
   807 }
       
   808 
       
   809 TInt CTestMkfifo::IntgTest7()
       
   810 {
       
   811 	TInt err = DeleteFifo();
       
   812 	if(err)
       
   813 		{
       
   814 		return err;
       
   815 		}
       
   816 	err = FifoCreate();
       
   817 	if(err)
       
   818 		{
       
   819 		return err;
       
   820 		}
       
   821 	err = FifoClose();
       
   822 	if(err)
       
   823 		{
       
   824 		return err;
       
   825 		}
       
   826 	err = DeleteFifo();
       
   827 	return err;
       
   828 }
       
   829 
       
   830 //********************************************************************//
       
   831 //******CTestMkfifo::TMkfifoReaderWriter******************************//
       
   832 //**mkfifo test using pthreads - 2 threads one reader and one writer**//
       
   833 //********************************************************************//
       
   834 static char path[MAXPATHLEN+1];
       
   835 const mode_t mode = 0666;
       
   836 int bufsize;
       
   837 static void *reader(void* arg) 
       
   838 {
       
   839 	arg = arg;
       
   840 	int fd;
       
   841 	int n;
       
   842 	char *buf;
       
   843 	TInt ret = KErrNone;
       
   844 	if ((fd = open(path, O_RDONLY, mode)) < 0) 
       
   845 		{
       
   846 		char *ptr = strerror(errno);
       
   847 		unlink(path);
       
   848 		ret = -1;
       
   849 		}	
       
   850 	if ((buf = (char *)malloc(bufsize)) == NULL)
       
   851 			{
       
   852 			char *ptr = strerror(errno);
       
   853 			}
       
   854 	do
       
   855 	{
       
   856 		if ((n = read(fd, buf, bufsize)) < 0)
       
   857 			{
       
   858 			ret = -1;
       
   859 			}
       
   860 		bufsize -= n;
       
   861 	}while(bufsize != 0);
       
   862 	close(fd);
       
   863 	free(buf);	
       
   864 	return (void *)ret;
       
   865 }
       
   866 
       
   867 static void *writer(void *arg) 
       
   868 {
       
   869 	arg = arg;
       
   870 	char *buf;
       
   871 	int fd;
       
   872 	int n = 0;
       
   873 	char t = 'A';
       
   874 	TInt ret = KErrNone;
       
   875 	if ((fd = open(path, O_WRONLY, mode)) < 0)
       
   876 		{
       
   877 		char *ptr = strerror(errno);
       
   878 		unlink(path);
       
   879 		ret = -1;
       
   880 		}
       
   881 	if ((buf = (char *)malloc(bufsize)) == NULL)
       
   882 			{
       
   883 			char *ptr = strerror(errno);
       
   884 			}			
       
   885 	memset(buf, t, bufsize);
       
   886 	n = write(fd, buf, bufsize);
       
   887 	if (n < 0) 
       
   888 		{
       
   889 		ret = -1;
       
   890 		}
       
   891 	close(fd);
       
   892 	free(buf);
       
   893 	return (void*)ret;
       
   894 }
       
   895 
       
   896 TInt CTestMkfifo::TMkfifoReaderWriter()
       
   897 {
       
   898 	pthread_t new_th[2];
       
   899 	bufsize = 1024;
       
   900 	TInt ret = KErrNone;
       
   901 	sprintf(path, "c:\\fifo.%d.%d", getpid(), 1);	
       
   902 	
       
   903 	if (mkfifo(path, mode) < 0)
       
   904 		{
       
   905 		INFO_PRINTF2(_L("errno %d\n"), errno);
       
   906 		char *ptr = strerror(errno);
       
   907 		INFO_PRINTF3(_L("mkfifo(%s) %s\n"), path, ptr);
       
   908 		ret = -1;
       
   909 		}
       
   910 	 // Create a write thread. 
       
   911 	 if(pthread_create(&new_th[0], NULL, writer, NULL) != 0)
       
   912 	 	{
       
   913 	  	perror("Error creating write thread \n");
       
   914 	  	return -1;
       
   915 	 	}
       
   916 	 // Create a read thread. 
       
   917 	 if(pthread_create(&new_th[1], NULL, reader, NULL) != 0)
       
   918 	 	{
       
   919 	  	perror("Error creating read thread \n");
       
   920 	  	return -1;
       
   921 	 	}
       
   922 	 // Wait for threads to return 	 
       
   923 	 if(pthread_join(new_th[0], NULL) != 0)//write thread returning
       
   924 	 	{
       
   925 	  	perror("Error in pthread_join() \n");
       
   926 	 	}
       
   927 	 if(pthread_join(new_th[1], NULL) != 0)//read thread returning
       
   928 		 {
       
   929 		 perror("Error in pthread_join() \n");
       
   930 		 }
       
   931 	unlink(path);	
       
   932 	return ret;
       
   933 }
       
   934 
       
   935 
       
   936 //********************************************************************//
       
   937 //******CTestMkfifo::TMkfifoReaderWriter1*****************************//
       
   938 //**mkfifo test using RThreads - 2 threads one reader and one writer**//
       
   939 //********************************************************************//
       
   940 int nread = 0;
       
   941 int nwrite = 0;
       
   942 
       
   943 static int reader1(void *arg) 
       
   944 {
       
   945 	arg = arg;
       
   946 	int fd;
       
   947 	char *buf;
       
   948 	int n = 0;
       
   949 	TInt ret = KErrNone;
       
   950 	
       
   951 	if ((fd = open(path, O_RDONLY, mode)) < 0) 
       
   952 		{
       
   953 		char *ptr = strerror(errno);
       
   954 		unlink(path);
       
   955 		ret = -1;
       
   956 		}
       
   957 	if ((buf = (char *)malloc(bufsize)) == NULL)
       
   958 			{
       
   959 			char *ptr = strerror(errno);
       
   960 			}
       
   961 	do
       
   962 	{
       
   963 		if ((n = read(fd, buf, bufsize)) < 0)
       
   964 			{
       
   965 			ret = -1;
       
   966 			}
       
   967 		bufsize -= n;
       
   968 		nread += n;
       
   969 	}while(bufsize != 0);
       
   970 	close(fd);
       
   971 	free(buf);	
       
   972 	return ret;
       
   973 }
       
   974 
       
   975 static int writer1(void *arg) 
       
   976 {
       
   977 	arg = arg;
       
   978 	int n = 0;
       
   979 	char *buf;
       
   980 	int fd;
       
   981 	char t = 'A';
       
   982 	TInt ret = KErrNone;
       
   983 	if ((fd = open(path, O_WRONLY, mode)) < 0)
       
   984 		{
       
   985 		char *ptr = strerror(errno);
       
   986 		unlink(path);
       
   987 		ret = -1;
       
   988 		}
       
   989 	if ((buf = (char *)malloc(bufsize)) == NULL)
       
   990 			{
       
   991 			char *ptr = strerror(errno);
       
   992 			}			
       
   993 	memset(buf, t, bufsize);
       
   994 	if ((n = write(fd, buf, bufsize)) < 0) 
       
   995 		{
       
   996 		ret = -1;
       
   997 		}
       
   998 	else
       
   999 		{
       
  1000 		nwrite = n;
       
  1001 		}	
       
  1002 	close(fd);
       
  1003 	free(buf);
       
  1004 	return ret;
       
  1005 }
       
  1006 
       
  1007 
       
  1008 
       
  1009 TInt CTestMkfifo::TMkfifoReaderWriter1()
       
  1010 {
       
  1011 	bufsize = 1024;
       
  1012 	TInt ret = KErrNone;
       
  1013 	sprintf(path, "c:\\fifo.%d.%d", getpid(), 1);	
       
  1014 	
       
  1015 	if (mkfifo(path, mode) < 0)
       
  1016 		{
       
  1017 		INFO_PRINTF2(_L("errno %d\n"), errno);
       
  1018 		char *ptr = strerror(errno);
       
  1019 		INFO_PRINTF3(_L("mkfifo(%s) %s\n"), path, ptr);
       
  1020 		ret = -1;
       
  1021 		}
       
  1022 	
       
  1023 	 // Create a write thread. 
       
  1024 	 RThread read, write;
       
  1025 	 write.Create(_L("write"), writer1, 2000, 2000, 4000, NULL);
       
  1026 	 write.Resume();
       
  1027 	 // Create a read thread. 
       
  1028 	 read.Create(_L("read"), reader1, 2000, 2000, 4000, NULL);
       
  1029 	 read.Resume();
       
  1030  	 // Wait for threads to return 
       
  1031 	 TRequestStatus swrite, sread;
       
  1032 	 write.Logon(swrite);
       
  1033 	read.Logon(sread);
       
  1034 	User::WaitForAnyRequest();
       
  1035 	User::WaitForAnyRequest();
       
  1036 	INFO_PRINTF3(_L("\nAll done\n%d %d\n"), nread, nwrite);	
       
  1037 	unlink(path);	
       
  1038 	return ret;
       
  1039 }
       
  1040 TInt CTestMkfifo::FifoCreateNULL1(  )
       
  1041 {
       
  1042 	_LIT(KFunc, "FifoCreateNULL1");
       
  1043     INFO_PRINTF1( KFunc);
       
  1044     
       
  1045     TInt err = mkfifo ("", 0666);
       
  1046     if ( err )
       
  1047         {
       
  1048         	if ( errno == ENOENT )
       
  1049             {
       
  1050             err = KErrNone;
       
  1051             }
       
  1052           else
       
  1053             {
       
  1054             ERR_PRINTF2( _L("mkfifo error: %d"), err);
       
  1055             }
       
  1056         }
       
  1057     return err;
       
  1058 }
       
  1059 
       
  1060 TInt CTestMkfifo::FifoCreate_invalid(  )
       
  1061 {
       
  1062 	_LIT(KFunc, "FifoCreate_invalid");
       
  1063     INFO_PRINTF1( KFunc);
       
  1064     
       
  1065 	char mbcbuf[64];
       
  1066    	mbcbuf[0] = (unsigned char)0x08;
       
  1067     
       
  1068     TInt err = mkfifo (mbcbuf, 0666);
       
  1069     if ( err )
       
  1070         {
       
  1071         	if ( errno == EILSEQ )
       
  1072             {
       
  1073             err = KErrNone;
       
  1074             }
       
  1075           else
       
  1076             {
       
  1077             ERR_PRINTF2( _L("mkfifo error: %d"), err);
       
  1078             }
       
  1079         }
       
  1080     return err;
       
  1081 }
       
  1082 //  End of File
       
  1083