Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1105 lines
19 KiB

  1. /* misc.c - miscellanous routines
  2. *
  3. * 06/16/88 MattS Created
  4. *
  5. * 10/15/89 MattS Added AutoDoc comment blocks
  6. */
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #ifdef MSDOS
  10. #include <io.h>
  11. #include <stdlib.h>
  12. #endif
  13. #include <fcntl.h>
  14. #include <malloc.h>
  15. #include <string.h>
  16. #include "misc.h"
  17. #define FALSE 0
  18. #define TRUE 1
  19. /*
  20. ** Compare a string with a hi-bit terminates string
  21. */
  22. #ifdef NOT_NEEDED
  23. /*
  24. * @doc MISC INTERNAL
  25. *
  26. * @func int | hb_strcmp | This function compares a string with a
  27. * hi-bit terminated string. (instead of zero terminated)
  28. *
  29. * @parm unsigned char * | str | Zero terminated string.
  30. *
  31. * @parm unsigned char * | hb_str | High-bit terminated string.
  32. *
  33. * @rdesc The return is the same as strcmp.
  34. *
  35. */
  36. hb_strcmp(str, hb_str)
  37. register unsigned char *str, *hb_str;
  38. {
  39. unsigned char hbc;
  40. for (;;) {
  41. hbc = *hb_str & (unsigned char)0x7f;
  42. if (hbc == *str) {
  43. if (*++str == 0)
  44. return (*hb_str & 0x80) ? 0 : -1;
  45. else if (*hb_str++ & 0x80)
  46. return 1;
  47. } else
  48. return *str - hbc;
  49. }
  50. }
  51. /*
  52. * @doc MISC
  53. *
  54. * @func int | hex_nyb | This function converts the hex character into
  55. * binary.
  56. *
  57. * @parm int | chr | The character to convert.
  58. *
  59. * @rdesc The return is the binary hex value of <p chr>.
  60. *
  61. */
  62. int hex_nyb(chr)
  63. register int chr;
  64. {
  65. if (chr >= '0' && chr <= '9')
  66. return chr - '0';
  67. else
  68. if (chr >= 'A' && chr <= 'Z')
  69. return chr - 'A' + 0xA;
  70. else
  71. return chr - 'a' + 0xA;
  72. }
  73. /*
  74. * @doc INTERNAL MISC
  75. *
  76. * @func int | hex_bytes | This function converts a hex string to binary.
  77. *
  78. * @parm char * | str | Specifies string to convert.
  79. *
  80. * @parm int | nbytes | Specifies length of string.
  81. *
  82. * @rdesc Returns binary value of hex string.
  83. *
  84. * @xref hex_nyb
  85. *
  86. */
  87. int hex_bytes(str, nbytes)
  88. register char *str;
  89. register int nbytes;
  90. {
  91. register int ret = 0;
  92. while (nbytes--) {
  93. ret <<= 4;
  94. ret += hex_nyb(*str++);
  95. }
  96. return ret;
  97. }
  98. /*
  99. * @doc INTERNAL MISC
  100. *
  101. * @func long | htoi | This function converts a hex string to a long.
  102. *
  103. * @parm char * | str | Specifies string to convert.
  104. *
  105. * @xref hex_bytes
  106. *
  107. */
  108. /*
  109. * Interpret an ASCII string as a hex number, convert to long int.
  110. */
  111. long htoi(str)
  112. char *str;
  113. {
  114. register long ret = 0;
  115. register unsigned char ch;
  116. while (ch = *str++)
  117. if (ch >= '0' && ch <= '9')
  118. ret = (ret << 4) + ch - '0';
  119. else if (ch >= 'A' && ch <= 'Z')
  120. ret = (ret << 4) + ch - 'A' + 10;
  121. else if (ch >= 'a' && ch <= 'z')
  122. ret = (ret << 4) + ch - 'a' + 10;
  123. else return ret;
  124. return ret;
  125. }
  126. /* nindex --
  127. *
  128. */
  129. /*
  130. *
  131. * @doc INTERNAL MISC
  132. *
  133. * @func int | nindex | looks for pattern p in string s starting at position start.
  134. *
  135. * @parm char * | p | Specifies pattern.
  136. *
  137. * @parm char * | s | Specifies string.
  138. *
  139. * @parm int | start | Specifies starting offset in string.
  140. *
  141. * @rdesc It returns an index to the position AFTER p if it succeeds; else -1.
  142. */
  143. int
  144. nindex(p,s,start)
  145. char p[];
  146. char s[];
  147. int start;
  148. {
  149. register int j = start;
  150. register int i;
  151. int match = -1;
  152. int nLineLen = strlen(s);
  153. int nPattLen = strlen(p);
  154. while ( j < nLineLen )
  155. {
  156. /*find first letter of pattern
  157. */
  158. while (j < nLineLen)
  159. {
  160. if (p[0] == s[j++])
  161. { /* found 1st char of pattern */
  162. match = 1; /* toggle flag */
  163. break; /* go on to next step */
  164. }
  165. }
  166. if (match == 1)
  167. {
  168. /* look for rest of pattern
  169. */
  170. for (i = 1; (i < nPattLen) && (j < nLineLen) ; i++,j++)
  171. {
  172. if (p[i] != s[j])
  173. {
  174. match = -1; /* pattern doesn't match: toggle flag */
  175. break; /* stop looking */
  176. }
  177. }
  178. if (j >= nLineLen) /* if we're at end, it doesn't matter */
  179. {
  180. match = -1;
  181. break; /* break main loop with flag = fail */
  182. }
  183. else
  184. {
  185. if (match == 1) /* found the pattern */
  186. {
  187. match = j; /* match points to char after pattern */
  188. break; /* break main loop with flag = success */
  189. }
  190. }
  191. }
  192. }
  193. return(match);
  194. }
  195. /*
  196. *
  197. * @doc INTERNAL MISC
  198. *
  199. * @func int | parse | Split of the line, point <p fl> array to each record, records are seperated by
  200. * <p sep> charactor.
  201. *
  202. * @parm char * | cp | Specifies line.
  203. *
  204. * @parm char ** | fl | Specifies array of argument pointers.
  205. *
  206. * @parm char | sep | Specifies argument seperator character.
  207. *
  208. * @rdesc This function returns the number of fields it found.
  209. */
  210. int
  211. parse(cp, fl, sep)
  212. register char *cp, **fl, sep;
  213. {
  214. register int nfields = 1;
  215. *fl++ = cp;
  216. while (1) {
  217. if (*cp == sep) {
  218. nfields++;
  219. *fl++ = cp + 1;
  220. *cp = 0;
  221. } else if (*cp == '\n' || *cp == '\0') {
  222. *cp = 0;
  223. return nfields;
  224. }
  225. cp++;
  226. }
  227. }
  228. /*
  229. *
  230. * Progress routines
  231. *
  232. *
  233. */
  234. long pinc, ploc, loc;
  235. /*
  236. *
  237. * @doc INTERNAL MISC
  238. *
  239. * @func void | init_progress | This function initializes the progress
  240. * indicator.
  241. *
  242. * @parm long | size | Specifies the total size.
  243. *
  244. * @xref show_progress
  245. *
  246. */
  247. void
  248. init_progress(size)
  249. long size;
  250. {
  251. ploc = pinc = size / 80;
  252. loc = 0;
  253. }
  254. /*
  255. *
  256. * @doc INTERNAL MISC
  257. *
  258. * @func void | show_progress | This function displays the progress
  259. * according to the initialized value in <f init_progress>.
  260. *
  261. * @parm int | amount | Specifies the delta processed since last call
  262. *
  263. */
  264. void
  265. show_progress(int amt)
  266. {
  267. if( (loc + amt) >ploc)
  268. {
  269. while( (loc + pinc) <= ploc+ amt) {
  270. loc+=pinc;
  271. fputc('.', stdout);
  272. fflush(stdout);
  273. }
  274. ploc += amt;
  275. }
  276. }
  277. /*
  278. ** mem_to_long and long_to_mem are used to convert back and forth between
  279. * the byte-order independant representation of integers and the
  280. * the internal C format
  281. */
  282. /*
  283. *
  284. * @doc INTERNAL MISC
  285. *
  286. * @func long | mem_to_long | Used to convert back and forth between
  287. * the byte-order independant representation of integers and the
  288. * the internal C format.
  289. *
  290. * @parm unsigned char * |cp| Specifies the memory to convert.
  291. *
  292. * @parm short |nbytes | Specifies the number of bytes to convert.
  293. *
  294. * @rdesc This function returns the Local version of the value contained
  295. * in the memory location.
  296. *
  297. * @comm This function was meant to provide byte-order independence capability
  298. * in C code and was used in Microsoft Bookshelf and the Microsoft Library
  299. * products.
  300. *
  301. * @xref long_to_mem
  302. *
  303. */
  304. long
  305. mem_to_long(cp, nbytes)
  306. register unsigned char *cp;
  307. register short nbytes;
  308. {
  309. register int i;
  310. register long ret = 0;
  311. for (i = nbytes; i > 0;) {
  312. ret <<= 8;
  313. ret += cp[--i];
  314. }
  315. return ret;
  316. }
  317. /*
  318. *
  319. * @doc INTERNAL MISC
  320. *
  321. * @func void | long_to_mem | This function is used to convert back and forth between
  322. * the byte-order independant representation of integers and the
  323. * the internal C format.
  324. *
  325. * @parm long | value | Specifies the value to save into the memory.
  326. *
  327. * @parm unsigned char * |cp| Specifies the memory buffer.
  328. *
  329. * @parm short |nbytes | Specifies the number of bytes to convert.
  330. *
  331. * @comm This function was meant to provide byte-order independence capability
  332. * in C code and was used in Microsoft Bookshelf and the Microsoft Library
  333. * products.
  334. *
  335. * @xref mem_to_long
  336. *
  337. */
  338. void
  339. long_to_mem(val, cp, nbytes)
  340. register long val;
  341. register unsigned char *cp;
  342. register int nbytes;
  343. {
  344. register int i;
  345. for (i = 0; i < nbytes; i++) {
  346. *cp++ = (unsigned char)val & (unsigned char)0xff;
  347. val >>= 8;
  348. }
  349. }
  350. /*
  351. ** This function is available under Sun 3.2 UNIX but not on MS-DOS so I
  352. ** had to write it in order to use the index building tools under MS-DOS
  353. **
  354. ** getopt(argc, argv, template)
  355. ** argc, argv are the arguments to main
  356. ** template is a string of legal arguemnt characters.
  357. ** if a character in the template is followed by a ':', this means
  358. ** that the option takes an additional argument which will either be the
  359. ** remainder of the argument string if this argument strings ended,
  360. ** the next argument.
  361. **
  362. ** RETURNS:
  363. ** '?' on error, print usage and exit
  364. ** EOF when end of options is hit, use "optind" to get non option args
  365. ** chr means that option "chr" was specified and if it takes an
  366. ** additional argument, that would be in the string "optarg"
  367. **
  368. ** Here is an example program which just echos back how it parsed the command
  369. ** line, using getopt. This template says the program has flags 'a' and 'b'
  370. ** and options 'c' and 'd'. Legal commmand lines would be:
  371. ** D>prog hello there folks
  372. ** D>prog -a hello folks
  373. ** D>prog -ab -chello -d there folks
  374. ** D>prog -abc hello -dthere folks
  375. **
  376. ** extern char *optarg;
  377. ** extern int optind;
  378. ** char *my_template = "abc:d:";
  379. **
  380. ** main(argc, argv)
  381. ** char **argv;
  382. ** {
  383. ** int arg;
  384. **
  385. ** while (arg = getopt(argv, argv, my_template))
  386. ** if (optarg)
  387. ** printf("-%c %s\n", arg, optarg);
  388. ** else if (arg == '?') {
  389. ** printf("Argument error\n");
  390. ** break;
  391. ** } else
  392. ** printf("-%c\n", arg);
  393. ** for (arg = optind; arg < argc - 1; arg++)
  394. ** printf("### %s\n", argv[arg]);
  395. ** }
  396. */
  397. static int argNum;
  398. static char *optparse;
  399. int optind = 1;
  400. char *optarg;
  401. getopt(argc, argv, template)
  402. int argc;
  403. char **argv,
  404. *template;
  405. {
  406. register char *arg, *tp;
  407. optarg = NULL;
  408. if (!optparse)
  409. if (++argNum < argc) {
  410. arg = argv[argNum];
  411. if (*arg != '-') {
  412. optind = argNum;
  413. return EOF;
  414. }
  415. optparse = arg + 1;
  416. } else
  417. return EOF;
  418. for (tp = template; *tp; tp++)
  419. if (*tp == *optparse) {
  420. if (tp[1] == ':') {
  421. if (optparse[1]) {
  422. optarg = optparse + 1;
  423. optparse = 0;
  424. } else
  425. if (++argNum == argc)
  426. return '?';
  427. else
  428. optarg = argv[argNum];
  429. }
  430. if (optparse && *++optparse == 0)
  431. optparse = NULL;
  432. return *tp;
  433. }
  434. return '?';
  435. }
  436. #endif
  437. /*
  438. /*
  439. *
  440. * @doc INTERNAL MISC
  441. *
  442. * @func int | findlshortname | This function returns the offset of the
  443. * end of the complete path and file name not counting the file extension.
  444. *
  445. * @parm char * | fullname | Specifies the name to search.
  446. *
  447. * @rdesc The return value is the offset of the end of the complete path
  448. * and file name not counting the file extension.
  449. *
  450. */
  451. int
  452. findlshortname(fullname) /* find the length of the short name */
  453. /* full path/file name not counting extension */
  454. char *fullname;
  455. {
  456. char *ch;
  457. int cnt;
  458. if(!fullname || !*fullname)
  459. return(0);
  460. cnt=strlen(fullname);
  461. ch=fullname+cnt;
  462. while(*ch!='.' && *ch!='\\' && *ch!='/' && cnt)
  463. {
  464. ch--;
  465. cnt--;
  466. }
  467. if(!cnt)
  468. return(strlen(fullname));
  469. return(ch - fullname);
  470. }
  471. /* getblong - get the next number from a buffer. Returns a long positive
  472. * value if successful, otherwise returns -1. Scans-off leading BS.
  473. * Parameters: takes a pointer to a char and a pointer to an int which
  474. * will become a pointer the character following the number.
  475. */
  476. /*
  477. *
  478. * @doc INTERNAL MISC
  479. *
  480. * @func long | getblong | ATOL for buffer.
  481. *
  482. * @parm char * | line | Specifies buffer.
  483. *
  484. * @parm int * | i | Specifies current offset into buffer. This value is
  485. * updated as the buffer is scanned.
  486. *
  487. * @rdesc This function returns the Long value of the ASCII value contained
  488. * in the buffer at the specified offset.
  489. *
  490. * The offset is updated to the next character after the last ASCII character.
  491. *
  492. */
  493. long
  494. getblong(line, i)
  495. char *line;
  496. int *i;
  497. {
  498. int pos = *i;
  499. long result = 0;
  500. int nLineLen = strlen(line);
  501. /* while (!isdigit(line[pos]) && pos < nLineLen) */
  502. /* ++pos; */
  503. if (pos == nLineLen)
  504. return ((long)-1);
  505. while (isdigit(line[pos]) && pos < nLineLen)
  506. result = 10 * result + line[pos++] - '0';
  507. *i = pos;
  508. return (result);
  509. }
  510. #ifdef NOT_NEEDED
  511. /*
  512. *
  513. * @doc INTERNAL MISC
  514. *
  515. * @func char * | parse_sec_name | This function parses the section
  516. * name from a complete name.
  517. *
  518. * It was written for use in for Compound File indexing tools for
  519. * Microsoft Library.
  520. *
  521. * @parm char ** | ppch | Specifies the name to parse.
  522. *
  523. * @rdesc Ack.
  524. *
  525. */
  526. char *
  527. parse_sec_name(char **ppch)
  528. {
  529. char *pch;
  530. char *p2ch;
  531. int j;
  532. if(!ppch || !*ppch)
  533. return(NULL);
  534. pch=*ppch;
  535. while(*pch && *pch!='!')
  536. pch++;
  537. if(!*pch) /* default section name is file name */
  538. return(*ppch);
  539. *pch='\0';
  540. pch++;
  541. p2ch=*ppch;
  542. *ppch=pch;
  543. return(p2ch);
  544. }
  545. #endif
  546. /* *
  547. * *
  548. * Memory Management routines *
  549. * *
  550. * *
  551. * */
  552. char _achmemout[]= "Oh my, we seem to be out of %smemory. %ld Allocated\n" ;
  553. /* Generic memory management */
  554. /*
  555. *
  556. * @doc INTERNAL MISC
  557. *
  558. * @func char * | cp_alloc | Allocates memory for the string and copies
  559. * it into the buffer and returns it.
  560. *
  561. * @parm char * | pch| Specifies string to copy.
  562. *
  563. * @comm The buffer should be freed with <f my_free>.
  564. *
  565. */
  566. char *
  567. cp_alloc(pch)
  568. char *pch;
  569. {
  570. char *pch2;
  571. if(!pch)
  572. return(NULL);
  573. pch2=my_malloc(strlen(pch)+1);
  574. strcpy(pch2,pch);
  575. return pch2;
  576. }
  577. /*
  578. *
  579. * @doc INTERNAL MISC
  580. *
  581. * @func void | memfil | Fills the memory with zero.
  582. *
  583. * @parm int * | mem | Specifies the memory block to fill.
  584. *
  585. * @parm unsigned int | size | Specifies the size of the memory block.
  586. *
  587. * @comm The size of the memory block does not have to be a multiple of 2.
  588. *
  589. */
  590. void
  591. memfil(mem,size)
  592. register int *mem;
  593. register unsigned int size;
  594. {
  595. char flag=FALSE;
  596. char *pch;
  597. if(size&1) /* it's odd so int fill won't work all the way */
  598. {
  599. flag=TRUE;
  600. size--;
  601. }
  602. size=size/2;
  603. while(size--)
  604. *mem++=0;
  605. if(flag)
  606. {
  607. pch=(char *)mem;
  608. *pch='\0';
  609. }
  610. }
  611. /*
  612. *
  613. * @doc INTERNAL MISC
  614. *
  615. * @func char * | clear_alloc | This function allocates memory and
  616. * initialized it to zeros.
  617. *
  618. * @parm unsigned int | size | Specifies the size of the memory to allocate.
  619. *
  620. * @comm The allocated memory should be freed with <f my_free>.
  621. *
  622. */
  623. char *
  624. clear_alloc(size)
  625. unsigned int size;
  626. {
  627. char *pmem;
  628. pmem=my_malloc(size);
  629. memfil((int *)pmem,size);
  630. return pmem;
  631. }
  632. static long ivemalloc=0;
  633. /*
  634. *
  635. * @doc INTERNAL MISC
  636. *
  637. * @func char * | my_malloc | Allocates memory and checks for errors.
  638. *
  639. * @parm unsigned int | size | Specifies the size of the memory to allocate.
  640. *
  641. * @comm The allocated memory should be freed with <f my_free>.
  642. *
  643. */
  644. char *
  645. my_malloc(size)
  646. unsigned int size;
  647. {
  648. char *pmem;
  649. int hck;
  650. if(size>32767)
  651. {
  652. fprintf(stderr,"Size >32K\n");
  653. exit(666);
  654. }
  655. hck=_heapchk();
  656. if(hck == _HEAPBADNODE)
  657. {
  658. fprintf(stderr,"Bad node in Heap. Ack!\n");
  659. exit(666);
  660. }
  661. if(hck == _HEAPBADBEGIN)
  662. {
  663. fprintf(stderr,"Bad begin in Heap. Ack!\n");
  664. exit(666);
  665. }
  666. pmem=(char *)malloc(size);
  667. if(pmem==NULL)
  668. {
  669. fprintf(stderr,_achmemout,"",ivemalloc);
  670. exit(777);
  671. }
  672. ivemalloc+=size;
  673. return(pmem);
  674. }
  675. /*
  676. *
  677. * @doc INTERNAL MISC
  678. *
  679. * @func void | my_free | Frees the specified buffer.
  680. *
  681. * @parm void * | buffer | Specifies the buffe to free.
  682. *
  683. */
  684. void
  685. my_free(void * buffer)
  686. {
  687. if(!buffer)
  688. return;
  689. ivemalloc-=_msize(buffer);
  690. free(buffer);
  691. return;
  692. }
  693. /* used by old indexing tools */
  694. char *cpalloc(str)
  695. char *str;
  696. {
  697. return(cp_alloc(str));
  698. }
  699. /*
  700. *
  701. * @doc INTERNAL MISC
  702. *
  703. * @func void | setmem | Sets the memory to the specified value.
  704. *
  705. * @parm char * | mem | Specifies the memory.
  706. *
  707. * @parm int | size | Specifies the size of the memory block.
  708. *
  709. * @parm char | val | Specifies the value to set the memory to.
  710. *
  711. * @comm Filling with zero is much faster with <f mem_fil>.
  712. *
  713. */
  714. void
  715. setmem(src, size, val)
  716. register char *src;
  717. register int size;
  718. register char val;
  719. {
  720. while (size-- > 0)
  721. *src++ = val;
  722. }
  723. /*
  724. *
  725. * @doc INTERNAL MISC
  726. *
  727. * @func void | movmem | This function moves the specified memory.
  728. * Overlap is not checked.
  729. *
  730. * @parm char * | src | Source of copy.
  731. *
  732. * @parm char * | dst | Destination of copy.
  733. *
  734. * @parm int | len | Specifies number of bytes to copy.
  735. *
  736. */
  737. void
  738. movmem(src,dst,len)
  739. register char *src, *dst;
  740. register int len;
  741. {
  742. while (len-- > 0)
  743. *dst++ = *src++;
  744. }
  745. #ifdef NOT_NEEDED
  746. /* explicit NEAR memory management calls */
  747. char near *
  748. ncp_alloc(pch)
  749. char near *pch;
  750. {
  751. char near *pch2;
  752. if(!pch)
  753. return(NULL);
  754. pch2=nmy_malloc(strlen(pch)+1);
  755. strcpy(pch2,pch);
  756. return pch2;
  757. }
  758. void
  759. nmemfil(mem,size)
  760. register int near *mem;
  761. register unsigned int size;
  762. {
  763. char flag=FALSE;
  764. char near *pch;
  765. if(size&1) /* it's odd so int fill won't work all the way */
  766. {
  767. flag=TRUE;
  768. size--;
  769. }
  770. size=size/2;
  771. while(size--)
  772. *mem++=0;
  773. if(flag)
  774. {
  775. pch=(char near *)mem;
  776. *pch='\0';
  777. }
  778. }
  779. char near *
  780. nclear_alloc(size)
  781. unsigned int size;
  782. {
  783. char near *pmem;
  784. pmem=nmy_malloc(size);
  785. memfil((int near*)pmem,size);
  786. return pmem;
  787. }
  788. static long ivenmalloc=0;
  789. char near *
  790. nmy_malloc(size)
  791. unsigned int size;
  792. {
  793. char near *pmem;
  794. pmem=(char near *)_nmalloc(size);
  795. if(pmem==NULL)
  796. {
  797. fprintf(stderr,_achmemout,"NEAR ",ivenmalloc);
  798. exit(777);
  799. }
  800. ivenmalloc+=size;
  801. return(pmem);
  802. }
  803. /* used by old indexing tools */
  804. char near *
  805. ncpalloc(str)
  806. char near *str;
  807. {
  808. return(ncp_alloc(str));
  809. }
  810. void
  811. nsetmem(src, size, val)
  812. register char near *src;
  813. register int size;
  814. register char val;
  815. {
  816. while (size-- > 0)
  817. *src++ = val;
  818. }
  819. void
  820. nmovmem(src,dst,len)
  821. register char near *src;
  822. register char near *dst;
  823. register int len;
  824. {
  825. while (len-- > 0)
  826. *dst++ = *src++;
  827. }
  828. /* explicit FAR memory management calls */
  829. char far *
  830. fcp_alloc(pch)
  831. char far *pch;
  832. {
  833. char far *pch2;
  834. if(!pch)
  835. return(NULL);
  836. pch2=fmy_malloc(strlen(pch)+1);
  837. strcpy(pch2,pch);
  838. return pch2;
  839. }
  840. void
  841. fmemfil(mem,size)
  842. register int far *mem;
  843. register unsigned int size;
  844. {
  845. char flag=FALSE;
  846. char far *pch;
  847. if(size&1) /* it's odd so int fill won't work all the way */
  848. {
  849. flag=TRUE;
  850. size--;
  851. }
  852. size=size/2;
  853. while(size--)
  854. *mem++=0;
  855. if(flag)
  856. {
  857. pch=(char far *)mem;
  858. *pch='\0';
  859. }
  860. }
  861. char far *
  862. fclear_alloc(size)
  863. unsigned int size;
  864. {
  865. char far *pmem;
  866. pmem=fmy_malloc(size);
  867. fmemfil((int far*)pmem,size);
  868. return pmem;
  869. }
  870. static long ivefmalloc=0;
  871. char far *
  872. fmy_malloc(size)
  873. unsigned int size;
  874. {
  875. char far *pmem;
  876. pmem=(char far *)_fmalloc(size);
  877. if(pmem==NULL)
  878. {
  879. fprintf(stderr,_achmemout,"FAR ",ivefmalloc);
  880. exit(777);
  881. }
  882. ivefmalloc+=size;
  883. return(pmem);
  884. }
  885. /* used by old indexing tools */
  886. char far *
  887. fcpalloc(str)
  888. char far *str;
  889. {
  890. return(fcp_alloc(str));
  891. }
  892. void
  893. fsetmem(src, size, val)
  894. register char far *src;
  895. register int size;
  896. register char val;
  897. {
  898. while (size-- > 0)
  899. *src++ = val;
  900. }
  901. void
  902. fmovmem(src,dst,len)
  903. register char far *src;
  904. register char far *dst;
  905. register int len;
  906. {
  907. while (len-- > 0)
  908. *dst++ = *src++;
  909. }
  910. #endif
  911. /*
  912. *
  913. * @doc INTERNAL MISC
  914. *
  915. * @func void | mymktemp | Make a temporary file. The returned file name is
  916. * guranteed to be unique and not already exist.
  917. *
  918. * @parm char * | lpszpath | Specifies the path to create the file.
  919. *
  920. * @parm char * | lpszbuffer | Specifies the buffer to receive the
  921. * full path/file name.
  922. *
  923. *
  924. */
  925. void
  926. mymktemp(char * lpszpath, char * lpszbuffer)
  927. {
  928. static int i=0;
  929. FILE *fp;
  930. sprintf(lpszbuffer,"%sdoc%05X.xxx",lpszpath,i++);
  931. fp=fopen(lpszbuffer,"r");
  932. if(!fp)
  933. return;
  934. fclose(fp);
  935. while(fp)
  936. {
  937. if(i>30000)
  938. {
  939. fprintf(stderr,"ERROR: Cannot create temporary files on %s.\n",lpszpath);
  940. exit(1);
  941. }
  942. sprintf(lpszbuffer,"%sdoc%05X.xxx",lpszpath,i++);
  943. fp=fopen(lpszbuffer,"r");
  944. if(fp)
  945. fclose(fp);
  946. }
  947. }