⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 format.c

📁 国外网站上的一些精典的C程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/***  Writen by:  Wayne Halsdorf**  Finished:   12-Jul-1994****  This program is released to the PUBLIC DOMAIN.****  A method for formatting floppy disks.****  Testted with Microsoft C 7.0*/#include    <stdio.h>#include    <stdlib.h>#include    <string.h>#if defined(__TURBOC__) #include    <alloc.h>#elif defined(__ZTC__) #if !defined(__SC__) || (__SC__ < 0x700)  #error FORMAT.C >> _fcalloc() not supported bt ZTC or SC ver. 6.xx #endif#else #include    <malloc.h>#endif#include    <dos.h>#include    <direct.h>#include    <time.h>#include    <conio.h>#include    <bios.h>#include    <ctype.h>#include    "format.h"DPB        FAR        *dpb;FPB        FAR        *fpb;APB        FAR        *apb;IOPB       FAR        *iopb;BOOTSECTOR FAR        *BootSector;FORMAT_TABLE    D360[] = {      {"360K", 0, 1, 2, 40,  9, 7, 0xFD, 1, 3, 706},      {"360K", 0, 1, 2, 40,  9, 7, 0xFD, 2, 2, 354},      {"360K", 0, 1, 2, 40,  9, 5, 0xFD, 4, 1, 178}};FORMAT_TABLE    D1200[] = {      {"360K", 0, 1, 2, 40,  9,  7, 0xFD, 1, 3,  706},      {"360K", 0, 1, 2, 40,  9,  7, 0xFD, 2, 2,  354},      {"360K", 0, 1, 2, 40,  9,  5, 0xFD, 4, 1,  178},      {"1.2M", 1, 0, 2, 80, 15,  7, 0xF9, 1, 7, 2378},      {"1.2M", 1, 0, 2, 80, 15,  7, 0xF9, 2, 4, 1192},      {"1.2M", 1, 0, 2, 80, 15,  7, 0xF9, 4, 2,  597},      {"1.2M", 1, 0, 2, 80, 15,  5, 0xF9, 8, 1,  299},      {"1.2M", 1, 0, 2, 80, 15, 14, 0xF9, 1, 7, 2371},      {"1.2M", 1, 0, 2, 80, 15, 15, 0xF9, 2, 4, 1188},      {"1.2M", 1, 0, 2, 80, 15, 15, 0xF9, 4, 2,  595},      {"1.2M", 1, 0, 2, 80, 15, 13, 0xF9, 8, 1,  298}};FORMAT_TABLE    D720[] = {      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 1, 5, 1422},      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 2, 3,  713},      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 4, 2,  357},      { "720K", 2, 0, 2, 80,  9,  5, 0xF9, 8, 1,  179}};FORMAT_TABLE    D1440[] = {      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 1, 5, 1422},      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 2, 3,  713},      { "720K", 2, 0, 2, 80,  9,  7, 0xF9, 4, 2,  357},      { "720K", 2, 0, 2, 80,  9,  5, 0xF9, 8, 1,  179},      {"1.44M", 7, 0, 2, 80, 18,  7, 0xF0, 1, 9, 2854},      {"1.44M", 7, 0, 2, 80, 18,  7, 0xF0, 2, 5, 1431},      {"1.44M", 7, 0, 2, 80, 18,  5, 0xF0, 4, 3,  717},      {"1.44M", 7, 0, 2, 80, 18,  3, 0xF0, 8, 2,  359},      {"1.44M", 7, 0, 2, 80, 18, 14, 0xF0, 1, 9, 2847},      {"1.44M", 7, 0, 2, 80, 18, 13, 0xF0, 2, 5, 1428},      {"1.44M", 7, 0, 2, 80, 18, 13, 0xF0, 4, 3,  715},      {"1.44M", 7, 0, 2, 80, 18, 11, 0xF0, 8, 2,  358}};unsigned int    GetKey(void){      while (!_bios_keybrd(_KEYBRD_READY))            ;      return(_bios_keybrd(_KEYBRD_READ) & 0xff);}void    GetLine(char FAR *in, unsigned int len){      unsigned int    k, l;      char FAR *p;      p = in;      l = 0;      *p = '\0';      do      {            k = GetKey();            if(k == 8 && l)            {                  l--;                  p--;                  *p = '\0';                  printf("\b \b");            }            else  if(__iscsym(k) && l < len)            {                  k = (unsigned int)toupper(k);                  *p = (char)k;                  l++;                  p++;                  *p = '\0';                  printf("%c", k);            }      } while (k != 0x0d);}void    InitVars(void){      dpb = _fcalloc(1, sizeof(DPB));      fpb = _fcalloc(1, sizeof(FPB));      apb = _fcalloc(1, sizeof(APB));      BootSector = _fcalloc(1, sizeof(BOOTSECTOR));}void    FreeVars(void){      if(apb)            _ffree(apb);      if(dpb)            _ffree(dpb);      if(fpb)            _ffree(fpb);      if(iopb->dta)            _ffree(iopb->dta);      if(iopb)            _ffree(iopb);      if(BootSector)            _ffree(BootSector);}void    Error_Message(char *message_){      printf(message_);      FreeVars();      exit(1);}unsigned int    AvailableDrives(void){      _outp(0x70, 16);      return(_inp(0x71));}void    SelectDrive(unsigned int *dr){      unsigned int    d, a;      a = 0;      d = AvailableDrives();      if(d & 0x0f)            /* drive b */            a = 2;      if(d & 0xf0)            /* drive a */            a |= 1;      switch(a)      {      case 0:            Error_Message("No floppies found\n");            break;      case 1:            *dr = 1;            break;      case 2:            *dr = 2;            break;      case 3:            printf("Format which drive (A or B) : ");            do            {                  d = GetKey();                  d = (unsigned int)(toupper(d) - 'A');            } while (d > 1);            printf("%c\n", d + 'A');            *dr = d + 1;            break;      }}void    GetDrive(char *c_, unsigned int *dr){      *dr = (unsigned int)(toupper(*c_) - 'A' + 1);      if(*dr < 1)            Error_Message("Attempted to format unknown device\n");      if(*dr > 2)            Error_Message("Attempted to format non-floppy device\n");}void    GetFloppyTable(unsigned int drive_number,                       unsigned int *s,                       FORMAT_TABLE **ft){      unsigned int    d;      d = AvailableDrives();      if(drive_number == 1)            d = d >> 4;      else  d = d & 15;      switch(d)      {      case 1:            *ft = D360;            *s = sizeof(D360) / sizeof(FORMAT_TABLE);            break;      case 2:            *ft = D1200;            *s = sizeof(D1200) / sizeof(FORMAT_TABLE);            break;      case 3:            *ft = D720;            *s = sizeof(D720) / sizeof(FORMAT_TABLE);            break;      case 4:            *ft = D1440;            *s = sizeof(D1440) / sizeof(FORMAT_TABLE);            break;      default:            Error_Message("Don't know about formats for select drive\n");            break;      }}void    DriveParameters(unsigned int *format,                        FORMAT_TABLE *ftable,                        unsigned int s){      unsigned int    x;      printf("Available formats for selected drive\n");      printf("    Format Directory Cluster  Clusters  Storage\n");      printf("     Type  Available  Size    Available Capacity\n");      for (x = 0; x < s; x++)      {            printf(" %c) %6s    %3u     %4u      %4u   %9lu\n", x + 'A',                   ftable[x].Formats_,                   ftable[x].Max_Entries * 16,                   ftable[x].Cluster_Size * 512,                   ftable[x].Available,                   512L * ftable[x].Available * ftable[x].Cluster_Size);      }      printf("Input which format: ");      do      {            x = GetKey();            x = (unsigned int)(toupper((int)x) - 'A');      } while (x >= s);      printf("%c\n", x + 'A');      *format = x;}unsigned int DOS_IOCTL(unsigned drive, unsigned func, void _far *data){      union   REGS   ir, or;      struct  SREGS  sr;      ir.x.ax = GENERIC_IO;      ir.x.bx = drive;      ir.x.cx = func;      ir.x.dx = _FP_OFF(data);      sr.ds = _FP_SEG(data);      _int86x(0x21, &ir, &or, &sr);      return(or.x.cflag);}unsigned int Extended_Error_Code(void){      union   _REGS   ir, or;      ir.h.ah = 0x59;      ir.x.bx = 0;      _int86(0x21, &ir, &or);      return(or.h.bh);}unsigned int SerialNumber(unsigned drive, unsigned func, void _far *data){      union   _REGS   ir, or;      struct  _SREGS  sr;      ir.x.ax = SERIAL_NUMBER + func;      ir.x.bx = drive;      ir.x.dx = _FP_OFF(data);      sr.ds = _FP_SEG(data);      _int86x(0x21, &ir, &or, &sr);      if(or.x.cflag)            return(or.x.ax);      else  return(0);}unsigned long GenSerialNumber(void){      union   _REGS   ir, or;      union {            unsigned long   d;            unsigned int    i[2];      } g;      ir.x.ax = GetDate;      _int86(0x21, &ir, &or);      g.i[0] = or.x.cx;      g.i[1] = or.x.dx;      ir.x.ax = GetTime;      _int86(0x21, &ir, &or);      g.i[0] += or.x.cx;      g.i[1] += or.x.dx;      return(g.d);}unsigned int    FlopyStatus(unsigned int drive){      union   _REGS   ir, or;      ir.h.ah = 1;      ir.x.dx = drive - 1;      _int86(0x13, &ir, &or);      return(or.h.al);}void    ResetDisk(unsigned int drive){      union   _REGS   ir, or;      ir.h.ah = 0;      ir.x.dx = drive - 1;      _int86(0x13, &ir, &or);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -