2008年11月2日 星期日

簡易 embedded linux 程式範例: 模擬 cp 指令,於模組B 上課時實作完成的

這兩天模組B:入式Linux系統實作與程式設計實務的課程內容進入了Embedded Linux 設計的部份了,在這兩天的課程中,我在課堂上實作很多範例,現貼上一支模擬 Linux cp 指令的程式範例,僅供參考^^。這一支範例允許一次自Source File讀寫1024byte的內容,之後複寫到Target File,如此可完整模擬cp 指令功能。程式碼如下:


#define BUFSIZE 1024
int main(int argc , char *argv[])  // cp source target
{
  if( argc == 3 )
  {
   //unsigned char nBYTE;
   char *buf;
    int sourfd, tarfd, readLen;   
   if( (sourfd=open( argv[1], O_RDONLY )) == -1)
   {
     printf( "ERR: source file error\r\n" );
     return 0;
    } 
   if( (tarfd=creat( argv[2] , O_APPEND )) == -1)
   {
     printf( "ERR: Target file error\r\n" );
     return 0;
    } 
    buf = (char *)malloc( BUFSIZE );
    while( (readLen = read (sourfd, (void *)buf, BUFSIZE) ) > 0)
    {
      if( write(tarfd, (void *)buf , readLen ) == -1 )
      {
       printf( "ERR: Copy error\r\n" );
       break;
      }
    } 
   close(sourfd);
   close(tarfd);
   free( buf );
  }
  else
    printf( "USAGE: cp source target[enter]\r\n" );
 return 0;
}
/*
ssize_t read (int filedes, void *buffer, size_t size)
ssize_t write (int filedes, const void *buffer, size_t size)
*/


沒有留言:

張貼留言

FPGA Verilog 的學習經驗,提供給要入門的新手

今天簡單說說 FPGA Verilog 的學習經驗,提供給要入門的新手: 1.對自己寫的FPGA Verilog程式,所生成的數位電路要心中有數。 這一點個人認為很重要,就正如寫 C語言,心中要能生成對應的組合語言一樣,我是這樣要求自己的。 雖然 FPGA Verilog語言...