linux off_t 类型,如何使用printf显示off_t,nlink_t,size_t和其他特殊类型?

在我的程序中,我统计了他们想要的文件并发送了数据。统计信息的字段struct均为特殊类型:

struct stat {

dev_t st_dev; /* ID of device containing file */

ino_t st_ino; /* inode number */

mode_t st_mode; /* protection */

nlink_t st_nlink; /* number of hard links */

uid_t st_uid; /* user ID of owner */

gid_t st_gid; /* group ID of owner */

dev_t st_rdev; /* device ID (if special file) */

off_t st_size; /* total size, in bytes */

blksize_t st_blksize; /* blocksize for file system I/O */

blkcnt_t st_blocks; /* number of 512B blocks allocated */

time_t st_atime; /* time of last access */

time_t st_mtime; /* time of last modification */

time_t st_ctime; /* time of last status change */

};

我的问题的相关代码如下:

len = snprintf( statbuf, STAT_BUFFER_SIZE,

"%crwxrwxrwx %lu %u %u %lld %s %s\r\n",

S_ISDIR( filestats.st_mode ) ? 'd' : '-',

(unsigned long ) filestats.st_nlink,

filestats.st_uid,

filestats.st_gid,

(unsigned long long ) filestats.st_size,

date,

filename);

如何以一种便携式且有效的方式打印这些类型?起初,我通过猜测正确的格式说明符来进行无强制转换。除了成为一个烦人的编程习惯外,这还意味着我的代码无法在32位系统上运行。现在,使用转换似乎可以正常工作,但是可以在几个平台上使用?

可以参考以下代码: ``` #include <stdio.h> #include <getopt.h> #include <stdlib.h> #include <unistd.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> #define MAX_PATH 1024 static struct option long_options[] = { {"all", no_argument, NULL, 'a'}, {"list", no_argument, NULL, 'l'}, {"help", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0} }; int main(int argc, char** argv) { int opt; int all_flag = 0, list_flag = 0; char path[MAX_PATH] = "."; while ((opt = getopt_long(argc, argv, "alh", long_options, NULL)) != -1) { switch (opt) { case 'a': all_flag = 1; break; case 'l': list_flag = 1; break; case 'h': printf("Usage: ls [OPTION] [DIRECTORY]\n"); printf("List information about the FILEs (the current directory by default).\n\n"); printf("Options:\n"); printf(" -a, --all do not ignore entries starting with .\n"); printf(" -l, --list use a long listing format\n"); printf(" -h, --help display this help and exit\n"); exit(0); default: break; } } if (argc > optind) { strncpy(path, argv[optind], MAX_PATH); } DIR* dirp = opendir(path); if (dirp == NULL) { printf("Cannot open directory: %s\n", path); exit(1); } struct dirent *dp; while ((dp = readdir(dirp)) != NULL) { if (all_flag == 0 && dp->d_name[0] == '.') { continue; } char full_path[MAX_PATH]; snprintf(full_path, sizeof(full_path), "%s/%s", path, dp->d_name); struct stat st; if (lstat(full_path, &st) == -1) { perror("lstat"); continue; } if (list_flag == 1) { char buf[100]; strftime(buf, sizeof(buf), "%b %d %H:%M", localtime(&st.st_mtime)); printf("%c%s %ld %s %s %ld %s %s\n", S_ISDIR(st.st_mode) ? 'd' : '-', st.st_mode & S_IRUSR ? "r" : "-", st.st_mode & S_IWUSR ? "w" : "-", st.st_mode & S_IXUSR ? "x" : "-", st.st_mode & S_IRGRP ? "r" : "-", st.st_mode & S_IWGRP ? "w" : "-", st.st_mode & S_IXGRP ? "x" : "-", st.st_mode & S_IROTH ? "r" : "-", st.st_mode & S_IWOTH ? "w" : "-", st.st_mode & S_IXOTH ? "x" : "-", st.st_nlink, getpwuid(st.st_uid)->pw_name, getgrgid(st.st_gid)->gr_name, st.st_size, buf, dp->d_name); } else { printf("%s ", dp->d_name); } } closedir(dirp); printf("\n"); return 0; } ``` 希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值