source:
npl/system/klibc/patches/implement-realpath.patch
@
5fedc40
Last change on this file since 5fedc40 was c5c522c, checked in by , 8 years ago | |
---|---|
|
|
File size: 2.3 KB |
-
usr/include/stdlib.h
From: Ben Hutchings <ben@decadent.org.uk> Date: Sat, 27 Sep 2014 15:04:15 +0100 Subject: [klibc] Implement realpath() Bug-Debian: https://bugs.debian.org/763049 Forwarded: http://www.zytor.com/pipermail/klibc/2016-January/003885.html This is needed as the basis for the readlink -f option. Signed-off-by: Ben Hutchings <ben@decadent.org.uk> ---
a b static __inline__ int grantpt(int __fd) 92 92 return 0; /* devpts does this all for us! */ 93 93 } 94 94 95 __extern char *realpath(const char *, char *); 96 95 97 #endif /* _STDLIB_H */ -
usr/klibc/Kbuild
a b klib-y += vsnprintf.o snprintf.o vsprint 60 60 send.o recv.o \ 61 61 access.o chmod.o chown.o dup2.o mknod.o poll.o rename.o stat.o \ 62 62 lchown.o link.o rmdir.o unlink.o utimes.o lstat.o mkdir.o \ 63 readlink.o select.o symlink.o pipe.o \63 readlink.o realpath.o select.o symlink.o pipe.o \ 64 64 ctype/isalnum.o ctype/isalpha.o ctype/isascii.o \ 65 65 ctype/isblank.o ctype/iscntrl.o ctype/isdigit.o \ 66 66 ctype/isgraph.o ctype/islower.o ctype/isprint.o \ -
new file usr/klibc/realpath.c
- + 1 #include <fcntl.h> 2 #include <limits.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <sys/types.h> 6 #include <unistd.h> 7 8 /* 9 * Note that this requires name to refer to an existing file. This is 10 * correct according to POSIX. However, BSD and GNU implementations 11 * also allow name to refer to a non-existing file in an existing 12 * directory. 13 */ 14 15 char *realpath(const char *name, char *resolved_name) 16 { 17 static const char proc_fd_prefix[] = "/proc/self/fd/"; 18 char proc_fd_name[sizeof(proc_fd_prefix) + sizeof(int) * 3]; 19 int allocated = 0; 20 int fd; 21 ssize_t len; 22 23 /* Open for path lookup only */ 24 fd = open(name, O_PATH); 25 if (fd < 0) 26 return NULL; 27 28 if (!resolved_name) { 29 resolved_name = malloc(PATH_MAX); 30 if (!resolved_name) 31 goto out_close; 32 allocated = 1; 33 } 34 35 /* Use procfs to read back the resolved name */ 36 sprintf(proc_fd_name, "%s%d", proc_fd_prefix, fd); 37 len = readlink(proc_fd_name, resolved_name, PATH_MAX - 1); 38 if (len < 0) { 39 if (allocated) 40 free(resolved_name); 41 resolved_name = NULL; 42 } else { 43 resolved_name[len] = 0; 44 } 45 46 out_close: 47 close(fd); 48 return resolved_name; 49 }
Note: See TracBrowser
for help on using the repository browser.