コード例 #1
0
void mcat(int argc, char **argv, int type)
{
    struct device *dev;
    struct device out_dev;
    char *drive, name[EXPAND_BUF];
    char errmsg[200];
    Stream_t *Stream;
    char buf[BUF_SIZE];

    mt_off_t address = 0;

    char mode = O_RDONLY;
    int optindex = 1;
    size_t len;

    noPrivileges = 1;

    if (argc < 2) {
        usage();
    }

    if (argv[1][0] == '-') {
        if (argv[1][1] != 'w') {
            usage();
        }
        mode = O_WRONLY;
        optindex++;
    }

    if (argc - optindex < 1)
        usage();


    if (skip_drive(argv[optindex]) == argv[optindex])
        usage();

    drive = get_drive(argv[optindex], NULL);

    /* check out a drive whose letter and parameters match */
    sprintf(errmsg, "Drive '%s:' not supported", drive);
    Stream = NULL;
    for (dev=devices; dev->name; dev++) {
        FREE(&Stream);
        if (strcmp(dev->drive, drive) != 0)
            continue;
        out_dev = *dev;
        expand(dev->name,name);
#ifdef USING_NEW_VOLD
        strcpy(name, getVoldName(dev, name));
#endif

        Stream = 0;
#ifdef USE_XDF
        Stream = XdfOpen(&out_dev, name, mode, errmsg, 0);
        if(Stream)
            out_dev.use_2m = 0x7f;

#endif

#ifdef USE_FLOPPYD
        if(!Stream)
            Stream = FloppydOpen(&out_dev, dev, name,
                                 mode, errmsg, 0, 1);
#endif


        if (!Stream)
            Stream = SimpleFileOpen(&out_dev, dev, name, mode,
                                    errmsg, 0, 1, 0);

        if( !Stream)
            continue;
        break;
    }

    /* print error msg if needed */
    if ( dev->drive == 0 ) {
        FREE(&Stream);
        fprintf(stderr,"%s\n",errmsg);
        exit(1);
    }

    if (mode == O_WRONLY) {
        while ((len = fread(buf, 1, BUF_SIZE, stdin))
                == BUF_SIZE) {
            WRITES(Stream, buf, address, BUF_SIZE);
            address += BUF_SIZE;
        }
        if (len)
            WRITES(Stream, buf, address, len);
    } else {
        while ((len = READS(Stream, buf, address, BUF_SIZE))
                == BUF_SIZE) {
            fwrite(buf, 1, BUF_SIZE, stdout);
            address += BUF_SIZE;
        }
        if (len)
            fwrite(buf, 1, len, stdout);
    }

    FREE(&Stream);
    exit(0);
}
コード例 #2
0
ファイル: mcat.c プロジェクト: edwacode/r6300v2
void mcat(int argc, char **argv, int type)
{
	struct device *dev;
	struct device out_dev;
	char drive, name[EXPAND_BUF];
        char errmsg[200];
        Stream_t *Stream;
	char buf[BUF_SIZE];

	mt_off_t address = 0;

	char mode = O_RDONLY;
	int optindex = 1;
	size_t len;

	noPrivileges = 1;

	if (argc < 2) {
		usage();
	}

	if (argv[1][0] == '-') {
		if (argv[1][1] != 'w') {
			usage();
		}
		mode = O_WRONLY;
		optindex++;
	}

	if (argc - optindex < 1)
		usage();


	if (!argv[optindex][0] || argv[optindex][1] != ':' 
	    || argv[optindex][2]) {
		usage();
	}

        drive = toupper(argv[optindex][0]);

        /* check out a drive whose letter and parameters match */       
        sprintf(errmsg, "Drive '%c:' not supported", drive);    
        Stream = NULL;
        for (dev=devices; dev->name; dev++) {
                FREE(&Stream);
                if (dev->drive != drive)
                        continue;
                out_dev = *dev;
                expand(dev->name,name);
#ifdef USING_NEW_VOLD
                strcpy(name, getVoldName(dev, name));
#endif

                Stream = 0;
#ifdef USE_XDF
                Stream = XdfOpen(&out_dev, name, mode, errmsg, 0);
				if(Stream)
                        out_dev.use_2m = 0x7f;

#endif

#ifdef USE_FLOPPYD
                if(!Stream)
                        Stream = FloppydOpen(&out_dev, dev, name, 
					     mode, errmsg, 0, 1);
#endif


                if (!Stream)
                        Stream = SimpleFileOpen(&out_dev, dev, name, mode,
						errmsg, 0, 1, 0);

                if( !Stream)
                        continue;
                break;
        }

        /* print error msg if needed */ 
        if ( dev->drive == 0 ){
                FREE(&Stream);
                fprintf(stderr,"%s\n",errmsg);
                exit(1);
        }


	if (mode == O_WRONLY) {
		mt_size_t size=0;
		size = out_dev.sectors * out_dev.heads * out_dev.tracks;
		size *= 512;
		while ((len = fread(buf, 1,
				    bufLen(BUF_SIZE, size, address),
				    stdin)) > 0) {			
			int r = WRITES(Stream, buf, address, len);
			fprintf(stderr, "Wrote to %d\n", (int) address);
			if(r < 0)
				break;
			address += len;
		}
	} else {
		while ((len = READS(Stream, buf, address, BUF_SIZE)) > 0) {
			fwrite(buf, 1, len, stdout);
			address += len;
		}
	}

	FREE(&Stream);
	exit(0);
}