Mercurial > trustbridge
annotate common/portpath.c @ 981:947a880ec5d3
(issue72) Set font color in red circle to white. Cleanup old code.
author | Emanuel Schuetze <emanuel@intevation.de> |
---|---|
date | Fri, 29 Aug 2014 16:08:33 +0200 |
parents | b3695a3399de |
children | faf58e9f518b |
rev | line source |
---|---|
404 | 1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU GPL (v>=2) | |
5 * and comes with ABSOLUTELY NO WARRANTY! | |
6 * See LICENSE.txt for details. | |
7 */ | |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
8 #include "portpath.h" |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
9 |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
10 #include <libgen.h> |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
11 #include <limits.h> |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
12 #include <stdio.h> |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
13 #include <stdlib.h> |
168
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
14 #include <sys/stat.h> |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
15 #include <sys/types.h> |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
16 #include <unistd.h> |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 char * |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 port_dirname(char *path) |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 { |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
22 #ifndef _WIN32 |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
23 return dirname(path); |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 #else |
164
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
25 char drive[_MAX_DRIVE]; |
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
26 char dir[_MAX_DIR]; |
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
27 _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
|
28 size_t dlen = strlen(dir); |
3343ddf43f42
Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents:
169
diff
changeset
|
29 if ((dlen > 0) && |
3343ddf43f42
Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents:
169
diff
changeset
|
30 ((dir[dlen-1] == '/') || (dir[dlen-1] == '\\'))) |
3343ddf43f42
Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents:
169
diff
changeset
|
31 dir[dlen-1] = '\0'; |
164
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
32 /* We assume: drive + dir is shorter than |
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
33 * drive + dir + fname + ext */ |
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
34 sprintf(path, "%s%s", drive, dir); |
6d64d7e9fa32
Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents:
146
diff
changeset
|
35 return path; |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
36 #endif |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
37 } |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
38 |
975
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
39 bool |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
40 port_mkdir(const char *path) |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
41 { |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
42 #ifndef _WIN32 |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
43 return mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0; |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
44 #else |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
45 /* TODO */ |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
46 printf("Should make path: %s\n", path); |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
47 return false; |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
48 #endif |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
49 } |
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
50 |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
51 char * |
975
b3695a3399de
(issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents:
404
diff
changeset
|
52 port_realpath(const char *path) |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 { |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 #ifndef _WIN32 |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 return realpath(path, NULL); |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
56 #else |
169
701b7036c5dc
Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents:
168
diff
changeset
|
57 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
|
58 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
|
59 return fp; |
701b7036c5dc
Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents:
168
diff
changeset
|
60 else |
701b7036c5dc
Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents:
168
diff
changeset
|
61 return NULL; |
146
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 #endif |
306e4db11761
Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
63 } |
168
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
64 |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
65 bool |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
66 port_fileexits(char *path) |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
67 { |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
68 int ret; |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
69 #ifndef _WIN32 |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
70 struct stat sb; |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
71 ret = stat(path, &sb); |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
72 #else |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
73 struct _stat sb; |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
74 ret = _stat(path, &sb); |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
75 #endif |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
76 |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
77 if (ret == 0) |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
78 return true; |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
79 else |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
80 return false; |
f100861dad8f
Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents:
166
diff
changeset
|
81 } |
176
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
82 |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
83 bool |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
84 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
|
85 { |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
86 int ret; |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
87 #ifndef _WIN32 |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
88 struct stat sb; |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
89 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
|
90 #else |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
91 struct _stat sb; |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
92 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
|
93 #endif |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
94 |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
95 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
|
96 return true; |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
97 else |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
98 return false; |
70d627e9e801
New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents:
170
diff
changeset
|
99 } |