home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / warp / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  5.3 KB  |  330 lines

  1. /* $Header: /b/iv/src/warp/RCS/util.c,v 1.1 1993/06/09 23:21:32 iv Exp iv $ */
  2.  
  3. /* $Log: util.c,v $
  4.  * Revision 1.1  1993/06/09  23:21:32  iv
  5.  * Initial revision
  6.  *
  7.  * Revision 7.0.1.2  86/10/20  12:07:46  lwall
  8.  * Made all exits reset tty.
  9.  * 
  10.  * Revision 7.0.1.1  86/10/16  10:54:02  lwall
  11.  * Added Damage.  Fixed random bugs.
  12.  * 
  13.  * Revision 7.0  86/10/08  15:14:31  lwall
  14.  * Split into separate files.  Added amoebas and pirates.
  15.  * 
  16.  */
  17.  
  18. #include "EXTERN.h"
  19. #include "warp.h"
  20. #include "sys/dir.h"
  21. #include "object.h"
  22. #include "sig.h"
  23. #include "term.h"
  24. #include "INTERN.h"
  25. #include "util.h"
  26.  
  27. void
  28. util_init()
  29. {
  30.     ;
  31. }
  32.  
  33. void
  34. movc3(len,src,dest)
  35. #ifdef vax
  36. char *dest, *src;
  37. int len;
  38. {
  39.     asm("movc3 4(ap),*8(ap),*12(ap)");
  40. }
  41. #else
  42. Reg1 char *dest;
  43. Reg2 char *src;
  44. Reg3 int len;
  45. {
  46.     if (dest <= src) {
  47.     for (; len; len--) {
  48.         *dest++ = *src++;
  49.     }
  50.     }
  51.     else {
  52.     dest += len;
  53.     src += len;
  54.     for (; len; len--) {
  55.         *--dest = *--src;
  56.     }
  57.     }
  58. }
  59. #endif
  60.  
  61. void
  62. no_can_do(what)
  63. char *what;
  64. {
  65.     fprintf(stderr,"Sorry, your terminal is too %s to play warp.\r\n",what);
  66.     finalize(1);
  67. }
  68.  
  69. int
  70. exdis(maxnum)
  71. int maxnum;
  72. {
  73.     double temp, temp2;
  74.     double exp();
  75.     double log();
  76.  
  77.     temp = (double) maxnum;
  78. #ifndef lint
  79.     temp2 = (double) myrand();
  80. #else
  81.     temp2 = 0.0;
  82. #endif
  83. #if RANDBITS == 15
  84.     return (int) exp(temp2 * log(temp)/0x7fff);
  85. #else
  86. #if RANDBITS == 16
  87.     return (int) exp(temp2 * log(temp)/0xffff);
  88. #else
  89.     return (int) exp(temp2 * log(temp)/0x7fffffff);
  90. #endif
  91. #endif
  92. }
  93.  
  94. static char nomem[] = "warp: out of memory!\r\n";
  95.  
  96. /* paranoid version of malloc */
  97.  
  98. char *
  99. safemalloc(size)
  100. MEM_SIZE size;
  101. {
  102.     char *ptr;
  103.     char *malloc();
  104.  
  105.     ptr = malloc(size?size:1);    /* malloc(0) is NASTY on our system */
  106.     if (ptr != Nullch)
  107.     return ptr;
  108.     else {
  109.     fputs(nomem,stdout);
  110.     sig_catcher(0);
  111.     }
  112.     /*NOTREACHED*/
  113. }
  114.  
  115. /* safe version of string copy */
  116.  
  117. char *
  118. safecpy(to,from,len)
  119. char *to;
  120. Reg2 char *from;
  121. Reg1 int len;
  122. {
  123.     Reg3 char *dest = to;
  124.  
  125.     if (from != Nullch) 
  126.     for (len--; len && (*dest++ = *from++); len--) ;
  127.     *dest = '\0';
  128.     return to;
  129. }
  130.  
  131. /* copy a string up to some (non-backslashed) delimiter, if any */
  132.  
  133. char *
  134. cpytill(to,from,delim)
  135. Reg2 char *to;
  136. Reg1 char *from;
  137. Reg3 int delim;
  138. {
  139.     for (; *from; from++,to++) {
  140.     if (*from == '\\' && from[1] == delim)
  141.         from++;
  142.     else if (*from == delim)
  143.         break;
  144.     *to = *from;
  145.     }
  146.     *to = '\0';
  147.     return from;
  148. }
  149.  
  150. /* return ptr to little string in big string, NULL if not found */
  151.  
  152. char *
  153. instr(big, little)
  154. char *big, *little;
  155.  
  156. {
  157.     Reg3 char *t;
  158.     Reg1 char *s;
  159.     Reg2 char *x;
  160.  
  161.     for (t = big; *t; t++) {
  162.     for (x=t,s=little; *s; x++,s++) {
  163.         if (!*x)
  164.         return Nullch;
  165.         if (*s != *x)
  166.         break;
  167.     }
  168.     if (!*s)
  169.         return t;
  170.     }
  171.     return Nullch;
  172. }
  173.  
  174. /* effective access */
  175.  
  176. #ifdef SETUIDGID
  177. int
  178. eaccess(filename, mod)
  179. char *filename;
  180. int mod;
  181. {
  182.     int protection, euid;
  183.     
  184.     mod &= 7;                /* remove extraneous garbage */
  185.     if (stat(filename, &filestat) < 0)
  186.     return -1;
  187.     euid = geteuid();
  188.     if (euid == ROOTID)
  189.     return 0;
  190.     protection = 7 & (filestat.st_mode >>
  191.       (filestat.st_uid == euid ? 6 :
  192.         (filestat.st_gid == getegid() ? 3 : 0)
  193.       ));
  194.     if ((mod & protection) == mod)
  195.     return 0;
  196.     errno = EACCES;
  197.     return -1;
  198. }
  199. #endif
  200.  
  201. /*
  202.  * Get working directory
  203.  */
  204.  
  205. #ifdef GETWD
  206. #define    dot    "."
  207. #define    dotdot    ".."
  208.  
  209. static    char    *name;
  210.  
  211. static    DIR    *dirp;
  212. static    int    off;
  213. static    struct    stat    d, dd;
  214. static    struct    direct    *dir;
  215.  
  216. char *
  217. getwd(np)
  218. char *np;
  219. {
  220.     long rdev, rino;
  221.  
  222.     *np++ = '/';
  223.     *np = 0;
  224.     name = np;
  225.     off = -1;
  226.     stat("/", &d);
  227.     rdev = d.st_dev;
  228.     rino = d.st_ino;
  229.     for (;;) {
  230.         stat(dot, &d);
  231.         if (d.st_ino==rino && d.st_dev==rdev)
  232.             goto done;
  233.         if ((dirp = opendir(dotdot)) == Null(DIR *))
  234.             prexit("getwd: cannot open ..\r\n");
  235.         stat(dotdot, &dd);
  236.         chdir(dotdot);
  237.         if(d.st_dev == dd.st_dev) {
  238.             if(d.st_ino == dd.st_ino)
  239.                 goto done;
  240.             do
  241.                 if ((dir = readdir(dirp)) == Null(struct direct *))
  242.                     prexit("getwd: read error in ..\r\n");
  243.             while (dir->d_ino != d.st_ino);
  244.         }
  245.         else do {
  246.                 if ((dir = readdir(dirp)) == Null(struct direct *))
  247.                     prexit("getwd: read error in ..\r\n");
  248.                 stat(dir->d_name, &dd);
  249.             } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  250.         cat();
  251.         closedir(dirp);
  252.     }
  253. done:
  254.     name--;
  255.     if (chdir(name) < 0) {
  256.         printf("getwd: can't cd back to %s\r\n",name);
  257.         sig_catcher(0);
  258.     }
  259.     return (name);
  260. }
  261.  
  262. void
  263. cat()
  264. {
  265.     Reg1 int i;
  266.     Reg2 int j;
  267.  
  268.     i = -1;
  269.     while (dir->d_name[++i] != 0);
  270.     if ((off+i+2) > 1024-1)
  271.         return;
  272.     for(j=off+1; j>=0; --j)
  273.         name[j+i+1] = name[j];
  274.     if (off >= 0)
  275.         name[i] = '/';
  276.     off=i+off+1;
  277.     name[off] = 0;
  278.     for(--i; i>=0; --i)
  279.         name[i] = dir->d_name[i];
  280. }
  281.  
  282. void
  283. prexit(cp)
  284. char *cp;
  285. {
  286.     write(2, cp, strlen(cp));
  287.     sig_catcher(0);
  288. }
  289. #else
  290. char *
  291. getwd(np)            /* shorter but slower */
  292. char *np;
  293. {
  294.     FILE *popen();
  295.     FILE *pipefp = popen("/bin/pwd","r");
  296.  
  297.     if (pipefp == Nullfp) {
  298.     printf("Can't run /bin/pwd\r\n");
  299.     finalize(1);
  300.     }
  301.     Fgets(np,512,pipefp);
  302.     np[strlen(np)-1] = '\0';    /* wipe out newline */
  303.     pclose(pipefp);
  304.     return np;
  305. }
  306. #endif
  307.  
  308. /* copy a string to a safe spot */
  309.  
  310. char *
  311. savestr(str)
  312. char *str;
  313. {
  314.     Reg1 char *newaddr = safemalloc((MEM_SIZE)(strlen(str)+1));
  315.  
  316.     strcpy(newaddr,str);
  317.     return newaddr;
  318. }
  319.  
  320. char *
  321. getval(nam,def)
  322. char *nam,*def;
  323. {
  324.     char *val;
  325.  
  326.     if ((val = getenv(nam)) == Nullch || !*val)
  327.     val = def;
  328.     return val;
  329. }
  330.