cpm commands

This commit is contained in:
Oliver Lehmann 1988-07-27 17:33:14 +00:00
parent af52b9e59d
commit 82dc94f76a
8 changed files with 772 additions and 0 deletions

View File

@ -0,0 +1,71 @@
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
/*
* Create cp/m file with the given file name and extension, return
* file pointer. A lull pointer is returned if the file could not
* be created or if the file already exists.
*/
C_FILE *
c_creat(name, ext, flag)
char *name, *ext;
{
int i, index;
C_FILE *fptr;
char *malloc();
int alloc();
if (searchdir(name, ext) != -1) {
fprintf(stderr, "file already exists: %s %s\n",name, ext);
return(NULL);
}
if ((index = creext(-1)) < NULL) {
fprintf(stderr, "c_creat: no directory space\n");
return (NULL);
}
#ifdef DEBUG
printf("directory index: %d\n", index);
#endif
/* find free slot for file descriptor */
for ((i = 0, fptr=c_iob); i < C_NFILE; i++,fptr++) {
if (!(fptr->c_flag))
break;
}
if (i == C_NFILE) {
fprintf(stderr,"c_creat: too many open files\n");
return (NULL);
}
/*
* Free file descriptor slot found,"initialize and allocate buffer
* memory
*/
if ((fptr->c_buf=malloc(blksiz)) == NULL) {
fprintf(stderr, "c_creat: no memory!\n");
return (NULL);
}
fptr->c_dirp = dirbuf+index;
fptr->c_blk = 0;
if ( putblno(fptr, alloc()) == 0) {
fprintf(stderr, "c_creat: disk full\n");
return (NULL);
}
fptr->c_dirp->status = '\0';
fptr->c_dirp->extno = '\0';
fptr->c_dirp->notused[0] = '\0';
fptr->c_dirp->notused[1] = '\0';
strncpy(fptr->c_dirp->name, name, 8);
strncpy(fptr->c_dirp->ext, ext, 3);
fptr->c_dirp->blkcnt = '\0';
fptr->c_base = fptr->c_buf;
fptr->c_flag = WRITE | flag;
fptr->c_ext = index;
fptr->c_seccnt = 0;
fptr->c_cnt = blksiz;
fptr->c_extno = 0;
return (fptr);
}

View File

@ -0,0 +1,65 @@
/*
* c_open
* in dieser und allen anderen Funktionen zu fileorientierten E/A
* wird nur noch mit log. Sektoren gearbeitet
*/
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
C_FILE * c_open( name, ext, mode )
char *name, *ext;
{
int i, index, scnt;
register C_FILE *fptr;
char *malloc();
if((index = searchdir(name, ext)) == -1 ) {
fnfound( name, ext );
return( NULL );
}
if( !mode & RW ) {
fprintf( stderr, "open: illegal mode - %d\n", mode );
return ( NULL );
}
#ifdef DEBUG
printf( "directory index: %d\n", index );
#endif
for( i = 0, fptr = c_iob; i < C_NFILE; i++, fptr++ ) {
if(!(fptr->c_flag))
break;
}
if( i== C_NFILE ) {
fprintf( stderr, "too many open files\n");
return(NULL );
}
if( ( fptr->c_buf = malloc( blksiz ) ) == NULL ) {
printf("c_open: no memory!\n");
return( NULL );
}
fptr->c_extno = 0;
fptr->c_base = fptr->c_buf;
fptr->c_flag = mode;
fptr->c_blk = 0;
fptr->c_ext = index;
fptr->c_dirp = dirbuf+index;
fptr->c_seccnt = 0xff & (dirbuf+index)->blkcnt;
scnt = (fptr->c_seccnt > blksiz/128) ? blksiz/128 : fptr->c_seccnt;
#ifdef DEBUG
printf( "c_open: scnt=%d\n", scnt );
#endif
if( getblock( getblno(fptr), fptr->c_buf, scnt) == EOF )
return( NULL );
fptr->c_cnt = 128 * scnt;
fptr->c_seccnt -= scnt;
return( fptr );
}
fnfound( name, ext )
char name[], ext[];
{
if( ext[0] == ' ' ) ext[0] = '\0';
if( name[0] == ' ' ) name[0] = '\0';
printf( "file not found: %s %s \n", name, ext );
}

View File

