File: debug.c

package info (click to toggle)
gesftpserver 1~ds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 896 kB
  • sloc: ansic: 7,887; python: 192; makefile: 60; sh: 4
file content (106 lines) | stat: -rw-r--r-- 2,589 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
/*
 * This file is part of the Green End SFTP Server.
 * Copyright (C) 2007, 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
 */

/** @file debug.c @brief Debug support implementation */

#include "sftpserver.h"

#include "debug.h"
#include <ctype.h>
#include <stdarg.h>
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <string.h>

/** @brief Output file for debug information */
static FILE *debugfp;

const char *sftp_debugpath;

int sftp_debugging;

/** @brief Ensure that @ref debugfp is open */
static void opendebug(void) {
  assert(sftp_debugging);
  if(!debugfp) {
    if(sftp_debugpath) {
      int fd;

      if((fd = open(sftp_debugpath, O_WRONLY|O_CREAT|O_TRUNC, 0600)) >= 0)
        debugfp = fdopen(fd, "w");
      else
        fprintf(stderr, "%s: %s\n", sftp_debugpath, strerror(errno));
    }
    if(!debugfp)
      debugfp = stderr;
  }
}

void sftp_debug_hexdump(const void *ptr, size_t n) {
  const unsigned char *p = ptr;
  size_t i, j;
  char buffer[80], *output;

  opendebug();
  for(i = 0; i < n; i += 16) {
    output = buffer;
    output += sprintf(output, "%4lx ", (unsigned long)i);
    for(j = 0; j < 16; ++j)
      if(i + j < n)
	output += sprintf(output, " %02x", p[i + j]);
      else {
        strcpy(output, "   ");
        output += 3;
      }
    strcpy(output, "  ");
    output += 2;
    for(j = 0; j < 16; ++j)
      if(i + j < n)
        *output++ = isprint(p[i + j]) ? p[i+j] : '.';
    *output++ = '\n';
    *output = 0;
    fputs(buffer, debugfp);
  }
  fflush(debugfp);
}

void sftp_debug_printf(const char *fmt, ...) {
  va_list ap;
  const int save_errno = errno;

  opendebug();
  va_start(ap, fmt);
  vfprintf(debugfp, fmt, ap);
  va_end(ap);
  fputc('\n', debugfp);
  fflush(debugfp);
  errno = save_errno;
}

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