home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / b / bmh02src.zip / PC.C < prev    next >
C/C++ Source or Header  |  1992-08-16  |  3KB  |  139 lines

  1. /*
  2.    pc.c : Copyright Paul Healy, EI9GL, 1992.
  3.  
  4.    Derived from bm.
  5.  
  6.    Copyright 1986 Bdale Garbee, All Rights Reserved.
  7.    Permission granted for non-commercial copying and use, provided
  8.    this notice is retained.
  9.    Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
  10.    Permission granted for non-commercial copying and use, provided
  11.    this notice is retained.
  12.  
  13.    This file contains MSDOS specific functions
  14.  
  15.    911215 : Added this header
  16.    920809 : Added option for command line editing with CED
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <ctype.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <dos.h>
  26. #include <dir.h>
  27. #include <conio.h>
  28. #include "pc.h"
  29. #include "rc.h"
  30.  
  31. void
  32. setvideo(char *s)
  33. {
  34. #ifdef __TURBOC__
  35.    if (strncmp("bios", s, 4) == 0)
  36.       directvideo = 0;
  37.    else
  38.       directvideo = 1;
  39. #endif
  40. }
  41.  
  42. char *
  43. getline(char *line, int size)
  44. {
  45.    char *opt = getrc(editline);
  46.  
  47.    if ( (opt == NULL) || (tolower(*opt) == 'n'))
  48.       return fgets(line, size, stdin);
  49.    else {
  50.       char *p;
  51.       line[0] = size;
  52.       line[1] = 0;     /* INT 21h, AH=0Ah, DS:DX[1] = length of previous line */
  53.       p = cgets(line); /* that can be returned - just for safety */
  54.       putchar('\n');   /* emulate fgets */
  55.       return p;
  56.       }
  57. }
  58.  
  59. static int
  60. numfiles(char *name)
  61. {
  62.    struct ffblk sbuf;
  63.    int n = 0;
  64.  
  65.    if (findfirst(name, &sbuf, FA_HIDDEN | FA_SYSTEM | FA_DIREC ) == 0) {
  66.       n++;
  67.       while (findnext(&sbuf) == 0)
  68.          n++;
  69.       }
  70.  
  71.    return n;
  72. }
  73.  
  74. /* directory search utility for DOS */
  75.  
  76. void
  77. filedir(char *name, int times, char *ret_str)
  78. {
  79.    static struct ffblk sbuf;
  80.  
  81.    if (times == 0) {
  82.       if (findfirst(name, &sbuf, FA_HIDDEN | FA_SYSTEM | FA_DIREC ))
  83.          sbuf.ff_name[0] = '\0'; /* make eof/directory mark */
  84.       }
  85.    else
  86.       if (findnext(&sbuf))
  87.          sbuf.ff_name[0] = '\0'; /* make eof/directory mark */
  88.    strcpy(ret_str, sbuf.ff_name);
  89.    strlwr(ret_str);
  90. }
  91.  
  92. static int
  93. getdir(char *name, String buf[], int max)
  94. {
  95.    int i = 0;
  96.  
  97.    filedir(name, 0, buf[i]);
  98.    while ((buf[i][0] != '\0') && (i<max) ) {
  99.       i++;
  100.       filedir(name, 1, buf[i]);
  101.       }
  102.    if (i)
  103.       qsort(buf, i, sizeof(String), strcmp);
  104.    return i;
  105. }
  106.  
  107. void
  108. initdir(Directory *d)
  109. {
  110.    d->buf = NULL;
  111. }
  112.  
  113. char *
  114. readdir(Directory *d, char *name)
  115. {
  116.    if (d->buf == NULL) {  /* get directory listing to buffer */
  117.       int maxfound;
  118.  
  119.       if ( (maxfound = numfiles(name)) == NULL)
  120.          return NULL; /* empty directory */
  121.       if ( (d->buf = (String *) malloc(sizeof(String) * maxfound) ) == NULL) {
  122.          fprintf(stderr, "readdir: no room for directory listing\n");
  123.          return NULL;
  124.          }
  125.       d->num = getdir(name, d->buf, maxfound);
  126.       if (d->num != maxfound)
  127.          fprintf(stderr, "readdir: warning, directory changed during listing\n");
  128.       d->ptr = 0;
  129.       }
  130.    else {
  131.       d->ptr = d->ptr + 1;
  132.       if (d->ptr == d->num) {
  133.          free(d->buf);
  134.          d->buf = NULL;
  135.          }
  136.       }
  137.    return d->ptr<d->num ? d->buf[d->ptr] : NULL;
  138. }
  139.