⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pfdisk.c

📁 又一个选择启动分区的程序源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  int	index;		/* partition index (0..3) */
  uint	id;		/* ID code (see syscodes.c) */
  uint	first,last;	/* user supplied cylinders */
  uint	c,h,s;		/* working cyl,head,sect, */
  uint	len;		/* chars seen by sscanf */
  ulong	lsbeg, lslen;	/* logical begin, length */

  /* Value check the index */
  index = *cmdp - '1';
  if (index < 0 || index > 3)
    return("index");
  pp = (struct part *) &buffer[LOC_PT + index * 16];
  np = &buffer[LOC_NT + index * 8];

  /* Read System ID */
  if (sscanf(argp,"%i%n", &id, &len) < 1)
    return("id");
  argp += len;

  /* If ID==0, just clear out the entry and return. */
  if (id == 0) {
    strncpy( (char *) pp, "", 16);
    if (useNTable) strncpy( np, "", 8);
    return((char *)0);
  }

  /* Read first and last cylinder */
  if (sscanf(argp,"%i%i%n",&first, &last, &len) < 2)
    return("first last (missing)");
  argp += len;

  /* Reasonable start,end cylinder numbers? */
  if (first > last)	return("first > last");
  if (first > 1023)	return("first > 1023");
  if (last >= cyls)	return("last >= cyls");

  /* Get (optional) system name. */
  if (*argp == '\n') {	/* no name given, use default */
    newname = nameID(id);
  } else {		/* use the given name */
    /* skip leading space */
    while (*argp == ' ') argp++;
    newname = argp;
    /* Remove newline from end */
    while (isgraph(*argp)||*argp==' ') argp++;
    *argp = '\0';
    useNTable = 1;
  }

  /* Set the ID and name. */
  pp->sysid = id;
  if (useNTable) {
    strncpy(np, newname, 8);
    strcpy(&buffer[LOC_GWR], "Gordon W. Ross");
  }

  /* set beginning c,h,s */
  c = first;
  /* if c == 0, head == 1 (reserve track 0) */
  h = (first) ? 0 : 1;
  s = 1;
  pp->b_cyl = c & 0xFF;
  pp->b_head = h;
  pp->b_sec = s | ((c >> 2) & 0xC0);
  /* Set the logical sector begin field */
  lsbeg = lslen = chs2long(c,h,s); /* using lslen as temp. */
  pp->lsBeg[0] = lslen & 0xff; lslen >>= 8;
  pp->lsBeg[1] = lslen & 0xff; lslen >>= 8;
  pp->lsBeg[2] = lslen & 0xff; lslen >>= 8;
  pp->lsBeg[3] = lslen & 0xff; lslen >>= 8;

  /* set ending c,h,s (last may be larger than 1023) */
  c = (last>1023) ? 1023 : last; /* limit c to 1023 */
  h = heads - 1; s = sectors;
  pp->e_cyl = c & 0xFF;
  pp->e_head = h;
  pp->e_sec = s | ((c >> 2) & 0xC0);
  /* Set the logical sector length field (using REAL end cylinder) */
  lslen = chs2long(last,h,s) + 1 - lsbeg;
  pp->lsLen[0] = lslen & 0xff; lslen >>= 8;
  pp->lsLen[1] = lslen & 0xff; lslen >>= 8;
  pp->lsLen[2] = lslen & 0xff; lslen >>= 8;
  pp->lsLen[3] = lslen & 0xff; lslen >>= 8;

  return((char *)0);	/* success */
} /* setPartition() */

char * makeActive(argp)	/* return error string or zero */
char	*argp;
{
  struct part *pp;	/* partition entry */
  int	i,act;		/* which one becomes active */

  if (sscanf(argp,"%d", &act) < 1)
    return("missing index");
  act--;			/* make it zero-origin */

  i=0; pp = (struct part *) &buffer[LOC_PT];
  while (i<4) {
    pp->active = 0;
    if (i == act) {
      if (pp->sysid == 0) return("partition empty");
      pp->active = 0x80;
    }
    i++; pp++;
  }
  return((char *)0);
}

char * setGeometry(argp)	/* return string on error */
char	*argp;
{
  int	c,h,s;

  if (sscanf(argp,"%i%i%i", &c, &h, &s) < 3)
    return("(missing)");
  if (c<1) return("cyls");
  if (h<1) return("heads");
  if (s<1) return("sectors");
  cyls=c; heads=h; sectors=s;
  return((char *)0);
}

