annotate common/portpath.c @ 288:c4a989a0d6cf

Ensure that the utf8 string is always 0 terminated.
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 02 Apr 2014 13:49:04 +0000
parents 70d627e9e801
children 17e1c8f37d72
rev   line source
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
1 #include "portpath.h"
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
2
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
3 #include <libgen.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
4 #include <limits.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
5 #include <stdio.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
6 #include <stdlib.h>
168
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
7 #include <sys/stat.h>
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
8 #include <sys/types.h>
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
9 #include <unistd.h>
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
10
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
11
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
12 char *
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
13 port_dirname(char *path)
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
14 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
15 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
16 return dirname(path);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
17 #else
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
18 char drive[_MAX_DRIVE];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
19 char dir[_MAX_DIR];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
20 _splitpath(path, drive, dir, NULL, NULL);
170
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
21 size_t dlen = strlen(dir);
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
22 if ((dlen > 0) &&
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
23 ((dir[dlen-1] == '/') || (dir[dlen-1] == '\\')))
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
24 dir[dlen-1] = '\0';
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
25 /* We assume: drive + dir is shorter than
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
26 * drive + dir + fname + ext */
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
27 sprintf(path, "%s%s", drive, dir);
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
28 return path;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
29 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
30 }
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
31
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
32 char *
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
33 port_realpath(char *path)
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
34 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
35 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
36 return realpath(path, NULL);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
37 #else
169
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
38 char *fp = _fullpath(NULL, path, 0);
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
39 if (port_fileexits(fp))
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
40 return fp;
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
41 else
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
42 return NULL;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
43 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
44 }
168
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
45
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
46 bool
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
47 port_fileexits(char *path)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
48 {
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
49 int ret;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
50 #ifndef _WIN32
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
51 struct stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
52 ret = stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
53 #else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
54 struct _stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
55 ret = _stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
56 #endif
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
57
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
58 if (ret == 0)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
59 return true;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
60 else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
61 return false;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
62 }
176
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
63
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
64 bool
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
65 port_isdir(char *path)
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
66 {
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
67 int ret;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
68 #ifndef _WIN32
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
69 struct stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
70 ret = stat(path, &sb);
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
71 #else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
72 struct _stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
73 ret = _stat(path, &sb);
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
74 #endif
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
75
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
76 if ((ret == 0) && S_ISDIR(sb.st_mode))
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
77 return true;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
78 else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
79 return false;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
80 }

http://wald.intevation.org/projects/trustbridge/