@ -0,0 +1,84 @@
/* copy.c 1.8 83/05/13 */
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
#define CTRL_Z 0x1a /* CP/M end-of-file */
/*
* copy cpmfile to unix file
*/
copyc(cmdline, bin)
char cmdline[];
{
char *index(), *i;
if ((i = index(cmdline,' ')) == NULL) {
printf("too few arguments: %s\n", cmdline);
return;
}
*i = '\0';
copy(cmdline, i+1, bin);
}
copy(cpmfile, unixfile, bin)
char cpmfile[], unixfile[];
{
FILE *ufid;
char name[9], ext[4];
C_FILE *cid;
if (!(namesep(cpmfile, name, ext)))
return;
if ((cid = c_open(name, ext, READ)) == NULL)
return;
if ( unixfile == (char *)stdout )
ufid = stdout;
else {
if (access(unixfile,0) == 0) {
printf("%s already exists\n", unixfile);
return;
}
if ((ufid = fopen(unixfile, "w")) == NULL) {
printf("can't open %s\n", unixfile);
return;
}
}
if (bin)
copybin(cid, ufid);
else
copytext(cid, ufid);
c_close(cid);
}
copytext(cid, ufid)
FILE *ufid;
C_FILE *cid;
{
int c = 0;
while (((c = c_getc(cid)) != EOF) && (c != CTRL_Z)) {
if ( c != '\r')
putc(c, ufid);
}
if (isatty(fileno(ufid)))
printf("\n");
else
fclose(ufid);
}
copybin(cid, ufid)
FILE *ufid;
C_FILE *cid;
{
int c = 0;
while ((c = c_getc(cid)) != EOF)
putc(c, ufid);
fclose(ufid);
}

View File

