std.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /* This file contains code for X-CHESS.
  2. Copyright (C) 1986 Free Software Foundation, Inc.
  3. This file is part of X-CHESS.
  4. X-CHESS is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the X-CHESS General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. X-CHESS, but only under the conditions described in the
  12. X-CHESS General Public License. A copy of this license is
  13. supposed to have been given to you along with X-CHESS so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* RCS Info: $Revision: 1.1 $ on $Date: 86/11/01 17:08:40 $
  18. * $Source: /users/faustus/xchess/RCS/std.c,v $
  19. * Copyright (c) 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
  20. *
  21. * Utility routines.
  22. */
  23. #include "std.h"
  24. #ifndef IBMPC
  25. #include <sys/types.h>
  26. #endif not IBMPC
  27. #ifdef UNIX
  28. #include <signal.h>
  29. #include <pwd.h>
  30. #endif UNIX
  31. #ifdef BSD
  32. #include <sys/time.h>
  33. #include <sys/resource.h>
  34. #endif BSD
  35. extern char **environ;
  36. bool
  37. prefix(p, s)
  38. register char *p, *s;
  39. {
  40. while (*p && (*p == *s))
  41. p++, s++;
  42. if (!*p)
  43. return (true);
  44. else
  45. return (false);
  46. }
  47. /* Create a copy of a string. */
  48. char *
  49. copy(str)
  50. char *str;
  51. {
  52. char *p, *tmalloc();
  53. p = tmalloc(strlen(str) + 1);
  54. strcpy(p, str);
  55. return(p);
  56. }
  57. /* Determine whether sub is a substring of str. */
  58. bool
  59. substring(sub, str)
  60. register char *str, *sub;
  61. {
  62. register char *s;
  63. while(*str) {
  64. if(*str == *sub) {
  65. for(s = sub; *s; s++)
  66. if(*s != *str++)
  67. break;
  68. if(*s == '\0')
  69. return (true);
  70. }
  71. str++;
  72. }
  73. return (false);
  74. }
  75. /* Malloc num bytes and initialize to zero. Fatal error if the space can't
  76. * be malloc'd.
  77. */
  78. char *
  79. tmalloc(num)
  80. register int num;
  81. {
  82. register char *s;
  83. char *malloc();
  84. s = malloc((unsigned) num);
  85. if (!s) {
  86. fatal("malloc: can't allocate %d bytes", num);
  87. }
  88. bzero(s, num);
  89. return(s);
  90. }
  91. char *
  92. trealloc(ptr, num)
  93. char *ptr;
  94. int num;
  95. {
  96. register char *s;
  97. char *realloc();
  98. s = realloc(ptr, (unsigned) num);
  99. if (!s) {
  100. fatal("realloc: can't allocate %d bytes", num);
  101. }
  102. /* Well, this won't be zeroed... Too bad... */
  103. return(s);
  104. }
  105. /* Append one character to a string. Don't check for overflow. */
  106. void
  107. appendc(s, c)
  108. char *s, c;
  109. {
  110. while (*s)
  111. s++;
  112. *s++ = c;
  113. *s = '\0';
  114. return;
  115. }
  116. int
  117. scannum(str)
  118. char *str;
  119. {
  120. int i = 0;
  121. while(isdigit(*str))
  122. i = i * 10 + *(str++) - '0';
  123. return(i);
  124. }
  125. /* Case insensitive prefix. */
  126. bool
  127. ciprefix(p, s)
  128. register char *p, *s;
  129. {
  130. while (*p) {
  131. if ((isupper(*p) ? tolower(*p) : *p) !=
  132. (isupper(*s) ? tolower(*s) : *s))
  133. return(false);
  134. p++;
  135. s++;
  136. }
  137. return (true);
  138. }
  139. /* Case insensitive strcmp... */
  140. bool
  141. cieq(p, s)
  142. register char *p, *s;
  143. {
  144. while (*p) {
  145. if ((isupper(*p) ? tolower(*p) : *p) !=
  146. (isupper(*s) ? tolower(*s) : *s))
  147. return(false);
  148. p++;
  149. s++;
  150. }
  151. return (!*s);
  152. }
  153. #ifdef BSD
  154. /* Return the date. Return value is static data. */
  155. char *
  156. datestring()
  157. {
  158. register char *tzn;
  159. struct tm *tp;
  160. static char tbuf[40];
  161. char *ap;
  162. struct timeval tv;
  163. struct timezone tz;
  164. char *timezone(), *asctime();
  165. int i;
  166. struct tm *localtime();
  167. (void) gettimeofday(&tv, &tz);
  168. tp = localtime((time_t *) &tv.tv_sec);
  169. ap = asctime(tp);
  170. tzn = timezone(tz.tz_minuteswest, tp->tm_isdst);
  171. sprintf(tbuf, "%.20s", ap);
  172. if (tzn)
  173. strcat(tbuf, tzn);
  174. strcat(tbuf, ap + 19);
  175. i = strlen(tbuf);
  176. tbuf[i - 1] = '\0';
  177. return (tbuf);
  178. }
  179. #else BSD
  180. /* Give it a try... */
  181. char *
  182. datestring()
  183. {
  184. long i;
  185. static char buf[64];
  186. i = time(0);
  187. strcpy(buf, ctime(&i));
  188. buf[strlen(buf) - 1] = '\0'; /* Kill the nl. */
  189. return (buf);
  190. }
  191. #endif
  192. /* How many seconds have elapsed in running time. */
  193. int
  194. seconds()
  195. {
  196. #ifdef BSD
  197. struct rusage ruse;
  198. getrusage(RUSAGE_SELF, &ruse);
  199. return (ruse.ru_utime.tv_sec);
  200. #else BSD
  201. #endif BSD
  202. }
  203. /* A few things that may not exist on non-unix systems. */
  204. #ifndef BSD
  205. #ifndef index
  206. char *
  207. index(s, c)
  208. register char *s;
  209. register char c;
  210. {
  211. while ((*s != c) && (*s != '\0'))
  212. s++;
  213. if (*s == '\0')
  214. return ((char *) 0);
  215. else
  216. return (s);
  217. }
  218. #endif not index
  219. #ifndef rindex
  220. char *
  221. rindex(s, c)
  222. register char *s;
  223. register char c;
  224. {
  225. register char *t;
  226. for (t = s; *t != '\0'; t++);
  227. while ((*t != c) && (t != s))
  228. t--;
  229. if (t == s)
  230. return ((char *) 0);
  231. else
  232. return (t);
  233. }
  234. #endif not rindex
  235. #ifndef bcopy
  236. void
  237. bcopy(from, to, num)
  238. register char *from, *to;
  239. register int num;
  240. {
  241. while (num-- > 0)
  242. *to++ = *from++;
  243. return;
  244. }
  245. #endif not bcopy
  246. #ifndef bzero
  247. void
  248. bzero(ptr, num)
  249. register char *ptr;
  250. register int num;
  251. {
  252. while (num-- > 0)
  253. *ptr++ = '\0';
  254. return;
  255. }
  256. #endif not bzero
  257. /* This might not be around... If not then forget about sorting... */
  258. void qsort() {}
  259. #endif BSD
  260. char *
  261. gettok(s)
  262. char **s;
  263. {
  264. char buf[BSIZE];
  265. int i = 0;
  266. while (isspace(**s))
  267. (*s)++;
  268. if (!**s)
  269. return (NULL);
  270. while (**s && !isspace(**s))
  271. buf[i++] = *(*s)++;
  272. buf[i] = '\0';
  273. while (isspace(**s))
  274. (*s)++;
  275. return (copy(buf));
  276. }
  277. /* Die horribly. */
  278. /* VARARGS1 */
  279. void
  280. fatal(s, args)
  281. char *s;
  282. {
  283. fputs("Internal Error: ", stderr);
  284. _doprnt(s, &args, stderr);
  285. putc('\n', stderr);
  286. kill(getpid(), SIGIOT);
  287. /* NOTREACHED */
  288. }
  289. void
  290. setenv(name, value)
  291. char *name, *value;
  292. {
  293. int i;
  294. char **xx, *s;
  295. s = tmalloc(strlen(name) + 2);
  296. sprintf(s, "%s=", name);
  297. /* Copy the old environment... */
  298. for (i = 0; environ[i]; i++)
  299. if (prefix(s, environ[i]))
  300. break;
  301. if (!environ[i]) {
  302. xx = (char **) tmalloc((i + 2) * sizeof (char *));
  303. for (i = 0; environ[i]; i++)
  304. xx[i] = environ[i];
  305. xx[i + 1] = NULL;
  306. environ = xx;
  307. } else
  308. xx = environ;
  309. xx[i] = tmalloc(strlen(name) + strlen(value) + 2);
  310. sprintf(xx[i], "%s=%s", name, value);
  311. return;
  312. }
  313. char *
  314. getusername()
  315. {
  316. int i = getuid();
  317. struct passwd *pw = getpwuid(i);
  318. return (pw ? pw->pw_name : NULL);
  319. }
  320. char *
  321. gethome()
  322. {
  323. int i = getuid();
  324. struct passwd *pw = getpwuid(i);
  325. return (pw ? pw->pw_dir : "/strange");
  326. }
  327. char *
  328. tildexpand(s)
  329. char *s;
  330. {
  331. struct passwd *pw;
  332. char *n, buf[64];
  333. int i;
  334. if (*s != '~')
  335. return (copy(s));
  336. for (s++, i = 0; *s != '/'; s++, i++)
  337. buf[i] = *s;
  338. buf[i] = '\0';
  339. if (!i)
  340. pw = getpwuid(getuid());
  341. else
  342. pw = getpwnam(buf);
  343. if (!pw)
  344. return (s);
  345. n = tmalloc(strlen(s) + strlen(pw->pw_dir) + 1);
  346. strcpy(n, pw->pw_dir);
  347. strcat(n, s);
  348. return (n);
  349. }