gpt4 book ai didi

c - 在 C 中实现 ls 命令

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:25 25 4
gpt4 key购买 nike

我正在为我的代码而苦苦挣扎。我需要在我的 C 中实现 lsls -lls -ils -R 命令程序。到目前为止,我只做了 ls -lls -ils -R,因为我不确定如何实现 ls 因为没有参数。挑战还在于,在执行程序后,用户应该能够键入 % myls -l 并获得与命令 ls -l 相同的结果。

现在我还收到了段错误 11 :(

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
#include <sys/ioctl.h>

DIR * dir;
struct dirent * ptr;

int main(int argc,char *argv[])
{
// declaration for different parameters

void _l(char *direct);
void _i(char *direct);
void _R(char *direct);

// get the argc[]
char para[20];
char direct[100];
memset(direct, 0, sizeof(direct));
// copy the parameter like -l
strcpy(para, argv[1]);
// copy the path like ./
strcpy(direct, argv[2]);

// call the corresponding function according to parameters

if (strcmp(para, "-l") == 0)
{
_l(direct);
}
if (strcmp(para, "-i") == 0)
{
_i(direct);
}
if (strcmp(para, "-R") == 0)
{
_R(direct);
}


}


void _l(char *direct)
{
void mode_to_letter(int mode, char *str);
// define struct stat
struct stat fst;
struct tm * mytime = (struct tm *) malloc(sizeof(struct tm));
char str[12];
dir = opendir(direct);
// skip . and ..
readdir(dir);
readdir(dir);
while((ptr = readdir(dir)) != NULL)
{
stat(ptr->d_name, &fst);
// permission information
mode_to_letter(fst.st_mode, str);
// file type and permission
printf("%s", str);
// file hard links
printf(" %d", fst.st_nlink);
// file's owner
printf(" %s", getpwuid(fst.st_uid)->pw_name);
// file's owner group
printf(" %s", getgrgid(fst.st_gid)->gr_name);
// file size
printf(" %ld", (long)fst.st_size);
// file time
mytime = localtime(&fst.st_mtime);
printf(" %d-%02d-%02d %02d:%02d", mytime->tm_year + 1900, mytime->tm_mon + 1, mytime->tm_mday, mytime->tm_hour, mytime->tm_min);
// file name
printf(" %s", ptr->d_name);
printf("\n");
}
}

void mode_to_letter(int mode, char *str)
{
str[0] = '-';

// judge file type
if(S_ISDIR(mode)) str[0] = 'd';
if(S_ISCHR(mode)) str[0] = 'c';
if(S_ISBLK(mode)) str[0] = 'b';

// judge permission for owner
if(mode & S_IRUSR) str[1] = 'r';
else str[1] = '-';
if(mode & S_IWUSR) str[2] = 'w';
else str[2] = '-';
if(mode & S_IXUSR) str[3] = 'x';
else str[3] = '-';

// judge permission for owner group
if(mode & S_IRGRP) str[4] = 'r';
else str[4] = '-';
if(mode & S_IWGRP) str[5] = 'w';
else str[5] = '-';
if(mode & S_IXGRP) str[6] = 'x';
else str[6] = '-';

// judge permission for others
if(mode & S_IROTH) str[7] = 'r';
else str[7] = '-';
if(mode & S_IWOTH) str[8] = 'w';
else str[8] = '-';
if(mode & S_IXOTH) str[9] = 'x';
else str[9] = '-';

str[10] = '\0';
}

void _i(char *direct)
{
dir = opendir(direct);
readdir(dir);
readdir(dir);
while((ptr = readdir(dir)) != NULL)
{
// get inode id
printf("%ld ", (long)ptr->d_ino);
printf("%s\n", ptr->d_name);
}
closedir(dir);
}



void _R(char *direct)
{
dir = opendir(direct);

while((ptr = readdir(dir)) != NULL)
{
// skip . and ..
if(!strcmp(ptr->d_name,".") || !strcmp(ptr->d_name,".."))
continue;
// if directory
if (ptr->d_type & DT_DIR)
{
printf("%s: \n", ptr->d_name);
// do recursively
_R(ptr->d_name);
}
else
// print the name
printf("%s ", ptr->d_name);
}
}

最佳答案

您正在检查 argv[1]argv[2] 的内容,而没有检查 argv 是否足够长以包含它们。如果它们不存在,则会导致段错误。 argv 的长度作为 argc 传递。一个简单的解决方法(并为普通 ls 调用一个函数)是:

if (argc == 1) {
_noarg(); // implement this function for a plain "ls"
} else {
char para[20];
char direct[100];
memset(direct, 0, sizeof(direct));
// copy the parameter like -l
strcpy(para, argv[1]);
if (argc > 2) {
// copy the path like ./
strcpy(direct, argv[2]);
}
// ... your other if statements that
// call corresponding functions go here
}

然后确保检查_l_i_R 函数中的direct 是否为NULL从中读取,因为这也会导致段错误。如果它为 NULL,您将希望使用当前目录。

关于c - 在 C 中实现 ls 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com