~ubuntu-branches/ubuntu/raring/ifile/raring

« back to all changes in this revision

Viewing changes to error.c

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2004-11-19 23:30:24 UTC
  • Revision ID: james.westby@ubuntu.com-20041119233024-3s7sqpy963jx22eu
Tags: upstream-1.3.4
ImportĀ upstreamĀ versionĀ 1.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is error.c - library for message and error reporting */
 
2
 
 
3
/* ifile - intelligent mail filter for EXMH/MH
 
4
   Copyright (C) 1997  Jason Rennie <jrennie@ai.mit.edu>
 
5
 
 
6
   This program is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU General Public License
 
8
   as published by the Free Software Foundation; either version 2
 
9
   of the License, or (at your option) any later version.
 
10
   
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
   
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
   */
 
20
 
 
21
#include <stdarg.h>     /* va_start(), va_end(), va_list */
 
22
#include <stdio.h>      /* fopen(), fclose() */
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <errno.h>      /* required by SunOS 5.5.1/gcc-2.7.2 for errno var */
 
26
#include <time.h>       /* for srand(time(NULL)) call */
 
27
#include <sys/stat.h>   /* for chmod() call */
 
28
 
 
29
#include <ifile.h>
 
30
 
 
31
extern arguments args;
 
32
 
 
33
FILE * INFO;
 
34
 
 
35
/* Returns a newly allocated character array which contains the
 
36
   portion of FULL_PATH postceeding the last '/' in FULL_PATH,
 
37
   or the entire string if FULL_PATH contains no '/' characters */
 
38
/* written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
 
39
char *
 
40
ifile_strip_path (char * full_path)
 
41
{
 
42
  char buf[MAX_STR_LEN];
 
43
  int buf_index = 0;       /* index of last character in BUF */
 
44
  int full_path_index = 0; /* index of current character in FULL_PATH */
 
45
  char * executable_name;  /* pointer to string to be returned */
 
46
 
 
47
  while (full_path[full_path_index])
 
48
    {
 
49
      buf[buf_index++] = full_path[full_path_index++];
 
50
      if (full_path[full_path_index] == '/')
 
51
        {
 
52
          buf_index = 0;
 
53
          full_path_index++;
 
54
        }
 
55
    }
 
56
  buf[buf_index] = '\0';
 
57
  executable_name = malloc(strlen(buf)+1);
 
58
  strcpy(executable_name, buf);
 
59
 
 
60
  return executable_name;
 
61
}
 
62
 
 
63
/* Opens info log file to report information/errors (if requested) */
 
64
/* Written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
 
65
FILE *
 
66
ifile_open_log (int argc, char ** argv)
 
67
{
 
68
  char *info_file;
 
69
 
 
70
  /* if no temp file is to be created, immediately return */
 
71
  if (!args.tmp_file) return NULL;
 
72
 
 
73
  srand(time(NULL));
 
74
 
 
75
  /* Create string with full path for ~/.ifile_log */
 
76
  info_file = ifile_sprintf("%s/.ifile.log", getenv("HOME"));
 
77
 
 
78
  if ((INFO = fopen(info_file, "w")))
 
79
    {
 
80
      chmod(info_file, 0600);
 
81
      ifile_verbosify(ifile_progress, "%s called\n", IFILE_VERSION);
 
82
      
 
83
      ifile_verbosify(ifile_verbose, "Successfully opened log file: %s\n",
 
84
                      info_file);
 
85
      return INFO;
 
86
    }
 
87
  else
 
88
    {
 
89
      ifile_verbosify(ifile_quiet, "Not able to open log file: %s  Error: %d\n", info_file, errno);
 
90
      return NULL;
 
91
    }
 
92
}
 
93
 
 
94
 
 
95
/* Closes info log file if necessary */
 
96
/* Written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
 
97
void
 
98
ifile_close_log ()
 
99
{
 
100
  if (!args.tmp_file) return;
 
101
 
 
102
  fclose(INFO);
 
103
  ifile_verbosify(ifile_verbose, "Closed log file.\n");
 
104
}
 
105
 
 
106
 
 
107
/* Print the printf-style FORMAT string and arguments on STDERR, only if
 
108
   ARGS->VERBOSITY is equal or greater than the argument VERBOSITY_LEVEL. */
 
109
/* Written by Jason Rennie <jrennie@ai.mit.edu> for ifile
 
110
   adapted from bow_verbosify() - written by Andrew Kachites McCallum */
 
111
int
 
112
ifile_verbosify (int verbosity_level, const char *format, ...)
 
113
{
 
114
  int ret = 0;
 
115
  va_list ap;
 
116
 
 
117
  /* The following few lines makes it possible to see the priority
 
118
     levels of the various lines of output */
 
119
 
 
120
  if (verbosity_level <= args.verbosity)
 
121
    {
 
122
      va_start (ap, format);
 
123
      ret = vfprintf (stderr, format, ap);
 
124
      va_end (ap);
 
125
    }
 
126
  fflush (stderr);
 
127
 
 
128
  if ((verbosity_level <= args.verbosity) ||
 
129
      (verbosity_level <= ifile_progress))
 
130
    {
 
131
      va_start (ap, format);
 
132
      if (INFO != NULL) vfprintf (INFO, format, ap);
 
133
      va_end (ap);
 
134
    }
 
135
  if (INFO != NULL) fflush (INFO);
 
136
 
 
137
  return ret;
 
138
}
 
139
 
 
140
 
 
141
/* Reports an error in printf style format and halts the program */
 
142
/* strerror() is currently not used to report errors because Sun machines
 
143
   will not compile the function for some reason */
 
144
/* Written by Jason Rennie <jrennie@ai.mit.edu> for ifile
 
145
   adapted from bow_error() - written by Andrew Kachites McCallum */
 
146
void
 
147
ifile_error (const char *format, ...)
 
148
{
 
149
  va_list ap;
 
150
 
 
151
  va_start (ap, format);
 
152
  if (INFO) vfprintf (INFO, format, ap);
 
153
  va_end (ap);
 
154
  /*  fprintf("Error: %s\n", strerror(errno)); */
 
155
 
 
156
  va_start (ap, format);
 
157
  vfprintf (stderr, format, ap);
 
158
  va_end (ap);
 
159
  /*  fprintf(stderr, "Error: %s\n\n", strerror(errno)); */
 
160
 
 
161
  ifile_close_log(INFO);
 
162
 
 
163
  exit (-1);
 
164
}