mkdir fails with tilde on OS X in C?

前端 未结 1 383
再見小時候
再見小時候 2020-12-21 18:29

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          


        
相关标签:
1条回答
  • 2020-12-21 19:10

    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.

    0 讨论(0)
提交回复
热议问题