listPTable()		/* print out partition table */
{
  struct part * pp;	/* partition table entry */
  char	*name;
  int	i;		/* partition number */
  int	numActive=0;	/* active partition [1-4], 0==none */
  uint	pbc,pbh,pbs;	/* physical beginning  c,h,s */
  uint	pec,peh,pes;	/* physical ending     c,h,s */
  uint	lbc,lbh,lbs;	/* logical beginning   c,h,s */
  uint	lec,leh,les;	/* logical ending      c,h,s */
  ulong	lsbeg,lslen;	/* logical sectors: begin, length */

  printf("# Partition table on device: %s\n", devname);
  printf("geometry %d %d %d (cyls heads sectors)\n",
	 cyls, heads, sectors);
  printf("#  ID  First(cyl)  Last(cyl)  Name     ");
  printf("# start, length (sectors)\n");

  for (i=0; i<4; i++) {
    pp = (struct part *) &buffer[LOC_PT + i * 16];

    if (pp->active) {
      if(numActive)
	fprintf(stderr,"Error: multiple active partitions.\n");
      else numActive = i+1;
    }

    /* physical beginning c,h,s */
    pbc = pp->b_cyl & 0xff | (pp->b_sec << 2) & 0x300;
    pbh = pp->b_head;
    pbs = pp->b_sec & 0x3F;

    /* physical ending c,h,s */
    pec = pp->e_cyl & 0xff | (pp->e_sec << 2) & 0x300;
    peh = pp->e_head;
    pes = pp->e_sec & 0x3F;

    /* compute logical beginning (c,h,s) */
    lsbeg = ((((((pp->lsBeg[3] ) << 8 )
		| pp->lsBeg[2] ) << 8 )
		| pp->lsBeg[1] ) << 8 )
		| pp->lsBeg[0] ;
    long2chs(lsbeg, &lbc, &lbh, &lbs);
    /* compute logical ending (c,h,s) */
    lslen = ((((((pp->lsLen[3]) << 8 )
		| pp->lsLen[2]) << 8 )
		| pp->lsLen[1]) << 8 )
		| pp->lsLen[0] ;
    /* keep beginning <= end ... */
    if (lslen > 0) long2chs(lsbeg+lslen-1, &lec, &leh, &les);
    else	   long2chs(lsbeg,	   &lec, &leh, &les);

    if (useNTable)
      name = &buffer[LOC_NT + i * 8];
    else
      name = nameID((uint) pp->sysid);

    /* show physical begin, logical end (works for cyl>1023) */
    /*      #  ID  First(cyl)  Last(cyl)  Name... # ... */
    printf("%d %3d   %4d       %4d       %-8.8s # %ld, %ld\n",
	   i+1, pp->sysid, pbc, lec, name, lsbeg, lslen );

    /* That's all, for an empty partition. */
    if (pp->sysid == 0) continue;

    /*
     * Now do some consistency checks...
     */

    /* Same physical / logical beginning? */
    if (pbc != lbc || pbh != lbh || pbs != lbs ) {
      printf("# note: first(%d): ", i+1);
      printf("phys=(%d,%d,%d) ",    pbc, pbh, pbs);
      printf("logical=(%d,%d,%d)\n",lbc, lbh, lbs);
    }
    /* Same physical / logical ending? */
    if (pec != lec || peh != leh || pes != les ) {
      printf("# note:  last(%d): ", i+1);
      printf("phys=(%d,%d,%d) ",    pec, peh, pes);
      printf("logical=(%d,%d,%d)\n",lec, leh, les);
    }

    /* Beginning on cylinder boundary? */
    if (pbc == 0) { /* exception: start on head 1 */
      if (pbh != 1 || pbs != 1) {
	printf("# note: first(%i): ", i+1);
	printf("phys=(%d,%d,%d) ", pbc, pbh, pbs);
	printf("should be (%d,1,1)\n", pbc);
      }
    } else { /* not on cyl 0 */
      if (pbh != 0 || pbs != 1) {
	printf("# note: first(%i): ", i+1);
	printf("phys=(%d,%d,%d) ", pbc, pbh, pbs);
	printf("should be (%d,0,1)\n", pbc);
      }
    }

    /* Ending on cylinder boundary? */
    if (peh != (heads-1) || pes != sectors) {
      printf("# note: last(%i): ", i+1);
      printf("phys=(%d,%d,%d) ", pec, peh, pes);
      printf("should be (%d,%d,%d)\n",
	     pec, heads-1, sectors);
    }

  } /* for */
  printf("active: %d  %s\n", numActive,
	 (numActive) ? "" : "(none)");
} /* listPTable() */

ulong chs2long(c,h,s)
uint c,h,s;
{
  ulong	l;
  if (s<1) s=1;
  l  = c; l *= heads;
  l += h; l *= sectors;
  l += (s - 1);
  return(l);
}

long2chs(ls, c, h, s)	/* convert logical sec-num to c,h,s */
ulong	ls;		/* Logical Sector number */
uint	*c,*h,*s;	/* cyl, head, sector */
{
  int	spc = heads * sectors;
  *c = ls / spc;
  ls = ls % spc;
  *h = ls / sectors;
  *s = ls % sectors + 1;	/* sectors count from 1 */
}

char * nameID(n)
unsigned int n;
{
  struct intString *is;

  is = sysCodes;
  while (is->i) {
    if (is->i == n) return(is->s);
    is++;
  }
  if (!n) return(is->s);
  return("unknown");
}

int printIDs()		/* print the known system IDs */
{
  struct intString * is = sysCodes;

  /* This might need to do more processing eventually, i.e.
   * if (prompt) { ... do more processing ... }
   */
  printf("_ID_\t__Name__ ____Description____\n");
  while (is->i) {
    printf("%3d\t%s\n", is->i, is->s);
    is++;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -