public software.sextractor

[/] [trunk/] [src/] [fits/] [fitscleanup.c] - Blame information for rev 173

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 bertin
/*
2
                                  fitscleanup.c
3
 
4
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
*
6
*       Part of:        The LDAC Tools
7
*
8
*       Author:         E.BERTIN (IAP)
9
*
10
*       Contents:       Signal-catching routines to clean-up temporary files
11
*
12 173 bertin
*       Last modify:    16/07/2007
13 2 bertin
*
14
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15
*/
16
 
17
#ifdef HAVE_CONFIG_H
18
#include        "config.h"
19
#endif
20
 
21
#include <signal.h>
22
#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
 
26
#include "fitscat_defs.h"
27
#include "fitscat.h"
28
 
29
#define CLEANUP_NFILES  64
30
 
31
void    (*exit_func)(void);
32
char    **cleanup_filename;
33
int     cleanup_nfiles;
34
 
35
/****** cleanup_files ********************************************************
36
PROTO   void cleanup_files(void)
37
PURPOSE Remove temporary files on exit.
38
INPUT   -.
39
OUTPUT  -.
40
NOTES   -.
41
AUTHOR  E. Bertin (IAP)
42
VERSION 25/04/2002
43
 ***/
44
void    cleanup_files(void)
45
  {
46
   char         **filename;
47
   int          i;
48
 
49
  filename = cleanup_filename;
50
  for (i=cleanup_nfiles; i--;)
51
    {
52
    remove(*filename);
53
    free(*(filename++));
54
    }
55
  if (cleanup_nfiles)
56
    {
57
    free(cleanup_filename);
58
    cleanup_nfiles = 0;
59
    }
60
 
61
  return;
62
  }
63
 
64
 
65
/****** add_cleanupfilename **************************************************
66
PROTO   void add_cleanupfilename(char *filename)
67
PURPOSE Add a file name to the list of files to be cleaned up at exit.
68
INPUT   pointer to filename char string.
69
OUTPUT  -.
70
NOTES   -.
71
AUTHOR  E. Bertin (IAP)
72
VERSION 10/01/2003
73
 ***/
74
void    add_cleanupfilename(char *filename)
75
  {
76
 
77
  if (!cleanup_nfiles)
78
    {
79
    QMALLOC(cleanup_filename, char *, CLEANUP_NFILES);
80
    }
81
  else if (!(cleanup_nfiles%CLEANUP_NFILES))
82
    {
83
    QREALLOC(cleanup_filename, char *, cleanup_nfiles+CLEANUP_NFILES);
84
    }
85
  QMALLOC(cleanup_filename[cleanup_nfiles], char, MAXCHARS);
86
  strcpy(cleanup_filename[cleanup_nfiles++], filename);
87
 
88
  return;
89
  }
90
 
91
 
92
/****** remove_cleanupfilename ***********************************************
93
PROTO   void remove_cleanupfilename(char *filename)
94
PURPOSE remove a file name from the list of files to be cleaned up at exit.
95
INPUT   pointer to filename char string.
96
OUTPUT  -.
97
NOTES   -.
98
AUTHOR  E. Bertin (IAP)
99 173 bertin
VERSION 16/07/2007
100 2 bertin
 ***/
101
void    remove_cleanupfilename(char *filename)
102
  {
103
   char         **filename2, **filename3;
104
   int          i, j;
105
 
106
  if (!cleanup_nfiles)
107
    return;
108
 
109
/* Search the cleanup filename list for a match */
110
  filename2 = cleanup_filename;
111
  for (i=cleanup_nfiles; i--;)
112
    if (!strcmp(filename, *(filename2++)))
113
      {
114
/* Match found: update the list and free memory is necessary*/
115
      filename3 = filename2 - 1;
116 173 bertin
      free(*filename3);
117 2 bertin
      for (j=i; j--;)
118
        *(filename3++) = *(filename2++);
119
      if (!((--cleanup_nfiles)%CLEANUP_NFILES))
120
        {
121
        if (cleanup_nfiles)
122
          {
123
          QREALLOC(cleanup_filename, char *, cleanup_nfiles);
124
          }
125
        else
126
          free(cleanup_filename);
127
        }
128
      break;
129
      }
130
 
131
  return;
132
  }
133
 
134
 
135
/****** install_cleanup ******************************************************
136
PROTO   void install_cleanup(void (*func)(void))
137
PURPOSE Install the signal-catching and exit routines to start cleanup_files().
138
INPUT   A pointer to a function to be executed on exit.
139
OUTPUT  -.
140
NOTES   Catches everything except STOP and KILL signals.
141
AUTHOR  E. Bertin (IAP)
142
VERSION 25/04/2002
143
 ***/
144
void    install_cleanup(void (*func)(void))
145
  {
146
   void signal_function(int signum);
147
 
148
  exit_func = func;
149
 
150
  atexit(cleanup_files);
151
/* Catch CTRL-Cs */
152
  signal(SIGINT, signal_function);
153
/* Catch bus errors */
154
  signal(SIGBUS, signal_function);
155
/* Catch segmentation faults */
156
  signal(SIGSEGV, signal_function);
157
/* Catch floating exceptions */
158
  signal(SIGFPE, signal_function);
159
 
160
  return;
161
  }
162
 
163
 
164
/****** signal_function ******************************************************
165
PROTO   void signal_function(void)
166
PURPOSE The routine called when a signal is catched. Clean up temporary files
167
        and execute a user-provided function.
168
INPUT   signal number.
169
OUTPUT  -.
170
NOTES   .
171
AUTHOR  E. Bertin (IAP)
172
VERSION 25/04/2002
173
 ***/
174
void    signal_function(int signum)
175
  {
176
  cleanup_files();
177
  if (exit_func)
178
    exit_func();
179
 
180
  switch(signum)
181
    {
182
    case SIGINT:
183
      fprintf(stderr, "^C\n");
184
      exit(-1);
185
    case SIGBUS:
186
      fprintf(stderr, "bus error\n");
187
      exit(-1);
188
    case SIGSEGV:
189
      fprintf(stderr, "segmentation fault\n");
190
      exit(-1);
191
    case SIGFPE:
192
      fprintf(stderr, "floating exception\n");
193
      exit(-1);
194
    default:
195
      exit(-1);
196
    }
197
 
198
  return;
199
  }
200