FTW->base 和一些字符串(路径)returns 的总和如何成为一个字符串?
How can sum of FTW->base and some string (path) returns a string?
我这样调用 nftw()
函数:
nftw("/sys/class/tty", opearate_on_tty_item, 1, flags)
和函数 nftw()
对它在文件夹 /sys/class/tty
.
中找到的每个项目执行函数 operate_on_tty_item()
函数 operate_on_tty_item()
直接从 nftw()
获取参数并检查当前 /sys/class/tty/<device>
设备是否有子文件夹 device/driver
(这意味着它是物理device) 并以稍微不同的格式打印此设备,即 /dev/<device>
。该函数本身如下所示:
static int opearate_on_tty_item(const char *path, const struct stat *buffer, int flags, struct FTW *ftw_structure)
{
max_sub_directories = 1;
// If depth is higher than max_sub_directories, ignore this field and advance to the next file.
if (ftw_structure->level > max_sub_directories) {
// Tell nftw() to continue and take next item.
return 0;
}
// Concatenate string of the object's path, to which we append "/device/driver".
strcat(directory_concatenation, path);
strcat(directory_concatenation, "/device/driver");
// Open the directory stored in concatenated string and check if it exists
if ((directory = opendir(directory_concatenation))) {
// Reset concatenated string to null string.
directory_concatenation[0] = '[=12=]';
// Concatenate string.
strcat(directory_concatenation, "/dev/");
strcat(directory_concatenation, path + ftw_structure->base);
printf("%s\n", directory_concatenation);
}
// Close the opened directory.
closedir(directory);
// Reset concatenated string to null string.
directory_concatenation[0] = '[=12=]';
return 0;
}
我不明白的地方是:
strcat(directory_concatenation, path + ftw_structure->base);
这一行总结了 const char * path
和 ftw_structure->base
。但是后面的printf()
printf("%s\n", directory_concatenation);
打印一个字符串(例如/dev/ttyUSB0
)!这怎么可能?这个字符串是如何用一个常量字符指针和一个整数的求和运算创建的?
我完全糊涂了,因为FTW成员FTW.base是在POSIXheaderftw.h
中这样定义的(它是一个整数):
/* Structure used for fourth argument to callback function for `nftw'. */
struct FTW
{
int base;
int level;
};
那么 POSIX 兼容系统如何知道显示字符串呢?这到底是怎么计算的?
How is this possible?
似乎 directory_concatenation
是一个指向包含 /dev/ttyUSB0
后跟零字节的内存的指针。
How is this string created using the sum operation of a constant character pointer and an integer?
整数的值被添加到指针存储的地址,增加指针值。因为从文档中:
The fourth argument that nftw() supplies when calling fn() is a
structure of type FTW:
struct FTW {
int base;
int level;
};
base is the offset of the filename (i.e., basename component) in the
pathname given in fpath
. [...]
计算结果是一个指向表示路径文件名的字符串的指针。然后strcat
在directory_concatenation
指针指向的内存中找到第一个零字节,复制表示该文件名的字节,一个字节一个字节到起始位置从 directory_concatenation
指向的内存中的零字节位置开始。尾随的零字节也被复制,从而创建一个新字符串,将文件名的值连接到前一个字符串。
我这样调用 nftw()
函数:
nftw("/sys/class/tty", opearate_on_tty_item, 1, flags)
和函数 nftw()
对它在文件夹 /sys/class/tty
.
operate_on_tty_item()
函数 operate_on_tty_item()
直接从 nftw()
获取参数并检查当前 /sys/class/tty/<device>
设备是否有子文件夹 device/driver
(这意味着它是物理device) 并以稍微不同的格式打印此设备,即 /dev/<device>
。该函数本身如下所示:
static int opearate_on_tty_item(const char *path, const struct stat *buffer, int flags, struct FTW *ftw_structure)
{
max_sub_directories = 1;
// If depth is higher than max_sub_directories, ignore this field and advance to the next file.
if (ftw_structure->level > max_sub_directories) {
// Tell nftw() to continue and take next item.
return 0;
}
// Concatenate string of the object's path, to which we append "/device/driver".
strcat(directory_concatenation, path);
strcat(directory_concatenation, "/device/driver");
// Open the directory stored in concatenated string and check if it exists
if ((directory = opendir(directory_concatenation))) {
// Reset concatenated string to null string.
directory_concatenation[0] = '[=12=]';
// Concatenate string.
strcat(directory_concatenation, "/dev/");
strcat(directory_concatenation, path + ftw_structure->base);
printf("%s\n", directory_concatenation);
}
// Close the opened directory.
closedir(directory);
// Reset concatenated string to null string.
directory_concatenation[0] = '[=12=]';
return 0;
}
我不明白的地方是:
strcat(directory_concatenation, path + ftw_structure->base);
这一行总结了 const char * path
和 ftw_structure->base
。但是后面的printf()
printf("%s\n", directory_concatenation);
打印一个字符串(例如/dev/ttyUSB0
)!这怎么可能?这个字符串是如何用一个常量字符指针和一个整数的求和运算创建的?
我完全糊涂了,因为FTW成员FTW.base是在POSIXheaderftw.h
中这样定义的(它是一个整数):
/* Structure used for fourth argument to callback function for `nftw'. */
struct FTW
{
int base;
int level;
};
那么 POSIX 兼容系统如何知道显示字符串呢?这到底是怎么计算的?
How is this possible?
似乎 directory_concatenation
是一个指向包含 /dev/ttyUSB0
后跟零字节的内存的指针。
How is this string created using the sum operation of a constant character pointer and an integer?
整数的值被添加到指针存储的地址,增加指针值。因为从文档中:
The fourth argument that nftw() supplies when calling fn() is a structure of type FTW:
struct FTW { int base; int level; };
base is the offset of the filename (i.e., basename component) in the pathname given in
fpath
. [...]
计算结果是一个指向表示路径文件名的字符串的指针。然后strcat
在directory_concatenation
指针指向的内存中找到第一个零字节,复制表示该文件名的字节,一个字节一个字节到起始位置从 directory_concatenation
指向的内存中的零字节位置开始。尾随的零字节也被复制,从而创建一个新字符串,将文件名的值连接到前一个字符串。