home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / mtools2 / part02 < prev    next >
Encoding:
Text File  |  1992-01-10  |  61.6 KB  |  2,431 lines

  1. Newsgroups: comp.sources.unix
  2. From: fthood!egray@uxc.cso.uiuc.edu (Emmet P. Gray)
  3. Subject: v25i098: mtools2 - tools to manipulate MSDOS filesystems, V2.0, Part02/03
  4. Sender: sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: fthood!egray@uxc.cso.uiuc.edu (Emmet P. Gray)
  8. Posting-Number: Volume 25, Issue 98
  9. Archive-Name: mtools2/part02
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 2 (of 3)."
  18. # Contents:  mtools/Configure mtools/buf_write.c mtools/dir_read.c
  19. #   mtools/dir_write.c mtools/mattrib.c mtools/mcd.c mtools/mcopy.c
  20. #   mtools/mdel.c mtools/mformat.c mtools/mlabel.c mtools/mmd.c
  21. #   mtools/mrd.c mtools/mren.c mtools/mtype.c
  22. # Wrapped by lai@risotto on Sat Jan 11 12:03:34 1992
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f 'mtools/Configure' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'mtools/Configure'\"
  26. else
  27. echo shar: Extracting \"'mtools/Configure'\" \(4629 characters\)
  28. sed "s/^X//" >'mtools/Configure' <<'END_OF_FILE'
  29. XHow to configure Mtools to your system...
  30. X
  31. X1) Things you'll need to know.
  32. X
  33. X    The full path to the devices you want to use.
  34. X
  35. X    The number of tracks, heads, and sectors supported by each device.
  36. X
  37. X        5.25" 160k = 40 tracks, 1 head, and 8 sectors
  38. X        5.25" 180k = 40 tracks, 1 head, and 9 sectors
  39. X        5.25" 320k = 40 tracks, 2 heads, and 8 sectors
  40. X        5.25" 360k = 40 tracks, 2 heads, and 9 sectors
  41. X        5.25" 1.2m = 80 tracks, 2 heads, and 15 sectors
  42. X        3.5"  720k = 80 tracks, 2 heads, and 9 sectors
  43. X        3.5" 1.44m = 80 tracks, 2 heads, and 18 sectors
  44. X
  45. X    If you choose to use a hard disk partition as one of your devices,
  46. X    then you may have to change the read/write permissions on that
  47. X    partition.
  48. X
  49. X2) The Makefile and msdos.h files
  50. X
  51. X    The following pre-processor variables are used in Mtools.  They
  52. X    should be set with "-Dvariable" in CFLAGS in the Makefile.
  53. X
  54. X        BSD    for systems without System V style strings functions
  55. X            and System V style utime().
  56. X
  57. X        ISC    default devices for ISC's Unix V.
  58. X
  59. X        UNIXPC    default devices for AT&T Unix PC 7300/3B1
  60. X
  61. X        SPARC    default devices for SunOS 4.1 on a SPARCstation
  62. X
  63. X    There is a variable in msdos.h called SIG_TYPE which should be
  64. X    configured to the "base" return type of signal();
  65. X
  66. X3) The devices "database" in devices.c
  67. X
  68. X    WARNING:  The devices.c file, as distributed, has *NO* devices
  69. X    defined.  You *MUST* select from one of the examples or edit the
  70. X    devices database yourself.
  71. X
  72. X    struct device {
  73. X        char drive;            /* the drive letter */
  74. X        char *name;            /* full path to device */
  75. X        long offset;            /* skip this many bytes */
  76. X        int fat_bits;            /* FAT encoding scheme */
  77. X        int mode;            /* any special open() flags */
  78. X        int (*gioctl) ();        /* gioctl() if needed */
  79. X        int tracks;            /* tracks */
  80. X        int heads;            /* heads */
  81. X        int sectors;            /* sectors */
  82. X    };
  83. X
  84. X    drive    the drive letter (in uppercase) that you want to associate
  85. X        to this device.
  86. X
  87. X    name    full path to the device.  Shell expansion is done on the
  88. X        name so things like $HOME/vpix/C: will work.
  89. X
  90. X    offset    skip over a partition table.  Useful for msdos disk "images"
  91. X        such as those VP/ix uses.
  92. X
  93. X    fat_bits The encoding scheme for the File Allocation Table.  Currently
  94. X        supports only 12 and 16 bit FATs.
  95. X
  96. X    mode    any special open() mode flags (not normally used, except
  97. X        in the AT&T Unixpc 7300/3b1).
  98. X
  99. X    gioctl    any special ioctl() calls required after opening the device
  100. X        (not normally used, except in the AT&T Unixpc 7300/3b1).
  101. X
  102. X    tracks    the number of tracks supported by this device or zero if
  103. X        this type of sanity checking is not required.  Do NOT use
  104. X        this field for hard disks or disk "images".  Also used by
  105. X        gioctl(), if present, as a default starting place.
  106. X
  107. X    heads    the number of heads supported by this device or zero if
  108. X        this type of sanity checking is not required.
  109. X
  110. X    sectors    the number of sectors supported by this device or zero if
  111. X        this type of sanity checking is not required.
  112. X
  113. X
  114. X    If a device supports more than one disk capacity (1.2m and
  115. X    360k), then you should place the most frequently used capacity
  116. X    first.  You may get an error message from the device driver when
  117. X    the disk capacity doesn't match the device, but these can be
  118. X    safely ignored.
  119. X
  120. X4)  Examples devices
  121. X
  122. X    for Interactive 386ix 2.0.2 with:
  123. X        1.2m 5.25" as a: (supports 1.2m, 360k, and 320k)
  124. X        1.44m 3.5" as b: (supports 1.44m, 720k)
  125. X        hard disk partition as c:
  126. X        vpix disk image as d:
  127. X        user's vpix disk image as e:
  128. X
  129. X        {'A', "/dev/rdsk/f0q15dt", 0L, 12, 0, (int (*)()) 0, 80, 2, 15},
  130. X        {'A', "/dev/rdsk/f0d9dt", 0L, 12, 0, (int (*)()) 0, 40, 2, 9},
  131. X        {'A', "/dev/rdsk/f0d8dt", 0L, 12, 0, (int (*)()) 0, 40, 2, 8},
  132. X        {'B', "/dev/rdsk/f13ht", 0L, 12, 0, (int (*)()) 0, 80, 2, 18},
  133. X        {'B', "/dev/rdsk/f13dt", 0L, 12, 0, (int (*)()) 0, 80, 2, 9},
  134. X        {'C', "/dev/rdsk/0p1", 0L, 16, 0, (int (*)()) 0, 0, 0, 0},
  135. X        {'D', "/usr/vpix/defaults/C:", 8704L, 12, 0, (int (*)()) 0, 0, 0, 0},
  136. X        {'E', "$HOME/vpix/C:", 8704L, 12, 0, (int (*)()) 0, 0, 0, 0},
  137. X        {'\0', (char *) NULL, 0L, 0, 0, (int (*)()) 0, 0, 0, 0}
  138. X
  139. X
  140. X    for AT&T Unixpc 7300/3b1 with:
  141. X        360k 5.25" as a: (supports 360k, and 320k, 180k, 160k)
  142. X        DOS73 disk image as c:
  143. X
  144. X        {'A', "/dev/rfp020", 0L, 12, O_NDELAY, init_unixpc, 40, 2, 9},
  145. X        {'C', "/usr/bin/DOS/dvd000", 0L, 12, 0, (int (*)()) 0, 0, 0, 0}
  146. X        {'\0', (char *) NULL, 0L, 0, 0, (int (*)()) 0, 0, 0, 0}
  147. X
  148. X
  149. X    for Sun SparcStation with:
  150. X        1.44m 3.5" as a: (supports 1.44m and 720k)
  151. X
  152. X        {'A', "/dev/rfd0c", 0L, 12, 0, (int (*)()) 0, 80, 2, 18},
  153. X        {'A', "/dev/rfd0c", 0L, 12, 0, (int (*)()) 0, 80, 2, 9},
  154. X        {'\0', (char *) NULL, 0L, 0, 0, (int (*)()) 0, 0, 0, 0}
  155. X
  156. X        Note that the two devices are actually the same physical
  157. X        device, and that only the number of sectors are
  158. X        different.
  159. END_OF_FILE
  160. if test 4629 -ne `wc -c <'mtools/Configure'`; then
  161.     echo shar: \"'mtools/Configure'\" unpacked with wrong size!
  162. fi
  163. # end of 'mtools/Configure'
  164. fi
  165. if test -f 'mtools/buf_write.c' -a "${1}" != "-c" ; then 
  166.   echo shar: Will not clobber existing file \"'mtools/buf_write.c'\"
  167. else
  168. echo shar: Extracting \"'mtools/buf_write.c'\" \(3057 characters\)
  169. sed "s/^X//" >'mtools/buf_write.c' <<'END_OF_FILE'
  170. X/*
  171. X * Do full cylinder buffered writes to slow devices.  Uses a simple
  172. X * buffered read/delayed write algorithm
  173. X */
  174. X
  175. X#include <stdio.h>
  176. X#include "msdos.h"
  177. X
  178. Xextern int fd, disk_size, disk_dirty, dir_start, dir_len;
  179. Xextern int clus_size;
  180. Xextern long disk_offset, disk_current;
  181. Xextern unsigned char *disk_buf;
  182. Xstatic int blank_cyl();
  183. X
  184. Xvoid
  185. Xdisk_write(start, buf, len)
  186. Xlong start;
  187. Xunsigned char *buf;
  188. Xint len;
  189. X{
  190. X    register long i;
  191. X    int length;
  192. X    unsigned char *buf_ptr, *disk_ptr;
  193. X    char *memcpy();
  194. X    void perror(), exit(), disk_flush();
  195. X    long where, tail, lseek();
  196. X
  197. X                    /* don't use cache? */
  198. X    if (disk_size == 1) {
  199. X        where = (start * MSECTOR_SIZE) + disk_offset;
  200. X        if (lseek(fd, where, 0) < 0) {
  201. X            perror("disk_write: lseek");
  202. X            exit(1);
  203. X        }
  204. X                    /* write it! */
  205. X        if (write(fd, (char *) buf, (unsigned int) len) != len) {
  206. X            perror("disk_write: write");
  207. X            exit(1);
  208. X        }
  209. X        return;
  210. X    }
  211. X
  212. X    tail = start + (len / MSECTOR_SIZE) - 1;
  213. X    for (i = start; i <= tail; i++) {
  214. X                    /* a cache miss... */
  215. X        if (i < disk_current || i >= disk_current + disk_size) {
  216. X
  217. X            if (disk_dirty)
  218. X                disk_flush();
  219. X
  220. X            /*
  221. X             * If there is something on the new cylinder that
  222. X             * you want to keep, you'll have to read it first
  223. X             * before writing.
  224. X             */
  225. X            if (blank_cyl(i))
  226. X                disk_current = (i / disk_size) * disk_size;
  227. X            else {
  228. X                disk_current = (i / disk_size) * disk_size;
  229. X                where = (disk_current * MSECTOR_SIZE) + disk_offset;
  230. X                length = disk_size * MSECTOR_SIZE;
  231. X
  232. X                    /* move to next location */
  233. X                if (lseek(fd, where, 0) < 0) {
  234. X                    perror("disk_write: lseek");
  235. X                    exit(1);
  236. X                }
  237. X                    /* read it! */
  238. X                if (read(fd, (char *) disk_buf, (unsigned int) length) != length) {
  239. X                    perror("disk_write: read");
  240. X                    exit(1);
  241. X                }
  242. X            }
  243. X        }
  244. X                    /* a cache hit ... */
  245. X        buf_ptr = buf + ((i - start) * MSECTOR_SIZE);
  246. X        disk_ptr = disk_buf + ((i - disk_current) * MSECTOR_SIZE);
  247. X        memcpy((char *) disk_ptr, (char *) buf_ptr, MSECTOR_SIZE);
  248. X        disk_dirty = 1;
  249. X    }
  250. X    return;
  251. X}
  252. X
  253. X/*
  254. X * Flush a dirty buffer to disk.  Resets disk_dirty to zero.
  255. X * All errors are fatal.
  256. X */
  257. X
  258. Xvoid
  259. Xdisk_flush()
  260. X{
  261. X    int len;
  262. X    long where, lseek();
  263. X    void perror(), exit();
  264. X
  265. X    if (fd < 0 || disk_current < 0L || !disk_dirty)
  266. X        return;
  267. X
  268. X    where = (disk_current * MSECTOR_SIZE) + disk_offset;
  269. X    if (lseek(fd, where, 0) < 0) {
  270. X        perror("disk_flush: lseek");
  271. X        exit(1);
  272. X    }
  273. X                    /* write it! */
  274. X    len = disk_size * MSECTOR_SIZE;
  275. X    if (write(fd, (char *) disk_buf, (unsigned int) len) != len) {
  276. X        perror("disk_flush: write");
  277. X        exit(1);
  278. X    }
  279. X    disk_dirty = 0;
  280. X    return;
  281. X}
  282. X
  283. X/*
  284. X * Determine if the cylinder has some useful information on it.  Returns a 1
  285. X * if it is blank.
  286. X */
  287. X
  288. Xstatic int
  289. Xblank_cyl(num)
  290. Xlong num;
  291. X{
  292. X    register unsigned int i;
  293. X    unsigned int start, end, fat_decode();
  294. X    long sector;
  295. X
  296. X    sector = (num / disk_size) * disk_size;
  297. X    if (!sector)
  298. X        return(0);
  299. X
  300. X    start = ((sector - dir_start - dir_len) / clus_size) + 2;
  301. X    end = ((sector + disk_size - dir_start - dir_len) / clus_size) + 2;
  302. X
  303. X    for (i = start; i < end; i++) {
  304. X                    /* if fat_decode returns non-zero */
  305. X        if (fat_decode(i))
  306. X            return(0);
  307. X    }
  308. X    return(1);
  309. X}
  310. END_OF_FILE
  311. if test 3057 -ne `wc -c <'mtools/buf_write.c'`; then
  312.     echo shar: \"'mtools/buf_write.c'\" unpacked with wrong size!
  313. fi
  314. # end of 'mtools/buf_write.c'
  315. fi
  316. if test -f 'mtools/dir_read.c' -a "${1}" != "-c" ; then 
  317.   echo shar: Will not clobber existing file \"'mtools/dir_read.c'\"
  318. else
  319. echo shar: Extracting \"'mtools/dir_read.c'\" \(3300 characters\)
  320. sed "s/^X//" >'mtools/dir_read.c' <<'END_OF_FILE'
  321. X#include <stdio.h>
  322. X#include "msdos.h"
  323. X
  324. Xlong dir_chain[MAX_DIR_SECS];        /* chain of sectors in directory */
  325. Xunsigned char *dir_buf;            /* the directory buffer */
  326. Xint dir_dirty;                /* is the buffer dirty? */
  327. X
  328. Xextern int dir_len, dir_start, clus_size, dir_entries, fat_error;
  329. Xextern unsigned int last_fat;
  330. X
  331. X/*
  332. X * Read a directory entry, return a pointer a static structure.
  333. X */
  334. X
  335. Xstruct directory *
  336. Xdir_read(num)
  337. Xint num;
  338. X{
  339. X    char *memcpy();
  340. X    unsigned char *offset;
  341. X    static struct directory dir;
  342. X
  343. X    offset = dir_buf + (num * MDIR_SIZE);
  344. X    memcpy((char *) &dir, (char *) offset, MDIR_SIZE);
  345. X    return(&dir);
  346. X}
  347. X
  348. X/*
  349. X * Fill in the global variable dir_chain[].  Argument is the starting
  350. X * cluster number.  Returns -1 on error.
  351. X */
  352. X
  353. Xint
  354. Xfill_chain(num)
  355. Xunsigned int num;
  356. X{
  357. X    register int i, length;
  358. X    unsigned int next, fat_decode();
  359. X    unsigned char *offset;
  360. X    char *malloc();
  361. X    void free(), perror(), exit(), disk_read(), dir_flush();
  362. X
  363. X    length = 0;
  364. X    /* CONSTCOND */
  365. X    while (1) {
  366. X        dir_chain[length] = (long) (num - 2) * clus_size + dir_start + dir_len;
  367. X        length++;
  368. X                    /* sectors, not clusters! */
  369. X        for (i = 1; i < clus_size; i++) {
  370. X            dir_chain[length] = dir_chain[length - 1] + 1L;
  371. X            length++;
  372. X        }
  373. X
  374. X        if (length >= MAX_DIR_SECS) {
  375. X            fprintf(stderr, "fill_chain: directory too large\n");
  376. X            return(-1);
  377. X        }
  378. X                    /* get next cluster number */
  379. X        next = fat_decode(num);
  380. X        if (next == 1) {
  381. X            fprintf(stderr, "fill_chain: FAT problem\n");
  382. X            fat_error++;
  383. X            return(-1);
  384. X        }
  385. X                    /* end of cluster chain */
  386. X        if (next >= last_fat)
  387. X            break;
  388. X        num = next;
  389. X    }
  390. X    if (dir_dirty)
  391. X        dir_flush();
  392. X                    /* fill the dir_buf */
  393. X    free((char *) dir_buf);
  394. X    dir_buf = (unsigned char *) malloc((unsigned int) length * MSECTOR_SIZE);
  395. X    if (dir_buf == NULL) {
  396. X        perror("fill_chain: malloc");
  397. X        exit(1);
  398. X    }
  399. X
  400. X    for (i = 0; i < length; i++) {
  401. X        offset = dir_buf + (i * MSECTOR_SIZE);
  402. X        disk_read(dir_chain[i], offset, MSECTOR_SIZE);
  403. X    }
  404. X
  405. X    dir_entries = length * 16;
  406. X    return(0);
  407. X}
  408. X
  409. X/*
  410. X * Reset the global variable dir_chain[] to the root directory.
  411. X */
  412. X
  413. Xvoid
  414. Xreset_chain(code)
  415. Xint code;
  416. X{
  417. X    register int i;
  418. X    char *malloc();
  419. X    void free(), disk_read(), dir_flush(), exit(), perror();
  420. X
  421. X    if (dir_dirty)
  422. X        dir_flush();
  423. X
  424. X    for (i = 0; i < dir_len; i++)
  425. X        dir_chain[i] = (long) dir_start + i;
  426. X
  427. X    if (code == OLD)
  428. X        free((char *) dir_buf);
  429. X
  430. X    dir_buf = (unsigned char *) malloc((unsigned int) dir_len * MSECTOR_SIZE);
  431. X    if (dir_buf == NULL) {
  432. X        perror("reset_chain: malloc");
  433. X        exit(1);
  434. X    }
  435. X    disk_read((long) dir_start, dir_buf, dir_len * MSECTOR_SIZE);
  436. X
  437. X    dir_entries = dir_len * 16;
  438. X    return;
  439. X}
  440. X
  441. X/*
  442. X * Get rid of spaces in an MSDOS 'raw' name (one that has come from the
  443. X * directory structure) so that it can be used for regular expression
  444. X * matching with a unix filename.  Also used to 'unfix' a name that has
  445. X * been altered by dos_name().  Returns a pointer a static buffer.
  446. X */
  447. X
  448. Xchar *
  449. Xunix_name(name, ext)
  450. Xunsigned char *name, *ext;
  451. X{
  452. X    char *s, tname[9], text[4], *strcpy(), *strcat(), *strchr();
  453. X    char *strncpy();
  454. X    static char ans[13];
  455. X
  456. X    strncpy(tname, (char *) name, 8);
  457. X    tname[8] = '\0';
  458. X    if (s = strchr(tname, ' '))
  459. X        *s = '\0';
  460. X
  461. X    strncpy(text, (char *) ext, 3);
  462. X    text[3] = '\0';
  463. X    if (s = strchr(text, ' '))
  464. X        *s = '\0';
  465. X
  466. X    if (*text) {
  467. X        strcpy(ans, tname);
  468. X        strcat(ans, ".");
  469. X        strcat(ans, text);
  470. X    }
  471. X    else
  472. X        strcpy(ans, tname);
  473. X    return(ans);
  474. X}
  475. END_OF_FILE
  476. if test 3300 -ne `wc -c <'mtools/dir_read.c'`; then
  477.     echo shar: \"'mtools/dir_read.c'\" unpacked with wrong size!
  478. fi
  479. # end of 'mtools/dir_read.c'
  480. fi
  481. if test -f 'mtools/dir_write.c' -a "${1}" != "-c" ; then 
  482.   echo shar: Will not clobber existing file \"'mtools/dir_write.c'\"
  483. else
  484. echo shar: Extracting \"'mtools/dir_write.c'\" \(4191 characters\)
  485. sed "s/^X//" >'mtools/dir_write.c' <<'END_OF_FILE'
  486. X#include <stdio.h>
  487. X#include <time.h>
  488. X#include <ctype.h>
  489. X#include "msdos.h"
  490. X
  491. Xextern int fd, dir_dirty, clus_size, dir_entries;
  492. Xextern long dir_chain[MAX_DIR_SECS];
  493. Xextern unsigned char *dir_buf;
  494. X
  495. X/*
  496. X * Write a directory entry.  A simple cache is used instead of something
  497. X * more elaborate.
  498. X */
  499. X
  500. Xvoid
  501. Xdir_write(num, dir)
  502. Xint num;
  503. Xstruct directory *dir;
  504. X{
  505. X    unsigned char *offset;
  506. X    char *memcpy();
  507. X
  508. X    offset = dir_buf + (num * MDIR_SIZE);
  509. X    memcpy((char *) offset, (char *) dir, MDIR_SIZE);
  510. X    dir_dirty = 1;
  511. X    return;
  512. X}
  513. X
  514. X/*
  515. X * Write a partially filled directory buffer to disk.  Resets dir_dirty to
  516. X * zero.
  517. X */
  518. X
  519. Xvoid
  520. Xdir_flush()
  521. X{
  522. X    int i, length;
  523. X    unsigned char *offset;
  524. X    void disk_write();
  525. X
  526. X    if (fd < 0 || !dir_dirty)
  527. X        return;
  528. X
  529. X    length = dir_entries / 16;
  530. X    for (i = 0; i < length; i++) {
  531. X        offset = dir_buf + (i * MSECTOR_SIZE);
  532. X        disk_write(dir_chain[i], offset, MSECTOR_SIZE);
  533. X    }
  534. X    dir_dirty = 0;
  535. X    return;
  536. X}
  537. X
  538. X/*
  539. X * Convert a Unix filename to a legal MSDOS name.  Returns a pointer to
  540. X * a static area.  Will truncate file and extension names, will
  541. X * substitute the letter 'X' for any illegal character in the name.
  542. X */
  543. X
  544. Xunsigned char *
  545. Xdos_name(filename, verbose)
  546. Xchar *filename;
  547. Xint verbose;
  548. X{
  549. X    static char *dev[9] = {"CON", "AUX", "COM1", "COM2", "PRN", "LPT1",
  550. X    "LPT2", "LPT3", "NUL"};
  551. X    char *s, buf[MAX_PATH], *name, *ext, *strcpy(), *strpbrk(), *strrchr();
  552. X    static unsigned char ans[13];
  553. X    int dot, modified, len;
  554. X    register int i;
  555. X
  556. X    strcpy(buf, filename);
  557. X    name = buf;
  558. X                    /* skip drive letter */
  559. X    if (buf[0] && buf[1] == ':')
  560. X        name = &buf[2];
  561. X                    /* zap the leading path */
  562. X    if (s = strrchr(name, '/'))
  563. X        name = s + 1;
  564. X    if (s = strrchr(name, '\\'))
  565. X        name = s + 1;
  566. X
  567. X    ext = "";
  568. X    dot = 0;
  569. X    len = strlen(name);
  570. X    for (i = 0; i < len; i++) {
  571. X        s = name + len -i -1;
  572. X        if (*s == '.' && !dot) {
  573. X            dot = 1;
  574. X            *s = '\0';
  575. X            ext = s +1;
  576. X        }
  577. X        if (islower(*s))
  578. X            *s = toupper(*s);
  579. X    }
  580. X    if (*name == '\0') {
  581. X        name = "X";
  582. X        if (verbose)
  583. X            printf("\"%s\" Null name component, using \"%s.%s\"\n", filename, name, ext);
  584. X    }
  585. X    for (i = 0; i < 9; i++) {
  586. X        if (!strcmp(name, dev[i])) {
  587. X            *name = 'X';
  588. X            if (verbose)
  589. X                printf("\"%s\" Is a device name, using \"%s.%s\"\n", filename, name, ext);
  590. X        }
  591. X    }
  592. X    if (strlen(name) > 8) {
  593. X        *(name + 8) = '\0';
  594. X        if (verbose)
  595. X            printf("\"%s\" Name too long, using, \"%s.%s\"\n", filename, name, ext);
  596. X    }
  597. X    if (strlen(ext) > 3) {
  598. X        *(ext + 3) = '\0';
  599. X        if (verbose)
  600. X            printf("\"%s\" Extension too long, using \"%s.%s\"\n", filename, name, ext);
  601. X    }
  602. X    modified = 0;
  603. X    while (s = strpbrk(name, "^+=/[]:',?*\\<>|\". ")) {
  604. X        modified++;
  605. X        *s = 'X';
  606. X    }
  607. X    while (s = strpbrk(ext, "^+=/[]:',?*\\<>|\". ")) {
  608. X        modified++;
  609. X        *s = 'X';
  610. X    }
  611. X    if (modified && verbose)
  612. X        printf("\"%s\" Contains illegal character(s), using \"%s.%s\"\n", filename, name, ext);
  613. X
  614. X    sprintf((char *) ans, "%-8.8s%-3.3s", name, ext);
  615. X    return(ans);
  616. X}
  617. X
  618. X/*
  619. X * Make a directory entry.  Builds a directory entry based on the
  620. X * name, attribute, starting cluster number, and size.  Returns a pointer
  621. X * to a static directory structure.
  622. X */
  623. X
  624. Xstruct directory *
  625. Xmk_entry(filename, attr, fat, size, date)
  626. Xunsigned char *filename;
  627. Xunsigned char attr;
  628. Xunsigned int fat;
  629. Xlong size, date;
  630. X{
  631. X    int i;
  632. X    char *strncpy();
  633. X    static struct directory ndir;
  634. X    struct tm *now, *localtime();
  635. X    unsigned char hour, min_hi, min_low, sec;
  636. X    unsigned char year, month_hi, month_low, day;
  637. X
  638. X    now = localtime(&date);
  639. X    strncpy((char *) ndir.name, (char *) filename, 8);
  640. X    strncpy((char *) ndir.ext, (char *) filename + 8, 3);
  641. X    ndir.attr = attr;
  642. X    for (i = 0; i < 10; i++)
  643. X        ndir.reserved[i] = '\0';
  644. X    hour = now->tm_hour << 3;
  645. X    min_hi = now->tm_min >> 3;
  646. X    min_low = now->tm_min << 5;
  647. X    sec = now->tm_sec / 2;
  648. X    ndir.time[1] = hour + min_hi;
  649. X    ndir.time[0] = min_low + sec;
  650. X    year = (now->tm_year - 80) << 1;
  651. X    month_hi = (now->tm_mon + 1) >> 3;
  652. X    month_low = (now->tm_mon + 1) << 5;
  653. X    day = now->tm_mday;
  654. X    ndir.date[1] = year + month_hi;
  655. X    ndir.date[0] = month_low + day;
  656. X    ndir.start[1] = fat / 0x100;
  657. X    ndir.start[0] = fat % 0x100;
  658. X    ndir.size[3] = size / 0x1000000L;
  659. X    ndir.size[2] = (size % 0x1000000L) / 0x10000L;
  660. X    ndir.size[1] = ((size % 0x1000000L) % 0x10000L) / 0x100;
  661. X    ndir.size[0] = ((size % 0x1000000L) % 0x10000L) % 0x100;
  662. X    return(&ndir);
  663. X}
  664. END_OF_FILE
  665. if test 4191 -ne `wc -c <'mtools/dir_write.c'`; then
  666.     echo shar: \"'mtools/dir_write.c'\" unpacked with wrong size!
  667. fi
  668. # end of 'mtools/dir_write.c'
  669. fi
  670. if test -f 'mtools/mattrib.c' -a "${1}" != "-c" ; then 
  671.   echo shar: Will not clobber existing file \"'mtools/mattrib.c'\"
  672. else
  673. echo shar: Extracting \"'mtools/mattrib.c'\" \(3434 characters\)
  674. sed "s/^X//" >'mtools/mattrib.c' <<'END_OF_FILE'
  675. X/*
  676. X * Change MSDOS file attribute flags
  677. X *
  678. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  679. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  680. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  681. X *                     Environmental Management Office
  682. X *                     Fort Hood, TX 76544-5057
  683. X */
  684. X
  685. X#include <stdio.h>
  686. X#include "msdos.h"
  687. X#include "patchlevel.h"
  688. X
  689. Xint fd = -1;                /* the file descriptor for the device */
  690. Xint dir_start;                /* starting sector for directory */
  691. Xint dir_len;                /* length of directory (in sectors) */
  692. Xint dir_entries;            /* number of directory entries */
  693. Xint clus_size;                /* cluster size (in sectors) */
  694. Xchar *mcwd;                /* the Current Working Directory */
  695. Xint fat_error;                /* FAT error detected? */
  696. X
  697. X#define ADD    1
  698. X#define REMOVE    (-1)
  699. X#define LEAVE    0
  700. X
  701. Xmain(argc, argv)
  702. Xint argc;
  703. Xchar *argv[];
  704. X{
  705. X    int entry, ismatch, oops, fargn, read_only, hidden, sys, archive;
  706. X    int i, action;
  707. X    char *filename, *newfile, *unix_name(), drive, get_drive();
  708. X    char *get_path(), *pathname, *get_name(), *fix_mcwd(), last_drive;
  709. X    void exit(), dir_write(), dir_flush(), disk_flush();
  710. X    struct directory *dir, *dir_read();
  711. X
  712. X    oops = 0;
  713. X    fargn = -1;
  714. X    archive = LEAVE;
  715. X    hidden = LEAVE;
  716. X    read_only = LEAVE;
  717. X    sys = LEAVE;
  718. X                    /* can't use getopt(3)... */
  719. X    for (i = 1; i < argc; i++) {
  720. X        switch (argv[i][0]) {
  721. X            case '-':
  722. X                action = REMOVE;
  723. X                break;
  724. X            case '+':
  725. X                action = ADD;
  726. X                break;
  727. X            default:
  728. X                fargn = i;
  729. X                break;
  730. X        }
  731. X        if (fargn != -1)
  732. X            break;
  733. X
  734. X        switch (argv[i][1]) {
  735. X            case 'a':
  736. X            case 'A':
  737. X                archive = action;
  738. X                break;
  739. X            case 'h':
  740. X            case 'H':
  741. X                hidden = action;
  742. X                break;
  743. X            case 'r':
  744. X            case 'R':
  745. X                read_only = action;
  746. X                break;
  747. X            case 's':
  748. X            case 'S':
  749. X                sys = action;
  750. X                break;
  751. X            default:
  752. X                oops++;
  753. X                break;
  754. X        }
  755. X        if (oops)
  756. X            break;
  757. X    }
  758. X    if (argc < 3 || argv[fargn][0] == '\0' || oops) {
  759. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  760. X        fprintf(stderr, "Usage: %s [-a|+a] [-h|+h] [-r|+r] [-s|+s] msdosfile [msdosfiles...]\n", argv[0]);
  761. X        exit(1);
  762. X    }
  763. X    last_drive = 'x';
  764. X    mcwd = fix_mcwd();
  765. X
  766. X    for (i = fargn; i < argc; i++) {
  767. X        drive = get_drive(argv[i]);
  768. X        if (last_drive != drive) {
  769. X            if (init(drive, 2)) {
  770. X                fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  771. X                continue;
  772. X            }
  773. X            last_drive = drive;
  774. X        }
  775. X        filename = get_name(argv[i]);
  776. X        pathname = get_path(argv[i]);
  777. X        if (subdir(drive, pathname))
  778. X            continue;
  779. X
  780. X                    /* see if exists and do it */
  781. X        ismatch = 0;
  782. X        for (entry = 0; entry < dir_entries; entry++) {
  783. X            dir = dir_read(entry);
  784. X                    /* if empty */
  785. X            if (dir->name[0] == 0x0)
  786. X                break;
  787. X                    /* if erased */
  788. X            if (dir->name[0] == 0xe5)
  789. X                continue;
  790. X                    /* if dir or volume label */
  791. X            if ((dir->attr & 0x10) || (dir->attr & 0x08))
  792. X                continue;
  793. X
  794. X            newfile = unix_name(dir->name, dir->ext);
  795. X
  796. X                    /* do it... */
  797. X            if (match(newfile, filename)) {
  798. X                if (archive == ADD)
  799. X                    dir->attr |= 0x20;
  800. X                if (archive == REMOVE)
  801. X                    dir->attr &= ~0x20;
  802. X                if (hidden == ADD)
  803. X                    dir->attr |= 0x02;
  804. X                if (hidden == REMOVE)
  805. X                    dir->attr &= ~0x02;
  806. X                if (read_only == ADD)
  807. X                    dir->attr |= 0x01;
  808. X                if (read_only == REMOVE)
  809. X                    dir->attr &= ~0x01;
  810. X                if (sys == ADD)
  811. X                    dir->attr |= 0x04;
  812. X                if (sys == REMOVE)
  813. X                    dir->attr &= ~0x04;
  814. X                dir_write(entry, dir);
  815. X                ismatch++;
  816. X            }
  817. X        }
  818. X        if (!ismatch)
  819. X            fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  820. X    }
  821. X    dir_flush();
  822. X    disk_flush();
  823. X    close(fd);
  824. X    exit(0);
  825. X}
  826. END_OF_FILE
  827. if test 3434 -ne `wc -c <'mtools/mattrib.c'`; then
  828.     echo shar: \"'mtools/mattrib.c'\" unpacked with wrong size!
  829. fi
  830. # end of 'mtools/mattrib.c'
  831. fi
  832. if test -f 'mtools/mcd.c' -a "${1}" != "-c" ; then 
  833.   echo shar: Will not clobber existing file \"'mtools/mcd.c'\"
  834. else
  835. echo shar: Extracting \"'mtools/mcd.c'\" \(2639 characters\)
  836. sed "s/^X//" >'mtools/mcd.c' <<'END_OF_FILE'
  837. X/*
  838. X * Change MSDOS directories
  839. X *
  840. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  841. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  842. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  843. X *                     Environmental Management Office
  844. X *                     Fort Hood, TX 76544-5057
  845. X */
  846. X
  847. X#include <stdio.h>
  848. X#include "msdos.h"
  849. X#include "patchlevel.h"
  850. X
  851. Xint fd = -1;                /* the file descriptor for the device */
  852. Xint dir_start;                /* starting sector for directory */
  853. Xint dir_len;                /* length of directory (in sectors) */
  854. Xint dir_entries;            /* number of directory entries */
  855. Xint clus_size;                /* cluster size (in sectors) */
  856. Xchar *mcwd;                /* the Current Working Directory */
  857. Xint fat_error;                /* FAT error detected? */
  858. X
  859. Xmain(argc, argv)
  860. Xint argc;
  861. Xchar *argv[];
  862. X{
  863. X    FILE *fp;
  864. X    char *fix_mcwd(), *strcpy(), newpath[MAX_PATH], *get_name();
  865. X    char *get_path(), *pathname, *filename, drive, *strcat();
  866. X    char get_drive(), *mcwd_path, *getenv(), *expand();
  867. X    void exit();
  868. X
  869. X    if (argc > 2) {
  870. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  871. X        fprintf(stderr, "Usage: %s: msdosdirectory\n", argv[0]);
  872. X        exit(1);
  873. X    }
  874. X                    /* only report the mcwd */
  875. X    mcwd = fix_mcwd();
  876. X    if (argc == 1) {
  877. X        printf("%s\n", mcwd);
  878. X        exit(0);
  879. X    }
  880. X
  881. X    drive = get_drive(argv[1]);
  882. X    filename = get_name(argv[1]);
  883. X    pathname = get_path(argv[1]);
  884. X
  885. X    if (init(drive, 0)) {
  886. X        fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  887. X        exit(1);
  888. X    }
  889. X
  890. X    /*
  891. X     * Move to "first guess" subdirectory, so that is_dir() can
  892. X     * search to see if filename is also a directory.
  893. X     */
  894. X    if (subdir(drive, pathname)) {
  895. X        fprintf(stderr, "%s: Directory not found\n", argv[0]);
  896. X        exit(1);
  897. X    }
  898. X                    /* is filename really a subdirectory? */
  899. X    strcpy(newpath, pathname);
  900. X    if (is_dir(filename)) {
  901. X        if (newpath[strlen(newpath) -1] != '/')
  902. X            strcat(newpath, "/");
  903. X        strcat(newpath, filename);
  904. X
  905. X                    /* move to real subdirectory */
  906. X        if (subdir(drive, newpath)) {
  907. X            fprintf(stderr, "%s: Directory not found\n", argv[0]);
  908. X            exit(1);
  909. X        }
  910. X    }
  911. X    else {
  912. X        if (*filename != '\0') {
  913. X            fprintf(stderr, "%s: Directory not found\n", argv[0]);
  914. X            exit(1);
  915. X        }
  916. X    }
  917. X                    /* it checked out ok, so save it */
  918. X    mcwd_path = getenv("MCWD");
  919. X    if (mcwd_path == NULL || *mcwd_path == '\0')
  920. X        mcwd_path = "$HOME/.mcwd";
  921. X
  922. X    if (!(fp = fopen(expand(mcwd_path), "w"))) {
  923. X        fprintf(stderr, "%s: Can't open '%s' for write\n", argv[0], expand(mcwd_path));
  924. X        exit(1);
  925. X    }
  926. X    fprintf(fp, "%c:%s\n", drive, newpath);
  927. X    fclose(fp);
  928. X    exit(0);
  929. X}
  930. X
  931. X/*
  932. X * stubs for read-only programs
  933. X */
  934. X
  935. Xvoid
  936. Xdisk_flush()
  937. X{
  938. X    extern int disk_dirty;
  939. X
  940. X    disk_dirty = 0;
  941. X    return;
  942. X}
  943. X
  944. Xvoid
  945. Xdir_flush()
  946. X{
  947. X    extern int dir_dirty;
  948. X
  949. X    dir_dirty = 0;
  950. X    return;
  951. X}
  952. END_OF_FILE
  953. if test 2639 -ne `wc -c <'mtools/mcd.c'`; then
  954.     echo shar: \"'mtools/mcd.c'\" unpacked with wrong size!
  955. fi
  956. # end of 'mtools/mcd.c'
  957. fi
  958. if test -f 'mtools/mcopy.c' -a "${1}" != "-c" ; then 
  959.   echo shar: Will not clobber existing file \"'mtools/mcopy.c'\"
  960. else
  961. echo shar: Extracting \"'mtools/mcopy.c'\" \(4332 characters\)
  962. sed "s/^X//" >'mtools/mcopy.c' <<'END_OF_FILE'
  963. X/*
  964. X * A front-end to the mread/mwrite commands.
  965. X *
  966. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  967. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  968. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  969. X *                     Environmental Management Office
  970. X *                     Fort Hood, TX 76544-5057
  971. X */
  972. X
  973. X#include <stdio.h>
  974. X#include <ctype.h>
  975. X#include "patchlevel.h"
  976. X
  977. X#define NONE    0
  978. X#define MREAD    1
  979. X#define MWRITE    2
  980. X#define MKDIR
  981. X
  982. Xmain(argc, argv)
  983. Xint argc;
  984. Xchar *argv[];
  985. X{
  986. X    extern int optind;
  987. X    extern char *optarg;
  988. X    int i, oops, msdos_args, unix_args, destination;
  989. X    char **nargv, **malloc();
  990. X    void exit();
  991. X                    /* get command line options */
  992. X    msdos_args = 0;
  993. X    unix_args = 0;
  994. X    oops = 0;
  995. X    while ((i = getopt(argc, argv, "tnvm")) != EOF) {
  996. X        switch (i) {
  997. X            case 't':
  998. X            case 'n':
  999. X            case 'v':
  1000. X            case 'm':
  1001. X                break;
  1002. X            default:
  1003. X                oops = 1;
  1004. X                break;
  1005. X        }
  1006. X    }
  1007. X
  1008. X    if (oops || (argc - optind) < 2) {
  1009. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1010. X        fprintf(stderr, "Usage: %s [-tnvm] sourcefile targetfile\n", argv[0]);
  1011. X        fprintf(stderr, "       %s [-tnvm] sourcefile [sourcefiles...] targetdirectory\n", argv[0]);
  1012. X        exit(1);
  1013. X    }
  1014. X                    /* last file determines the direction */
  1015. X    if (argv[argc - 1][1] == ':')
  1016. X        destination = MWRITE;
  1017. X    else
  1018. X        destination = MREAD;
  1019. X
  1020. X                    /* count the arguments */
  1021. X    for (i = optind; i < argc; i++) {
  1022. X        if (argv[i][1] == ':')
  1023. X            msdos_args++;
  1024. X        else
  1025. X            unix_args++;
  1026. X    }
  1027. X
  1028. X    if (destination == MREAD && unix_args > 1) {
  1029. X        fprintf(stderr, "%s: Duplicate destination files\n", argv[0]); 
  1030. X        exit(1);
  1031. X    }
  1032. X                    /* chaining of mread and mwrite */
  1033. X    if (destination == MWRITE && msdos_args > 1)
  1034. X        chain(argc, argv);
  1035. X
  1036. X    /*
  1037. X     * Copy the *argv[] array in case your Unix doesn't end the array
  1038. X     * with a null when it passes it to main()
  1039. X     */
  1040. X    nargv = (char **) malloc((unsigned int) (argc + 1) * sizeof(*argv));
  1041. X    nargv[0] = "mcopy";
  1042. X    for (i = 1; i < argc; i++)
  1043. X        nargv[i] = argv[i];
  1044. X    nargv[argc] = NULL;
  1045. X
  1046. X    if (destination == MWRITE)
  1047. X        execvp("mwrite", nargv);
  1048. X    else
  1049. X        execvp("mread", nargv);
  1050. X}
  1051. X
  1052. Xchain(argc, argv)
  1053. Xint argc;
  1054. Xchar *argv[];
  1055. X{
  1056. X    extern int optind;
  1057. X    int i, j, pid, status;
  1058. X    char *tmpdir, *mktemp(), **nargv, **malloc(), buf[256], *strcpy();
  1059. X    char *unixname(), *realloc();
  1060. X    void exit();
  1061. X
  1062. X    nargv = (char **) malloc((unsigned int) (argc + 4) * sizeof(*argv));
  1063. X    nargv[0] = "mread";
  1064. X    nargv[1] = "-n";
  1065. X                    /* copy only the msdos arguments */
  1066. X    j = 2;
  1067. X    for (i = optind; i < argc -1; i++) {
  1068. X        if (argv[i][1] == ':')
  1069. X            nargv[j++] = argv[i];
  1070. X    }
  1071. X                    /* create a temp directory */
  1072. X    tmpdir = mktemp("/tmp/mtoolsXXXXXX");
  1073. X    if (mkdir(tmpdir, 0777) < 0)
  1074. X        perror("mkdir");
  1075. X
  1076. X    nargv[j++] = tmpdir;
  1077. X    nargv[j] = NULL;
  1078. X
  1079. X    printf("reading...\n");
  1080. X    if (!(pid = fork()))
  1081. X        execvp("mread", nargv);
  1082. X
  1083. X    while (wait(&status) != pid)
  1084. X        ;
  1085. X                    /* reconstruct the argv[] */
  1086. X    nargv[0] = "sh";
  1087. X    nargv[1] = "-c";
  1088. X    nargv[2] = (char *) malloc(7);
  1089. X    strcpy(nargv[2], "mwrite");
  1090. X
  1091. X    j = 3;
  1092. X    for (i = 1; i < argc -1; i++) {
  1093. X        /*
  1094. X         * Substitute the msdos arguments for their unix
  1095. X         * counterparts that have already been copied to tmpdir.
  1096. X         */
  1097. X        if (argv[i][1] == ':')
  1098. X            sprintf(buf, "%s/%s", tmpdir, unixname(argv[i]));
  1099. X        else
  1100. X            strcpy(buf, argv[i]);
  1101. X
  1102. X        nargv[2] = (char *) realloc(nargv[2], sizeof(nargv[2]) + sizeof(buf));
  1103. X        strcat(nargv[2], " ");
  1104. X        strcat(nargv[2], buf);
  1105. X    }
  1106. X                    /* protect last arg from expansion */
  1107. X    sprintf(buf, "'%s'", argv[i]);
  1108. X    nargv[2] = (char *) realloc(nargv[2], sizeof(nargv[2]) + sizeof(buf));
  1109. X    strcat(nargv[2], " ");
  1110. X    strcat(nargv[2], buf);
  1111. X
  1112. X    nargv[3] = NULL;
  1113. X
  1114. X    printf("writing...\n");
  1115. X    if (!(pid = fork()))
  1116. X        execvp("sh", nargv);
  1117. X
  1118. X    while (wait(&status) != pid)
  1119. X        ;
  1120. X                    /* clobber the directory */
  1121. X    sprintf(buf, "rm -fr %s", tmpdir);
  1122. X    system(buf);
  1123. X    exit(0);
  1124. X}
  1125. X
  1126. Xchar *
  1127. Xunixname(filename)
  1128. Xchar *filename;
  1129. X{
  1130. X    char *s, *temp, *strcpy(), *strrchr(), buf[256];
  1131. X    static char ans[13];
  1132. X
  1133. X    strcpy(buf, filename);
  1134. X    temp = buf;
  1135. X                    /* skip drive letter */
  1136. X    if (buf[0] && buf[1] == ':')
  1137. X        temp = &buf[2];
  1138. X                    /* find the last separator */
  1139. X    if (s = strrchr(temp, '/'))
  1140. X        temp = s + 1;
  1141. X    if (s = strrchr(temp, '\\'))
  1142. X        temp = s + 1;
  1143. X                    /* xlate to lower case */
  1144. X    for (s = temp; *s; ++s) {
  1145. X        if (isupper(*s))
  1146. X            *s = tolower(*s);
  1147. X    }
  1148. X
  1149. X    strcpy(ans, temp);
  1150. X    return(ans);
  1151. X}
  1152. X
  1153. X#ifdef MKDIR
  1154. Xmkdir(path, mode)
  1155. Xchar *path;
  1156. Xint mode;
  1157. X{
  1158. X    char buf[256];
  1159. X    sprintf(buf, "mkdir %s", path);
  1160. X    return(system(buf));
  1161. X}
  1162. X#endif /* MKDIR */
  1163. END_OF_FILE
  1164. if test 4332 -ne `wc -c <'mtools/mcopy.c'`; then
  1165.     echo shar: \"'mtools/mcopy.c'\" unpacked with wrong size!
  1166. fi
  1167. # end of 'mtools/mcopy.c'
  1168. fi
  1169. if test -f 'mtools/mdel.c' -a "${1}" != "-c" ; then 
  1170.   echo shar: Will not clobber existing file \"'mtools/mdel.c'\"
  1171. else
  1172. echo shar: Extracting \"'mtools/mdel.c'\" \(3601 characters\)
  1173. sed "s/^X//" >'mtools/mdel.c' <<'END_OF_FILE'
  1174. X/*
  1175. X * Delete an MSDOS file
  1176. X *
  1177. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  1178. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  1179. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  1180. X *                     Environmental Management Office
  1181. X *                     Fort Hood, TX 76544-5057
  1182. X */
  1183. X
  1184. X#include <stdio.h>
  1185. X#include <signal.h>
  1186. X#include "msdos.h"
  1187. X#include "patchlevel.h"
  1188. X
  1189. Xint fd = -1;                /* the file descriptor for the device */
  1190. Xint dir_start;                /* starting sector for directory */
  1191. Xint dir_len;                /* length of directory (in sectors) */
  1192. Xint dir_entries;            /* number of directory entries */
  1193. Xint clus_size;                /* cluster size (in sectors) */
  1194. Xchar *mcwd;                /* the Current Working Directory */
  1195. Xint fat_error;                /* FAT error detected? */
  1196. X
  1197. Xstatic int got_signal();
  1198. X
  1199. Xmain(argc, argv)
  1200. Xint argc;
  1201. Xchar *argv[];
  1202. X{
  1203. X    int i, ismatch, entry, nogo, verbose, fargn;
  1204. X    unsigned int start;
  1205. X    char *filename, *newfile, *get_name(), *unix_name(), *get_path();
  1206. X    char *pathname, ans[10], drive, get_drive(), last_drive, *fix_mcwd();
  1207. X    void exit(), fat_write(), dir_write(), dir_flush(), disk_flush();
  1208. X    struct directory *dir, *dir_read();
  1209. X                    /* catch signals */
  1210. X    signal(SIGINT, (SIG_TYPE(*) ()) got_signal);
  1211. X    signal(SIGTERM, (SIG_TYPE(*) ()) got_signal);
  1212. X    signal(SIGQUIT, (SIG_TYPE(*) ()) got_signal);
  1213. X
  1214. X    if (argc > 1 && !strcmp(argv[1], "-v")) {
  1215. X        verbose = 1;
  1216. X        fargn = 2;
  1217. X    }
  1218. X    else {
  1219. X        verbose = 0;
  1220. X        fargn = 1;
  1221. X    }
  1222. X    if (argc < 2 || (argv[1][0] == '-' && !verbose)) {
  1223. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1224. X        fprintf(stderr, "Usage: %s [-v] msdosfile [msdosfiles...]\n", argv[0]);
  1225. X        exit(1);
  1226. X    }
  1227. X    last_drive = 'x';
  1228. X    mcwd = fix_mcwd();
  1229. X
  1230. X    for (i = fargn; i < argc; i++) {
  1231. X        drive = get_drive(argv[i]);
  1232. X        if (drive != last_drive) {
  1233. X            if (last_drive != 'x') {
  1234. X                fat_write();
  1235. X                dir_flush();
  1236. X                disk_flush();
  1237. X            }
  1238. X
  1239. X            if (init(drive, 2)) {
  1240. X                fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  1241. X                continue;
  1242. X            }
  1243. X            last_drive = drive;
  1244. X        }
  1245. X        filename = get_name(argv[i]);
  1246. X        pathname = get_path(argv[i]);
  1247. X        if (subdir(drive, pathname))
  1248. X            continue;
  1249. X
  1250. X        nogo = 0;
  1251. X        ismatch = 0;
  1252. X        for (entry = 0; entry < dir_entries; entry++) {
  1253. X            dir = dir_read(entry);
  1254. X                    /* if empty */
  1255. X            if (dir->name[0] == 0x0)
  1256. X                break;
  1257. X                    /* if erased */
  1258. X            if (dir->name[0] == 0xe5)
  1259. X                continue;
  1260. X                    /* if dir or volume label */
  1261. X            if ((dir->attr & 0x10) || (dir->attr & 0x08))
  1262. X                continue;
  1263. X
  1264. X            newfile = unix_name(dir->name, dir->ext);
  1265. X                    /* see it if matches the pattern */
  1266. X            if (match(newfile, filename)) {
  1267. X                if (verbose)
  1268. X                    printf("Removing %s\n", newfile);
  1269. X                ismatch = 1;
  1270. X                if (dir->attr & 0x01) {
  1271. X                    while (!nogo) {
  1272. X                        printf("%s: \"%s\" is read only, erase anyway (y/n) ? ", argv[0], newfile);
  1273. X                        gets(ans);
  1274. X                        if (ans[0] == 'y' || ans[0] == 'Y')
  1275. X                            break;
  1276. X                        if (ans[0] == 'n' || ans[0] == 'N')
  1277. X                            nogo = 1;
  1278. X                    }
  1279. X                    if (nogo)
  1280. X                        continue;
  1281. X                }
  1282. X                start = dir->start[1] * 0x100 + dir->start[0];
  1283. X                if (fat_free(start))
  1284. X                    break;
  1285. X                dir->name[0] = 0xe5;
  1286. X                dir_write(entry, dir);
  1287. X            }
  1288. X        }
  1289. X        if (fat_error)
  1290. X            break;
  1291. X
  1292. X        if (!ismatch)
  1293. X            fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  1294. X    }
  1295. X                    /* write the FAT, flush the buffers */
  1296. X    fat_write();
  1297. X    dir_flush();
  1298. X    disk_flush();
  1299. X    close(fd);
  1300. X    exit(0);
  1301. X}
  1302. X
  1303. X/*
  1304. X * Do a graceful exit if the program is interrupted.  This will reduce
  1305. X * (but not eliminate) the risk of generating a corrupted disk on
  1306. X * a user abort.
  1307. X */
  1308. X
  1309. Xstatic int
  1310. Xgot_signal()
  1311. X{
  1312. X    void exit(), disk_flush(), fat_write(), dir_flush();
  1313. X
  1314. X    if (fd < 0)
  1315. X        exit(1);
  1316. X    fat_write();
  1317. X    dir_flush();
  1318. X    disk_flush();
  1319. X    close(fd);
  1320. X    exit(1);
  1321. X}
  1322. END_OF_FILE
  1323. if test 3601 -ne `wc -c <'mtools/mdel.c'`; then
  1324.     echo shar: \"'mtools/mdel.c'\" unpacked with wrong size!
  1325. fi
  1326. # end of 'mtools/mdel.c'
  1327. fi
  1328. if test -f 'mtools/mformat.c' -a "${1}" != "-c" ; then 
  1329.   echo shar: Will not clobber existing file \"'mtools/mformat.c'\"
  1330. else
  1331. echo shar: Extracting \"'mtools/mformat.c'\" \(6144 characters\)
  1332. sed "s/^X//" >'mtools/mformat.c' <<'END_OF_FILE'
  1333. X/*
  1334. X * Add an MSDOS filesystem to a low level formatted diskette.
  1335. X *
  1336. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  1337. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  1338. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  1339. X *                     Environmental Management Office
  1340. X *                     Fort Hood, TX 76544-5057
  1341. X */
  1342. X
  1343. X#include <stdio.h>
  1344. X#include <ctype.h>
  1345. X#include "msdos.h"
  1346. X#include "patchlevel.h"
  1347. X
  1348. Xint fd, dir_dirty, dir_entries;
  1349. Xlong dir_chain[MAX_DIR_SECS];
  1350. Xunsigned char *dir_buf;
  1351. X
  1352. Xmain(argc, argv)
  1353. Xint argc;
  1354. Xchar *argv[];
  1355. X{
  1356. X    extern int optind;
  1357. X    extern char *optarg;
  1358. X    extern struct device devices[];
  1359. X    struct bootsector boot;
  1360. X    int i, c, oops, tracks, heads, sectors, fat_len, dir_len, clus_size;
  1361. X    int tot_sectors, num_clus, fat_guess;
  1362. X    long time(), now, lseek();
  1363. X    char drive, *name, *expand();
  1364. X    char *strncpy(), *memset(), *memcpy();
  1365. X    unsigned char media, label[12], buf[MSECTOR_SIZE];
  1366. X    struct device *dev;
  1367. X    struct directory *dir, *mk_entry();
  1368. X    void exit(), perror();
  1369. X
  1370. X    oops = 0;
  1371. X    tracks = 0;
  1372. X    heads = 0;
  1373. X    sectors = 0;
  1374. X    label[0] = '\0';
  1375. X                    /* get command line options */
  1376. X    while ((c = getopt(argc, argv, "t:h:s:l:")) != EOF) {
  1377. X        switch (c) {
  1378. X            case 't':
  1379. X                tracks = atoi(optarg);
  1380. X                break;
  1381. X            case 'h':
  1382. X                heads = atoi(optarg);
  1383. X                break;
  1384. X            case 's':
  1385. X                sectors = atoi(optarg);
  1386. X                break;
  1387. X            case 'l':
  1388. X                sprintf((char *) label, "%-11.11s", optarg);
  1389. X                break;
  1390. X            default:
  1391. X                oops = 1;
  1392. X                break;
  1393. X        }
  1394. X    }
  1395. X
  1396. X    if (oops || (argc - optind) != 1) {
  1397. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1398. X        fprintf(stderr, "Usage: %s [-t tracks] [-h heads] [-s sectors] [-l label] device\n", argv[0]);
  1399. X        exit(1);
  1400. X    }
  1401. X
  1402. X    drive = argv[argc -1][0];
  1403. X    if (islower(drive))
  1404. X        drive = toupper(drive);
  1405. X
  1406. X                    /* check out the drive letter */
  1407. X    dev = devices;
  1408. X    while (dev->drive) {
  1409. X        if (dev->drive == drive)
  1410. X            break;
  1411. X        dev++;
  1412. X    }
  1413. X    if (!dev->drive) {
  1414. X        fprintf(stderr, "Drive '%c:' not supported\n", drive);
  1415. X        exit(1);
  1416. X    }
  1417. X    if (dev->tracks == 0) {
  1418. X        fprintf(stderr, "Non-removable media is not supported\n");
  1419. X        exit(1);
  1420. X    }
  1421. X                    /* find the right one */
  1422. X    if (!dev->gioctl) {
  1423. X        while (dev->drive == drive) {
  1424. X            if ((!tracks || dev->tracks == tracks) && (!heads || dev->heads == heads) && (!sectors || dev->sectors == sectors))
  1425. X                break;
  1426. X            dev++;
  1427. X        }
  1428. X    }
  1429. X    if (dev->drive != drive) {
  1430. X        fprintf(stderr, "%s: Paramaters not supported\n", argv[0]);
  1431. X        exit(1);
  1432. X    }
  1433. X                    /* open the device */
  1434. X    name = expand(dev->name);
  1435. X    if ((fd = open(name, 2 | dev->mode)) < 0) {
  1436. X        perror("init: open");
  1437. X        exit(1);
  1438. X    }
  1439. X                    /* fill in the blanks */
  1440. X    if (!tracks)
  1441. X        tracks = dev->tracks;
  1442. X    if (!heads)
  1443. X        heads = dev->heads;
  1444. X    if (!sectors)
  1445. X        sectors = dev->sectors;
  1446. X
  1447. X                    /* set parameters, if needed */
  1448. X    if (dev->gioctl) {
  1449. X        if ((*(dev->gioctl)) (fd, tracks, heads, sectors))
  1450. X            exit(1);
  1451. X    }
  1452. X                    /* do a "test" read */
  1453. X    if (read(fd, (char *) buf, MSECTOR_SIZE) != MSECTOR_SIZE) {
  1454. X        fprintf(stderr, "%s: Error reading from '%s', wrong parameters?\n", argv[0], name);
  1455. X        exit(1);
  1456. X    }
  1457. X                    /* get the parameters */
  1458. X    tot_sectors = tracks * heads * sectors;
  1459. X    switch (tot_sectors) {
  1460. X        case 320:        /* 40t * 1h * 8s = 160k */
  1461. X            media = 0xfe;
  1462. X            clus_size = 1;
  1463. X            dir_len = 4;
  1464. X            fat_len = 1;
  1465. X            break;
  1466. X        case 360:        /* 40t * 1h * 9s = 180k */
  1467. X            media = 0xfc;
  1468. X            clus_size = 1;
  1469. X            dir_len = 4;
  1470. X            fat_len = 2;
  1471. X            break;
  1472. X        case 640:        /* 40t * 2h * 8s = 320k */
  1473. X            media = 0xff;
  1474. X            clus_size = 2;
  1475. X            dir_len = 7;
  1476. X            fat_len = 1;
  1477. X            break;
  1478. X        case 720:        /* 40t * 2h * 9s = 360k */
  1479. X            media = 0xfd;
  1480. X            clus_size = 2;
  1481. X            dir_len = 7;
  1482. X            fat_len = 2;
  1483. X            break;
  1484. X        case 1440:        /* 80t * 2h * 9s = 720k */
  1485. X            media = 0xf9;
  1486. X            clus_size = 2;
  1487. X            dir_len = 7;
  1488. X            fat_len = 3;
  1489. X            break;
  1490. X        case 2400:        /* 80t * 2h * 15s = 1.2m */
  1491. X            media = 0xf9;
  1492. X            clus_size = 1;
  1493. X            dir_len = 14;
  1494. X            fat_len = 7;
  1495. X            break;
  1496. X        case 2880:        /* 80t * 2h * 18s = 1.44m */
  1497. X            media = 0xf0;
  1498. X            clus_size = 1;
  1499. X            dir_len = 14;
  1500. X            fat_len = 9;
  1501. X            break;
  1502. X        default:        /* a non-standard format */
  1503. X            media = 0xf0;
  1504. X            if (heads == 1)
  1505. X                clus_size = 1;
  1506. X            else
  1507. X                clus_size = (tot_sectors > 2000) ? 1 : 2;
  1508. X            if (heads == 1)
  1509. X                dir_len = 4;
  1510. X            else
  1511. X                dir_len = (tot_sectors > 2000) ? 14 : 7;
  1512. X            /*
  1513. X             * Estimate the fat length, then figure it out.  The
  1514. X             * 341 is the number of 12 bit fat entries in a sector.
  1515. X             */
  1516. X            fat_guess = ((tot_sectors / clus_size) / 341.0) + 0.95;
  1517. X            num_clus = (tot_sectors -dir_len - (2 * fat_guess) -1) / clus_size;
  1518. X            fat_len = (num_clus / 341.0) + 1;
  1519. X            break;
  1520. X    }
  1521. X                    /* the boot sector */
  1522. X    memset((char *) &boot, '\0', MSECTOR_SIZE);
  1523. X    boot.jump[0] = 0xeb;
  1524. X    boot.jump[1] = 0x44;
  1525. X    boot.jump[2] = 0x90;
  1526. X    strncpy((char *) boot.banner, "Mtools  ", 8);
  1527. X    boot.secsiz[0] = 512 % 0x100;
  1528. X    boot.secsiz[1] = 512 / 0x100;
  1529. X    boot.clsiz = (unsigned char) clus_size;
  1530. X    boot.nrsvsect[0] = 1;
  1531. X    boot.nrsvsect[1] = 0;
  1532. X    boot.nfat = 2;
  1533. X    boot.dirents[0] = (dir_len * 16) % 0x100;
  1534. X    boot.dirents[1] = (dir_len * 16) / 0x100;
  1535. X    boot.psect[0] = tot_sectors % 0x100;
  1536. X    boot.psect[1] = tot_sectors / 0x100;
  1537. X    boot.descr = media;
  1538. X    boot.fatlen[0] = fat_len % 0x100;
  1539. X    boot.fatlen[1] = fat_len / 0x100;
  1540. X    boot.nsect[0] = sectors % 0x100;
  1541. X    boot.nsect[1] = sectors / 0x100;
  1542. X    boot.nheads[0] = heads % 0x100;
  1543. X    boot.nheads[1] = heads / 0x100;
  1544. X
  1545. X                    /* write the boot */
  1546. X    lseek(fd, 0L, 0);
  1547. X    write(fd, (char *) &boot, MSECTOR_SIZE);
  1548. X                    /* first fat */
  1549. X    memset((char *) buf, '\0', MSECTOR_SIZE);
  1550. X    buf[0] = media;
  1551. X    buf[1] = 0xff;
  1552. X    buf[2] = 0xff;
  1553. X    write(fd, (char *) buf, MSECTOR_SIZE);
  1554. X    memset((char *) buf, '\0', MSECTOR_SIZE);
  1555. X    for (i = 1; i < fat_len; i++)
  1556. X        write(fd, (char *) buf, MSECTOR_SIZE);
  1557. X                    /* second fat */
  1558. X    buf[0] = media;
  1559. X    buf[1] = 0xff;
  1560. X    buf[2] = 0xff;
  1561. X    write(fd, (char *) buf, MSECTOR_SIZE);
  1562. X    memset((char *) buf, '\0', MSECTOR_SIZE);
  1563. X    for (i = 1; i < fat_len; i++)
  1564. X        write(fd, (char *) buf, MSECTOR_SIZE);
  1565. X                    /* the root directory */
  1566. X    if (label[0] != '\0') {
  1567. X        time(&now);
  1568. X        dir = mk_entry(label, 0x08, 0, 0L, now);
  1569. X        memcpy((char *) buf, (char *) dir, MDIR_SIZE);
  1570. X    }
  1571. X    write(fd, (char *) buf, MSECTOR_SIZE);
  1572. X    memset((char *) buf, '\0', MSECTOR_SIZE);
  1573. X    for (i = 1; i < dir_len; i++)
  1574. X        write(fd, (char *) buf, MSECTOR_SIZE);
  1575. X    close(fd);
  1576. X    exit(0);
  1577. X}
  1578. X
  1579. X/* hooks for the missing parts */
  1580. Xvoid disk_write() {}
  1581. END_OF_FILE
  1582. if test 6144 -ne `wc -c <'mtools/mformat.c'`; then
  1583.     echo shar: \"'mtools/mformat.c'\" unpacked with wrong size!
  1584. fi
  1585. # end of 'mtools/mformat.c'
  1586. fi
  1587. if test -f 'mtools/mlabel.c' -a "${1}" != "-c" ; then 
  1588.   echo shar: Will not clobber existing file \"'mtools/mlabel.c'\"
  1589. else
  1590. echo shar: Extracting \"'mtools/mlabel.c'\" \(3079 characters\)
  1591. sed "s/^X//" >'mtools/mlabel.c' <<'END_OF_FILE'
  1592. X/*
  1593. X * Make an MSDOS volume label
  1594. X *
  1595. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  1596. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  1597. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  1598. X *                     Environmental Management Office
  1599. X *                     Fort Hood, TX 76544-5057
  1600. X */
  1601. X
  1602. X#include <stdio.h>
  1603. X#include <ctype.h>
  1604. X#include "msdos.h"
  1605. X#include "patchlevel.h"
  1606. X
  1607. Xint fd = -1;                /* the file descriptor for the device */
  1608. Xint dir_start;                /* starting sector for directory */
  1609. Xint dir_len;                /* length of directory (in sectors) */
  1610. Xint dir_entries;            /* number of directory entries */
  1611. Xint clus_size;                /* cluster size (in sectors) */
  1612. Xchar *mcwd;                /* the Current Working Directory */
  1613. Xint fat_error;                /* FAT error detected? */
  1614. X
  1615. Xmain(argc, argv)
  1616. Xint argc;
  1617. Xchar *argv[];
  1618. X{
  1619. X    int entry, slot, fargn, verbose, oops;
  1620. X    char filename[30], *strncpy(), drive, ans[10], *strncat();
  1621. X    char *strcpy(), *fix_mcwd();
  1622. X    unsigned char fixed[12], vol[12];
  1623. X    void exit(), dir_write(), dir_flush(), disk_flush();
  1624. X    struct directory *dir, *dir_read(), *mk_entry();
  1625. X    long time(), now;
  1626. X
  1627. X    fargn = 1;
  1628. X    verbose = 0;
  1629. X    oops = 0;
  1630. X    if (argc > 1) {
  1631. X        if (!strcmp(argv[1], "-v")) {
  1632. X            fargn = 2;
  1633. X            verbose = 1;
  1634. X        }
  1635. X        if (argv[1][0] == '-' && !verbose)
  1636. X            oops++;
  1637. X    }
  1638. X    if (argc < 2 || argv[fargn][1] != ':' || oops) {
  1639. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1640. X        fprintf(stderr, "Usage: %s [-v] drive:\n", argv[0]);
  1641. X        exit(1);
  1642. X    }
  1643. X    mcwd = fix_mcwd();
  1644. X
  1645. X    drive = argv[fargn][0];
  1646. X    if (islower(drive))
  1647. X        drive = toupper(drive);
  1648. X
  1649. X    if (init(drive, 2)) {
  1650. X        fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  1651. X        exit(1);
  1652. X    }
  1653. X                    /* see if a label exists and get slot */
  1654. X    slot = -1;
  1655. X    vol[0] = '\0';
  1656. X    for (entry = 0; entry < dir_entries; entry++) {
  1657. X        dir = dir_read(entry);
  1658. X                    /* if empty */
  1659. X        if (dir->name[0] == 0x0) {
  1660. X            if (slot < 0)
  1661. X                slot = entry;
  1662. X            break;
  1663. X        }
  1664. X                    /* if erased */
  1665. X        if (dir->name[0] == 0xe5) {
  1666. X            if (slot < 0)
  1667. X                slot = entry;
  1668. X            continue;
  1669. X        }
  1670. X                    /* if not a volume label */
  1671. X        if (!(dir->attr & 0x08))
  1672. X            continue;
  1673. X
  1674. X        slot = entry;
  1675. X        strncpy((char *) vol, (char *) dir->name, 8);
  1676. X        vol[8] = '\0';
  1677. X        strncat((char *) vol, (char *) dir->ext, 3);
  1678. X        vol[11] = '\0';
  1679. X
  1680. X        printf("Volume in drive %c is \"%s\"\n", drive, vol);
  1681. X        break;
  1682. X    }
  1683. X    if (slot < 0) {
  1684. X        fprintf(stderr, "%s: No directory slots\n", argv[0]);
  1685. X        exit(1);
  1686. X    }
  1687. X    if (vol[0] == '\0')
  1688. X        printf("Volume in drive %c is unlabeled\n", drive);
  1689. X
  1690. X                    /* ask for new label */
  1691. X    printf("Enter the new volume label (11 characters): ");
  1692. X    gets(filename);
  1693. X    if (filename[0] != '\0') {
  1694. X        sprintf((char *) fixed, "%-11.11s", filename);
  1695. X        if (strlen(filename) > 11 && verbose)
  1696. X            printf("New label is \"%s\"\n", fixed);
  1697. X    }
  1698. X    else {
  1699. X        if (vol[0] == '\0')
  1700. X            exit(0);
  1701. X
  1702. X        printf("Delete volume label (y/n): ");
  1703. X        gets(ans);
  1704. X        if (ans[0] == 'y' || ans[0] == 'Y') {
  1705. X            strcpy((char *) fixed, (char *) vol);
  1706. X            fixed[0] = 0xe5;
  1707. X        }
  1708. X        else
  1709. X            exit(0);
  1710. X    }
  1711. X                    /* make directory entry */
  1712. X    time(&now);
  1713. X    dir = mk_entry(fixed, 0x08, 0, 0L, now);
  1714. X    dir_write(slot, dir);
  1715. X
  1716. X    dir_flush();
  1717. X    disk_flush();
  1718. X    close(fd);
  1719. X    exit(0);
  1720. X}
  1721. END_OF_FILE
  1722. if test 3079 -ne `wc -c <'mtools/mlabel.c'`; then
  1723.     echo shar: \"'mtools/mlabel.c'\" unpacked with wrong size!
  1724. fi
  1725. # end of 'mtools/mlabel.c'
  1726. fi
  1727. if test -f 'mtools/mmd.c' -a "${1}" != "-c" ; then 
  1728.   echo shar: Will not clobber existing file \"'mtools/mmd.c'\"
  1729. else
  1730. echo shar: Extracting \"'mtools/mmd.c'\" \(5127 characters\)
  1731. sed "s/^X//" >'mtools/mmd.c' <<'END_OF_FILE'
  1732. X/*
  1733. X * Make an MSDOS subdirectory
  1734. X *
  1735. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  1736. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  1737. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  1738. X *                     Environmental Management Office
  1739. X *                     Fort Hood, TX 76544-5057
  1740. X */
  1741. X
  1742. X#include <stdio.h>
  1743. X#include <signal.h>
  1744. X#include "msdos.h"
  1745. X#include "patchlevel.h"
  1746. X
  1747. Xint fd = -1;                /* the file descriptor for the device */
  1748. Xint dir_start;                /* starting sector for directory */
  1749. Xint dir_len;                /* length of directory (in sectors) */
  1750. Xint dir_entries;            /* number of directory entries */
  1751. Xint clus_size;                /* cluster size (in sectors) */
  1752. Xchar *mcwd;                /* the Current Working Directory */
  1753. Xint fat_error;                /* FAT error detected? */
  1754. X
  1755. Xstatic int got_signal();
  1756. Xstatic void empty_dir();
  1757. X
  1758. Xmain(argc, argv)
  1759. Xint argc;
  1760. Xchar *argv[];
  1761. X{
  1762. X    int i, entry, slot, fargn, verbose, oops;
  1763. X    extern unsigned int end_fat;
  1764. X    unsigned int fat, dot, next_fat();
  1765. X    char filename[13], *newfile, drive, get_drive(), *get_path();
  1766. X    char *strcpy(), *fix_mcwd(), *pathname, *unix_name(), last_drive;
  1767. X    unsigned char *fixed, *dos_name();
  1768. X    void exit(), fat_write(), dir_write(), disk_flush(), dir_flush();
  1769. X    struct directory *dir, *dir_read(), *mk_entry();
  1770. X    long time(), now;
  1771. X                    /* catch signals */
  1772. X    signal(SIGINT, (SIG_TYPE(*) ()) got_signal);
  1773. X    signal(SIGTERM, (SIG_TYPE(*) ()) got_signal);
  1774. X    signal(SIGQUIT, (SIG_TYPE(*) ()) got_signal);
  1775. X
  1776. X    fargn = 1;
  1777. X    verbose = 0;
  1778. X    oops = 0;
  1779. X    if (argc > 1) {
  1780. X        if (!strcmp(argv[1], "-v")) {
  1781. X            fargn = 2;
  1782. X            verbose = 1;
  1783. X        }
  1784. X        if (argv[1][0] == '-' && !verbose)
  1785. X            oops++;
  1786. X    }
  1787. X    if (argc == 1 || oops) {
  1788. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1789. X        fprintf(stderr, "Usage: %s [-v] msdosdirectory [msdosdirectories...]\n", argv[0]);
  1790. X        exit(1);
  1791. X    }
  1792. X    last_drive = 'x';
  1793. X    mcwd = fix_mcwd();
  1794. X
  1795. X    for (i = fargn; i < argc; i++) {
  1796. X        drive = get_drive(argv[i]);
  1797. X        if (drive != last_drive) {
  1798. X            if (last_drive != 'x') {
  1799. X                fat_write();
  1800. X                dir_flush();
  1801. X                disk_flush();
  1802. X            }
  1803. X
  1804. X            if (init(drive, 2)) {
  1805. X                fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  1806. X                continue;
  1807. X            }
  1808. X            last_drive = drive;
  1809. X        }
  1810. X                    /* serves the get_name() function too */
  1811. X        fixed = dos_name(argv[i], verbose);
  1812. X
  1813. X        strcpy(filename, unix_name(fixed, fixed + 8));
  1814. X        pathname = get_path(argv[i]);
  1815. X
  1816. X        if (subdir(drive, pathname))
  1817. X            continue;
  1818. X                    /* see if exists and get slot */
  1819. X        slot = -1;
  1820. X        dot = 0;
  1821. X        oops = 0;
  1822. X        for (entry = 0; entry < dir_entries; entry++) {
  1823. X            dir = dir_read(entry);
  1824. X
  1825. X                    /* if empty */
  1826. X            if (dir->name[0] == 0x0) {
  1827. X                if (slot < 0)
  1828. X                    slot = entry;
  1829. X                break;
  1830. X            }
  1831. X                    /* if erased */
  1832. X            if (dir->name[0] == 0xe5) {
  1833. X                if (slot < 0)
  1834. X                    slot = entry;
  1835. X                continue;
  1836. X            }
  1837. X                    /* if not a directory */
  1838. X            if (!(dir->attr & 0x10))
  1839. X                continue;
  1840. X
  1841. X            newfile = unix_name(dir->name, dir->ext);
  1842. X                    /* save the 'dot' directory info */
  1843. X            if (!strcmp(".", newfile))
  1844. X                dot = dir->start[1] * 0x100 + dir->start[0];
  1845. X
  1846. X            if (!strcmp(filename, newfile)) {
  1847. X                fprintf(stderr, "%s: Directory \"%s\" already exists\n", argv[0], filename);
  1848. X                oops++;
  1849. X                break;
  1850. X            }
  1851. X        }
  1852. X        if (oops)
  1853. X            continue;
  1854. X                    /* no '.' entry means root directory */
  1855. X        if (dot == 0 && slot < 0) {
  1856. X            fprintf(stderr, "%s: No directory slots\n", argv[0]);
  1857. X            continue;
  1858. X        }
  1859. X                    /* make the directory grow */
  1860. X        if (dot && slot < 0) {
  1861. X            if (dir_grow(dot)) {
  1862. X                fprintf(stderr, "%s: Disk full\n", argv[0]);
  1863. X                continue;
  1864. X            }
  1865. X                    /* first slot in the new part */
  1866. X            slot = entry;
  1867. X        }
  1868. X                    /* grab a starting cluster */
  1869. X        if ((fat = next_fat(0)) == 1) {
  1870. X            fprintf(stderr, "%s: Disk full\n", argv[0]);
  1871. X            continue;
  1872. X        }
  1873. X                    /* make directory entry */
  1874. X        time(&now);
  1875. X        dir = mk_entry(fixed, 0x10, fat, 0L, now);
  1876. X        dir_write(slot, dir);
  1877. X                    /* write the cluster */
  1878. X        empty_dir(fat, dot);
  1879. X        fat_encode(fat, end_fat);
  1880. X    }
  1881. X                    /* write the FAT, flush the buffers */
  1882. X    fat_write();
  1883. X    dir_flush();
  1884. X    disk_flush();
  1885. X    close(fd);
  1886. X    exit(0);
  1887. X}
  1888. X
  1889. X/*
  1890. X * Write an empty directory 'template' to the cluster starting at 'dot'.
  1891. X */
  1892. X
  1893. Xstatic void
  1894. Xempty_dir(dot, dot_dot)
  1895. Xunsigned int dot, dot_dot;
  1896. X{
  1897. X    int buflen;
  1898. X    long start;
  1899. X    char buf[MAX_CLUSTER], *memcpy(), *memset();
  1900. X    struct directory *dir, *mk_entry();
  1901. X    void disk_write();
  1902. X    long time(), now;
  1903. X
  1904. X    start = (long) (dot - 2) * clus_size + dir_start + dir_len;
  1905. X
  1906. X    buflen = clus_size * MSECTOR_SIZE;
  1907. X    time(&now);
  1908. X                    /* make the '.' and '..' entries */
  1909. X    dir = mk_entry((unsigned char *) ".          ", 0x10, dot, 0L, now);
  1910. X    memcpy(&buf[0], (char *) dir, MDIR_SIZE);
  1911. X    dir = mk_entry((unsigned char *) "..         ", 0x10, dot_dot, 0L, now);
  1912. X    memcpy(&buf[MDIR_SIZE], (char *) dir, MDIR_SIZE);
  1913. X
  1914. X                    /* zero the remainder */
  1915. X    memset(&buf[2 * MDIR_SIZE], '\0', buflen - (2 * MDIR_SIZE));
  1916. X
  1917. X                    /* write the cluster */
  1918. X    disk_write(start, (unsigned char *) buf, buflen);
  1919. X    return;
  1920. X}
  1921. X
  1922. X/*
  1923. X * Do a graceful exit if the program is interrupted.  This will reduce
  1924. X * (but not eliminate) the risk of generating a corrupted disk on
  1925. X * a user abort.
  1926. X */
  1927. X
  1928. Xstatic int
  1929. Xgot_signal()
  1930. X{
  1931. X    void exit(), disk_flush(), fat_write(), dir_flush();
  1932. X
  1933. X    if (fd < 0)
  1934. X        exit(1);
  1935. X    fat_write();
  1936. X    dir_flush();
  1937. X    disk_flush();
  1938. X    close(fd);
  1939. X    exit(1);
  1940. X}
  1941. END_OF_FILE
  1942. if test 5127 -ne `wc -c <'mtools/mmd.c'`; then
  1943.     echo shar: \"'mtools/mmd.c'\" unpacked with wrong size!
  1944. fi
  1945. # end of 'mtools/mmd.c'
  1946. fi
  1947. if test -f 'mtools/mrd.c' -a "${1}" != "-c" ; then 
  1948.   echo shar: Will not clobber existing file \"'mtools/mrd.c'\"
  1949. else
  1950. echo shar: Extracting \"'mtools/mrd.c'\" \(4300 characters\)
  1951. sed "s/^X//" >'mtools/mrd.c' <<'END_OF_FILE'
  1952. X/*
  1953. X * Delete an MSDOS subdirectory
  1954. X *
  1955. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  1956. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  1957. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  1958. X *                     Environmental Management Office
  1959. X *                     Fort Hood, TX 76544-5057
  1960. X */
  1961. X
  1962. X#include <stdio.h>
  1963. X#include <signal.h>
  1964. X#include "msdos.h"
  1965. X#include "patchlevel.h"
  1966. X
  1967. Xint fd = -1;                /* the file descriptor for the device */
  1968. Xint dir_start;                /* starting sector for directory */
  1969. Xint dir_len;                /* length of directory (in sectors) */
  1970. Xint dir_entries;            /* number of directory entries */
  1971. Xint clus_size;                /* cluster size (in sectors) */
  1972. Xchar *mcwd;                /* the Current Working Directory */
  1973. Xint fat_error;                /* FAT error detected? */
  1974. X
  1975. Xstatic int got_signal(), is_empty();
  1976. X
  1977. Xmain(argc, argv)
  1978. Xint argc;
  1979. Xchar *argv[];
  1980. X{
  1981. X    int i, ismatch, entry, oops, empty;
  1982. X    unsigned int start;
  1983. X    char *filename, *newfile, *get_name(), drive, *pathname, *get_path();
  1984. X    char *unix_name(), get_drive(), last_drive, *fix_mcwd();
  1985. X    void exit(), fat_write(), dir_write(), disk_flush(), dir_flush();
  1986. X    struct directory *dir, *dir_read();
  1987. X                    /* catch signals */
  1988. X    signal(SIGINT, (SIG_TYPE(*) ()) got_signal);
  1989. X    signal(SIGTERM, (SIG_TYPE(*) ()) got_signal);
  1990. X    signal(SIGQUIT, (SIG_TYPE(*) ()) got_signal);
  1991. X
  1992. X    if (argc == 1) {
  1993. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  1994. X        fprintf(stderr, "Usage: %s mdsosdirectory [msdosdirectories...]\n", argv[0]);
  1995. X        exit(1);
  1996. X    }
  1997. X
  1998. X    last_drive = 'x';
  1999. X    mcwd = fix_mcwd();
  2000. X
  2001. X    for (i = 1; i < argc; i++) {
  2002. X        drive = get_drive(argv[i]);
  2003. X        if (drive != last_drive) {
  2004. X            if (last_drive != 'x') {
  2005. X                fat_write();
  2006. X                dir_flush();
  2007. X                disk_flush();
  2008. X            }
  2009. X
  2010. X            if (init(drive, 2)) {
  2011. X                fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  2012. X                continue;
  2013. X            }
  2014. X            last_drive = drive;
  2015. X        }
  2016. X
  2017. X        filename = get_name(argv[i]);
  2018. X        pathname = get_path(argv[i]);
  2019. X        if (subdir(drive, pathname))
  2020. X            continue;
  2021. X
  2022. X        oops = 0;
  2023. X        ismatch = 0;
  2024. X        for (entry = 0; entry < dir_entries; entry++) {
  2025. X            dir = dir_read(entry);
  2026. X                    /* if empty */
  2027. X            if (dir->name[0] == 0x0)
  2028. X                break;
  2029. X                    /* if erased */
  2030. X            if (dir->name[0] == 0xe5)
  2031. X                continue;
  2032. X                    /* if not dir */
  2033. X            if (!(dir->attr & 0x10))
  2034. X                continue;
  2035. X
  2036. X            newfile = unix_name(dir->name, dir->ext);
  2037. X            if (match(newfile, filename)) {
  2038. X                start = dir->start[1] * 0x100 + dir->start[0];
  2039. X                if ((empty = is_empty(start)) < 0)
  2040. X                    break;
  2041. X                if (!empty) {
  2042. X                    fprintf(stderr, "%s: Directory \"%s\" is not empty\n", argv[0], filename);
  2043. X                    oops++;
  2044. X                    break;
  2045. X                }
  2046. X                if (!start) {
  2047. X                    fprintf(stderr, "%s: Can't remove root directory\n", argv[0]);
  2048. X                    oops++;
  2049. X                    break;
  2050. X                }
  2051. X                if (fat_free(start))
  2052. X                    break;
  2053. X                dir->name[0] = 0xe5;
  2054. X                dir_write(entry, dir);
  2055. X                ismatch = 1;
  2056. X            }
  2057. X        }
  2058. X        if (fat_error)
  2059. X            break;
  2060. X
  2061. X        if (oops)
  2062. X            continue;
  2063. X
  2064. X        if (!ismatch)
  2065. X            fprintf(stderr, "%s: Directory \"%s\" not found\n", argv[0], filename);
  2066. X    }
  2067. X                    /* write the FAT, flush the buffers */
  2068. X    fat_write();
  2069. X    dir_flush();
  2070. X    disk_flush();
  2071. X    close(fd);
  2072. X    exit(0);
  2073. X}
  2074. X
  2075. X/*
  2076. X * See if directory is empty.  Returns 1 if empty, 0 if not, and -1 on error.
  2077. X * Can't use subdir() and dir_read() as it would clobber the globals.
  2078. X */
  2079. X
  2080. Xstatic int
  2081. Xis_empty(fat)
  2082. Xunsigned int fat;
  2083. X{
  2084. X    register int i;
  2085. X    int next, buflen;
  2086. X    long sector;
  2087. X    extern unsigned int last_fat, fat_decode();
  2088. X    unsigned char tbuf[MAX_CLUSTER];
  2089. X    void disk_read();
  2090. X
  2091. X    /* CONSTCOND */
  2092. X    while (1) {
  2093. X        sector = (long) (fat - 2) * clus_size + dir_start + dir_len;
  2094. X        buflen = clus_size * MSECTOR_SIZE;
  2095. X        disk_read(sector, tbuf, buflen);
  2096. X
  2097. X                    /* check first character of name */
  2098. X        for (i = 0; i < MSECTOR_SIZE; i += MDIR_SIZE) {
  2099. X            if (tbuf[i] == '.')
  2100. X                continue;
  2101. X            if (tbuf[i] != 0x0 && tbuf[i] != 0xe5)
  2102. X                return(0);
  2103. X        }
  2104. X                    /* get next cluster number */
  2105. X        next = fat_decode(fat);
  2106. X        if (next == 1) {
  2107. X            fprintf(stderr, "is_empty: FAT problem\n");
  2108. X            return(-1);
  2109. X        }
  2110. X                    /* end of cluster chain */
  2111. X        if (next >= last_fat)
  2112. X            break;
  2113. X        fat = next;
  2114. X    }
  2115. X    return(1);
  2116. X}
  2117. X
  2118. X/*
  2119. X * Do a graceful exit if the program is interrupted.  This will reduce
  2120. X * (but not eliminate) the risk of generating a corrupted disk on
  2121. X * a user abort.
  2122. X */
  2123. X
  2124. Xstatic int
  2125. Xgot_signal()
  2126. X{
  2127. X    void exit(), disk_flush(), fat_write(), dir_flush();
  2128. X
  2129. X    if (fd < 0)
  2130. X        exit(1);
  2131. X    fat_write();
  2132. X    dir_flush();
  2133. X    disk_flush();
  2134. X    close(fd);
  2135. X    exit(1);
  2136. X}
  2137. END_OF_FILE
  2138. if test 4300 -ne `wc -c <'mtools/mrd.c'`; then
  2139.     echo shar: \"'mtools/mrd.c'\" unpacked with wrong size!
  2140. fi
  2141. # end of 'mtools/mrd.c'
  2142. fi
  2143. if test -f 'mtools/mren.c' -a "${1}" != "-c" ; then 
  2144.   echo shar: Will not clobber existing file \"'mtools/mren.c'\"
  2145. else
  2146. echo shar: Extracting \"'mtools/mren.c'\" \(3089 characters\)
  2147. sed "s/^X//" >'mtools/mren.c' <<'END_OF_FILE'
  2148. X/*
  2149. X * Rename an existing MSDOS file
  2150. X *
  2151. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  2152. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  2153. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  2154. X *                     Environmental Management Office
  2155. X *                     Fort Hood, TX 76544-5057
  2156. X */
  2157. X
  2158. X#include <stdio.h>
  2159. X#include "msdos.h"
  2160. X#include "patchlevel.h"
  2161. X
  2162. Xint fd = -1;                /* the file descriptor for the device */
  2163. Xint dir_start;                /* starting sector for directory */
  2164. Xint dir_len;                /* length of directory (in sectors) */
  2165. Xint dir_entries;            /* number of directory entries */
  2166. Xint clus_size;                /* cluster size (in sectors) */
  2167. Xchar *mcwd;                /* the Current Working Directory */
  2168. Xint fat_error;                /* FAT error detected? */
  2169. X
  2170. Xmain(argc, argv)
  2171. Xint argc;
  2172. Xchar *argv[];
  2173. X{
  2174. X    int entry, ismatch, nogo, fargn, verbose;
  2175. X    char filename[13], *newfile, *strncpy(), *unix_name();
  2176. X    char new[13], ans[10], *temp, *strcpy(), drive, get_drive();
  2177. X    char *get_path(), *pathname, *get_name(), *fix_mcwd();
  2178. X    unsigned char *target, *dos_name();
  2179. X    void exit(), dir_write(), disk_flush(), dir_flush();
  2180. X    struct directory *dir, *dir_read();
  2181. X
  2182. X    fargn = 1;
  2183. X    verbose = 0;
  2184. X    if (argc > 1) {
  2185. X        if (!strcmp(argv[1], "-v")) {
  2186. X            fargn = 2;
  2187. X            verbose = 1;
  2188. X        }
  2189. X    }
  2190. X    if (argc != fargn + 2) {
  2191. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  2192. X        fprintf(stderr, "Usage: %s [-v] sourcefile targetfile\n", argv[0]);
  2193. X        exit(1);
  2194. X    }
  2195. X    mcwd = fix_mcwd();
  2196. X
  2197. X    drive = get_drive(argv[1]);
  2198. X    if (init(drive, 2)) {
  2199. X        fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  2200. X        exit(1);
  2201. X    }
  2202. X    strcpy(filename, get_name(argv[fargn]));
  2203. X    pathname = get_path(argv[fargn]);
  2204. X    if (subdir(drive, pathname))
  2205. X        exit(1);
  2206. X
  2207. X    temp = get_name(argv[fargn + 1]);
  2208. X    target = dos_name(argv[fargn + 1], verbose);
  2209. X
  2210. X    strcpy(new, unix_name(target, target + 8));
  2211. X    nogo = 0;
  2212. X                    /* the name supplied may be altered */
  2213. X    if (strcmp(temp, new) && verbose) {
  2214. X        while (!nogo) {
  2215. X            printf("Do you accept \"%s\" as the new filename (y/n) ? ", new);
  2216. X            gets(ans);
  2217. X            if (ans[0] == 'y' || ans[0] == 'Y')
  2218. X                break;
  2219. X            if (ans[0] == 'n' || ans[0] == 'N')
  2220. X                nogo = 1;
  2221. X        }
  2222. X    }
  2223. X    if (nogo)
  2224. X        exit(0);
  2225. X                    /* see if exists and do it */
  2226. X    ismatch = 0;
  2227. X    for (entry = 0; entry < dir_entries; entry++) {
  2228. X        dir = dir_read(entry);
  2229. X                    /* if empty */
  2230. X        if (dir->name[0] == 0x0)
  2231. X            break;
  2232. X                    /* if erased */
  2233. X        if (dir->name[0] == 0xe5)
  2234. X            continue;
  2235. X                    /* if volume label */
  2236. X        if ((dir->attr & 0x08))
  2237. X            continue;
  2238. X                    /* you may rename a directory */
  2239. X        newfile = unix_name(dir->name, dir->ext);
  2240. X
  2241. X                    /* if the new name already exists */
  2242. X        if (!strcmp(new, newfile)) {
  2243. X            fprintf(stderr, "%s: File \"%s\" already exists\n", argv[0], new);
  2244. X            exit(1);
  2245. X        }
  2246. X                    /* if the old name exists */
  2247. X        if (match(newfile, filename)) {
  2248. X            ismatch = 1;
  2249. X            break;
  2250. X        }
  2251. X    }
  2252. X    if (!ismatch) {
  2253. X        fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  2254. X        exit(1);
  2255. X    }
  2256. X                    /* so go ahead and do it */
  2257. X    strncpy((char *) dir->name, (char *) target, 8);
  2258. X    strncpy((char *) dir->ext, (char *) target + 8, 3);
  2259. X    dir_write(entry, dir);
  2260. X
  2261. X    dir_flush();
  2262. X    disk_flush();
  2263. X    close(fd);
  2264. X    exit(0);
  2265. X}
  2266. END_OF_FILE
  2267. if test 3089 -ne `wc -c <'mtools/mren.c'`; then
  2268.     echo shar: \"'mtools/mren.c'\" unpacked with wrong size!
  2269. fi
  2270. # end of 'mtools/mren.c'
  2271. fi
  2272. if test -f 'mtools/mtype.c' -a "${1}" != "-c" ; then 
  2273.   echo shar: Will not clobber existing file \"'mtools/mtype.c'\"
  2274. else
  2275. echo shar: Extracting \"'mtools/mtype.c'\" \(2931 characters\)
  2276. sed "s/^X//" >'mtools/mtype.c' <<'END_OF_FILE'
  2277. X/*
  2278. X * Display contents of an MSDOS file
  2279. X *
  2280. X * Emmet P. Gray            US Army, HQ III Corps & Fort Hood
  2281. X * ...!uunet!uiucuxc!fthood!egray    Attn: AFZF-DE-ENV
  2282. X * fthood!egray@uxc.cso.uiuc.edu    Directorate of Engineering & Housing
  2283. X *                     Environmental Management Office
  2284. X *                     Fort Hood, TX 76544-5057
  2285. X */
  2286. X
  2287. X#include <stdio.h>
  2288. X#include "msdos.h"
  2289. X#include "patchlevel.h"
  2290. X
  2291. Xint fd = -1;                /* the file descriptor for the device */
  2292. Xint dir_start;                /* starting sector for directory */
  2293. Xint dir_len;                /* length of directory (in sectors) */
  2294. Xint dir_entries;            /* number of directory entries */
  2295. Xint clus_size;                /* cluster size (in sectors) */
  2296. Xchar *mcwd;                /* the Current Working Directory */
  2297. Xint fat_error;                /* FAT error detected? */
  2298. X
  2299. Xmain(argc, argv)
  2300. Xint argc;
  2301. Xchar *argv[];
  2302. X{
  2303. X    extern int optind;
  2304. X    extern char *optarg;
  2305. X    int i, ismatch, entry, c, oops, textmode, stripmode;
  2306. X    unsigned int fat;
  2307. X    long size;
  2308. X    char *filename, *newfile, *get_name(), *unix_name(), *pathname;
  2309. X    char *get_path(), drive, get_drive(), last_drive, *fix_mcwd();
  2310. X    void exit();
  2311. X    struct directory *dir, *dir_read();
  2312. X
  2313. X                    /* get command line options */
  2314. X    oops = 0;
  2315. X    stripmode = 0;
  2316. X    textmode = 0;
  2317. X    while ((c = getopt(argc, argv, "st")) != EOF) {
  2318. X        switch (c) {
  2319. X            case 's':
  2320. X                stripmode = 1;
  2321. X                break;
  2322. X            case 't':
  2323. X                textmode = 1;
  2324. X                break;
  2325. X            default:
  2326. X                oops = 1;
  2327. X                break;
  2328. X        }
  2329. X    }
  2330. X
  2331. X    if (oops || (argc - optind) < 1) {
  2332. X        fprintf(stderr, "Mtools version %s, dated %s\n", VERSION, DATE);
  2333. X        fprintf(stderr, "Usage: %s [-st] msdosfile [msdosfiles...]\n", argv[0]);
  2334. X        exit(1);
  2335. X    }
  2336. X    last_drive = 'x';
  2337. X    mcwd = fix_mcwd();
  2338. X
  2339. X    for (i = optind; i < argc; i++) {
  2340. X        drive = get_drive(argv[i]);
  2341. X        if (drive != last_drive) {
  2342. X            if (init(drive, 0)) {
  2343. X                fprintf(stderr, "%s: Cannot initialize '%c:'\n", argv[0], drive);
  2344. X                continue;
  2345. X            }
  2346. X            last_drive = drive;
  2347. X        }
  2348. X        filename = get_name(argv[i]);
  2349. X        pathname = get_path(argv[i]);
  2350. X        if (subdir(drive, pathname))
  2351. X            continue;
  2352. X
  2353. X        ismatch = 0;
  2354. X        for (entry = 0; entry < dir_entries; entry++) {
  2355. X            dir = dir_read(entry);
  2356. X                    /* if empty */
  2357. X            if (dir->name[0] == 0x0)
  2358. X                break;
  2359. X                    /* if erased */
  2360. X            if (dir->name[0] == 0xe5)
  2361. X                continue;
  2362. X                    /* if dir or volume label */
  2363. X            if ((dir->attr & 0x10) || (dir->attr & 0x08))
  2364. X                continue;
  2365. X
  2366. X            newfile = unix_name(dir->name, dir->ext);
  2367. X
  2368. X                    /* see it if matches the pattern */
  2369. X            if (match(newfile, filename)) {
  2370. X                fat = dir->start[1] * 0x100 + dir->start[0];
  2371. X                size = dir->size[3] * 0x1000000L + dir->size[2] * 0x10000L + dir->size[1] * 0x100 + dir->size[0];
  2372. X                if (file_read(stdout, fat, textmode, stripmode, size))
  2373. X                    break;
  2374. X                ismatch = 1;
  2375. X            }
  2376. X        }
  2377. X        if (fat_error)
  2378. X            break;
  2379. X
  2380. X        if (!ismatch)
  2381. X            fprintf(stderr, "%s: File \"%s\" not found\n", argv[0], filename);
  2382. X    }
  2383. X    close(fd);
  2384. X    exit(0);
  2385. X}
  2386. X
  2387. X/*
  2388. X * stubs for read-only programs
  2389. X */
  2390. X
  2391. Xvoid
  2392. Xdisk_flush()
  2393. X{
  2394. X    extern int disk_dirty;
  2395. X
  2396. X    disk_dirty = 0;
  2397. X    return;
  2398. X}
  2399. X
  2400. Xvoid
  2401. Xdir_flush()
  2402. X{
  2403. X    extern int dir_dirty;
  2404. X
  2405. X    dir_dirty = 0;
  2406. X    return;
  2407. X}
  2408. END_OF_FILE
  2409. if test 2931 -ne `wc -c <'mtools/mtype.c'`; then
  2410.     echo shar: \"'mtools/mtype.c'\" unpacked with wrong size!
  2411. fi
  2412. # end of 'mtools/mtype.c'
  2413. fi
  2414. echo shar: End of archive 2 \(of 3\).
  2415. cp /dev/null ark2isdone
  2416. MISSING=""
  2417. for I in 1 2 3 ; do
  2418.     if test ! -f ark${I}isdone ; then
  2419.     MISSING="${MISSING} ${I}"
  2420.     fi
  2421. done
  2422. if test "${MISSING}" = "" ; then
  2423.     echo You have unpacked all 3 archives.
  2424.     rm -f ark[1-9]isdone
  2425. else
  2426.     echo You still need to unpack the following archives:
  2427.     echo "        " ${MISSING}
  2428. fi
  2429. ##  End of shell archive.
  2430. exit 0
  2431.