Home | History | Annotate | Line # | Download | only in warp
util.c revision 1.4
      1 /* Header: util.c,v 7.0.1.2 86/10/20 12:07:46 lwall Exp */
      2 
      3 /* Log:	util.c,v
      4  * Revision 7.0.1.2  86/10/20  12:07:46  lwall
      5  * Made all exits reset tty.
      6  *
      7  * Revision 7.0.1.1  86/10/16  10:54:02  lwall
      8  * Added Damage.  Fixed random bugs.
      9  *
     10  * Revision 7.0  86/10/08  15:14:31  lwall
     11  * Split into separate files.  Added amoebas and pirates.
     12  *
     13  */
     14 
     15 #include "EXTERN.h"
     16 #include "warp.h"
     17 #include "sys/dir.h"
     18 #include "object.h"
     19 #include "sig.h"
     20 #include "term.h"
     21 #include "INTERN.h"
     22 #include "util.h"
     23 
     24 void
     25 util_init(void)
     26 {
     27     ;
     28 }
     29 
     30 void
     31 movc3(int len, char *src, char *dest)
     32 #ifdef vax
     33 {
     34     asm("movc3 4(ap),*8(ap),*12(ap)");
     35 }
     36 #else
     37 {
     38     if (dest <= src) {
     39 	for (; len; len--) {
     40 	    *dest++ = *src++;
     41 	}
     42     }
     43     else {
     44 	dest += len;
     45 	src += len;
     46 	for (; len; len--) {
     47 	    *--dest = *--src;
     48 	}
     49     }
     50 }
     51 #endif
     52 
     53 void
     54 no_can_do(const char *what)
     55 {
     56     fprintf(stderr,"Sorry, your terminal is too %s to play warp.\r\n",what);
     57     finalize(1);
     58 }
     59 
     60 int
     61 exdis(int maxnum)
     62 {
     63     double temp, temp2;
     64 
     65     temp = (double) maxnum;
     66 #ifndef lint
     67     temp2 = (double) myrand();
     68 #else
     69     temp2 = 0.0;
     70 #endif
     71 #if RANDBITS == 15
     72     return (int) exp(temp2 * log(temp)/0x7fff);
     73 #else
     74 #if RANDBITS == 16
     75     return (int) exp(temp2 * log(temp)/0xffff);
     76 #else
     77     return (int) exp(temp2 * log(temp)/0x7fffffff);
     78 #endif
     79 #endif
     80 }
     81 
     82 static char nomem[] = "warp: out of memory!\r\n";
     83 
     84 /* paranoid version of malloc */
     85 
     86 void *
     87 safemalloc(size_t size)
     88 {
     89     char *ptr;
     90 
     91     ptr = malloc(size?size:1);	/* malloc(0) is NASTY on our system */
     92     if (ptr != NULL)
     93 	return ptr;
     94     else {
     95 	fputs(nomem,stdout);
     96 	sig_catcher(0);
     97     }
     98     /*NOTREACHED*/
     99     return NULL;
    100 }
    101 
    102 /* safe version of string copy */
    103 
    104 char *
    105 safecpy(char *to, const char *from, size_t len)
    106 {
    107     char *dest = to;
    108 
    109     if (from != NULL)
    110 	for (len--; len && (*dest++ = *from++); len--)
    111 	    continue;
    112     *dest = '\0';
    113     return to;
    114 }
    115 
    116 /* copy a string up to some (non-backslashed) delimiter, if any */
    117 
    118 char *
    119 cpytill(char *to, const char *from, int delim)
    120 {
    121     for (; *from; from++,to++) {
    122 	if (*from == '\\' && from[1] == delim)
    123 	    from++;
    124 	else if (*from == delim)
    125 	    break;
    126 	*to = *from;
    127     }
    128     *to = '\0';
    129     return __UNCONST(from);
    130 }
    131 
    132 /* return ptr to little string in big string, NULL if not found */
    133 
    134 char *
    135 instr(const char *big, const char *little)
    136 
    137 {
    138     const char *t;
    139     const char *s;
    140     const char *x;
    141 
    142     for (t = big; *t; t++) {
    143 	for (x=t,s=little; *s; x++,s++) {
    144 	    if (!*x)
    145 		return NULL;
    146 	    if (*s != *x)
    147 		break;
    148 	}
    149 	if (!*s)
    150 	    return __UNCONST(t);
    151     }
    152     return NULL;
    153 }
    154 
    155 /* effective access */
    156 
    157 #ifdef SETUIDGID
    158 int
    159 eaccess(const char *filename, mode_t mod)
    160 {
    161     mode_t protection;
    162     uid_t euid;
    163 
    164     mod &= 7;				/* remove extraneous garbage */
    165     if (stat(filename, &filestat) < 0)
    166 	return -1;
    167     euid = geteuid();
    168     if (euid == ROOTID)
    169 	return 0;
    170     protection = 7 & (filestat.st_mode >>
    171       (filestat.st_uid == euid ? 6 :
    172         (filestat.st_gid == getegid() ? 3 : 0)
    173       ));
    174     if ((mod & protection) == mod)
    175 	return 0;
    176     errno = EACCES;
    177     return -1;
    178 }
    179 #endif
    180 
    181 void
    182 prexit(const char *cp)
    183 {
    184 	write(2, cp, strlen(cp));
    185 	sig_catcher(0);
    186 }
    187 
    188 /* copy a string to a safe spot */
    189 
    190 char *
    191 savestr(const char *str)
    192 {
    193     char *newaddr = safemalloc((size_t)(strlen(str)+1));
    194 
    195     strcpy(newaddr, str);
    196     return newaddr;
    197 }
    198 
    199 char *
    200 getval(const char *nam, const char *def)
    201 {
    202     const char *val;
    203 
    204     if ((val = getenv(nam)) == NULL || !*val)
    205 	val = def;
    206     return __UNCONST(val);
    207 }
    208