在C程序中使用chmod [英] Using chmod in a C program

查看:230
本文介绍了在C程序中使用chmod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用chmod一个程序,我需要设置文件的权限(比如hello.t),我要读被从文件中设置的权限。为此,我第一次到一个字符数组读取权限,然后试图修改文件(hello.t)的权限。但我看到的权限在一个奇怪的方式设置。

I have a program where i need to set the permissions of a file (say hello.t)using chmod and I have to read the permissions to be set from a file. For this I first read the permissions in to a character array and then try to modify the permissions of the file (hello.t). But I see that permissions are set in a weird manner.

我已经写了一个示例程序:

A sample program I have written:

main()
{
    char mode[4]="0777";
    char buf[100]="/home/hello.t";
    int i;
    i = atoi(mode);
    if (chmod (buf,i) < 0)
        printf("error in chmod");
}

我看到文件的权限设置为777。能否请您帮助我如何从一个字符数组看完后同样设置文件的权限。

I see that the permissions of the file are not set to 777. Can you please help me out on how to set the permissions of the file after reading the same from a character array.

谢谢,结果
拉哈夫

Thanks,
Raghav

推荐答案

的atoi()函数只转换小数,而不是八进制。

The atoi() function only translates decimal, not octal.

有关八进制的转换,使用与strtol()(或如的克里斯小丑,年轻指出, strtoul将() - 尽管文件权限模式适用于Unix的所有有效大小适合16位之内,所以永远产生负与0或8为基数反正)。实际上,在这种情况下,指定8是最好的。它使人们能够写777,并得到正确的八进制值。如果指定的0基地,该字符串777是十进制(再次)。

For octal conversion, use strtol() (or, as Chris Jester-Young points out, strtoul() - though the valid sizes of file permission modes for Unix all fit within 16 bits, and so will never produce a negative long anyway) with either 0 or 8 as the base. Actually, in this context, specifying 8 is best. It allows people to write 777 and get the correct octal value. With a base of 0 specified, the string 777 is decimal (again).

此外:


  • 请不要使用隐式int回报类型的main();所要求的C99和使用是明确的 INT主要(无效) INT主(INT ARGC,字符** argv的)

  • 不要切碎后空关你的字符串玩。

  • Do not use 'implicit int' return type for main(); be explicit as required by C99 and use int main(void) or int main(int argc, char **argv).
  • Do not play with chopping trailing nulls off your string.

char mode[4] = "0777";


这prevents C来自存储终端空 - 坏了!用途:

This prevents C from storing a terminal null - bad! Use:

char mode[] = "0777";

该分配给存储与空终结符所需的5个字节。

This allocates the 5 bytes needed to store the string with a null terminator.

标准错误报告错误,而不是标准输出

把所有的变化一起得出:

Putting all the changes together yields:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

int main(int argc, char **argv)
{
    char mode[] = "0777";
    char buf[100] = "/home/hello.t";
    int i;
    i = strtol(mode, 0, 8);
    if (chmod (buf,i) < 0)
    {
        fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
                argv[0], buf, mode, errno, strerror(errno));
        exit(1);
    }
    return(0);
}

要小心错误号;当函数被调用它可以改变。它是足够安全的在这里,但在许多情况下,这是一个好主意,捕捉错误号成一个局部变量,并使用打印操作等方面的局部变量。

Be careful with errno; it can change when functions are called. It is safe enough here, but in many scenarios, it is a good idea to capture errno into a local variable and use the local variable in printing operations, etc.

请注意过的code确实没有错误的与strtol的结果检查()。在这种情况下,它是足够安全的;如果用户提供的值,这将是一个坏主意,相信他们能够得到它的权利。

Note too that the code does no error checking on the result of strtol(). In this context, it is safe enough; if the user supplied the value, it would be a bad idea to trust them to get it right.

最后一个意见:通常,你不应该使用的文件(或目录)777权限。对于文件,这意味着你不介意谁可以修改你的可执行程序,或怎么样。这通常不是这种情况;你做护理(或应该关心)谁修改你的程序。一般来说,不要使数据文件的可执行所有;当文件是可执行文件,不给公众写访问​​,并在组写访问斜眼看。对于目录,公共写权限意味着你不介意谁删除的任何文件的目录(或添加文件)。再次,偶尔,这可能是正确的权限设置来使用,但很少正确的。 (对于目录,它通常是使用粘性位也是一个好主意:1777权限是通常用于 / tmp目录,例如 - 但不是在MacOS [X]。)

One last comment: generally, you should not use 777 permission on files (or directories). For files, it means that you don't mind who gets to modify your executable program, or how. This is usually not the case; you do care (or should care) who modifies your programs. Generally, don't make data files executable at all; when files are executable, do not give public write access and look askance at group write access. For directories, public write permission means you do not mind who removes any of the files in the directory (or adds files). Again, occasionally, this may be the correct permission setting to use, but it is very seldom correct. (For directories, it is usually a good idea to use the 'sticky bit' too: 1777 permission is what is typically used on /tmp, for example - but not on MacOS X.)

这篇关于在C程序中使用chmod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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