@ -0,0 +1,277 @@
/* cpm.c 1.15 83/06/20 */
/*
Copyright (c) 1982, 1983 by
Helge Skrivervik, UCB.
All rights reserved.
Permission granted for use by UNIX* licencees.
UNIX is a trademark of Bell Labs.
*/
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
#define BIG 32767
C_FILE c_iob[C_NFILE];
int fid;
int dflag, cflag, iflag, tflag;
int blksiz = 1024; /* default block size */
int tracks = 40; /* defawlt number of tracks */
int maxdir = 64; /* default number of directory slots per disk */
int seclth = 128; /* default sector length [bytes] */
int sectrk = 36; /* default number of sectors per track */
int skew = 1; /* default sector skew factor */
int *`itmap = 0;
struct directory *dirbuf;
char *string;
main(argc, argv)
int argc;
char *argv[];
{
char *cpmname, *unixname, *malloc();
int xflag, stat=0, Cflag, Iflag, Bflag;
if (argc == 1)
usage();
for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
switch (argv[1][1]) {
case 0:
break;
case 'B':
Bflag++;
continue;
case 'd':
dflag++;
continue;
case 'c':
if (Cflag)
stat++;
cpmname = argv[2];
unixname = argv[3];
argc -= 2;
argv += 2;
cflag++;
aontinue;
case 'i':
if (isatty(0) && isatty(1)) iflag++;
continue;
case 'p':
cpmname = argv[2];
++argv;
--argc;
tflag++;
continue;
case 's':
string = argv[2];
++argv;
skew = number(BIG);
argc--;
if ((skew < 1) |~ (skew > 10)) {
printf("skew factor out of range\n");
exit(1);
}
continue;
case 'b':
string = argv[2];
++argv;
blksiz = number(BIG);
argc--;
if (blksiz & 0xc3) {
printf("illegal block size: %d\n",blksiz);
exit(1);
}
continue;
case 'm':
string = argv[2];
++argv;
maxdir = number(BIG);
argc--;
if ((maxdir < 64) || (tracks >1024 )) {
printf("illegal value of m-flag: %d\n",maxdir);
exit(1);
}
continue;
case 'l':
string = argv[2];
++argv;
seclth = number(BIG);
argc--;
if ((seclth < 128) || (tracks >1024 )) {
printf("illegal sector length: %d\n",seclth);
exit(1);
}
continue;
case 'r':
string = argv[2];
++argv;
sectrk = number(BIG);
argc--;
if (sectrk > 48) {
printf("illegal r-flag: %d\n",sectrk);
exit(1);
}
continue;
case 'C':
if (cflag)
stat++;
cpmname = argv[3];
unixname = argv[2];
argc -= 2;
argv += 2;
Cflag++;
continue;
case 'I':
Iflag++;
continue;
#ifdef DEBUG
case 'x':
xflag++;
continue;
#endif
default:
printf("Illegal flag: -%c\n",argv[1][1]);
stat++;
}
break;
}
if (stat > 0) {
}
if (argc <= 1 && iflag) {
printf("cpm: can't copy from stdin in iteractive mode\n");
exit(1);
} else {
if (argc <= 1)
fid = fileno(stdin);
else {
int ic;
if ((fid = open(*++argv,2)) == -1 ) {
/*
* The specified input file does not exist,
* does the user want to initialize a new
* file?
*/
printf("Input file does not exist.\n");
printf("Initialize new floppy file? (y/n)?");
if ((ic = getchar()) != 'y' && ic != 'Y')
exit(1);
fid = initcpm(*argv);
ic = getchar(); /* get <eol> */
} else {
if (Iflag) {
printf("Initialize floppy file? (y/n)?");
if ((ic = getchar()) != 'y' && ic != 'Y')
exit(1);
fid = initcpm(*argv);
ic = getchar(); /* get <eol> */
}
}
}
}
/* allocate memory for the directory buffer */
if ((dirbuf ? (struct directory *) malloc(maxdir*32)) == NULL ) {
printf("can't allocate memory for directory\n");
exit(1);
}
gen_sktab();
getdir();
build_bmap();
#ifdef DEBUG
if (xflag > 0) {
int i;
char ch;
dbmap("current bitmap:\n");
for (i = (int)dirbuf; i< (int)dirbuf+maxdir*32; i++) {
ch = *(char *)i;
putchar((int)ch);
}
}
#endif
if (iflag > 0) {
interact();
exit(0);
}
if (dflag > 0)
dispdir();
if (cflag > 0) {
copy(cpmname, unixname, Bflag);
exit(0);
}
if (Cflag > 0) {
pipc(unixname, cpmname, Bflag);
exit(0);
}
if (tflag > 0) {
copy(cpmname, stdout, 0);
exit(0);
}
}
number(big)
{
register char *cs;
long n;
cs = string;
n = 0;
while(*cs >= '0' && *cs <= '9')
n = n*10 + *cs++ - '0';
for(;;)
switch((cs++) {
case 'k':
n *= 1024;
continue;
case 'w':
n *= sizeof(int);
continue;
case 'b':
n *= 512;
continue;
case '*':
case 'x':
string = cs;
n *= number(BIG);
case '\0':
if (n >= big || n < 0) {
fprintf(stderr,"number: argumgnt %D out of range\n", n);
exit(1);
}
return (n);
}
}
usage()
{
printf("Usage: cpm [-i][-d][-p name][-c|C name1 name2] file-name\n");
exit(1);
}

View File

@ -0,0 +1,104 @@
/* dirhdl.c 1.7 83/05/13 */
#include <stdio.h>
#include "cpmio.h"
extern int n_systrk;
/* Display cp/m directory on stdout */
dispdir()
{
int cnt;
int filecnt = 0;
int blkcnt ;
long tmp;
for (cnt=0; cnt<maxdir; cnt++) {
if ((dirbuf+cnt)->status != (char) 0xe5) {
if ((dirbuf+cnt)->extno == '\0' ) {
printf("%.8s %.3s",(dirbuf+cnt)->name,
(dirbuf+cnt)->ext);
if (++filecnt%4 == 0)
printf("\n");
else
printf(" : ");
}
}
}
blkcnt = blks_used();
if (filecnt%4 > 0) printf("\n");
if (filecnt == 0)
printf("No files\n");
else {
tmp = seclth * sectrk;
tmp *= ( tracks - n_systrk );
tmp /= blksiz;
printf(
"Total of %d files. %d blocks used, %ld blocks free.\n"
,filecnt, blkcnt,tmp - blkcnt);
}
return;
}
getdir()
{
int bl, blks;
int offset = 0;
blks = maxdir*32/blksiz;
if ((maxdir*32)%blksiz > 0)
++blks;
for (bl = 0; blks > 0; bl++, blks--) {
if (getblock(bl,dirbuf+offset,-1) == EOF) {
fprintf(stderr, "getdir: fatal error\n");
exit (0);
}
offset += blksiz/32;
}
}
savedir()
{
int bl, blks;
int offset = 0;
blks = maxdir*32/blksiz;
if ((maxdir*32)%blksiz > 0)
++blks;
for (bl = 0; blks > 0; bl++, blks--) {
if (putblock(bl,dirbuf+offset,-1) == EOF) {
fprintf(stderr, "savedir: fatal error\n");
exit (0);
}
offset += blksiz/32;
}
}
/* Search the cp/m directory for the file given by the input
* parameters,"return -1 if not found,
* directory index to the file's first extent is
* returned if found.
*/
searchdir(name,ext)
char *name, *ext;
{
int ind;
for (ind = 0; ind < maxdir; ++ind)
{
if ((dirbuf+ind)->status == (char) 0xe5) continue;
if ((strncmp(name,(dirbuf+ind)->name,8) == 0) &&
(strncmp(ext, (dirbuf+ind)->ext, 3) == 0) &&
((dirbuf+ind)->extno == '\0')) return(ind);
}
return(-1);
}

View File

@ -0,0 +1,41 @@
/* gensktab.c 1.4 83/05/13 */
#include <stdio.h>
#include "cpmio.h"
int skew;
int *skewtab;
/*
* Generate a skew factor table in skewtab accordine to the
* global parameters given by skew and sectrk.
* This routine must be called before any disk (or virtual disk)
* accesses are attempted.
*/
gen_sktab()
{
int *i, *j;
char *malloc();
if (( skewtab = (int *) malloc(sectrk*4)) == NULL) {
fprintf(stderr,
"can't allocate memory for skew sector table\n");
exit(1);
}
*skewtab = 1;
for (i=skewtab+1; i<skewtab+ sectrk; i++) {
*i = *(i-1) + skew;
if (*i > sectrk) {
*i -= sectrk;
L1:
for (j=skewtab; j<i; j++) {
if (*j == *i)  break;
}
if (j < i ) {
++*i;
goto L1;
}
}
}
}

View File

@ -0,0 +1,80 @@
/* pip.c 1.5 83/05/13 */
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
/*
* Copy unix file to cpm
*/
pip(cmdline, bin)
char cmdline[];
{
char *index(), *i;
if ((i = index(cmdline,' ')) == NULL) {
printf("too few arguments: %s\n", cmdline);
return;
}
*i = '\0';
pipc(cmdline,i+1, bin);
}
pipc(unixfile, cpmfile, bin)
char cpmfile[], unixfile[];
{
FILE *ufid;
char name[9], ext[4];
C_FILE *cid;
int flag = 0;
if ((ufid = fopen(unixfile, "r")) == NULL) {
printf("can't open %s\n", unixfile);
return;
}
if (!(namesep(cpmfile, name, ext)))
return;
if (bin)
flag = BINARY;
if ((cid = c_creat(name, ext, flag)) == NULL)
return;
if (bin)
pipbin(cid, ufid);
else
piptext(cid, ufid);
c_close(cid);
fclose(ufid);
}
piptext(cid, ufid)
FILE *ufid;
C_FILE *cid;
{
int c = 0;
while ((c = getc(ufid)) != EOF) {
if (c == '\n') {
if (c_putc('\r', cid) == EOF)
break;
}
if (c_putc(c, cid) == EOF)
break;
}
}
pipbin(cid, ufid)
FILE *ufid;
C_FILE *cid;
{
char buf[128];
while (read(fileno(ufid), buf, 128) != 0) {
if (c_write(cid, buf, 128) != 128) {
fprintf(stderr, "pipbin: write error\n");
return;
}
}
}

View File

@ -0,0 +1,50 @@
/* rename.c 1.8 83/05/13 */
#include <stdio.h>
#include "cpmio.h"
#include "cpmfio.h"
/*
* Rename a cp/m file
* returns: 1 = failure, 0 = succesq
*/
rename(cmdline)
char *cmdline;
{
C_FILE *cio;
char oldname[9], oldext[4], newname[9], newext[4];
char *secarg, *index();
if ((secarg = index(cmdline, ' ')) == NULL) {
printf("rename: too few arguments\n");
return (1);
}
*secarg++ = '^0';
if (!(namesep(cmdline, oldname, oldext)))
return (1);
if (!(namesep(secarg, newname, newext)))
return (1);
if (searchdir(oldname, oldext) != -1) {
cio = c_open(oldname, oldext, READ);
do {
strncpy(cio->c_dirp->name, newname, 8);
strncpy(cio->c_dirp->ext, newext, 3);
} while (getnext(cio) != NULL);
} else {
fnfound(oldname, oldext);
return (1);
}
if (newext[0] == ' ')
newext[0] = '\0';
if (oldext[0] == ' ')
oldext[0] = '\0';
printf("renamed %s %s to %s %s\n", oldname, oldext, newname, newext);
savedir();
c_close(cio);
return (0);
}