File: utils.c

package info (click to toggle)
ras 1.03-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 264 kB
  • ctags: 113
  • sloc: ansic: 1,085; sh: 481; makefile: 68
file content (257 lines) | stat: -rw-r--r-- 5,827 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
    ras - Redundant Archive System
    Copyright (C) 1999  Nick Cleaton

    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

    Nick Cleaton <nick@cleaton.net>
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <errno.h>
#include <stddef.h>

#include "common.h"
#include "utils.h"


/*************************************************************************
**
**  Exiting (and ensuring that all output files are removed) if an error
**  occurs.
*/

struct outfile_elt
{
   struct outfile_elt *next;
   char *filename;
};

struct outfile_elt *outfile_linkedlist_head;

/*************************************************************************/

void exit_because_of_error(int errtype)
{
   struct outfile_elt *oe;

   for( oe = outfile_linkedlist_head ; oe ; oe = oe->next )
   {  unlink(oe->filename);
   }

   switch (errtype)
   {
      case INVALID_INPUT:
         exit(2);
      case NOT_ENOUGH:
         exit(1);
      case EXTERNAL_FAILURE:
         exit(2); 
      case INTERNAL_FAILURE:
         exit(2);
      default:
         fprintf(stderr,PROGNAME ": internal failure: invalid arg to exit\n");
	 exit(2);
   }
}

/*************************************************************************/

FILE *open_output_file(char *name)
{
   struct outfile_elt *oe;
   FILE *f;

   oe = nofree(Malloc( sizeof(*oe) ));

   f = fopen(name, "wb");
   if( !f )
   {  fprintf(stderr, PROGNAME ": unable to open %s for output: %s\n",
                                                    name, strerror(errno));
      exit_because_of_error(EXTERNAL_FAILURE);
   }

   /* prepend new filename to linked list */
   oe->filename = name;
   oe->next = outfile_linkedlist_head;
   outfile_linkedlist_head = oe;

   return f;
}

/**************************************************************************/

void init_unlink_list()
{
   outfile_linkedlist_head = NULL;
}

/**************************************************************************/



/*************************************************************************
**
**  Testing for error conditions
*/

void myassert(int cond, char *msg)
{
   if( !cond )
   {  fprintf(stderr, PROGNAME ": assert failed: %s\n",msg);
      exit_because_of_error(INTERNAL_FAILURE);
   }
}

/*************************************************************************/



/**************************************************************************
**
**  Keeping track of how many segments there are in the archive, and giving
**  an error if 2 things that fix how many segments there must be disagree.
*/

static int segment_count = -1;  /*  -1 indicates not fixed yet  */

static char *segment_count_source;

int get_segment_count()
{
   return segment_count;
}

void register_segment_count(int count, char *source)
{
   if( (count < 2) || (count > MAX_SEGMENT_COUNT) )
   {  fprintf(stderr,PROGNAME ": %s specifies a segment count of %i, "
                              "out of bounds\n", source, count);
      exit_because_of_error(INVALID_INPUT);
   }

   if( segment_count == -1 )
   {  segment_count = count;
      segment_count_source = source;
   }
   else if( segment_count != count )
   {  fprintf(stderr,PROGNAME ": %s specifies a segment count of %i,\n"
                     PROGNAME ": but %s has already fixed it at %i\n",
		     source, count, segment_count_source, segment_count );
      exit_because_of_error(INVALID_INPUT);
   }
}

/*****************************************************************************/




/**************************************************************************
**
**  String manipulation
*/

unsigned char fit_int_in_uchar(int i)
{
   if( (i<0) || (i>255) )
   {  fprintf(stderr,PROGNAME ": can't fit int %i in a uchar\n", i);
      exit_because_of_error(INTERNAL_FAILURE);
   }
   return (unsigned char) i;
}

/*************************************************************************/

int countchar(char *str, char c)
{
   int count =0;

   while( *str )
      if(*str++ == c)
         count++;

   return count;
}

/**************************************************************************/

char *popfirstword(char **s, char sep)
{
   char *first_word;

   if( !**s )
      return NULL;

   first_word = *s;
   while( **s && (**s != sep) )
      (*s)++;

   if( **s )
   {  **s = '\0';
      (*s)++;
   }

   return first_word;
}

/*************************************************************************/




/**************************************************************************
**
**  Memory allocation
*/

void *Malloc(size_t bytes)
{  
   void *p;
   p = malloc(bytes);
   if( !p )
   {  fprintf(stderr, PROGNAME ": out of memory\n");
      exit_because_of_error(EXTERNAL_FAILURE);
   }
   return p;
}

/*************************************************************************/

char *Strdup(char *s)
{
   char *a;
   
   a = Malloc(strlen(s) + 1);
   strcpy(a, s);
   return a;
}

/*************************************************************************/

void *nofree(void *ptr)
{
   return ptr;
}

/*************************************************************************/