File: utils.c

package info (click to toggle)
gesftpserver 1~ds-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 952 kB
  • sloc: ansic: 7,887; python: 192; makefile: 62; sh: 4
file content (171 lines) | stat: -rw-r--r-- 3,744 bytes parent folder | download | duplicates (5)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * This file is part of the Green End SFTP Server.
 * Copyright (C) 2007, 2010, 2011 Richard Kettlewell
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "sftpserver.h"
#include "utils.h"
#include "debug.h"
#include "alloc.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <syslog.h>
#include <limits.h>
#include <assert.h>

int log_syslog;

int do_read(int fd, void *buffer, size_t size) {
  size_t sofar = 0;
  ssize_t n;
  char *ptr = buffer;

  while(sofar < size) {
    n = read(fd, ptr + sofar, size - sofar);
    if(n > 0)
      sofar += n;
    else if(n == 0)
      return -1;                        /* eof */
    else
      fatal("read error: %s", strerror(errno));
  }
  return 0;                             /* ok */
}

void *xmalloc(size_t n) {
  void *ptr;

  if(n) {
    if(!(ptr = malloc(n)))
      fatal("xmalloc: out of memory (%zu)", n);
    return ptr;
  } else
    return 0;
}

void *xcalloc(size_t n, size_t size) {
  void *ptr;

  if(n && size) {
    if(!(ptr = calloc(n, size)))
      fatal("xcalloc: out of memory (%zu, %zu)", n, size);
    return ptr;
  } else
    return 0;
}

void *xrecalloc(void *ptr, size_t n, size_t size) {
  if(n > SIZE_MAX / size)
    fatal("xrecalloc: out of memory (%zu, %zu)", n, size);
  n *= size;
  if(n) {
    if(!(ptr = realloc(ptr, n)))
      fatal("xrecalloc: out of memory (%zu)", n);
    return ptr;
  } else {
    free(ptr);
    return 0;
  }
}

void *xrealloc(void *ptr, size_t n) {
  if(n) {
    if(!(ptr = realloc(ptr, n)))
      fatal("xrealloc: out of memory (%zu)", n);
    return ptr;
  } else {
    free(ptr);
    return 0;
  }
}

char *xstrdup(const char *s) {
  return strcpy(xmalloc(strlen(s) + 1), s);
}

static void (*exitfn)(int) attribute((noreturn)) = exit;

void fatal(const char *msg, ...) {
  va_list ap;

  va_start(ap, msg);
  if(log_syslog)
    vsyslog(LOG_ERR, msg, ap);
  else {
    fprintf(stderr, "FATAL: ");
    vfprintf(stderr, msg, ap);
    fputc('\n', stderr);
  }
  va_end(ap);
  exitfn(-1);
}

void forked(void) {
  exitfn = _exit;
}

pid_t xfork(void) {
  pid_t pid;

  if((pid = fork()) < 0)
    fatal("fork: %s", strerror(errno));
  if(!pid)
    forked();
  return pid;
}

char *appendn(struct allocator *a, char *s, size_t *ns,
              const char *t, size_t lt) {
  const size_t ls = s ? strlen(s) : 0, need = lt + ls + 1;
  
  if(need > *ns) {
    size_t newsize = *ns ? *ns : 16;

    while(need > newsize && newsize)
      newsize *= 2;
    if(!newsize)
      fatal("appendn: out of memory (%zu, %zu)", ls, need);
    s = sftp_alloc_more(a, s, *ns, newsize);
    *ns = newsize;
  } else {
    // need should always be at least 1 so need<=*ns => *ns > 0 => s != 0.
    assert(s);
  }

  memcpy(s + ls, t, lt);
  s[ls + lt] = 0;
  return s;
}

char *append(struct allocator *a, char *s, size_t *ns, 
             const char *t) {
  return appendn(a, s, ns, t, strlen(t));
}

/*
Local Variables:
c-basic-offset:2
comment-column:40
fill-column:79
indent-tabs-mode:nil
End:
*/