~ubuntu-branches/ubuntu/maverick/grafx2/maverick

« back to all changes in this revision

Viewing changes to realpath.c

  • Committer: Bazaar Package Importer
  • Author(s): Gürkan Sengün
  • Date: 2010-03-22 12:07:47 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100322120747-g0jel6vf6mjkc53s
Tags: 2.2-1
* New upstream version, fixes FTBFS with binutils-gold. (Closes: #554742)
* Bump standards version to 3.8.4.
* debian/control: Add liblua5.1-0-dev and pkg-config to build depends.
* debian/rules: Drop dh_desktop call.
* debian/copyright: Update years.
* Switch to dpkg-source format version 3.0 (quilt).
* debian/watch: Added.
* Added patch to fix spelling errors in source code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <limits.h>
2
 
#include <stdlib.h>
3
 
#include <errno.h>
4
 
#include <fcntl.h>
5
 
#include <string.h>
6
 
#include <unistd.h>
7
 
 
8
 
#if defined(__AROS__) || defined(__BEOS__) || defined(__MORPHOS__) || defined(__GP2X__) || defined(__amigaos__)
9
 
// These platforms don't have realpath().
10
 
// We use the following implementation, found in:
11
 
// http://amiga.sourceforge.net/amigadevhelp/FUNCTIONS/GeekGadgets/realpath/ex02_realpath.c
12
 
//
13
 
// When tested on Debian, this piece of code doesn't resolve
14
 
// symbolic link in the filename itself, only on the directories in
15
 
// the path. So this implementation is limited, it's really better to
16
 
// use realpath() if your platform has it.
17
 
  
18
 
    #if defined(__GP2X__)
19
 
        // This is a random default value ...
20
 
        #define PATH_MAX 32768
21
 
    #endif
22
 
  
23
 
    static char *sep(char *path)
24
 
    {
25
 
        char *tmp, c;
26
 
        
27
 
        tmp = strrchr(path, '/');
28
 
        if(tmp) {
29
 
            c = tmp[1];
30
 
            tmp[1] = 0;
31
 
            if (chdir(path)) {
32
 
                return NULL;
33
 
            }
34
 
            tmp[1] = c;
35
 
            
36
 
            return tmp + 1;
37
 
        }
38
 
        return path;
39
 
    }
40
 
 
41
 
    char *Realpath(const char *_path, char *resolved_path)
42
 
    {
43
 
        int fd = open(".", O_RDONLY), l;
44
 
        char current_dir_path[PATH_MAX];
45
 
        char path[PATH_MAX], lnk[PATH_MAX], *tmp = (char *)"";
46
 
        
47
 
        if (fd < 0) {
48
 
            return NULL;
49
 
        }
50
 
        getcwd(current_dir_path,PATH_MAX);
51
 
        strncpy(path, _path, PATH_MAX);
52
 
        
53
 
        if (chdir(path)) {
54
 
            if (errno == ENOTDIR) {
55
 
                #if defined(__WIN32__) || defined(__MORPHOS__) || defined(__amigaos__)
56
 
                    // No symbolic links and no readlink()
57
 
                    l = -1;
58
 
                #else
59
 
                    l = readlink(path, lnk, PATH_MAX);
60
 
                    #endif
61
 
                    if (!(tmp = sep(path))) {
62
 
                        resolved_path = NULL;
63
 
                        goto abort;
64
 
                    }
65
 
                    if (l < 0) {
66
 
                        if (errno != EINVAL) {
67
 
                            resolved_path = NULL;
68
 
                            goto abort;
69
 
                        }
70
 
                    } else {
71
 
                        lnk[l] = 0;
72
 
                        if (!(tmp = sep(lnk))) {
73
 
                            resolved_path = NULL;
74
 
                            goto abort;
75
 
                        }
76
 
                    }
77
 
            } else {
78
 
                resolved_path = NULL;
79
 
                goto abort;
80
 
            }
81
 
        }
82
 
        
83
 
        if(resolved_path==NULL) // if we called realpath with null as a 2nd arg
84
 
            resolved_path = (char*) malloc( PATH_MAX );
85
 
                
86
 
        if (!getcwd(resolved_path, PATH_MAX)) {
87
 
            resolved_path = NULL;
88
 
            goto abort;
89
 
        }
90
 
        
91
 
        if(strcmp(resolved_path, "/") && *tmp) {
92
 
            strcat(resolved_path, "/");
93
 
        }
94
 
        
95
 
        strcat(resolved_path, tmp);
96
 
      abort:
97
 
        chdir(current_dir_path);
98
 
        close(fd);
99
 
        return resolved_path;
100
 
    }
101
 
            
102
 
#elif defined (__WIN32__)
103
 
// Mingw has a working equivalent. It only has reversed arguments.
104
 
    char *Realpath(const char *_path, char *resolved_path)
105
 
    {
106
 
        return _fullpath(resolved_path,_path,260);
107
 
    }
108
 
#else
109
 
// Use the stdlib function.
110
 
    char *Realpath(const char *_path, char *resolved_path)
111
 
    {
112
 
        return realpath(_path, resolved_path);
113
 
    }
114
 
#endif
115
 
 
116