home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / RAMDISK / SRDSK141.ZIP / SRDISK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-06  |  5.3 KB  |  230 lines

  1. /*
  2. **      ReSizeable RAMDisk formatter
  3. **
  4. **      Copyright (c) 1992 Marko Kohtala
  5. **
  6. **      Some documentation and license available in accompanying file
  7. **      SRDISK.DOC. If not, contact author by sending E-mail from
  8. **
  9. **              Internet, Bitnet etc. to 'Marko.Kohtala@hut.fi'
  10. **              CompuServe to '>INTERNET:Marko.Kohtala@hut.fi'
  11. **
  12. **      or by calling Airline QBBS, 24H, HST, V.32, V.42, MNP,
  13. **      +358-0-8725380, and leaving mail to me, Marko Kohtala.
  14. **
  15. **      In general, this is FREEWARE.
  16. **
  17. **      Compilable with Borland C++ 3.0.
  18. **
  19. */
  20.  
  21. #include "srdisk.h"
  22. #include <stdio.h>
  23. #include <conio.h>
  24. #include <ctype.h>
  25. #include <time.h>
  26. #include <dos.h>
  27.  
  28. struct config_s far *mainconf;
  29. struct config_s far *conf;
  30.  
  31. struct format_s f, newf;
  32. int changed_format;   /* Differences between f and newf */
  33.  
  34. int root_files = 1;   /* Number of files in root directory */
  35. int max_bps = 512;    /* Largest possible sector size on system */
  36.  
  37. /*
  38. **  SYNTAX
  39. */
  40.  
  41. void print_syntax(void)
  42. {
  43.   fputs(
  44.   "\n"
  45.   "Syntax:  SRDISK [<drive letter>[:]] [<size>] [/F:<DOS disk type>]\n"
  46.   "\t\t[/S:<sector size>] [/C:<cluster size>] [/D:<dir entries>]\n"
  47.   "\t\t[/A:<FAT copies>] [/M:<size>[:<size>[...]]] [/W[-]]\n"
  48.   "\t\t[/V:<verbose>] [/O] [/Y] [/E] [/?]\n\n"
  49.  
  50.   "Anything inside [] is optional, the brackets must not be typed.\n"
  51.   "'<something>' must be replaced by what 'something' tells.\n\n"
  52.  
  53.   "<drive letter> specifies the drive that is the RAM disk.\n"
  54.   "<size> determines the disk size in kilobytes.\n"
  55.   "/F:<DOS disk type> may be one of 1, 160, 180, 320, 360, 720, 1200, 1440.\n"
  56.   "/S:<sector size> is a power of 2 in range from 128 to 512 bytes.\n"
  57.   "/C:<cluster size> is a power of 2 in range from 128 to 8192 bytes.\n"
  58.   "/D:<dir entries> is the maximum number of entries in the root directory.\n"
  59.   "/A:<FAT copies> number of File Allocation Tables on disk. 1 is enough.\n"
  60.   "/M List of max memory to allocate by each driver in driver chain\n"
  61.   "/W Write protect disk, /W- enables writes.\n"
  62.   "/V Verbose level from 1 (quiet) to 5 (verbose); default 2.\n"
  63.   "/E Set environment variables SRDISKn (n=1,2,...) to SRDISK drive letters.\n"
  64.   "/O Old format as default.               /Y Yes, destroy the contents.\n"
  65.   "/? This help.\n"
  66.   , stderr);
  67. }
  68.  
  69.  
  70. /*
  71. **  ERROR HANDLING FUNCTIONS
  72. */
  73.  
  74. void syntax(char *err)
  75. {
  76.   fprintf(stderr, "\nSyntax error: %s\n", err);
  77.   print_syntax();
  78.   exit(3);
  79. }
  80.  
  81. void fatal(char *err)
  82. {
  83.   fprintf(stderr, "\nFatal error: %s\n", err);
  84.   exit(1);
  85. }
  86.  
  87. void error(char *err)
  88. {
  89.   fprintf(stderr, "\nError: %s\n", err);
  90.   return;
  91. }
  92.  
  93. void warning(char *err)
  94. {
  95.   fprintf(stderr, "\nWarning: %s\n", err);
  96.   return;
  97. }
  98.  
  99.  
  100. /*
  101. **  Local allocation routine with error check
  102. */
  103.  
  104. void *xalloc(size_t s)
  105. {
  106.   void *b = malloc(s);
  107.   if (!s) fatal("malloc() failed");
  108.   return b;
  109. }
  110.  
  111. /*
  112. **  Get Y/N response
  113. */
  114.  
  115. int getYN(void)
  116. {
  117.   int reply;
  118.  
  119.   if (force_f) reply = 'Y';
  120.   else {
  121.     do reply = toupper(getch());
  122.     while (reply != 'Y' && reply != 'N');
  123.   }
  124.   printf("%c\n", reply);
  125.   if (reply == 'N') return 0;
  126.   return 1;
  127. }
  128.  
  129.  
  130. /*
  131. **  DOS time format conversions
  132. */
  133.  
  134. dword DOS_time(time_t time)
  135. {
  136.   struct tm *ltime;
  137.   union {
  138.     struct {
  139.       unsigned int
  140.            sec2 : 5,
  141.            min : 6,
  142.            hour : 5,
  143.            day : 5,
  144.            month : 4,
  145.            year : 7;
  146.     } f;
  147.     dword l;
  148.   } file_time;
  149.  
  150.   ltime = localtime(&time);
  151.   file_time.f.sec2 = ltime->tm_sec;
  152.   file_time.f.min = ltime->tm_min;
  153.   file_time.f.hour = ltime->tm_hour;
  154.   file_time.f.day = ltime->tm_mday;
  155.   file_time.f.month = ltime->tm_mon + 1;
  156.   file_time.f.year = ltime->tm_year - 80;
  157.  
  158.   return file_time.l;
  159. }
  160.  
  161. /*
  162. **  CONFIGURATION POINTER CHECKUP
  163. */
  164.  
  165. struct config_s far *conf_ptr(struct dev_hdr _seg *dev)
  166. {
  167.   struct config_s far *conf;
  168.   if (!dev) return (void far *)NULL;
  169.   conf = MK_FP(dev, dev->conf);
  170.   if (dev->u.s.ID[0] != 'S'
  171.    || dev->u.s.ID[1] != 'R'
  172.    || dev->u.s.ID[2] != 'D'
  173.    || dev->v_format != V_FORMAT
  174.    || (conf->drive != '$'
  175.       && !(   (conf->drive >= 'A' && conf->drive <= 'Z')
  176.            || (conf->drive >= '1' && conf->drive <= '9')) )
  177.    || !conf->disk_IO
  178.    || !conf->malloc_off)
  179.     fatal("SRDISK devices' internal tables are messed up!");
  180.   return conf;
  181. }
  182.  
  183.  
  184. /*
  185. **  SET WRITE PROTECT
  186. */
  187.  
  188. void set_write_protect(void)
  189. {
  190.   if (newf.RW_access & WRITE_ACCESS) {
  191.     conf->RW_access &= ~WRITE_ACCESS;
  192.     if (verbose > 1)
  193.       printf("\nWrite protect enabled\n");
  194.   }
  195.   else {
  196.     conf->RW_access |= WRITE_ACCESS;
  197.     if (verbose > 1)
  198.       printf("\nWrite protect disabled\n");
  199.   }
  200. }
  201.  
  202. /*
  203. **  MAIN FUNCTION
  204. */
  205.  
  206. int main(int argc, char *argv[])
  207. {
  208.   printf("ReSizeable RAMDisk Formatter version "VERSION". "
  209.          "Copyright (c) 1992 Marko Kohtala.\n");
  210.  
  211.   if (argc > 1) parse_cmdline(argc, argv);
  212.   else if (verbose > 1) printf("\nFor help type 'SRDISK /?'.\n");
  213.  
  214.   if (verbose == -1) verbose = 2;
  215.  
  216.   init_drive();             /* Get pointer to driver configuration */
  217.  
  218.   if (f_set_env) set_env();
  219.  
  220.   if (format_f) format_disk();
  221.   else if (changed_format & WRITE_PROTECTION) set_write_protect();
  222.   else if (!f_set_env && verbose < 4 && verbose > 1) {
  223.     if (f.size) print_format(&f);
  224.     else printf("\nDrive %c: disabled\n", drive);
  225.   }
  226.  
  227.   return 0;
  228. }
  229.  
  230.