summaryrefslogtreecommitdiff
path: root/web/funnelAC/fwACsrc/misc.c
blob: ab1a2db9d0a9eb9d7fda4ecac247a86947069205 (plain)
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
/*##############################################################################

FUNNNELWEB COPYRIGHT
====================
FunnelWeb is a literate-programming macro preprocessor.

Copyright (C) 1992 Ross N. Williams.

   Ross N. Williams
   ross@spam.adelaide.edu.au
   16 Lerwick Avenue, Hazelwood Park 5066, Australia.

This program is free software; you can redistribute it and/or modify
it under the terms of Version 2 of the GNU General Public License as
published by the Free Software Foundation.

This program is distributed WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See Version 2 of the GNU General Public License for more details.

You should have received a copy of Version 2 of the GNU General Public
License along with this program. If not, you can FTP the license from
prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Section 2a of the license requires that all changes to this file be
recorded prominently in this file. Please record all changes here.

Programmers:
   RNW  Ross N. Williams  ross@spam.adelaide.edu.au

Changes:
   07-May-1992  RNW  Program prepared for release under GNU GPL V2.
   08-Aug-1995  ABC  Changed include file name from "memory.h"
                     to "fwmem.h" to avoid a clash with the C++
                     standard header file "memory.h".

##############################################################################*/


/******************************************************************************/
/*                                    MISC.C                                  */
/******************************************************************************/

#include <ctype.h>
#include "style.h"

#include "as.h"
#include "data.h"
#include "fwmem.h"
#include "misc.h"
#include "writfile.h"

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

EXPORT void wr_s  (s) char *s; {wf_wr(&f_s,s);}
EXPORT void wr_j  (s) char *s; {wf_wr(&f_j,s);}
EXPORT void wr_l  (s) char *s; {wf_wr(&f_l,s);}

EXPORT void wl_s  (s) char *s; {wf_wr(&f_s,s); wf_wr(&f_s,"\n");}
EXPORT void wl_j  (s) char *s; {wf_wr(&f_j,s); wf_wr(&f_j,"\n");}
EXPORT void wl_l  (s) char *s; {wf_wr(&f_l,s); wf_wr(&f_l,"\n");}

EXPORT void wr_sj (s) char *s; {wr_s(s); wr_j(s);}
EXPORT void wl_sj (s) char *s; {wl_s(s); wl_j(s);}

EXPORT void wr_sjl(s) char *s; {wr_s(s); wr_j(s); wr_l(s);}
EXPORT void wl_sjl(s) char *s; {wl_s(s); wl_j(s); wl_l(s);}

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

EXPORT char *chabbrev(ch)
int ch;
{
 switch (ch)
   {
    case  0: return "NUL";
    case  1: return "SOH";
    case  2: return "STX";
    case  3: return "ETX";
    case  4: return "EOT";
    case  5: return "ENQ";
    case  6: return "ACK";
    case  7: return "BEL";
    case  8: return  "BS";
    case  9: return  "HT";
    case 10: return  "LF";
    case 11: return  "VT";
    case 12: return  "FF";
    case 13: return  "CR";
    case 14: return  "SO";
    case 15: return  "SI";
    case 16: return "DLE";
    case 17: return "DC1";
    case 18: return "DC2";
    case 19: return "DC3";
    case 20: return "DC4";
    case 21: return "NAK";
    case 22: return "SYN";
    case 23: return "ETB";
    case 24: return "CAN";
    case 25: return  "EM";
    case 26: return "SUB";
    case 27: return "ESC";
    case 28: return  "FS";
    case 29: return  "GS";
    case 30: return  "RS";
    case 31: return  "US";
    case 127:return "DEL";
    default: return    "";
   }
}

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

EXPORT sign signof(n)
long n;
{
 if (n<0) return -1;
 if (n>0) return  1;
 return 0;
}

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

EXPORT char * eq_files(name1,name2,p_result)
char *name1;
char *name2;
bool *p_result;
{
 FILE *file1;
 FILE *file2;

 /* The following three lines define two comparison buffers. The next part of */
 /* the two files being compared are read into these buffers and compared.    */
 /* Using the buffers allows bulk reads, which speeds things up considerably. */
#define CMPBUFSIZ 16384
 STAVAR ubyte_ *buf1 = NULL;
 STAVAR ubyte_ *buf2 = NULL;

 /* Set the default result to be FALSE (no match). */
 *p_result=FALSE;

 /* Allocate the comparison buffers if they are not already allocated. */
 if (buf1 == NULL)
   {
    buf1=(ubyte_ *) mm_perm((size_t) CMPBUFSIZ);
    buf2=(ubyte_ *) mm_perm((size_t) CMPBUFSIZ);
   }

 /* Open, for binary reading, the two files to be compared. */
 file1=fopen(name1,"rb");
 if (file1 == FOPEN_F)
    return "Error opening the first file.";
 file2=fopen(name2,"rb");
 if (file2 == FOPEN_F)
   {fclose(file1);return "Error opening the second file.";}

 while (TRUE)
   { /* Compare a chunk. */
    size_t len1;
    size_t len2;

    /* Read a chunk from each file. */
    len1=fread(buf1,(size_t) 1,(size_t) CMPBUFSIZ,file1);
    len2=fread(buf2,(size_t) 1,(size_t) CMPBUFSIZ,file2);
    if (ferror(file1))
       return "Compare: Error reading the first file.";
    if (ferror(file2))
       return "Compare: Error reading the second file.";

    /* Compare the two chunks. */
    if (len1 != len2)                                           break;
    if (len1 ==    0)                         {*p_result=TRUE;  break;}
    if (memcmp(buf1,buf2,(size_t) len1) != 0)                   break;
   }

 if (fclose(file1) == FCLOSE_F)
    {*p_result=FALSE; return "Error closing the first file.";}
 if (fclose(file2) == FCLOSE_F)
    {*p_result=FALSE; return "Error closing the second file.";}

 /* If we have reached this point, we can be sure that no errors occurred. */
 return NULL;
}

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

EXPORT bool fexists(fn)
char *fn;
{
 FILE *file;
 bool success;

 file=fopen(fn,"rb");
 if (file==NULL)
    return FALSE;

 success= (fclose(file) != FCLOSE_F);
 as_cold(success,
         "fexists: Error closing file whose existence is being tested!");

 return TRUE;
}

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

EXPORT char *fn_temp()
{
 STAVAR uword filnum=1;
 STAVAR fn_t  fn;
 do
   {
    sprintf(fn,"fwtf%04lu.tmp",(ulong) filnum);
    filnum++;
   }
 while (fexists(fn));
 return fn;
}

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

EXPORT void strupper(s)
char *s;
{
 while (*s != EOS)
   {
    *s=toupper(*s);
    s++;
   }
}

/******************************************************************************/
/*                                End of MISC.C                               */
/******************************************************************************/