简体   繁体   English

使用stat来检查文件在C中是否可执行

[英]Using stat to check if a file is executable in C

For homework I have to write a C program and one of the things it has to do is check to see a file exists and if it is executable by the owner. 对于家庭作业,我必须编写一个C程序,它必须做的一件事就是检查文件是否存在以及它是否可由所有者执行。

Using (stat(path[j], &sb) >= 0 I'm able to see if the file indicated by path[j] exists. 使用(stat(path[j], &sb) >= 0我能看到路径[j]指示的文件是否存在。

I've looked through man pages, a lot of questions and answers on stackoverflow, and several websites but I'm not able to wrap my head around exactly how to check if a file is executable using stat. 我查看了man page,stackoverflow上的很多问题和答案,以及几个网站,但我无法完全理解如何使用stat检查文件是否可执行。 I thought it would be as simple as ((stat(path[j], &sb) >= 0) && (sb.st_mode > 0) && (S_IEXEC) but as far as I can tell by testing it, it seems to ignore the fact that these files aren't executable. 我认为它会像((stat(path[j], &sb) >= 0) && (sb.st_mode > 0) && (S_IEXEC)但据我所知,通过测试它似乎忽略了这些文件不可执行的事实。

I think that perhaps stat doesn't work the way I think it does. 我认为也许统计数据不像我认为的那样有效。 Assuming I use stat, how can I go about fixing this? 假设我使用stat,我该怎么办呢?

You can indeed use stat to do this. 你确实可以使用stat来做到这一点。 You just have to use S_IXUSR ( S_IEXEC is an old synonym of S_IXUSR ) to check if you have execute permission. 你只需要使用S_IXUSRS_IEXEC是一个古老的代名词S_IXUSR ),以检查是否有执行权限。 Bitwise AND operator ( & ) checks whether the bits of S_IXUSR are set or not. 按位AND运算符( & )检查是否设置了S_IXUSR的位。

if (stat(file, &sb) == 0 && sb.st_mode & S_IXUSR) 
    /* executable */
else  
    /* non-executable */

Example: 例:

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

int main(int argc, char **argv)
{
    if (argc > 1) {
        struct stat sb;
        printf("%s is%s executable.\n", argv[1], stat(argv[1], &sb) == 0 &&
                                                 sb.st_mode & S_IXUSR ? 
                                                 "" : " not");
    }
    return 0;
}   

尝试:

((stat(path[j], &sb) >= 0) && (sb.st_mode > 0) && (S_IEXEC & sb.st_mode)

We can leverage the libmagic.so library which comes along with the file(1) utility. 我们可以利用随文件(1)实用程序一起提供的libmagic.so库。 It can detect all executable like ELF, bash/python/perl scripts etc 它可以检测所有可执行文件,如ELF,bash / python / perl脚本等

Here's my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "magic.h"

int
main(int argc, char **argv)
{
   struct magic_set *ms;
   const char *result;
   char *desired;
   size_t desired_len;
   int i;
   FILE *fp;

   ms = magic_open(MAGIC_RAW);
   if (ms == NULL) {
      (void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory\n");
      return -1;
   }
   if (magic_load(ms, NULL) == -1) {
      (void)fprintf(stderr, "ERROR loading with NULL file: %s\n", magic_error(ms));
      return 11;
   }

   if (argc > 1) {
      if (argc != 2) {
         (void)fprintf(stderr, "Usage:  ./a.out </path/to/file>\n");
      } else {
         if ((result = magic_file(ms, argv[1])) == NULL) {
            (void)fprintf(stderr, "ERROR loading file %s: %s\n", argv[1], magic_error(ms));
            return -1;
         } else {
             if (strstr(result, (const char *)"executable")) {
                printf("%s: is executable\n", argv[1], result);
             }
         }
      }
   }
   magic_close(ms);
   return 0;
}

$ gcc test.c -I/path/to/magic.h /usr/lib/libmagic.so.1 $ gcc test.c -I / path / to / magic.h /usr/lib/libmagic.so.1

./a.out /bin/ls ./a.out / bin / ls

./a.out a.out ./a.out a.out

./a.out test.c ./a.out test.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM