File: openreadclose.c

package info (click to toggle)
minit 0.10-5
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 224 kB
  • ctags: 139
  • sloc: ansic: 1,655; makefile: 129; sh: 49; python: 33
file content (23 lines) | stat: -rw-r--r-- 464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef EMBEDDED
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#endif

int openreadclose(char *fn, char **buf, unsigned long *len) {
  int fd=open(fn,O_RDONLY);
  if (fd<0) return -1;
  if (!*buf) {
    *len=lseek(fd,0,SEEK_END);
    lseek(fd,0,SEEK_SET);
    *buf=(char*)malloc(*len+1);
    if (!*buf) {
      close(fd);
      return -1;
    }
  }
  *len=read(fd,*buf,*len);
  if (*len != (unsigned long)-1)
    (*buf)[*len]=0;
  return close(fd);
}