| 1 |
/* fnutil.c - file name utilities |
| 2 |
(Originally composed from several files in Lars Wirzenius' publib.) |
| 3 |
|
| 4 |
Copyright © 1994 Lars Wirzenius. All rights reserved. |
| 5 |
Copyright © 2004 Antti-Juhani Kaijanaho. All rights reserved. |
| 6 |
|
| 7 |
Redistribution and use in source and binary forms, with or without |
| 8 |
modification, are permitted provided that the following conditions |
| 9 |
are met: |
| 10 |
|
| 11 |
1. Redistributions of source code must retain the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer. |
| 13 |
|
| 14 |
2. Redistributions in binary form must reproduce the above |
| 15 |
copyright notice, this list of conditions and the following |
| 16 |
disclaimer in the documentation and/or other materials provided |
| 17 |
with the distribution. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 20 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 23 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 25 |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 |
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
#include <assert.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <pwd.h> |
| 36 |
#include <stdio.h> |
| 37 |
#include <stdlib.h> |
| 38 |
#include <string.h> |
| 39 |
#include <unistd.h> |
| 40 |
|
| 41 |
#ifndef USERNAME_MAX |
| 42 |
#define USERNAME_MAX 9 |
| 43 |
#endif |
| 44 |
|
| 45 |
char *fnbase(const char *fname) |
| 46 |
{ |
| 47 |
char *base; |
| 48 |
|
| 49 |
assert(fname != NULL); |
| 50 |
base = strrchr(fname, '/'); |
| 51 |
if (base == NULL) |
| 52 |
return (char *) fname; |
| 53 |
return base+1; |
| 54 |
} |
| 55 |
|
| 56 |
char * fnqualify(char const * path) |
| 57 |
{ |
| 58 |
size_t len, size; |
| 59 |
struct passwd *pwd; |
| 60 |
const char *p; |
| 61 |
|
| 62 |
assert(path != NULL); |
| 63 |
|
| 64 |
/* Is it qualified already? */ |
| 65 |
if (path[0] == '/') { |
| 66 |
return strdup(path); |
| 67 |
} |
| 68 |
|
| 69 |
/* Do we just need to prepend the current directory? */ |
| 70 |
if (path[0] != '~') { |
| 71 |
char * cwd = get_current_dir_name(); |
| 72 |
if (cwd == 0) return 0; |
| 73 |
len = strlen(cwd); |
| 74 |
size = len + 1 + strlen(path) + 1; /* +2 for '/' and '\0' */ |
| 75 |
char * res = malloc(size); |
| 76 |
if (res == 0) { |
| 77 |
free(cwd); |
| 78 |
return 0; |
| 79 |
} |
| 80 |
sprintf(res, "%s/%s", cwd, path); |
| 81 |
return res; |
| 82 |
} |
| 83 |
|
| 84 |
/* We need to do tilde substitution, get the password entry (which |
| 85 |
includes the name of the home directory) */ |
| 86 |
if (path[1] == '\0' || path[1] == '/') { |
| 87 |
pwd = getpwuid(getuid()); |
| 88 |
if (path[1] == '\0') |
| 89 |
p = path + 1; |
| 90 |
else |
| 91 |
p = path + 2; |
| 92 |
} else { |
| 93 |
|
| 94 |
p = strchr(path, '/'); |
| 95 |
if (p == NULL) |
| 96 |
p = strchr(path, '\0'); |
| 97 |
size = (size_t) (p-path); |
| 98 |
char * username = malloc(size); |
| 99 |
if (username == 0) { |
| 100 |
errno = ENOMEM; |
| 101 |
return 0; |
| 102 |
} |
| 103 |
memcpy(username, path+1, size); |
| 104 |
username[size-1] = '\0'; |
| 105 |
|
| 106 |
pwd = getpwnam(username); |
| 107 |
if (*p == '/') |
| 108 |
++p; |
| 109 |
free(username); |
| 110 |
} |
| 111 |
if (pwd == NULL) { |
| 112 |
errno = ENOENT; |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/* Now we have all the necessary information, build the result */ |
| 118 |
size = strlen(pwd->pw_dir) + 1 + strlen(p) + 1; |
| 119 |
char * result = malloc(size); |
| 120 |
if (result == 0) return 0; |
| 121 |
sprintf(result, "%s/%s", pwd->pw_dir, p); |
| 122 |
return result; |
| 123 |
} |