English 中文(简体)
在C实施“不能够起诉”
原标题:Implementing a Unix shell in C: check if file is executable
  • 时间:2011-01-07 19:51:42
  •  标签:
  • c
  • unix

我正致力于在C和Im实施一个“不完美”方案,目前正在处理相对道路问题。 值得注意的是,在投入指挥的同时。 现在,我必须每次都走到可起诉的全部道路上,那时候,我会简单地把“ls”或“cat”。

我已设法获得PATH的现成变量。 我的想法是,将变数分为“:”性质,然后在档案存在并且可以执行的情况下,对指挥名称进行每一次新的插图和检查。

For example if my PATH is: "/bin:/usr/bin" and I input "ls", I would like the program to check first if "/bin/ls" exists and is executable, if not move on to "/usr/bin/".

两个问题:

(1) 这样做是否好? (这必然是最好的。) 我只想确保它能够发挥作用。

2) 更重要的是, 如果档案存在并且可以执行,我如何在C进行检查?

我希望我足够清楚,......非常感谢:

最佳回答

页: 1 进行这种检查是错误的;它必然受到种族条件的影响。 相反,try,以适当的exec-family calls执行。 如果不能执行,你就错了。

还注意到您无需查询<代码>PATH;execvp。 你们可以这样做。

问题回答

? Psh。 途径比你需要多。

查阅access( systation。

if (access(filename, F_OK|X_OK) == 0)
{
    /* You can execute this file. */
}

请注意,档案存取或存档的任何检查都包含 >固有种族条件。 您可以要求<代码>execve。 此人在检查之后没有取消可起诉的范围,改变档案的所有权,删除档案等。 在你撰写法典并决定如何处理错误条件时,铭记这一点。

Use the stat function: http://linux.die.net/man/2/stat

e.g:

struct stat sb;
if(!stat(path, &sb))
{
  if(IS_REG(sb.st_mode) && sb.st_mode & 0111)
    printf("%s is executable
", path);
}

Call stat on the full pathname, and if it returns 0 (success), the target exists. In that case you can use the st_mode field in the returned structure to test whether the target is a directory, device, named pipe, or ordinary file. If it s a file, st_mode will also contain the permission bits. (If it s a directory, some "executable" bits might be set, but that would imply "searchable" rather than "executable".)

<><>Edit>: 正如其他人指出的,这取决于种族条件,并且有更好的办法实现你想要在这种特定情况下做的工作。

你们如何创造这一进程?

www.un.org/Depts/DGACM/index_spanish.htm ∗∗∗∗∗

The access() answer is the only sensible one. If you use stat, you ll have to parse /etc/group to find out all the groups you are in BESIDE getgid() and test them together with the group-executable bit.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签