linux文件系统的系统调用编程练习
四、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 命令)
写两个程序实现两个功能,咋写,求教大佬! 没看懂题目要求
人造人 发表于 2021-11-13 17:17
没看懂题目要求
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int rc;
if (argc != 2) {
printf("\nUsage: myrm <filename> \n\n");
return 1;
}
/* We do not check the permission here.Even a file has permission
000, it can still be removed without any warning */
rc = unlink(argv);
if (rc) {
printf("Error: %s\n", strerror(errno));
return 2;
}
return 0;
}
就好比这是一个实现删除文件功能的程序,再写一个其他什么功能的程序 #include <stdio.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int rc;
if(argc != 3) {
printf("\nUsage: mymv <filename> <filename> \n\n");
return 1;
}
/* We do not check the permission here.Even a file has permission
000, it can still be removed without any warning */
rc = rename(argv, argv);
if(rc) {
printf("Error: %s\n", strerror(errno));
return 2;
}
return 0;
}
页:
[1]