使用 stat (st_uid) 的分段错误(核心转储) [英] Segmentation fault (core dumped) using stat (st_uid)

查看:35
本文介绍了使用 stat (st_uid) 的分段错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UNIX 中编写一个简单的 C 脚本,它的工作方式类似于ls -l".我有一个工作部分,其中脚本列出了当前目录中的所有文件:

I would like to write a simple C script in UNIX that will work like "ls -l". I have a working part where the script lists all of the files in the current directory:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>

int main(int argc, char *argv[])
{
 DIR *katalog;
 struct dirent *dir;
 katalog = opendir(".");

if (argc == 1) {
printf("without option");
  if (katalog) {
    while ((dir = readdir(katalog)) {
     printf("%s \n", dir->d_name);
    }
    closedir(katalog);
  }
  return(0);
}    
}

现在我想添加有关 st_gid、st_uid、st_size 和 st_mtime 的信息.我从 st_uid 盯着.我的代码现在看起来像这样(它在 unix 下编译得很好).不幸的是,它给了我一个错误分段错误(核心转储)".我试图在 Stack 和 Internet 中寻找答案,我什至使用了其他线程的一些提示(例如:C 格式问题与 printf("%d", astatbuff->st_size);),但仍然出现错误......我不知道还有什么我可以改修……

Now I wanted to add information about the st_gid, st_uid, st_size and st_mtime. I stared from st_uid. My code looks like that now (it's compiling well under unix). Unfortunely, it gives me an error "Segmentation fault (core dumped)". I tried to look for the answer in the Stack and Internet, and I even used some hints from other threads (for example: C format issue with printf("%d", astatbuff->st_size);), but still the error occurs... I don't know what more I can change to repair it...

这是产生错误的代码:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h> 

int main(int argc, char *argv[])
{
 DIR *katalog;
 struct dirent *dir;
 katalog = opendir(".");
 struct stat *astat;

if (argc == 1) {
printf("Without option");
  if (katalog) {
    while ((dir = readdir(katalog)) != NULL && astat->st_uid != 0) {
     printf("%s %llu \n", dir->d_name, (unsigned long long)astat->st_uid);
    }
    closedir(katalog);
  }
  return(0);
}
}

推荐答案

由于 astat 尚未在 astat->st_uid 之前初始化/分配,代码展示 不确定的行为(UB).在 OP 情况下,代码崩溃了,

As astat is not yet initialize/assign before astat->st_uid, code exhibir undeifed behavior (UB). In OP case, the code crashed,

不是声明一个没有值的指针,而是将代码改为:

Instead of declaring a pointer with no value, code nneds to:

1) 声明一个 struct stat 对象.

1) Declare a struct stat object.

2) 使用 *stat() 调用填充它.ref.

2) Populated it with a *stat() call. ref.

int main(int argc, char *argv[]) {
  DIR *katalog;
  struct dirent *dir;
  katalog = opendir(".");

  //struct stat *astat;

  if (argc == 1) {
    printf("Without option");
    if (katalog) {
      while ((dir = readdir(katalog)) != NULL) {
        // add
        struct stat sb;
        if (lstat(dir->d_name, &sb) == -1) {
          perror("lstat");
          exit(EXIT_FAILURE);
        }
        if (sb.st_uid != 0) {
          printf("%s %llu \n", dir->d_name, (unsigned long long) sb.st_uid);
        }
      }
      closedir(katalog);
    }
  }
  return (0);
}

这篇关于使用 stat (st_uid) 的分段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