mkdir在C的OS X上出现代字号失败? [英] mkdir fails with tilde on OS X in C?
问题描述
我正在将C库移植到OSX,到目前为止,这还没有让我感到很头疼.在下一个功能中:
I'm porting a C library to OSX which haven't given me much of a headache until now. In the next function:
int createDirectory( char *directory ){
int error;
error = mkdir(directory, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if( error < 0 ){
if( errno != EEXIST ){
return errno;
}
}
return error;
}
无论directory
是什么,mkdir()
始终会因EPERM
失败(不允许操作).我不确定xcode可执行文件是否已沙盒化或缺少什么内容,但是传递给该函数的每条路径均失败.
No matter what directory
is, mkdir()
always fails with EPERM
(Operation not permitted). I'm not sure if the xcode executable is sandboxed or if I'm missing something, but every path I pass to the function fails.
我尝试从终端mkdir,并且创建文件夹没有问题,所以我不确定问题出在哪里.此功能在Linux和Solaris上正常运行.
I've tried to mkdir from the terminal and the folders are created without a problem, so I'm not sure where the problem is. This function works fine in Linux and Solaris.
示例路径:
"~/Library/Application\\ Support/myApp"
"~/Desktop/myApp"
第一个是库应创建的目录的实际示例.
The first one is an actual example of a directory the library should create.
推荐答案
OSX不会像bash
那样扩展'~'
字符(尽管它使用了bash
).
OSX does not expand the '~'
character as bash
does (although it uses bash
).
给出该程序,该程序在/tmp
中运行:
Given this program, running in /tmp
:
#include <stdlib.h>
#include <sys/stat.h>
#include <stdio.h>
int main(void)
{
char *given = "~/Library";
char result[1024];
char *s;
mkdir("~", 0755);
mkdir("~/Library", 0755);
if ((s = realpath(given, result)) != 0) {
printf ("%s\n", s);
} else {
perror("realpath");
}
return 0;
}
我在OSX上得到了这个结果:
I get this result on OSX:
/private/tmp/~/Library
我在Linux(Debian)以及Solaris 10上都得到了这个结果:
I get this result on Linux (Debian) as well as with Solaris 10:
/tmp/~/Library
如 中所述,为什么波浪号(〜)用双引号引起来吗? ,这最初是csh
shell 功能,其中bash
As noted in Why doesn't the tilde (~) expand inside double quotes?, this was originally a csh
shell feature which bash
incorporated long ago (citing a page from 1994). It is not implemented in any of the given systems' runtime libraries.
这篇关于mkdir在C的OS X上出现代字号失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!