2 Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Netatalk utility functions
22 #endif /* HAVE_CONFIG_H */
29 #include <sys/types.h>
34 #include <atalk/adouble.h>
36 #include <atalk/afp.h>
37 #include <atalk/logger.h>
38 #include <atalk/vfs.h>
39 #include <atalk/util.h>
40 #include <atalk/unix.h>
43 * @brief get cwd in static buffer
45 * @returns pointer to path or pointer to error messages on error
47 const char *getcwdpath(void)
49 static char cwd[MAXPATHLEN + 1];
52 if ((p = getcwd(cwd, MAXPATHLEN)) != NULL)
55 return strerror(errno);
59 * Takes a buffer with a path, strips slashs, returns basename
63 * "[/][dir/[...]]file"
65 * "[/][dir/[...]]dir/[/]"
66 * Result is "file" or "dir"
68 * @returns pointer to basename in path buffer, buffer is possibly modified
70 char *stripped_slashes_basename(char *p)
72 int i = strlen(p) - 1;
73 while (i > 0 && p[i] == '/')
75 return (strrchr(p, '/') ? strrchr(p, '/') + 1 : p);
79 * @brief symlink safe chdir replacement
81 * Only chdirs to dir if it doesn't contain symlinks.
83 * @returns 1 if a path element is a symlink, 0 otherwise, -1 on syserror
85 int lchdir(const char *dir)
87 char buf[MAXPATHLEN+1];
88 char cwd[MAXPATHLEN+1];
93 dir is a canonical path (without "../" "./" "//" )
98 if (getcwd(cwd, MAXPATHLEN) == NULL)
106 * chdir request | realpath result | ret
107 * (after getwcwd) | |
108 * =======================================
111 * /a/b/. | /c/d/e/f | 1
113 if (getcwd(buf, MAXPATHLEN) == NULL)
118 /* relative path requested,
121 for (; cwd[i]; i++) {
122 if (buf[i] != cwd[i])
133 for (i = 0; test[i]; i++) {
134 if (test[i] != dir[i]) {