home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / win95 / ext2tool.exe / SRC / LDISK.C < prev    next >
C/C++ Source or Header  |  1995-05-14  |  2KB  |  116 lines

  1. /***************************************************************************
  2.  * ldisk.c - Routines for handling logical disks
  3.  *
  4.  * Copyright (C) 1995 Claus Tondering, ct@login.dknet.dk
  5.  * This file may be redistributed under the terms of the GNU Public License.
  6.  ***************************************************************************/
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. #include "e2err.h"
  13. #include "part.h"
  14. #include "diskio.h"
  15. #include "ldisk.h"
  16. #include "ext2_fs.h"
  17.  
  18.  
  19. ino_t cwdino;
  20.  
  21. /****************************************************************************
  22.  * nametodisk() converts a disk identification in the E2CWD environment to a
  23.  * BIOS disk number and an offset and a length (both in blocks of 512 bytes).
  24.  *
  25.  * Returns an error code
  26.  ****************************************************************************/
  27.  
  28. int
  29. nametodisk(int *disk, long *offset, long *length)
  30. {
  31.     int err, fstype, subdisk;
  32.     struct part_desc pl[16];
  33.     char env[80], *envp, *pos1, *pos2, *cp;
  34.  
  35.     /* Decode E2CWD environment */
  36.     envp=getenv("E2CWD");
  37.     
  38.     if (!envp)
  39.         return E2E_NOCWD;
  40.  
  41.     strcpy(env,envp);            /* We are going to modify it, so we make a copy */
  42.  
  43.     pos1=strchr(env,':');
  44.  
  45.     if (!pos1)
  46.         return E2E_BADCWD;
  47.  
  48.     pos2=strchr(pos1+1,':');
  49.  
  50.     *pos1 = 0;
  51.     pos1++;
  52.  
  53.     for (cp=env; *cp; cp++)
  54.         if (!isdigit(*cp))
  55.             return E2E_BADCWD;
  56.  
  57.     *disk = atoi(env);
  58.  
  59.     if (pos2) {
  60.         *pos2 = 0;
  61.         pos2++;
  62.     }
  63.  
  64.     for (cp=pos1; *cp; cp++)
  65.         if (!isdigit(*cp))
  66.             return E2E_BADCWD;
  67.  
  68.     subdisk = atoi(pos1)-1;
  69.  
  70.     if (pos2) {
  71.         for (cp=pos2; *cp; cp++)
  72.             if (!isdigit(*cp))
  73.                 return E2E_BADCWD;
  74.  
  75.         cwdino = atoi(pos2);
  76.     }
  77.     else
  78.         cwdino = EXT2_ROOT_INO;
  79.  
  80.  
  81.     /* Check that disk contains an ext2 file system */
  82.  
  83.     if (!(*disk & 0x80)) {
  84.         /* Floppy disk */
  85.         *offset = 0;
  86.         *length = 1440*2; /* TBD */
  87.         fstype=getfstype(*disk, 0, 0);
  88.  
  89.         if (fstype<0)
  90.             return -fstype;
  91.  
  92.         if (fstype!=EXT2FS)
  93.             return E2E_BADFS;
  94.         
  95.         return 0;
  96.     }
  97.     
  98.     err=get_part(*disk, pl);
  99.     if (err)
  100.         return err;
  101.  
  102.     if (pl[subdisk].is_extended)
  103.         return E2E_BADFS;
  104.  
  105.     fstype=getfstype(*disk, pl[subdisk].parttype, pl[subdisk].start);
  106.     if (fstype<0)
  107.         return -fstype;
  108.     if (fstype!=EXT2FS)
  109.         return E2E_BADFS;
  110.  
  111.     *offset = pl[subdisk].start;
  112.     *length = pl[subdisk].length;
  113.  
  114.     return 0;
  115. }
  116.