文件系统:目录与文件
This commit is contained in:
34
C13-Linux系统编程/fs/big.c
Normal file
34
C13-Linux系统编程/fs/big.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage:...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int fd;
|
||||
|
||||
fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("open()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// 生成一个5G的文件
|
||||
// !!! 整数溢出
|
||||
lseek(fd, 5LL * 1024LL * 1024LL * 1024LL - 1LL, SEEK_SET);
|
||||
|
||||
// 写入尾0
|
||||
write(fd, "", 1);
|
||||
|
||||
close(fd);
|
||||
exit(0);
|
||||
}
|
||||
41
C13-Linux系统编程/fs/flen.c
Normal file
41
C13-Linux系统编程/fs/flen.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
// !!! 不是int
|
||||
static off_t flen(char *fname)
|
||||
{
|
||||
struct stat statres;
|
||||
|
||||
if (stat(fname, &statres) < 0)
|
||||
{
|
||||
perror("stat()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return statres.st_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取文件大小
|
||||
* @details
|
||||
* Usage: ./flen filename
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage:...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// !!! 通过makefile指定off_t 64位
|
||||
printf("%lld\n", flen(argv[1]));
|
||||
|
||||
exit(0);
|
||||
}
|
||||
54
C13-Linux系统编程/fs/ftype.c
Normal file
54
C13-Linux系统编程/fs/ftype.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
static int ftype(const char *fname)
|
||||
{
|
||||
struct stat statres;
|
||||
|
||||
if (stat(fname, &statres) < 0)
|
||||
{
|
||||
perror("stat()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (S_ISREG(statres.st_mode))
|
||||
return '-';
|
||||
else if (S_ISDIR(statres.st_mode))
|
||||
return 'd';
|
||||
else if (S_ISSOCK(statres.st_mode))
|
||||
return 's';
|
||||
else if (S_ISFIFO(statres.st_mode))
|
||||
return 'p';
|
||||
else if (S_ISBLK(statres.st_mode))
|
||||
return 'b';
|
||||
else if (S_ISCHR(statres.st_mode))
|
||||
return 'c';
|
||||
else if (S_ISLNK(statres.st_mode))
|
||||
return 'l';
|
||||
else
|
||||
return '?';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取文件类型
|
||||
* @details
|
||||
* Usage: ftype <filename>
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage:...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("%c\n", ftype(argv[1]));
|
||||
|
||||
exit(0);
|
||||
}
|
||||
46
C13-Linux系统编程/fs/glob.c
Normal file
46
C13-Linux系统编程/fs/glob.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <glob.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// #define PAT "/etc/a*.conf"
|
||||
#define PAT "/etc/*" // 不包括隐藏文件 .*
|
||||
|
||||
#if 0
|
||||
static int errfunc_(const char *errpath, int eerrno)
|
||||
{
|
||||
puts(errpath);
|
||||
fprintf(stderr, "ERROR MSG: %s\n", strerror(eerrno));
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief ls /etc/a*.conf
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
glob_t globres;
|
||||
int err;
|
||||
|
||||
err = glob(PAT, 0, NULL, &globres);
|
||||
if (err)
|
||||
{
|
||||
printf("ERROR CODE = %d\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (int i = 0; i < globres.gl_pathc; i++)
|
||||
{
|
||||
puts(globres.gl_pathv[i]);
|
||||
}
|
||||
|
||||
// !!!
|
||||
globfree(&globres);
|
||||
exit(0);
|
||||
}
|
||||
15
C13-Linux系统编程/fs/main.c
Normal file
15
C13-Linux系统编程/fs/main.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
printf("argc = %d\n", argc);
|
||||
|
||||
// for (int i = 0; i < argc; i++)
|
||||
for (int i = 0; argv[i] != NULL; i++)
|
||||
{
|
||||
puts(argv[i]);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
125
C13-Linux系统编程/fs/mydu.c
Normal file
125
C13-Linux系统编程/fs/mydu.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include <glob.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define PATHSIZE 1024
|
||||
|
||||
static int path_noloop(const char *path)
|
||||
{
|
||||
char *pos;
|
||||
|
||||
/**
|
||||
* @brief strrchr
|
||||
* @details
|
||||
*
|
||||
* 找到 path 中最后一个 '/' 字符的位置
|
||||
*/
|
||||
pos = strrchr(path, '/');
|
||||
if (NULL == pos)
|
||||
exit(1);
|
||||
|
||||
if ((0 == strcmp(pos + 1, ".") || 0 == strcmp(pos + 1, "..")))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int64_t mydu(const char *path)
|
||||
{
|
||||
// 优化递归
|
||||
// 将部分与递归无关的变量拿出栈,用 static 修饰
|
||||
static struct stat statres;
|
||||
static char nextpath[PATHSIZE];
|
||||
glob_t globres;
|
||||
int64_t sum;
|
||||
int i;
|
||||
|
||||
if (lstat(path, &statres) < 0)
|
||||
{
|
||||
perror("lstat()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!S_ISDIR(statres.st_mode))
|
||||
{
|
||||
/* 非目录 */
|
||||
return statres.st_blocks;
|
||||
/*
|
||||
* st_blocks 代表磁盘块数量,一个磁盘块大小为512字节
|
||||
* 所以除以2才表示多少个1k字节
|
||||
*/
|
||||
}
|
||||
|
||||
/* 目录 */
|
||||
/* 例如:/aaa/bbb/ccc/ddd/eee/fff */
|
||||
|
||||
strncpy(nextpath, path, PATHSIZE);
|
||||
strncat(nextpath, "/*", PATHSIZE); //[W]
|
||||
// if (glob(nextpath, 0, NULL, &globres))
|
||||
// {
|
||||
// perror("glob(), 1");
|
||||
// exit(1);
|
||||
// }
|
||||
glob(nextpath, 0, NULL, &globres);
|
||||
|
||||
// !!! 要考虑 . 和 .. 目录
|
||||
strncpy(nextpath, path, PATHSIZE);
|
||||
strncat(nextpath, "/.*", PATHSIZE); //[W]
|
||||
/*
|
||||
* 这个警告是因为 strncat 函数的第三个参数(即 PATHSIZE)等于目标字符串
|
||||
* nextpath 的大小。这可能会导致 nextpath 缓冲区溢出,因为 strncat
|
||||
* 函数会在目标字符串的末尾添加一个 null 字符('\0')。
|
||||
*/
|
||||
|
||||
// !!! APPEND
|
||||
// if (glob(nextpath, GLOB_APPEND, NULL, &globres))
|
||||
// {
|
||||
// perror("glob(), APPEND");
|
||||
// exit(1);
|
||||
// }
|
||||
glob(nextpath, GLOB_APPEND, NULL, &globres);
|
||||
|
||||
sum = statres.st_blocks;
|
||||
|
||||
for (i = 0; i < globres.gl_pathc; i++)
|
||||
{
|
||||
if (path_noloop(globres.gl_pathv[i]))
|
||||
{
|
||||
sum += mydu(globres.gl_pathv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// !!!
|
||||
globfree(&globres);
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief mydu
|
||||
* @details
|
||||
* Usage: mydu [FILE]...
|
||||
*
|
||||
* 用到了递归的思想
|
||||
*
|
||||
* !!! 未考虑权限问题
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: mydu [FILE]...\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("%lld\n", mydu(argv[1]) / 2);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
38
C13-Linux系统编程/fs/readdir.c
Normal file
38
C13-Linux系统编程/fs/readdir.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define PAT "/etc"
|
||||
|
||||
/**
|
||||
* @brief 解析PAT下文件个数
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
DIR *dp;
|
||||
struct dirent *cur;
|
||||
|
||||
dp = opendir(PAT);
|
||||
if (NULL == dp)
|
||||
{
|
||||
perror("opendir()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// !!! 注意括号
|
||||
while ((cur = readdir(dp)) != NULL)
|
||||
{
|
||||
puts(cur->d_name);
|
||||
}
|
||||
|
||||
closedir(dp);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user