/* #includes */ /*{{{C}}}*//*{{{*/ #include "config.h" #include #include #include #include #include #include #include #include #include #include "getopt_.h" #include "cpmfs.h" #ifdef USE_DMALLOC #include #endif /*}}}*/ const char cmd[]="cpmcp"; static int text=0; static int preserve=0; /** * Return the user number. * @param s CP/M filename in 0[0]:aaaaaaaa.bbb format. * @returns The user number or -1 for no match. */ static int userNumber(const char *s) /*{{{*/ { if (isdigit(*s) && *(s+1)==':') return (*s-'0'); if (isdigit(*s) && isdigit(*(s+1)) && *(s+2)==':') return (10*(*s-'0')+(*(s+1)-'0')); return -1; } /*}}}*/ /** * Copy one file from CP/M to UNIX. * @param root The inode for the root directory. * @param src The CP/M filename in 00aaaaaaaabbb format. * @param dest The UNIX filename. * @returns 0 for success, 1 for error. */ static int cpmToUnix(const struct cpmInode *root, const char *src, const char *dest) /*{{{*/ { struct cpmInode ino; int exitcode=0; if (cpmNamei(root,src,&ino)==-1) { fprintf(stderr,"%s: can not open `%s': %s\n",cmd,src,boo); exitcode=1; } else { struct cpmFile file; FILE *ufp; cpmOpen(&ino,&file,O_RDONLY); if ((ufp=fopen(dest,text ? "w" : "wb"))==(FILE*)0) { fprintf(stderr,"%s: can not create %s: %s\n",cmd,dest,strerror(errno)); exitcode=1; } else { int crpending=0; int ohno=0; int res; char buf[4096]; while ((res=cpmRead(&file,buf,sizeof(buf)))>0) { int j; for (j=0; j=argc) usage(); image=argv[optind++]; if (userNumber(argv[optind])>=0) /* cpm -> unix? */ /*{{{*/ { int i; struct stat statbuf; for (i=optind; i<(argc-1); ++i) if (userNumber(argv[i])==-1) usage(); todir=((argc-optind)>2); if (stat(argv[argc-1],&statbuf)==-1) { if (todir) usage(); } else if (S_ISDIR(statbuf.st_mode)) todir=1; else if (todir) usage(); readcpm=1; } /*}}}*/ else if (userNumber(argv[argc-1])>=0) /* unix -> cpm */ /*{{{*/ { int i; todir=0; for (i=optind; i<(argc-1); ++i) if (userNumber(argv[i])>=0) usage(); if ((argc-optind)>2 && *(strchr(argv[argc-1],':')+1)!='\0') usage(); if (*(strchr(argv[argc-1],':')+1)=='\0') todir=1; readcpm=0; } /*}}}*/ else usage(); /*}}}*/ /* open image file */ /*{{{*/ if ((err=Device_open(&super.dev,image,readcpm ? O_RDONLY : O_RDWR, devopts))) { fprintf(stderr,"%s: cannot open %s (%s)\n",cmd,image,err); exit(1); } if (cpmReadSuper(&super,&root,format)==-1) { fprintf(stderr,"%s: cannot read superblock (%s)\n",cmd,boo); exit(1); } /*}}}*/ if (readcpm) /* copy from CP/M to UNIX */ /*{{{*/ { int i; char *last=argv[argc-1]; cpmglob(optind,argc-1,argv,&root,&gargc,&gargv); /* trying to copy multiple files to a file? */ if (gargc>1 && !todir) usage(); for (i=0; i