- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 C 程序编写代码来实现 ls-al 命令,我已经获得了在没有打印权限的情况下实现它的代码,但我也想实现权限,但不知道如何实现。有什么建议么?我的代码在下面
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
// Last Modified
time_t t = my_stat.st_mtime;
localtime_r(&t, <);
char timebuf[80];
strftime(timebuf, sizeof(timebuf), "%c", <);
if (pwd != 0) {
printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, current_directory->d_name);
printf("\n");
} else {
printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, current_directory->d_name);
printf("\n");
}
}
closedir(directory);
return 0;
}
int main(int argc, char* argv[]) {
if ( argc == 1 ) {
return list_dir ( "." );
} else {
int ret = 0;
for (int i = 1; i < argc; i += 1 ) {
if ( list_dir ( argv[i] ) != 0 ) {
ret = 1;
}
}
return ret;
}
}
我已经尝试了很长时间才能向这段代码添加权限,但我似乎被卡住了,在这里我没有想法
我的代码的输出是:
kev 0 Thu Jun 20 13:39:49 2019 .
kev 0 Thu Jun 20 13:39:46 2019 ..
kev 24147 Thu Jun 20 12:24:40 2019 CMakeCache.txt
kev 0 Thu Jun 20 13:39:53 2019 CMakeFiles
kev 1426 Thu Jun 20 12:24:41 2019 cmake_install.cmake
kev 5160 Thu Jun 20 12:24:41 2019 Makefile
预期的输出是:
rw-r--r-- 1 kev 0 Thu Jun 20 13:39:49 2019 .
rw-r--r-- 1 kev 0 Thu Jun 20 13:39:46 2019 ..
-rw------- 24147 Thu Jun 20 12:24:40 2019 CMakeCache.txt
rw-r--r-- kev 0 Thu Jun 20 13:39:53 2019 CMakeFiles
-rw------- kev 1426 Thu Jun 20 12:24:41 2019 cmake_install.cmake
rw-r--r-- kev 5160 Thu Jun 20 12:24:41 2019 Makefile
最佳答案
您需要使用 struct stat
的 mode_t st_mode
字段。见统计(2):
The stat structure
All of these system calls return a stat structure, which contains the following fields:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* file type and mode */
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 filesystem I/O */
blkcnt_t st_blocks; /* number of 512B blocks allocated */
/* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */
struct timespec st_atim; /* time of last access */
struct timespec st_mtim; /* time of last modification */
struct timespec st_ctim; /* time of last status change */
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec #define st_ctime st_ctim.tv_sec };
[...]
The file type and mode (st_mode)
POSIX refers to the st_mode bits corresponding to the mask S_IFMT (see below) as the file type, the 12 bits corresponding to the mask 07777 as the file mode bits and the least significant 9 bits (0777) as the file permission bits.
The following mask values are defined for the file type of the st_mode field:
S_IFMT 0170000 bit mask for the file type bit field
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
[...]
The following mask values are defined for the file mode component of the st_mode field:
S_ISUID 04000 set-user-ID bit
S_ISGID 02000 set-group-ID bit (see below)
S_ISVTX 01000 sticky bit (see below)
S_IRWXU 00700 owner has read, write, and execute permission
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others (not in group) have read, write, and execute per‐
mission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
关于c - 如何为我的 ls-al c 程序添加权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56691583/
如果我执行 ls -la,我会得到如下结果 total 16 drwxr-xr-x 4 rockse staff 136 Apr 28 16:55 . drwx------+ 23 rocks
我找到了一些Linux文件,当我键入ls -lah时,它将输出以下权限格式: ... drwxr-xr-x. 2 root root ... -rw-rw-r--. 1 root r
我正在尝试从 python 执行 ls -l 来检查文件的最后修改日期。 os.listdir 不显示长列表格式。subprocess.call 显示格式,但实际上打印它,并返回 0。我希望能够将其放
以下 shell 函数定义卡在 Cygwin 的 bash 控制台 (RHEL/Ubuntu) 中,当它被调用时它只是退出终端。 $ function ls { ls; } $ ls 为什么会出现这种
为什么这两个命令的输出不同: ls | cat ls 第一个似乎用换行符分隔文件名。 这也适用于命令 suc 有 ls > outfile 和类似的东西。 我使用的是 Mac OSX,如果这有什么不同
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我使用 eshell-parse-command 'ls -l' 得到了这个: (progn (eshell-trap-errors (eshell-named-command "l
如何使用 RegExp 在 JavaScript 中实现类似于 alias 的功能? ALIAS = [{pattern: /^ls[$ ]/, replace: 'ls --color '}] s
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在编写一个 shell 程序,当将值传递给 execv() 时,我需要一个指向程序名称的 char 指针(即 ls),我需要一个指向参数的 char 指针数组的指针。 我会检查并解析用户的输入,如
我对使用 bash 编写脚本还比较陌生,并编写了以下脚本: # Set variables logfile=/tmp/peter/logfile.log indir=/mls/indir/IG/ te
所以基本上我有一个任务要使用 strace 进行分析,ls 命令需要哪些系统调用才能提供与 ls -l 相同的信息。 但是对于 ls -l 命令有许多奇怪的系统调用,例如 stat 64 被调用时使用
我在 ubuntu 的文件夹中有一个文件夹列表。IE。/mnt/source/customcode/files/brands/下 root@a919794ec280:/mnt/source/custo
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
#include #include #include #include #include int flag; void catch (int sig) { if (sig == SIGI
我使用 Process 来调用 shell(zsh),我想 cd ~ 或其他一些目录,例如在 shell 中定义的 cd $PROJ_ROOT。但似乎无法处理这些 token 。如何解决这个问题? s
我在我的 Centos5 机器上运行了这个: ls -al & ; ls -al 我期待它在后台运行 ls -al,同时在前台运行 ls -al,并演示终端的输出是如何被破坏的通过这样做。 但是,我得
当我在 PowerShell 中使用 Get-ChildItem 时,我会得到目录中以行格式设置的项目列表。该列表经常会超出屏幕,如果我想查看列表的其余部分或以前的命令和输出,则迫使我向上滚动。我想为
我试图用所有必要的参数绕过调用 JNIEnv->DefineClass 方法,但当我尝试从 java 调用该方法时,它总是返回 java.lang.NoClassDefFoundError: ls.H
我是一名优秀的程序员,十分优秀!