home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
SRC
/
msdos_diskaccess.lzh
/
MS_DISK_ACCESS
/
mkdfs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-04
|
4KB
|
179 lines
#include <stdio.h>
#include <errno.h>
#include <modes.h>
#include "msdos.h"
#include "bootblk.h"
#define VOLLBL 0x8
#define FAT 0xf9
static char *floppy = "/tos0@" ;
static char disklabel[12] ;
void move(), Write(), usage(), formatit() ;
static char *progname, buf[MSECSIZ] ;
main(argc,argv)
char **argv;
{
int c, fd, sec, fd1 = -1, len;
int fat_len = 3 ;
int fat = FAT ;
int dir_len = 7 ;
int hdflag=0, fflag = 0, preext = 0;
extern char *optarg;
progname = argv[0] ;
/* Initialize with spaces - modif bd, 06/90 */
sprintf(disklabel,"%-11.11s","form OS9_ST") ;
while( (c=getopt(argc,argv,"d:hfpxl:?")) != EOF ) {
switch(c) {
case 'f' :
fflag++ ;
break;
case 'h' :
hdflag++ ;
break;
case 'p' :
preext++;
break;
case 'x' :
preext = 2;
break;
case 'l' :
sprintf(disklabel,"%-11.11s",optarg) ;
break;
case 'd' :
floppy = optarg;
if (floppy[strlen(floppy-1)] != '@') {
fflag = 0;
} else {
fprintf(stderr,"%s: don't init other devives than default. to risky!\n",
progname);
exit(1);
}
fd1 = 0;
break;
default:
fprintf(stderr,"Unknown option \"%c\"\n",c) ;
case '?':
usage() ;
}
}
if (fflag)
formatit(hdflag) ;
/* Lets initialize the MSDOS FS */
if (fd1 == 0)
unlink(floppy);
len = (preext) ? 512*9*80*2 : 512 * 9 * 4;
if (((fd1 != 0) && (fd=open(floppy, S_IREAD+S_IWRITE)) == -1) ||
((fd1 == 0) && (fd=create(floppy, S_IWRITE+S_ISIZE,
S_IREAD+S_IWRITE, len)) == -1)) {
fprintf(stderr,"%s: open: ",progname) ;
perror(floppy) ;
exit(1) ;
}
if (fd1 == 0 && preext == 2) {
lseek(fd, len-1, 0);
write(fd, &floppy, 1);
lseek(fd, 0, 0);
}
if (fd1 != 0 && (fd1=open("/d0@",S_IREAD+S_IWRITE)) == -1 ) {
fprintf(stderr,"%s: open default (/d0@): ",progname) ;
perror(floppy) ;
exit(1) ;
}
if ( hdflag ) {
dir_len=14 ;
fat_len=9 ;
}
bzero(buf,sizeof(buf)) ;
bcopy(hdflag?hdboot:ldboot,buf,MSECSIZ) ; /* Create the boot block */
if (fd1 > 0) {
lseek(fd1, 256, 0);
Write(fd1, buf, 256);
lseek(fd, MSECSIZ, 0);
} else {
Write(fd, buf, MSECSIZ) ; /* Dump the boot block */
}
bzero(buf,sizeof(buf)) ;
for( c=0; c < 2 ; ++c ) {
buf[0] = fat ;
buf[1] = 0xff ;
buf[2] = 0xff ;
Write(fd,buf,MSECSIZ) ; /* First block of FAT */
bzero(buf,3) ;
for( sec=fat_len; --sec ; )
Write(fd,buf,MSECSIZ) ; /* Rest of FAT */
}
strcpy(((struct directory *)buf)->name,disklabel) ;
((struct directory *)buf)->attr= VOLLBL ;
Write(fd,buf,MSECSIZ) ; /* Root dir */
bzero(buf,strlen(disklabel)) ;
((struct directory *)buf)->attr= 0 ;
for( ; --dir_len ; )
Write(fd,buf,MSECSIZ) ; /* Root dir */
lseek(fd, 0, 0);
}
void
move(fd,sector)
int fd, sector ;
{
if ( lseek(fd,sector*MSECSIZ,0) != sector*MSECSIZ) {
fprintf(stderr,"%s: lseek: ",progname) ;
perror(floppy) ;
exit(1) ;
}
}
void
Write(fd,buf,count)
int fd,count ;
char * buf ;
{
if ( write(fd,buf,count) != count ) {
fprintf(stderr,"%s: write: ",progname) ;
perror(floppy) ;
exit(1) ;
}
}
void
usage()
{
fprintf(stderr,"Syntax: %s <opts>\n",progname) ;
fprintf(stderr,"Function: Builds an empty DOS filesystem\n") ;
fprintf(stderr,"\t-d<file> init filesystem on file <file>\n\
\t-f\treformat first\n\
\t(-h\thigh density) currently not supported\n\
\t-l<disklabel>\n\
\t-p\tpreextend file to full disksize (737280 Bytes)\n\
\t-x\tpreextend file to full disksize (737280 Bytes) and show it\n\
\t\tp allocates space, but fileentry shows only used part\n\
\t-?\tthis message\n") ;
exit(1) ;
}
void
formatit(hdq)
int hdq;
{
int retval;
system("ex diskcache -d /d0");
retval = system("ex form -5 -s9 -i1 /d0");
if (retval != 0) {
fprintf(stderr,"%s: ",progname);
fprintf(stderr,"The format operation may have failed\nTry again\n") ;
exit(retval);
}
}