File: xreadlink.c

package info (click to toggle)
limo 0.3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 144 kB
  • ctags: 159
  • sloc: ansic: 1,666; makefile: 80
file content (38 lines) | stat: -rw-r--r-- 657 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int xreadlink(char *from, char **to, size_t size) { 

  int retchars;

  if ( !to || !from ) {
    errno = EINVAL;
    return -1;
  }

  if ( !size || !*to ) {
    size = 256;
    *to = (char*)malloc(size);
    if ( !*to ) 
      return -1;

    retchars = readlink(from,*to,size);

    while ( retchars >= size ) { 
      size *= 2;
      free(*to);
      *to = (char*)malloc(size);
      if ( !*to ) { 
	return -1;
      }
      retchars = readlink(from,*to,size);
    }

    return size;
  } else {
    retchars = readlink(from,*to,size);
    (*to)[retchars] = '\0';
    return retchars;
  }
}