File: file.c

package info (click to toggle)
jack-tools 20101210-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 504 kB
  • sloc: ansic: 4,678; makefile: 122; lisp: 48; sh: 16
file content (65 lines) | stat: -rw-r--r-- 1,263 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*  file.c - (c) rohan drape, 2005-2006 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <stdint.h>
#include <netinet/in.h> 

#include "memory.h"
#include "failure.h"
#include "file.h"

bool file_exists_p(const char *filename)
{
  struct stat buf;
  int err = stat(filename, &buf);
  return err == 0;
}

#define GENERATE_STAT_FIELD(type,name)			\
  type stat_##name(const char *filename)		\
  {							\
    struct stat buf;					\
    int err = stat(filename, &buf);			\
    if(err) {						\
      fprintf(stderr, "stat() failed: %s\n", filename);	\
      FAILURE;						\
    }							\
    return buf.st_##name;				\
  }

GENERATE_STAT_FIELD(time_t, mtime);
GENERATE_STAT_FIELD(size_t, size);

int xpipe(int filedes[2])
{
  int err = pipe(filedes);
  if(err) {
    perror("pipe() failed");
    FAILURE;
  }
  return err;
}

ssize_t xwrite(int filedes, const void *buffer, size_t size)
{
  ssize_t err = write(filedes, buffer, size);
  if(err == -1) {
    perror("write() failed");
    FAILURE; 
  }
  return err;
}

ssize_t xread(int filedes, void *buffer, size_t size)
{
  ssize_t err = read(filedes, buffer, size);
  if(err == -1) {
    perror("read() failed");
    FAILURE; 
  }
  return err;
}