4 int mkdir_p(char *path
, mode_t mode
)
13 if (stat(path
, &st
) == 0)
18 while ((d
= strchr(d
, '/'))) {
20 err
= stat(path
, &st
) && mkdir(path
, mode
);
27 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
30 static int slow_copyfile(const char *from
, const char *to
)
35 FILE *from_fp
= fopen(from
, "r"), *to_fp
;
40 to_fp
= fopen(to
, "w");
44 while (getline(&line
, &n
, from_fp
) > 0)
45 if (fputs(line
, to_fp
) == EOF
)
57 int copyfile(const char *from
, const char *to
)
67 if (st
.st_size
== 0) /* /proc? do it slowly... */
68 return slow_copyfile(from
, to
);
70 fromfd
= open(from
, O_RDONLY
);
74 tofd
= creat(to
, 0755);
78 addr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fromfd
, 0);
79 if (addr
== MAP_FAILED
)
82 if (write(tofd
, addr
, st
.st_size
) == st
.st_size
)
85 munmap(addr
, st
.st_size
);
96 unsigned long convert_unit(unsigned long value
, char *unit
)
118 int readn(int fd
, void *buf
, size_t n
)
120 void *buf_start
= buf
;
123 int ret
= read(fd
, buf
, n
);
132 return buf
- buf_start
;