File: ratfor.c

package info (click to toggle)
conquest 8.1-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,984 kB
  • ctags: 3,086
  • sloc: ansic: 39,393; sh: 8,540; yacc: 446; makefile: 296; lex: 146
file content (264 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download
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
258
259
260
261
262
263
264
/* Functions that appear to be intrinsic to RATFOR */
/************************************************************************
 *
 * $Id: ratfor.c,v 1.8 2003/11/02 20:53:00 jon Exp $
 *
 * Copyright 1999-2004 Jon Trulson under the ARTISTIC LICENSE. (See LICENSE).
 ***********************************************************************/

/**********************************************************************/
/* Unix/C specific porting and supporting code Copyright (C)1994-1996 */
/* by Jon Trulson <jon@radscan.com> under the same terms and          */
/* conditions of the original copyright by Jef Poskanzer and Craig    */
/* Leres.                                                             */
/*                                                                    */
/**********************************************************************/


#include "c_defs.h"		/* includes everthing else */

/* alldig(char *) - returns YES if all chars in str are digits, else NO */
int alldig(char *buf)
{
  char *s;
  
  s = buf;
  while (*s)
    {
      if (!isdigit(*s))
	return(FALSE);
      s++;
    }
  
  return(TRUE);
}

/* c_type(char) - returns LETTER or DIGIT if letter or digit, returns ERR 
   otherwise */
int c_type(char c)
{

  if (isdigit(c))
    return(DIGIT);
  else if (isalpha(c))
    return(LETTER);

  return(ERR);
}

/* void concat(char str1[], char str2[], char buf[]) - concatenate str1 
   and str2 into buf */
void concat(char str1[], char str2[], char buf[])
{
  strcat(buf, str1);
  strcat(buf, str2);
}

/* c_index(char *, char) - get the index of ch in *buf */
int c_index(char *buf, char ch)
{
  char *s;
  int count;

  s = buf;
  count = 0;

  while (*s)
    {
      if (*s == ch)
	return(count);
      s++; 
      count++;
    }

  return(ERR);			/* No match */
}

/* void lower(char *buf) - lowercase a string */
void lower(char *buf)
{
  char *s;

  s = buf;

  while(*s)
    {
      *s = (char)tolower(*s);
      s++;
    }
}

/* void upper(char *buf) - upcase a string */
void upper(char *buf)
{
  char *s;

  s = buf;

  while(*s)
    {
      *s = (char)toupper(*s);
      s++;
    }
}

/* load the now[] array with the current time - make sure it's 
   decl [NOWSIZE] 
   always returns the current time in time_t format. 
*/
time_t getnow (int now[NOWSIZE], time_t thetime)
{
  struct tm *thetm;
  time_t thetimet, curtime;

  curtime = time(0);
  
  if (thetime == 0)	
    thetimet = curtime;		/* wants current time */
  else		
    thetimet = thetime;		/* wants supplied time conversion */

  if (now != NULL)		/* want to actually fill now[] */
    {
      thetm = localtime(&thetimet);

      now[1] = thetm->tm_year + 1900; /* year */
      now[2] = thetm->tm_mon + 1; /* month */
      now[3] = thetm->tm_mday;	/* day */
      now[4] = thetm->tm_hour;	/* hour */
      now[5] = thetm->tm_min;	/* minutes */
      now[6] = thetm->tm_sec;	/* seconds */
      now[7] = 500;		/* thousandths of seconds - not here */
    }

  return(curtime);
}

/* wkday(int, int, int) - all args ignored - returns day num */
int wkday (void)
{
  struct tm *thetm;
  time_t thetimet;

  thetimet = time(0);
  thetm = localtime(&thetimet);

  return(thetm->tm_wday + 1);
}
    
/* Find first bit that is set and return bit number */
int lib_ffs(int start, int len, int bits, int *rule)
{
  register int i;

  *rule = -1;
  for (i = start; i < len; i++)
    {				/* check the bits */
      if ((bits & ( 1 << i)) != 0)
	{			/* bit is set */
	  *rule = i;
	  break;
	}
    }

#ifdef DEBUG_AI
    clog("bits = %8X, rule = %d", bits, *rule);
#endif

  if (*rule == -1)
    return(ERR);
  else
    return(OK);
}

/* sleep with a millisecond granularity using poll() */
void c_sleep(real sleeptime)
{
#ifdef USE_SELECT

  unsigned int secs, usecs;
  struct timeval stime;


  if (sleeptime == 0.0)
    return;

				/* some systems don't grok usecs >1,000,000 */
  if (sleeptime >= 1.0)
    {
      secs = (int) sleeptime;
      usecs = (int) (1000000 * (sleeptime - (real) secs));
    }
  else
    {
      secs = 0;
      usecs = (1000000 * sleeptime);
    }
  
  stime.tv_sec = secs;
  stime.tv_usec  = usecs;
  
# if defined(LINUX)
  while (1)
  {
# endif

  if (select(0, NULL, NULL, NULL, &stime) == -1)
    {
      if (errno != EINTR) /* Interrupted system call */
           clog("c_sleep(): select() failed: errno = %d, %s",
                errno,strerror(errno));
    }

# if defined(LINUX)
  if (stime.tv_sec == 0 && stime.tv_usec == 0)
    break;
  } /* end while */
# endif

#else

  if (sleeptime == 0.0)
    return;

  /* Here I use poll to provide a msec */
  /* granularity sleep.  Actually it's */
  /* limited to 0.1 sec, but that's close */
  /* enough.  I don't know if it's wise */
  /* to supply a NULL poll struct, but it seems to work... */
    
  if (poll(NULL, 0, (int) (sleeptime * 1000)) == -1)
    {
      if (errno != EINTR) /* Interrupted system call */
	clog("csleep(): poll() failed: %s",
	     strerror(errno));
    }

#endif

  return;

}

				/* output an error and exit */
void error(char str[])
{
  printf("%s\n", str);
  fflush(stdout);
  
  exit(1);
}

real ctor(char *buf)
{
  static real r;
  float f;

  sscanf(buf, "%f", &f);

  r = (real) f;
  return(r);
}