librsync  2.0.1
util.c
1 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  *
3  * librsync -- the library for network deltas
4  *
5  * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 
23  /*
24  | On heroin, I have all the answers.
25  */
26 
27 
28 #include "config.h"
29 #include <sys/types.h>
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <sys/stat.h>
35 
36 #include "librsync.h"
37 #include "util.h"
38 #include "trace.h"
39 
40 void
41 rs_bzero(void *buf, size_t size)
42 {
43  memset(buf, 0, size);
44 }
45 
46 
47 void *
48 rs_alloc_struct0(size_t size, char const *name)
49 {
50  void *p;
51 
52  if (!(p = malloc(size))) {
53  rs_fatal("couldn't allocate instance of %s", name);
54  }
55  rs_bzero(p, size);
56  return p;
57 }
58 
59 
60 
61 void *
62 rs_alloc(size_t size, char const *name)
63 {
64  void *p;
65 
66  if (!(p = malloc(size))) {
67  rs_fatal("couldn't allocate instance of %s", name);
68  }
69 
70  return p;
71 }
72 
73 
74 void *
75 rs_realloc(void *ptr, size_t size, char const *name)
76 {
77  void *p;
78 
79  if (!(p = realloc(ptr, size))) {
80  rs_fatal("couldn't reallocate instance of %s", name);
81  }
82  return p;
83 }
84 
85 
86 #ifdef HAVE_FSTATI64
87 # ifdef stat
88 # undef stat
89 # endif
90 # define stat _stati64
91 # ifdef fstat
92 # undef fstat
93 # endif
94 # define fstat(f,s) _fstati64((f), (s))
95 #elif defined HAVE_FSTAT64
96 # ifdef stat
97 # undef stat
98 # endif
99 # define stat stat64
100 # ifdef fstat
101 # undef fstat
102 # endif
103 # define fstat(f,s) fstat64((f), (s))
104 #endif
105 
106 void
107 rs_get_filesize(FILE *f, rs_long_t *size)
108 {
109  struct stat st;
110  if (size && (fstat(fileno(f), &st) == 0) && (S_ISREG(st.st_mode))) {
111  *size = st.st_size;
112  }
113 }
long long rs_long_t
A long integer type that can handle the largest file offsets.
Public header for librsync.