我正在尝试键入一个C程序,它将显示用户对命令行上键入的文件的权限。为了测试它,我在main函数中键入了文件的路径名。它显示错误,说“无法统计Comp_Sci / project_1 / test.c”
我检查了目录,它确实包含一个名为test.c的文件,所以我认为错误在代码中。
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <sys/stat.h>
int filedata(const char *pathname) { // call the filedata function with the parameter pathname
// Use octarray for determining if permission bits set
static short octarray[9] = { 0400, 0200, 0100, 0040, 0020, 0010, 0004, 0002, 0001 };
// mnemonic codes for file permission, 10 characters long because of terminating null
static char perms[10] = "rwxrwxrwx";
struct stat statbuf; // statbuf is the name of our stat structure
char descrip[10]; // descrip is an array of chars of length 10
int j;
if (stat(pathname, &statbuf) == -1) {
fprintf(stderr, "Couldn't stat %s\n", pathname);
return (-1);
}
// put permissions into readable form
for (j = 0; j < 9; j++) {
// test whether permission was set using bitwise AND
if (statbuf.st_mode & octarray[j]) {
descrip[j] = perms[j];
}
else {
descrip[j] = '-';
}
} // end for-loop
descrip[9] = '\0'; // Make sure we have a string
// Display file information
printf("\nFile %s :\n", pathname);
//printf("\nSize %ld bytes\n", statbuf.st_size);
//printf("User_id %d, Group_id %d\n\n", statbuf.st_uid, statbuf.st_gid);
printf("Permissions: %s\n", descrip);
return (0);
}
int main (int argc, char *argv[]) {
filedata ("Comp_Sci/project_1/test.c");
return(0);
}