00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #include "bc_machdefs.h"
00083 #include "bc_macrorus.h"
00084 #include "bc_util.h"
00085 extern int gethostname (char *,
00086 size_t);
00087
00088
00089 static int isprime (unsigned int x);
00090
00091
00092 unsigned int CCutil_nextprime (unsigned int x)
00093 {
00094 if (x < 3)
00095 return 3;
00096 x |= 1;
00097 while (!isprime (x))
00098 x += 2;
00099 return x;
00100 }
00101
00102 static int isprime (unsigned int p)
00103 {
00104 unsigned int i;
00105
00106 if ((p & 1) == 0)
00107 return 0;
00108 for (i = 3; i * i <= p; i += 2)
00109 {
00110 if (p % i == 0)
00111 return 0;
00112 }
00113 return 1;
00114 }
00115
00116 int CCutil_our_gcd (int a,
00117 int b)
00118 {
00119 int c;
00120
00121 if (a < 0)
00122 a = -a;
00123 if (b < 0)
00124 b = -b;
00125 if (a > b)
00126 CC_SWAP (a, b, c);
00127
00128 while (a)
00129 {
00130 c = b % a;
00131 b = a;
00132 a = c;
00133 }
00134 return b;
00135 }
00136
00137 int CCutil_our_lcm (int a,
00138 int b)
00139 {
00140 int c;
00141
00142 if (a < 0)
00143 a = -a;
00144 if (b < 0)
00145 b = -b;
00146
00147 c = CCutil_our_gcd (a, b);
00148
00149 return (a / c) * b;
00150 }
00151
00152 char *CCutil_strchr (char *s,
00153 int c)
00154 {
00155 while (*s)
00156 {
00157 if (*s == c)
00158 return s;
00159 s++;
00160 }
00161 return (char *) NULL;
00162 }
00163
00164 const char *CCutil_strchr_c (const char *s,
00165 int c)
00166 {
00167 while (*s)
00168 {
00169 if (*s == c)
00170 return s;
00171 s++;
00172 }
00173 return (char *) NULL;
00174 }
00175
00176 char *CCutil_strrchr (char *s,
00177 int c)
00178 {
00179 char *l = (char *) NULL;
00180
00181 while (*s)
00182 {
00183 if (*s == c)
00184 l = s;
00185 s++;
00186 }
00187 return l;
00188 }
00189
00190 const char *CCutil_strrchr_c (const char *s,
00191 int c)
00192 {
00193 const char *l = (char *) NULL;
00194
00195 while (*s)
00196 {
00197 if (*s == c)
00198 l = s;
00199 s++;
00200 }
00201 return l;
00202 }
00203
00204 char *CCutil_strdup (const char *s)
00205 {
00206 char *p = CC_SAFE_MALLOC (strlen (s) + 1, char);
00207
00208 if (p == (char *) NULL)
00209 {
00210 fprintf (stderr, "Out of memory in CCutil_strdup\n");
00211 return (char *) NULL;
00212 }
00213 strcpy (p, s);
00214 return p;
00215 }
00216
00217 char *CCutil_strdup2 (const char *s)
00218 {
00219 char *p;
00220 int len;
00221
00222 for (len = 0; s[len]; len++)
00223 {
00224 if (s[len] == ' ' || s[len] == '\r' || s[len] == '\t' || s[len] == '\n')
00225 {
00226 break;
00227 }
00228 }
00229
00230 p = CC_SAFE_MALLOC (len + 1, char);
00231
00232 if (p == (char *) NULL)
00233 {
00234 fprintf (stderr, "Out of memory in CCutil_strdup2\n");
00235 return (char *) NULL;
00236 }
00237 strncpy (p, s, (unsigned) len);
00238 p[len] = '\0';
00239
00240 return p;
00241 }
00242
00243 void CCutil_readstr (FILE * f,
00244 char *s,
00245 int len)
00246 {
00247 int c;
00248
00249 while ((c = getc (f)) != EOF && c != ' ' && c != '\t' && c != '\r' &&
00250 c != '\n')
00251 {
00252 if (len > 1)
00253 {
00254 *s++ = c;
00255 len--;
00256 }
00257 }
00258 *s = '\0';
00259 }
00260
00261 void CCutil_printlabel (void)
00262 {
00263 #ifdef CC_NETREADY
00264 char buf[1024];
00265
00266 gethostname (buf, 1024);
00267 printf ("Host: %s Current process id: %d\n", buf, (int) getpid ());
00268 fflush (stdout);
00269 #else
00270 printf ("No label - need function to non-NETREADY machines\n");
00271 fflush (stdout);
00272 #endif
00273 }
00274
00275 int CCutil_print_command (int ac,
00276 char **av)
00277 {
00278 int rval = 0;
00279 int i,
00280 cmdlen = 0;
00281 char *cmdout = (char *) NULL;
00282
00283 for (i = 0; i < ac; i++)
00284 {
00285 cmdlen += strlen (av[i]) + 1;
00286 }
00287 cmdout = CC_SAFE_MALLOC (cmdlen, char);
00288 CCcheck_NULL (cmdout, "out of memory in print_command");
00289
00290 cmdlen = 0;
00291 for (i = 0; i < ac; i++)
00292 {
00293 strcpy (cmdout + cmdlen, av[i]);
00294 cmdlen += strlen (av[i]);
00295 cmdout[cmdlen] = ' ';
00296 cmdlen++;
00297 }
00298 cmdout[cmdlen - 1] = '\0';
00299 printf ("%s\n", cmdout);
00300 fflush (stdout);
00301
00302 CLEANUP:
00303
00304 CC_IFFREE (cmdout, char);
00305 return rval;
00306 }