[comp.sys.ibm.pc.programmer] How to format an MS-DOS disk from C?

drh@romeo.cs.duke.edu (D. Richard Hipp) (07/30/90)

I need to format a floppy disk under program control (Turbo-C 1.5).  Right
now I am using the function listed below.  It works, but relies on the
presence of COMMAND.COM.  It also does some needless disk I/O, and will
not run on a single disk computer.  Does anyone know how to format a
floppy disk directly?  Respond by mail and I will post a summary after
two weeks.  Thanx.

/*
** Format a disk.  "arg" is the argument to the DOS format command.
** Return false if a temporary file can not be opened.
*/
boolean format(char *arg){
  FILE *fp;
  char cmd[200];

  assert( strlen(arg)<160 );
  if( (fp=fopen("temporar.y","w"))==0 ) return false;
  fprintf(fp,"\nn\n");
  fclose(fp);
  sprintf(cmd,"format %s <temporar.y >temporar.y2",arg);
  system(cmd);
  unlink("temporar.y");
  unlink("temporar.y2");
  return true;
}