c - 在c中实现ls命令

标签 c linux shell segmentation-fault ls

我正在努力处理我的代码。我需要在我的 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/

相关文章:

c - 关闭堆栈保护

c# - for循环中可能发生的事情

linux - 来自 TP-LINK 路由器的 CSI(信道状态信息)

linux - 一个目录中的所有文件,linux

c - 将 GCC 内联汇编与采用立即值的指令一起使用

linux - 如何在目录中单独获取最新的文件名?

C系统无法运行hello world

Linux 通配符行为不正确

linux - 在不指定字符数限制的情况下检查 shell 脚本中的数字是否为十六进制 [bash]

c - sizeof 返回意外值