source: trunk/minix/commands/i386/mtools-3.9.7/mcat.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.7 KB
RevLine 
[9]1/*
2 * mcat.c
3 * Same thing as cat /dev/fd0 or cat file >/dev/fd0
4 * Something, that isn't possible with floppyd anymore.
5 */
6
7#include "sysincludes.h"
8#include "msdos.h"
9#include "mtools.h"
10#include "mainloop.h"
11#include "fsP.h"
12#include "xdf_io.h"
13#include "floppyd_io.h"
14#include "plain_io.h"
15
16void usage(void)
17{
18 fprintf(stderr, "Mtools version %s, dated %s\n",
19 mversion, mdate);
20 fprintf(stderr, "Usage: mcat [-w] device\n");
21 fprintf(stderr, " -w write on device else read\n");
22 exit(1);
23}
24
25#define BUF_SIZE 16000
26
27void mcat(int argc, char **argv, int type)
28{
29 struct device *dev;
30 struct device out_dev;
31 char *drive, name[EXPAND_BUF];
32 char errmsg[200];
33 Stream_t *Stream;
34 char buf[BUF_SIZE];
35
36 mt_off_t address = 0;
37
38 char mode = O_RDONLY;
39 int optindex = 1;
40 size_t len;
41
42 noPrivileges = 1;
43
44 if (argc < 2) {
45 usage();
46 }
47
48 if (argv[1][0] == '-') {
49 if (argv[1][1] != 'w') {
50 usage();
51 }
52 mode = O_WRONLY;
53 optindex++;
54 }
55
56 if (argc - optindex < 1)
57 usage();
58
59
60 if (skip_drive(argv[optindex]) == argv[optindex])
61 usage();
62
63 drive = get_drive(argv[optindex], NULL);
64
65 /* check out a drive whose letter and parameters match */
66 sprintf(errmsg, "Drive '%s:' not supported", drive);
67 Stream = NULL;
68 for (dev=devices; dev->name; dev++) {
69 FREE(&Stream);
70 if (strcmp(dev->drive, drive) != 0)
71 continue;
72 out_dev = *dev;
73 expand(dev->name,name);
74#ifdef USING_NEW_VOLD
75 strcpy(name, getVoldName(dev, name));
76#endif
77
78 Stream = 0;
79#ifdef USE_XDF
80 Stream = XdfOpen(&out_dev, name, mode, errmsg, 0);
81 if(Stream)
82 out_dev.use_2m = 0x7f;
83
84#endif
85
86#ifdef USE_FLOPPYD
87 if(!Stream)
88 Stream = FloppydOpen(&out_dev, dev, name,
89 mode, errmsg, 0, 1);
90#endif
91
92
93 if (!Stream)
94 Stream = SimpleFileOpen(&out_dev, dev, name, mode,
95 errmsg, 0, 1, 0);
96
97 if( !Stream)
98 continue;
99 break;
100 }
101
102 /* print error msg if needed */
103 if ( dev->drive == 0 ){
104 FREE(&Stream);
105 fprintf(stderr,"%s\n",errmsg);
106 exit(1);
107 }
108
109 if (mode == O_WRONLY) {
110 while ((len = fread(buf, 1, BUF_SIZE, stdin))
111 == BUF_SIZE) {
112 WRITES(Stream, buf, address, BUF_SIZE);
113 address += BUF_SIZE;
114 }
115 if (len)
116 WRITES(Stream, buf, address, len);
117 } else {
118 while ((len = READS(Stream, buf, address, BUF_SIZE))
119 == BUF_SIZE) {
120 fwrite(buf, 1, BUF_SIZE, stdout);
121 address += BUF_SIZE;
122 }
123 if (len)
124 fwrite(buf, 1, len, stdout);
125 }
126
127 FREE(&Stream);
128 exit(0);
129}
Note: See TracBrowser for help on using the repository browser.