1 /* Copyright (C) 2006 Claus-Justus Heine.
2  *
3  * This file is part of Geomview.
4  *
5  * Geomview is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * Geomview is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with Geomview; see the file COPYING.  If not, write
17  * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
18  * USA, or visit http://www.gnu.org.
19  */
20 
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 
25 #undef lstat
26 
27 #if HAVE_SYS_TYPES_H
28 # include <sys/types.h>
29 #endif
30 #if HAVE_STRING_H
31 # include <string.h>
32 #endif
33 #if HAVE_LIMITS_H
34 # include <limits.h>
35 #endif
36 #if HAVE_SYS_STAT_H
37 # include <sys/stat.h>
38 #endif
39 #if HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #if HAVE_ERRNO_H
43 # include <errno.h>
44 #endif
45 
46 #if !HAVE_DECL_ERRNO
47 extern int errno;
48 #endif
49 
50 #ifndef PATH_MAX
51 # define PATH_MAX 1024
52 #endif
53 #ifndef ENOENT
54 # define ENOENT 255
55 #endif
56 #ifndef ENAMETOOLONG
57 # define ENAMETOOLONG 255
58 #endif
59 #ifndef ENOSYS
60 # define ENOSYS 255
61 #endif
62 
gv_lstat(const char * path,struct stat * buf)63 int gv_lstat(const char *path, struct stat *buf)
64 {
65 #if !LSTAT_FOLLOWS_SLASHED_SYMLINK
66 	size_t pathlen;
67 	char mypath[PATH_MAX+1];
68 #endif
69 
70 #if HAVE_LSTAT_EMPTY_STRING_BUG
71 	if (path == NULL || *path == '\0') {
72 		errno = ENOENT;
73 		return -1;
74 	}
75 #endif
76 #if !LSTAT_FOLLOWS_SLASHED_SYMLINK
77 	pathlen = strlen(path);
78 	if (pathlen > 0 && path[pathlen-1] == '/') {
79 		if (pathlen == PATH_MAX) {
80 			errno = ENAMETOOLONG;
81 			return -1;
82 		}
83 		memcpy(mypath, path, pathlen);
84 		path = mypath;
85 		mypath[pathlen++] = '.';
86 		mypath[pathlen++] = '\0';
87 	}
88 #endif
89 #if HAVE_LSTAT
90 	return lstat(path, buf);
91 #else
92 	errno = ENOSYS;
93 	return -1;
94 #endif
95 }
96