鱼C论坛

 找回密码
 立即注册
查看: 2584|回复: 3

linux文件系统的系统调用编程练习

[复制链接]
发表于 2021-11-13 17:00:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
四、Linux中与文件系统相关的系统调用
1.通过使用man命令,查阅以下的系统调用的使用手册。
        文件操作
open, close, read, write, seek
creat, truncate, mknod, dup, dup2
link, unlink, rename, symlink
chmod, chown, umask
fcntl, flock, fstat, lstat, stat, utime
fsync, fdatasync
        目录操作
mkdir, chdir, rmdir
readdir, getdents
        库函数
fopen, fclose, fread, fwrite, fscanf, fprintf, fseek ,ftell, feof等

2.        文件系统的系统调用的编程练习
利用上面的系统调用,试写出自己的命令程序,完成以下功能(要求至少完成2例)
        How to create a file?
        How to delete a file? (rm 命令)
        How to copy one file to another file? (cp 命令)
        How to rename a file? (mv file 命令)
        How to truncate a file (or make it be of length zero)?
        How to append something to a file?
        How to lock a file? (read lock, write lock)
        How to generate a unique (temporary) file?
        How to create a directory? (mkdir 命令)
        How to remove a directory? (rmdir 命令)
        How to traverse a directory (or visit every file under it)? (ls –lR 命令)

写两个程序实现两个功能,咋写,求教大佬!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-11-13 17:17:47 | 显示全部楼层
没看懂题目要求
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-11-13 17:32:23 | 显示全部楼层
  1. #include <sys/stat.h>
  2. #include <sys/types.h>
  3. #include <errno.h>

  4. int main(int argc, char *argv[])
  5. {
  6.         int rc;

  7.         if (argc != 2) {
  8.                 printf("\nUsage: myrm <filename> \n\n");
  9.                 return 1;
  10.         }

  11. /* We do not check the permission here.  Even a file has permission
  12.    000, it can still be removed without any warning */

  13.         rc = unlink(argv[1]);
  14.         if (rc) {
  15.                 printf("Error: %s\n", strerror(errno));
  16.                 return 2;
  17.         }
  18.         return 0;
  19. }
复制代码

就好比这是一个实现删除文件功能的程序,再写一个其他什么功能的程序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-13 18:04:28 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>

  4. int main(int argc, char *argv[]) {
  5.     int rc;

  6.     if(argc != 3) {
  7.         printf("\nUsage: mymv <filename> <filename> \n\n");
  8.         return 1;
  9.     }

  10.     /* We do not check the permission here.  Even a file has permission
  11.        000, it can still be removed without any warning */

  12.     rc = rename(argv[1], argv[2]);
  13.     if(rc) {
  14.         printf("Error: %s\n", strerror(errno));
  15.         return 2;
  16.     }
  17.     return 0;
  18. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 00:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表