Back to index

glibc  2.9
readlink.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,94,95,97,2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <unistd.h>
00020 #include <hurd.h>
00021 #include <hurd/paths.h>
00022 #include <fcntl.h>
00023 #include <string.h>
00024 
00025 /* Read the contents of the symbolic link FILE_NAME into no more than
00026    LEN bytes of BUF.  The contents are not null-terminated.
00027    Returns the number of characters read, or -1 for errors.  */
00028 ssize_t
00029 __readlink (file_name, buf, len)
00030      const char *file_name;
00031      char *buf;
00032      size_t len;
00033 {
00034   error_t err;
00035   file_t file;
00036   struct stat64 st;
00037 
00038   file = __file_name_lookup (file_name, O_READ | O_NOLINK, 0);
00039   if (file == MACH_PORT_NULL)
00040     return -1;
00041 
00042   err = __io_stat (file, &st);
00043   if (! err)
00044     if (S_ISLNK (st.st_mode))
00045       {
00046        char *rbuf = buf;
00047 
00048        err = __io_read (file, &rbuf, &len, 0, len);
00049        if (!err && rbuf != buf)
00050          {
00051            memcpy (buf, rbuf, len);
00052            __vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
00053          }
00054       }
00055     else
00056       err = EINVAL;
00057 
00058   __mach_port_deallocate (__mach_task_self (), file);
00059 
00060   if (err)
00061     return __hurd_fail (err);
00062   else
00063     return len;
00064 }
00065 weak_alias (__readlink, readlink)