文章目录  1. 文件里面存放了5行数据,使用追加模式打开文件,打印前3行,并写入一行,期间使用ftell打印当前位置。 2. 修改文件的权限,注意必须使用命令行参数。 3. 使用两种方法打印当前目录。 4. 传递一个路径名,还有一个文件名,搜索对应路径下是否有该文件,有就打印显示该文件的绝对路径。 5. 传递任意一个目录路径,能够显示该目录的ls -l的效果。 6. 打印当前目录,然后改变当前目录,再打印当前目录。 7. 创建一个目录,然后删除掉它。 8. 实现tree命令的效果。 9. 实现cp -r命令的效果。 10.新建一个文件,里边内容为hello,通过mmap映射该文件后,修改hello为world,然后解除映射   
 
# include <func.h> int  main ( int  argc,  char  *  argv[ ] ) { ARGS_CHECK ( argc, 2 ) ; FILE * fp=  fopen ( "./file1" , "a+" ) ; fseek ( fp, 0 , SEEK_SET ) ; char  line[ 100 ] ; for ( int  i = 0  ; i< 3 ; i++ ) { if ( fgets ( line, sizeof ( line) , fp) != NULL ) { printf ( "%s" , line) ; } } printf ( "Current position :%ld\n" , ftell ( fp) ) ; fprintf ( fp, "This is a new line\n" ) ; fclose ( fp) ; return  0 ; 
} 
fprintf(fp, "This is a new line\n"); 这行代码在最后打印一句话的原因是因为你使用了追加模式打开文件。追加模式会将文件指针定位到文件末尾,所以在你使用 fseek(fp, 0, SEEK_SET); 将文件指针移动到文件开头后,接下来的写操作会将数据追加到文件末尾。因此,即使你已经将文件指针移动到了文件开头,写入的数据仍然会被追加到文件末尾,这就是为什么在结尾处打印一句话的原因。
 # include <func.h> int  main ( int  argc, char * * argv) { ARGS_CHECK ( argc, 3 ) ;    mode_t  mode; sscanf ( argv[ 1 ] , "%o" , & mode) ; int  ret =  chmod ( argv[ 2 ] , mode) ; printf ( "mode  = %o\n" , mode) ; ERROR_CHECK ( ret, NULL , "chmod" ) ; return  0 ; 
} 
 # include <func.h> int  main ( int  argc, char * * argv) { char  * ret =  getcwd ( NULL , 0 ) ; ERROR_CHECK ( ret, NULL , "getcwd" ) ; printf ( "%s\n" , ret) ; return  0 ; 
} 
# include <func.h> int  main ( int  argc, char * * argv) { char  buf[ 1024 ] = { 0 } ; char  * ret =  getcwd ( 0 , sizeof ( buf) ) ; ERROR_CHECK ( ret, NULL , "getcwd" ) ; printf ( "%s\n" , buf) ; printf ( "%s\n" , ret) ; return  0 ; 
} 
# include  <stdio.h> # include  <stdlib.h> # include  <string.h> # include  <unistd.h> # include  <limits.h> # include  <sys/stat.h> # include  <dirent.h> void  search_file ( const  char  * dir_path,  const  char  * file_name)  { DIR * dir =  opendir ( dir_path) ; if  ( dir ==  NULL )  { perror ( "opendir" ) ; return ; } struct  dirent  * entry; struct  stat  statbuf; char  full_path[ PATH_MAX] ; while  ( ( entry =  readdir ( dir) )  !=  NULL )  { snprintf ( full_path,  PATH_MAX,  "%s/%s" ,  dir_path,  entry-> d_name) ; if  ( stat ( full_path,  & statbuf)  ==  - 1 )  { perror ( "stat" ) ; continue ; } if  ( S_ISREG ( statbuf. st_mode)  &&  strcmp ( entry-> d_name,  file_name)  ==  0 )  { char  real_path[ PATH_MAX] ; if  ( realpath ( full_path,  real_path)  ==  NULL )  { perror ( "realpath" ) ; closedir ( dir) ; return ; } printf ( "Found file %s at path: %s\n" ,  file_name,  real_path) ; closedir ( dir) ; return ; } } printf ( "File %s not found in directory %s\n" ,  file_name,  dir_path) ; closedir ( dir) ; 
} int  main ( int  argc,  char  * argv[ ] )  { if  ( argc !=  3 )  { fprintf ( stderr ,  "Usage: %s <directory> <file_name>\n" ,  argv[ 0 ] ) ; return  1 ; } search_file ( argv[ 1 ] ,  argv[ 2 ] ) ; return  0 ; 
} # include  <stdio.h> # include  <stdlib.h> # include  <string.h> # include  <unistd.h> # include  <dirent.h> # include  <sys/stat.h> # include  <pwd.h> # include  <grp.h> # include  <time.h> void  display_ls_l ( const  char  * dir_path)  { DIR * dir =  opendir ( dir_path) ; if  ( dir ==  NULL )  { perror ( "opendir" ) ; return ; } struct  dirent  * entry; struct  stat  statbuf; while  ( ( entry =  readdir ( dir) )  !=  NULL )  { char  full_path[ PATH_MAX] ; snprintf ( full_path,  PATH_MAX,  "%s/%s" ,  dir_path,  entry-> d_name) ; if  ( stat ( full_path,  & statbuf)  ==  - 1 )  { perror ( "stat" ) ; continue ; } printf ( ( S_ISDIR ( statbuf. st_mode) )  ?  "d"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IRUSR)  ?  "r"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IWUSR)  ?  "w"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IXUSR)  ?  "x"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IRGRP)  ?  "r"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IWGRP)  ?  "w"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IXGRP)  ?  "x"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IROTH)  ?  "r"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IWOTH)  ?  "w"  :  "-" ) ; printf ( ( statbuf. st_mode &  S_IXOTH)  ?  "x"  :  "-" ) ; struct  passwd  * pw =  getpwuid ( statbuf. st_uid) ; struct  group  * gr =  getgrgid ( statbuf. st_gid) ; printf ( " %2ld %s %s %8ld %s %s\n" , ( long ) statbuf. st_nlink,  pw-> pw_name,  gr-> gr_name, ( long ) statbuf. st_size,  ctime ( & statbuf. st_mtime) ,  entry-> d_name) ; } closedir ( dir) ; 
} int  main ( int  argc,  char  * argv[ ] )  { if  ( argc !=  2 )  { fprintf ( stderr ,  "Usage: %s <directory>\n" ,  argv[ 0 ] ) ; return  1 ; } display_ls_l ( argv[ 1 ] ) ; return  0 ; 
} # include  <stdio.h> # include  <unistd.h> int  main ( )  { char  cwd[ 1024 ] ; if  ( getcwd ( cwd,  sizeof ( cwd) )  !=  NULL )  { printf ( "Current working directory: %s\n" ,  cwd) ; }  else  { perror ( "getcwd" ) ; return  1 ; } if  ( chdir ( "/tmp" )  !=  0 )  { perror ( "chdir" ) ; return  1 ; } if  ( getcwd ( cwd,  sizeof ( cwd) )  !=  NULL )  { printf ( "New working directory: %s\n" ,  cwd) ; }  else  { perror ( "getcwd" ) ; return  1 ; } return  0 ; 
} 
# include  <stdio.h> # include  <unistd.h> int  main ( )  { const  char  * dir_name =  "test_dir" ; if  ( mkdir ( dir_name,  0777 )  ==  - 1 )  { perror ( "mkdir" ) ; return  1 ; } printf ( "Directory created: %s\n" ,  dir_name) ; if  ( rmdir ( dir_name)  ==  - 1 )  { perror ( "rmdir" ) ; return  1 ; } printf ( "Directory deleted: %s\n" ,  dir_name) ; return  0 ; 
} 
# include  <stdio.h> # include  <stdlib.h> # include  <string.h> # include  <unistd.h> # include  <dirent.h> # include  <sys/stat.h> void  print_tree ( const  char  * path,  int  level)  { DIR * dir =  opendir ( path) ; if  ( dir ==  NULL )  { perror ( "opendir" ) ; return ; } struct  dirent  * entry; while  ( ( entry =  readdir ( dir) )  !=  NULL )  { if  ( strcmp ( entry-> d_name,  "." )  ==  0  ||  strcmp ( entry-> d_name,  ".." )  ==  0 )  { continue ; } struct  stat  statbuf; char  full_path[ PATH_MAX] ; snprintf ( full_path,  PATH_MAX,  "%s/%s" ,  path,  entry-> d_name) ; if  ( stat ( full_path,  & statbuf)  ==  - 1 )  { perror ( "stat" ) ; continue ; } for  ( int  i =  0 ;  i <  level -  1 ;  i++ )  { printf ( "|   " ) ; } if  ( level >  0 )  { printf ( "|-- " ) ; } printf ( "%s\n" ,  entry-> d_name) ; if  ( S_ISDIR ( statbuf. st_mode) )  { print_tree ( full_path,  level +  1 ) ; } } closedir ( dir) ; 
} int  main ( int  argc,  char  * argv[ ] )  { if  ( argc !=  2 )  { fprintf ( stderr ,  "Usage: %s <directory>\n" ,  argv[ 0 ] ) ; return  1 ; } print_tree ( argv[ 1 ] ,  0 ) ; return  0 ; 
} # include  <stdio.h> # include  <stdlib.h> # include  <string.h> # include  <unistd.h> # include  <dirent.h> # include  <sys/stat.h> # include  <fcntl.h> # include  <errno.h> # define  BUF_SIZE  1024 void  copy_file ( const  char  * src_path,  const  char  * dst_path)  { int  src_fd =  open ( src_path,  O_RDONLY) ; if  ( src_fd ==  - 1 )  { perror ( "open" ) ; return ; } int  dst_fd =  open ( dst_path,  O_WRONLY |  O_CREAT |  O_TRUNC,  0666 ) ; if  ( dst_fd ==  - 1 )  { perror ( "open" ) ; close ( src_fd) ; return ; } char  buf[ BUF_SIZE] ; ssize_t  bytes_read,  bytes_written; while  ( ( bytes_read =  read ( src_fd,  buf,  BUF_SIZE) )  >  0 )  { bytes_written =  write ( dst_fd,  buf,  bytes_read) ; if  ( bytes_written !=  bytes_read)  { perror ( "write" ) ; close ( src_fd) ; close ( dst_fd) ; return ; } } if  ( bytes_read ==  - 1 )  { perror ( "read" ) ; } close ( src_fd) ; close ( dst_fd) ; 
} void  copy_dir ( const  char  * src_path,  const  char  * dst_path)  { DIR * dir =  opendir ( src_path) ; if  ( dir ==  NULL )  { perror ( "opendir" ) ; return ; } if  ( mkdir ( dst_path,  0777 )  ==  - 1 )  { perror ( "mkdir" ) ; return ; } struct  dirent  * entry; struct  stat  statbuf; while  ( ( entry =  readdir ( dir) )  !=  NULL )  { if  ( strcmp ( entry-> d_name,  "." )  ==  0  ||  strcmp ( entry-> d_name,  ".." )  ==  0 )  { continue ; } char  src_child_path[ PATH_MAX] ; char  dst_child_path[ PATH_MAX] ; snprintf ( src_child_path,  PATH_MAX,  "%s/%s" ,  src_path,  entry-> d_name) ; snprintf ( dst_child_path,  PATH_MAX,  "%s/%s" ,  dst_path,  entry-> d_name) ; if  ( stat ( src_child_path,  & statbuf)  ==  - 1 )  { perror ( "stat" ) ; continue ; } if  ( S_ISDIR ( statbuf. st_mode) )  { copy_dir ( src_child_path,  dst_child_path) ; }  else  { copy_file ( src_child_path,  dst_child_path) ; } } closedir ( dir) ; 
} int  main ( int  argc,  char  * argv[ ] )  { if  ( argc !=  3 )  { fprintf ( stderr ,  "Usage: %s <source> <destination>\n" ,  argv[ 0 ] ) ; return  1 ; } copy_dir ( argv[ 1 ] ,  argv[ 2 ] ) ; return  0 ; 
} # include  <stdio.h> # include  <stdlib.h> # include  <sys/mman.h> # include  <sys/stat.h> # include  <fcntl.h> # include  <unistd.h> # include  <string.h> int  main ( )  { const  char  * file_path =  "test.txt" ; const  char  * data =  "hello" ; const  char  * new_data =  "world" ; int  fd =  open ( file_path,  O_RDWR |  O_CREAT,  0666 ) ; if  ( fd ==  - 1 )  { perror ( "open" ) ; return  1 ; } write ( fd,  data,  strlen ( data) ) ; close ( fd) ; fd =  open ( file_path,  O_RDWR) ; if  ( fd ==  - 1 )  { perror ( "open" ) ; return  1 ; } struct  stat  sb; if  ( fstat ( fd,  & sb)  ==  - 1 )  { perror ( "fstat" ) ; close ( fd) ; return  1 ; } char  * addr =  mmap ( NULL ,  sb. st_size,  PROT_READ |  PROT_WRITE,  MAP_SHARED,  fd,  0 ) ; if  ( addr ==  MAP_FAILED)  { perror ( "mmap" ) ; close ( fd) ; return  1 ; } memcpy ( addr,  new_data,  strlen ( new_data) ) ; if  ( munmap ( addr,  sb. st_size)  ==  - 1 )  { perror ( "munmap" ) ; close ( fd) ; return  1 ; } close ( fd) ; return  0 ; 
}