mkdir fails with tilde on OS X in C?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:00:13

问题


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;        
}

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.

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.

Example paths:

"~/Library/Application\\ Support/myApp"
"~/Desktop/myApp"

The first one is an actual example of a directory the library should create.


回答1:


OSX does not expand the '~' character as bash does (although it uses bash).

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;
}

I get this result on OSX:

/private/tmp/~/Library

I get this result on Linux (Debian) as well as with Solaris 10:

/tmp/~/Library

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.



来源:https://stackoverflow.com/questions/30285051/mkdir-fails-with-tilde-on-os-x-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!