home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mint095s / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  11.3 KB  |  598 lines

  1. /*
  2. Copyright 1990,1991 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /*
  6.  * misc. utility routines
  7.  */
  8.  
  9. #include "mint.h"
  10.  
  11. /*
  12.  * given an address, find the corresponding memory region in this program's
  13.  * memory map
  14.  */
  15.  
  16. MEMREGION *
  17. addr2mem(a)
  18.     virtaddr a;
  19. {
  20.     int i;
  21.  
  22.     for (i = 0; i < curproc->num_reg; i++) {
  23.         if (a == curproc->addr[i])
  24.             return curproc->mem[i];
  25.     }
  26.     return 0;
  27. }
  28.  
  29. /*
  30.  * given a pid, return the corresponding process
  31.  */
  32.  
  33. PROC *
  34. pid2proc(pid)
  35.     int pid;
  36. {
  37.     PROC *p;
  38.  
  39.     for (p = proclist; p; p = p->gl_next) {
  40.         if (p->pid == pid)
  41.             return p;
  42.     }
  43.     return 0;
  44. }
  45.  
  46. /*
  47.  * return a new pid
  48.  */
  49.  
  50. int
  51. newpid()
  52. {
  53.     static int _maxpid = 1;
  54.     int i;
  55. #ifndef NDEBUG
  56.     int j = 0;
  57. #endif
  58.  
  59.     do {
  60.         i = _maxpid++;
  61.         if (_maxpid >= 1000) _maxpid = 1;
  62.         assert(j++ < 1000);
  63.     } while (pid2proc(i));
  64.  
  65.     return i;
  66. }
  67.  
  68. /*
  69.  * zero out a block of memory, quickly; the block must be word-aligned,
  70.  * and should be long-aligned for speed reasons
  71.  */
  72.  
  73. void
  74. zero(place, size)
  75.     char *place;
  76.     long size;
  77. {
  78.     long cruft, *lp;
  79.  
  80.     cruft = size % 256;    /* quickzero does 256 byte blocks */
  81.     size = size / 256;
  82.     while (cruft > 0) {
  83.         *place++ = 0;
  84.         cruft--;
  85.     }
  86.     lp = (long *)place;
  87.     if (size > 0) {
  88.         quickzero(place, size);
  89.     }
  90. }
  91.  
  92. #ifdef JUNK_MEM
  93. void
  94. fillwjunk(place, size)
  95.     long *place;
  96.     long size;
  97. {
  98.     while (size > 0) {
  99.         *place++ = size;
  100.         size -= 4;
  101.     }
  102. }
  103. #endif
  104.  
  105. /*
  106.  * kernel memory allocation routines
  107.  */
  108.  
  109. #define KMAGIC ((MEMREGION *)0x87654321)
  110. #define KERMEM_THRESHOLD 264
  111.  
  112. void *
  113. kmalloc(size)
  114.     long size;
  115. {
  116.     MEMREGION *m = 0;
  117.     MEMREGION **p;
  118.  
  119.     size += sizeof(m) + sizeof (m);
  120. /*
  121.  * for small requests, we try kernel memory first, to cut down on fragmentation
  122.  */
  123.     if (size <= KERMEM_THRESHOLD)
  124.         m = get_region(ker, size);
  125.     else
  126.         m = get_region(alt, size);
  127.  
  128.     if (!m) m = get_region(core, size);
  129.     if (!m)    {
  130.         if (size <= KERMEM_THRESHOLD)
  131.             m = get_region(alt, size);
  132.         else
  133.             m = get_region(ker, size);
  134.     }
  135.     if (m) {
  136.         p = (MEMREGION **)m->loc;
  137.         *p++ = KMAGIC;
  138.         *p++ = m;
  139.         return (void *)p;
  140.     }
  141.     else {
  142.         return 0;
  143.     }
  144. }
  145.  
  146. /* allocate from ST memory only */
  147.  
  148. void *
  149. kcore(size)
  150.     long size;
  151. {
  152.     MEMREGION *m = 0;
  153.     MEMREGION **p;
  154.  
  155.     size += sizeof(m) + sizeof (m);
  156.     m = get_region(core, size);
  157.  
  158.     if (m) {
  159.         p = (MEMREGION **)m->loc;
  160.         *p++ = KMAGIC;
  161.         *p++ = m;
  162.         return (void *)p;
  163.     }
  164.     else {
  165.         return 0;
  166.     }
  167. }
  168.  
  169. void
  170. kfree(place)
  171.     void *place;
  172. {
  173.     MEMREGION **p;
  174.     MEMREGION *m;
  175.  
  176.     if (!place) return;
  177.     p = place;
  178.     p -= 2;
  179.     if (*p++ != KMAGIC) {
  180.         FATAL("kfree: memory not allocated by kmalloc");
  181.     }
  182.     m = *p++;
  183.     if (--m->links != 0) {
  184.         FATAL("kfree: block has %d links", m->links);
  185.     }
  186.     free_region(m);
  187. }
  188.  
  189. /*
  190.  * "user" memory allocation routines; the kernel can use these to
  191.  * allocate/free memory that will be attached in some way to a process
  192.  * (and freed automatically when the process exits)
  193.  */
  194. void *
  195. umalloc(size)
  196.     long size;
  197. {
  198.     return (void *)m_xalloc(size, 3);
  199. }
  200.  
  201. void
  202. ufree(block)
  203.     void *block;
  204. {
  205.     (void)m_free((virtaddr)block);
  206. }
  207.  
  208. /*
  209.  * convert a time in milliseconds to a GEMDOS style date/time
  210.  * timeptr[0] gets the time, timeptr[1] the date.
  211.  * BUG/FEATURE: in the conversion, it is assumed that all months have
  212.  * 30 days and all years have 360 days.
  213.  */
  214.  
  215. void
  216. ms_time(ms, timeptr)
  217.     ulong ms;
  218.     short *timeptr;
  219. {
  220.     ulong secs;
  221.     short tsec, tmin, thour;
  222.     short tday, tmonth, tyear;
  223.  
  224.     secs = ms / 1000;
  225.     tsec = secs % 60;
  226.     secs /= 60;        /* secs now contains # of minutes */
  227.     tmin = secs % 60;
  228.     secs /= 60;        /* secs now contains # of hours */
  229.     thour = secs % 24;
  230.     secs /= 24;        /* secs now contains # of days */
  231.     tday = secs % 30;
  232.     secs /= 30;
  233.     tmonth = secs % 12;
  234.     tyear = secs / 12;
  235.     *timeptr++ = (thour << 11) | (tmin << 5) | (tsec >> 1);
  236.     *timeptr = (tyear << 9) | ((tmonth + 1) << 5) | (tday+1);
  237. }
  238.  
  239. /*
  240.  * unixtim(time, date): convert a Dos style (time, date) pair into
  241.  * a Unix time (seconds from midnight Jan 1., 1970)
  242.  */
  243.  
  244. static int
  245. mth_start[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
  246.  
  247. long
  248. unixtim(time, date)
  249.     unsigned time, date;
  250. {
  251.     int sec, min, hour;
  252.     int mday, mon, year;
  253.     long y, s;
  254.  
  255.     sec = (time & 31) << 1;
  256.     min = (time >> 5) & 63;
  257.     hour = (time >> 11) & 31;
  258.     mday = date & 31;
  259.     mon = ((date >> 5) & 15) - 1;
  260.     year = 80 + ((date >> 9) & 255);
  261.  
  262. /* calculate tm_yday here */
  263.     y = (mday - 1) + mth_start[mon] + /* leap year correction */
  264.         ( ( (year % 4) != 0 ) ? 0 : (mon > 1) );
  265.  
  266.     s = (sec) + (min * 60L) + (hour * 3600L) +
  267.         (y * 86400L) + ((year - 70) * 31536000L) +
  268.         ((year - 69)/4) * 86400L;
  269.  
  270.     return s;
  271. }
  272.  
  273. /* convert a Unix time into a DOS time. The longword returned contains
  274.    the time word first, then the date word.
  275.    BUG: we completely ignore any time zone information.
  276. */
  277. #define SECS_PER_MIN    (60L)
  278. #define SECS_PER_HOUR   (3600L)
  279. #define SECS_PER_DAY    (86400L)
  280. #define SECS_PER_YEAR   (31536000L)
  281. #define SECS_PER_LEAPYEAR (SECS_PER_DAY + SECS_PER_YEAR)
  282.  
  283. static int
  284. days_per_mth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  285.  
  286. long
  287. dostim(t)
  288.     long t;
  289. {
  290.         unsigned long time, date;
  291.     int tm_hour, tm_min, tm_sec;
  292.     int tm_year, tm_mon, tm_mday;
  293.     int i;
  294.  
  295.     if (t <= 0) return 0;
  296.  
  297.     tm_year = 70;
  298.     while (t >= SECS_PER_YEAR) {
  299.         if ((tm_year & 0x3) == 0) {
  300.             if (t < SECS_PER_LEAPYEAR)
  301.                 break;
  302.             t -= SECS_PER_LEAPYEAR;
  303.         } else {
  304.             t -= SECS_PER_YEAR;
  305.         }
  306.         tm_year++;
  307.     }
  308.     tm_mday = t/SECS_PER_DAY;
  309.         days_per_mth[1] = (tm_year & 0x3) ? 28 : 29;
  310.         for (i = 0; tm_mday >= days_per_mth[i]; i++)
  311.                 tm_mday -= days_per_mth[i];
  312.         tm_mon = i+1;
  313.     tm_mday++;
  314.         t = t % SECS_PER_DAY;
  315.         tm_hour = t/SECS_PER_HOUR;
  316.         t = t % SECS_PER_HOUR;
  317.         tm_min = t/SECS_PER_MIN;
  318.         tm_sec = t % SECS_PER_MIN;
  319.  
  320.     if (tm_year < 80) {
  321.         tm_year = 80;
  322.         tm_mon = tm_mday = 1;
  323.         tm_hour = tm_min = tm_sec = 0;
  324.     }
  325.  
  326.     time = (tm_hour << 11) | (tm_min << 5) | (tm_sec >> 1);
  327.     date = ((tm_year - 80) & 0x7f) << 9;
  328.     date |= ((tm_mon) << 5) | (tm_mday);
  329.     return (time << 16) | date;
  330. }
  331.  
  332. /*
  333.  * Case insensitive string comparison. note that this only returns
  334.  * 0 (match) or nonzero (no match), and that the returned value
  335.  * is not a reliable indicator of any "order".
  336.  */
  337.  
  338. int
  339. strnicmp(str1, str2, len)
  340.     register const char *str1, *str2;
  341.     register int len;
  342. {
  343.     register char c1, c2;
  344.  
  345.     do {
  346.         c1 = *str1++; if (isupper(c1)) c1 = tolower(c1);
  347.         c2 = *str2++; if (isupper(c2)) c2 = tolower(c2);
  348.     } while (--len >= 0 && c1 && c1 == c2);
  349.  
  350.     if (len < 0 || c1 == c2)
  351.         return 0;
  352.     return c1 - c2;
  353. }
  354.  
  355. int
  356. stricmp(str1, str2)
  357.     const char *str1, *str2;
  358. {
  359.     return strnicmp(str1, str2, 0x7fff);
  360. }
  361.  
  362.  
  363. /*
  364.  * string utilities: strlwr() converts a string to lower case, strupr()
  365.  * converts it to upper case
  366.  */
  367.  
  368. char *
  369. strlwr(s)
  370.     char *s;
  371. {
  372.     char c;
  373.     char *old = s;
  374.  
  375.     while ((c = *s) != 0) {
  376.         if (isupper(c)) {
  377.             *s = _tolower(c);
  378.         }
  379.         s++;
  380.     }
  381.     return old;
  382. }
  383.  
  384. char *
  385. strupr(s)
  386.     char *s;
  387. {
  388.     char c;
  389.     char *old = s;
  390.  
  391.     while ((c = *s) != 0) {
  392.         if (islower(c)) {
  393.             *s = _toupper(c);
  394.         }
  395.         s++;
  396.     }
  397.     return old;
  398. }
  399.  
  400. #ifdef OWN_LIB
  401.  
  402. /*
  403.  * Case sensitive comparison functions.
  404.  */
  405.  
  406. int
  407. strncmp(str1, str2, len)
  408.     register const char *str1, *str2;
  409.     register int len;
  410. {
  411.     register char c1, c2;
  412.  
  413.     do {
  414.         c1 = *str1++;
  415.         c2 = *str2++;
  416.     } while (--len >= 0 && c1 && c1 == c2);
  417.  
  418.     if (len < 0) return 0;
  419.  
  420.     return c1 - c2;
  421. }
  422.  
  423. int
  424. strcmp(str1, str2)
  425.     const char *str1, *str2;
  426. {
  427.     register char c1, c2;
  428.  
  429.     do {
  430.         c1 = *str1++;
  431.         c2 = *str2++;
  432.     } while (c1 && c1 == c2);
  433.  
  434.     return c1 - c2;
  435. }
  436.  
  437.  
  438. /*
  439.  * some standard string functions
  440.  */
  441.  
  442. char *
  443. strcat(dst, src)
  444.     char *dst;
  445.     const char *src;
  446. {
  447.     register char *_dscan;
  448.  
  449.     for (_dscan = dst; *_dscan; _dscan++) ;
  450.     while (*_dscan++ = *src++) ;
  451.     return dst;
  452. }
  453.  
  454. char *
  455. strcpy(dst, src)
  456.     char *dst;
  457.     const char *src;
  458. {
  459.     register char *_dscan = dst;
  460.     while (*_dscan++ = *src++) ;
  461.     return dst;
  462. }
  463.  
  464. char *
  465. strncpy(dst, src, len)
  466.     char *dst;
  467.     const char *src;
  468.     int len;
  469. {
  470.     register char *_dscan = dst;
  471.     while (--len >= 0 && (*_dscan++ = *src++))
  472.         continue;
  473.     while (--len >= 0)
  474.         *_dscan++ = 0;
  475.     return dst;
  476. }
  477.  
  478. int
  479. strlen(scan)
  480.     const char *scan;
  481. {
  482.     register const char *_start = scan+1;
  483.     while (*scan++) ;
  484.     return (int)((long)scan - (long)_start);
  485. }
  486.  
  487. /*
  488.  * strrchr: find the last occurence of a character in a string
  489.  */
  490. char *
  491. strrchr(str, which)
  492.     const char *str;
  493.     register int which;
  494. {
  495.     register unsigned char c, *s;
  496.     register char *place;
  497.  
  498.     s = (unsigned char *)str;
  499.     place = 0;
  500.     do {
  501.         c = *s++;
  502.         if (c == which)
  503.             place = (char *)s-1;
  504.     } while (c);
  505.     return place;
  506. }
  507.  
  508. unsigned char _ctype[256] =
  509. {
  510.     _CTc, _CTc, _CTc, _CTc,                /* 0x00..0x03 */
  511.     _CTc, _CTc, _CTc, _CTc,                /* 0x04..0x07 */
  512.     _CTc, _CTc|_CTs, _CTc|_CTs, _CTc|_CTs,        /* 0x08..0x0B */
  513.     _CTc|_CTs, _CTc|_CTs, _CTc, _CTc,        /* 0x0C..0x0F */
  514.  
  515.     _CTc, _CTc, _CTc, _CTc,                /* 0x10..0x13 */
  516.     _CTc, _CTc, _CTc, _CTc,                /* 0x14..0x17 */
  517.     _CTc, _CTc, _CTc, _CTc,                /* 0x18..0x1B */
  518.     _CTc, _CTc, _CTc, _CTc,                /* 0x1C..0x1F */
  519.  
  520.     _CTs, _CTp, _CTp, _CTp,                /* 0x20..0x23 */
  521.     _CTp, _CTp, _CTp, _CTp,                /* 0x24..0x27 */
  522.     _CTp, _CTp, _CTp, _CTp,                /* 0x28..0x2B */
  523.     _CTp, _CTp, _CTp, _CTp,                /* 0x2C..0x2F */
  524.  
  525.     _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,    /* 0x30..0x33 */
  526.     _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,    /* 0x34..0x37 */
  527.     _CTd|_CTx, _CTd|_CTx, _CTp, _CTp,        /* 0x38..0x3B */
  528.     _CTp, _CTp, _CTp, _CTp,                /* 0x3C..0x3F */
  529.  
  530.     _CTp, _CTu|_CTx, _CTu|_CTx, _CTu|_CTx,        /* 0x40..0x43 */
  531.     _CTu|_CTx, _CTu|_CTx, _CTu|_CTx, _CTu,        /* 0x44..0x47 */
  532.     _CTu, _CTu, _CTu, _CTu,                /* 0x48..0x4B */
  533.     _CTu, _CTu, _CTu, _CTu,                /* 0x4C..0x4F */
  534.  
  535.     _CTu, _CTu, _CTu, _CTu,                /* 0x50..0x53 */
  536.     _CTu, _CTu, _CTu, _CTu,                /* 0x54..0x57 */
  537.     _CTu, _CTu, _CTu, _CTp,                /* 0x58..0x5B */
  538.     _CTp, _CTp, _CTp, _CTp,                /* 0x5C..0x5F */
  539.  
  540.     _CTp, _CTl|_CTx, _CTl|_CTx, _CTl|_CTx,        /* 0x60..0x63 */
  541.     _CTl|_CTx, _CTl|_CTx, _CTl|_CTx, _CTl,        /* 0x64..0x67 */
  542.     _CTl, _CTl, _CTl, _CTl,                /* 0x68..0x6B */
  543.     _CTl, _CTl, _CTl, _CTl,                /* 0x6C..0x6F */
  544.  
  545.     _CTl, _CTl, _CTl, _CTl,                /* 0x70..0x73 */
  546.     _CTl, _CTl, _CTl, _CTl,                /* 0x74..0x77 */
  547.     _CTl, _CTl, _CTl, _CTp,                /* 0x78..0x7B */
  548.     _CTp, _CTp, _CTp, _CTc,                /* 0x7C..0x7F */
  549.  
  550.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80..0x8F */
  551.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90..0x9F */
  552.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0..0xAF */
  553.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0..0xBF */
  554.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0..0xCF */
  555.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0..0xDF */
  556.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE0..0xEF */
  557.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xF0..0xFF */
  558. };
  559.  
  560. int toupper(c)
  561.     int c;
  562. {
  563.     return(islower(c) ? (c ^ 0x20) : (c));
  564. }
  565.  
  566. int tolower(c)
  567.     int c;
  568. {
  569.     return(isupper(c) ? (c ^ 0x20) : (c));
  570. }
  571.  
  572. /*
  573.  * converts a decimal string to an integer
  574.  */
  575.  
  576. int
  577. atoi(s)
  578.     const char *s;
  579. {
  580.     int d = 0;
  581.     int negflag = 0;
  582.     int c;
  583.  
  584.     while (*s && isspace(*s)) s++;
  585.     while (*s == '-' || *s == '+') {
  586.         if (*s == '-')
  587.             negflag ^= 1;
  588.         s++;
  589.     }
  590.     while ((c = *s++) && isdigit(c)) {
  591.         d = 10 * d + (c - '0');
  592.     }
  593.     if (negflag) d = -d;
  594.     return d;
  595. }
  596.  
  597. #endif /* OWN_LIB */
  598.