Example #1
0
/**
 * @brief build the BDS header
 *
 * The header is a 52 byte string with the data type info and 3
 * dimensions. The string must be correcly terminated by the null
 * character (in the 52 bytes) and its content readable with a
 * C scanf() function using the formatting string "%s%lu%lu%lu".
 *
 * Currently, the data type info can only be "flt" for floats. There
 * is no point using the full C type names since this string won't be
 * processed as a C code fragment and unsigned types would require two
 * words, so we use compact abbreviations. "flt" is the standard C
 * abbreviation for floats (see float.h macros).
 *
 * The header length is sufficient for 3x 2**32 dimensions; the
 * dimensions are checked and this function will abort() if the array
 * size can't fit into the header length.
 *
 * @param nx ny nc array dimensions
 * @return freshly allocated string pointer
 */
static char *_io_bds_hdr(size_t nx, size_t ny, size_t nc)
{
    char *hdr;
    int len;

    /* empty header fille with spaces */
    hdr = _IO_BDS_SAFE_MALLOC(_IO_BDS_HDR_LEN, char);
    memset(hdr, ' ', _IO_BDS_HDR_LEN);

    /* check length */
    if (1                       /* leading space */
        + strlen("flt") + 1     /* "flt" data type info + space */
        + ndigits(nx) + 1 + ndigits(ny) + 1 + ndigits(nc)
        + 1                     /* ending '\0' */
        > _IO_BDS_HDR_LEN)
        _IO_BDS_ABORT("array dimensions too large for the header format");

    /* inhibit splint, already checked for overflow */
    /*@-bufferoverflowhigh@ */
    len = sprintf(hdr, " %s %lu %lu %lu", "flt", nx, ny, nc);
    /*@=bufferoverflowhigh@ */

    /* terminate at the end of the string */
    hdr[len] = ' ';
    hdr[_IO_BDS_HDR_LEN - 1] = '\0';
    return hdr;
}
Example #2
0
File: p32.c Project: elly/euler
int ispandigital(int m2) {
	for (int m0 = 1; m0 < ceil(sqrt(m2)); ++m0) {
		int m1;
		if (m2 % m0 != 0)
			continue;
		m1 = m2 / m0;
		if (ndigits(m0) + ndigits(m1) + ndigits(m2) != 9)
			continue;
		if (sdigits(sdigits(sdigits(0, m0), m1), m2) == SD_ALL) {
			/* printf("bing! %d * %d = %d\n", m0, m1, m2); */
			return 1;
		}
	}
	return 0;
}
Example #3
0
void SelectLevelScreen::key_down(const KeyDownEvent& event) {
    switch (_state) {
        case SELECTING:
            switch (event.key()) {
                case Keys::K8:
                case Keys::N_TIMES:
                    _state          = UNLOCKING;
                    _unlock_chapter = 0;
                    _unlock_digits  = ndigits(plug.levels.size());
                    sys.sound.cloak_on();
                    return;
            }
        case UNLOCKING: {
            int digit = key_digit(event.key());
            if (digit < 0) {
                _state = SELECTING;
                break;
            }
            _unlock_chapter = (_unlock_chapter * 10) + digit;
            if (--_unlock_digits == 0) {
                _state = SELECTING;
                if (_unlock_chapter > plug.levels.size()) {
                    return;
                }
                sys.sound.cloak_off();
                Ledger::ledger()->unlock_chapter(_unlock_chapter);
                Ledger::ledger()->unlocked_chapters(&_chapters);
                adjust_interface();
            }
            return;
        } break;
        case FADING_OUT: return;
    }
    InterfaceScreen::key_down(event);
}
Example #4
0
int ispandigital(int n) {
	int d = ndigits(n);
	unsigned int sd = sdigits(0, n);
	for (int i = 1; i <= d; ++i)
		if (!(sd & (1 << i)))
			return 0;
	return 1;
}
Example #5
0
// compute ||A - Q*T*Q.T||
int test_mult_trd(int M, int N, int lb, int verbose, int flags)
{
  armas_x_dense_t A0, A1, tau0,  T0, T1, e1, e2, d1, d2;
  armas_conf_t conf = *armas_conf_default();
  int ok;
  DTYPE nrm;
  char *uplo = flags & ARMAS_UPPER ? "UPPER" : "LOWER";

  armas_x_init(&A0, N, N);
  armas_x_init(&A1, N, N);
  armas_x_init(&T0, N, N);
  armas_x_init(&T1, N, N);
  armas_x_init(&tau0, N, 1);

  // set source data
  armas_x_set_values(&A0, unitrand, flags);
  armas_x_mcopy(&A1, &A0);

  conf.lb = lb;
  armas_x_trdreduce(&A0, &tau0, flags, &conf);

  // make tridiagonal matrix T0
  armas_x_diag(&d1, &A0, 0);
  armas_x_diag(&d2, &T0, 0);
  armas_x_mcopy(&d2, &d1);
  if (flags & ARMAS_UPPER) {
    armas_x_diag(&e1, &A0, 1);
  } else {
    armas_x_diag(&e1, &A0, -1);
  }
  // copy off-diagonals
  armas_x_diag(&e2, &T0, 1);
  armas_x_mcopy(&e2, &e1);
  armas_x_diag(&e2, &T0, -1);
  armas_x_mcopy(&e2, &e1);

  // compute Q*T*Q.T
  armas_x_trdmult(&T0, &A0, &tau0, flags|ARMAS_LEFT, &conf);
  armas_x_trdmult(&T0, &A0, &tau0, flags|ARMAS_RIGHT|ARMAS_TRANS, &conf);

  // make result triangular (original matrix)
  armas_x_make_trm(&T0, flags);
  nrm = rel_error((DTYPE *)0, &T0, &A1, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = isFINE(nrm, N*__ERROR);
  printf("%s: [%s] Q*T*Q.T == A\n", PASS(ok), uplo);
  if (verbose > 0) {
    printf("  || rel error ||: %e [%d]\n", nrm, ndigits(nrm));
  }
  armas_x_release(&A0);
  armas_x_release(&A1);
  armas_x_release(&tau0);
  armas_x_release(&T0);
  armas_x_release(&T1);
  return ok;
}
Example #6
0
int test_reduce(int M, int N, int lb, int verbose, int flags)
{
  armas_x_dense_t A0, A1, tau0, tau1;
  armas_conf_t conf = *armas_conf_default();
  int ok;
  char uplo = flags & ARMAS_LOWER ? 'L' : 'U';
  DTYPE n0, n1;

  armas_x_init(&A0, N, N);
  armas_x_init(&A1, N, N);
  armas_x_init(&tau0, N, 1);
  armas_x_init(&tau1, N, 1);

  // set source data
  armas_x_set_values(&A0, unitrand, flags);
  armas_x_mcopy(&A1, &A0);

  conf.lb = 0;
  armas_x_trdreduce(&A0, &tau0, flags, &conf);

  conf.lb = lb;
  armas_x_trdreduce(&A1, &tau1, flags, &conf);

  n0 = rel_error((DTYPE *)0, &A0,   &A1,   ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  n1 = rel_error((DTYPE *)0, &tau0, &tau1, ARMAS_NORM_TWO, ARMAS_NONE, &conf);
  ok = isFINE(n0, N*__ERROR);

  printf("%s: unblk.TRD(A,%c) == blk.TRD(A,%c)\n", PASS(ok), uplo, uplo);
  if (verbose > 0) {
    printf("  || error.TRD(A,%c)||: %e [%d]\n", uplo, n0, ndigits(n0));
    printf("  || error.tau||      : %e [%d]\n", n1, ndigits(n1));
  }
  armas_x_release(&A0);
  armas_x_release(&A1);
  armas_x_release(&tau0);
  armas_x_release(&tau1);
  return ok;
}
Example #7
0
/*  -----------------------------------------------------------------------------------
 *  Test 6: Q(qr(A)).T * Q(qr(A)) == I 
 *    OK: ||I - Q.T*Q||_1 ~~ n*eps
 */
int test_build_identity(int M, int N, int K, int lb, int verbose)
{
  char *blk = lb > 0 ? "  blk" : "unblk";
  char ct = M == K ? 'M' : 'K';
  armas_x_dense_t A0, C0, tau0, D;
  int ok;
  DTYPE n0;
  armas_conf_t conf = *armas_conf_default();

  armas_x_init(&A0, M, N);
  armas_x_init(&C0, M, M);
  armas_x_init(&tau0, imin(M, N), 1);

  // set source data
  armas_x_set_values(&A0, unitrand, ARMAS_ANY);

  // factorize
  conf.lb = lb;
  armas_x_rqfactor(&A0, &tau0, &conf);

  // compute Q = buildQ(rq(A)), K first columns
  conf.lb = lb;
  if (armas_x_rqbuild(&A0, &tau0, K, &conf) < 0)
    printf("build error: %d\n", conf.error);

  // C0 = Q.T*Q - I
  armas_x_mult(0.0, &C0, 1.0, &A0, &A0, ARMAS_TRANSB, &conf);
  armas_x_diag(&D, &C0, 0);
  armas_x_add(&D, -1.0, &conf);

  n0 = armas_x_mnorm(&C0, ARMAS_NORM_ONE, &conf);

  ok = isOK(n0, N);
  printf("%s: %s Q(rq(A),%c).T * Q(rq(A),%c) == I\n", PASS(ok), blk, ct, ct);
  if (verbose > 0) {
    printf("  || rel error ||_1: %e [%d]\n", n0, ndigits(n0));
  }
  //armas_x_release(&A0);
  armas_x_release(&C0);
  armas_x_release(&tau0);
  return ok;
}
Example #8
0
/*  -----------------------------------------------------------------------------------
 *  Test 5: unblk.Q(rq(A)) == blk.Q(rq(A))
 *    OK: ||unblk.Q(rq(A)) - blk.Q(rq(A))||_1 ~~ n*eps
 */
int test_build(int M, int N, int K, int lb, int verbose)
{
  char ct = N == K ? 'N' : 'K';
  armas_x_dense_t A0, A1, tau0;
  int ok;
  DTYPE n0;
  armas_conf_t conf = *armas_conf_default();
  
  armas_x_init(&A0, M, N);
  armas_x_init(&A1, M, N);
  armas_x_init(&tau0, imin(M, N), 1);

  // set source data
  armas_x_set_values(&A0, unitrand, ARMAS_ANY);

  // factorize
  conf.lb = lb;
  armas_x_rqfactor(&A0, &tau0, &conf);
  armas_x_mcopy(&A1, &A0);
    
  // compute Q = buildQ(rq(A))
  conf.lb = 0;
  armas_x_rqbuild(&A0, &tau0, K, &conf);
  conf.lb = lb;
  armas_x_rqbuild(&A1, &tau0, K, &conf);

  // ||A1 - A0||/||A0||
  n0 = rel_error((DTYPE *)0, &A1, &A0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);

  ok = isOK(n0, N);
  printf("%s: unblk.Q(rq(A),%c) == blk.Q(rq(A),%c)\n", PASS(ok), ct, ct);
  if (verbose > 0) {
    printf("  || rel_error ||_1: %e [%d]\n", n0, ndigits(n0));
  }

  armas_x_release(&A0);
  armas_x_release(&A1);
  armas_x_release(&tau0);
  return ok;
}
Example #9
0
int test_build(int M, int N, int lb, int K, int verbose, int flags)
{
  armas_x_dense_t A0, tauq0, d0, QQt;
  armas_conf_t conf = *armas_conf_default();
  int ok;
  DTYPE nrm;
  int tN = M < N ? M : N;
  char *uplo = flags & ARMAS_UPPER ? "UPPER" : "LOWER";

  armas_x_init(&A0, N, N);
  armas_x_init(&tauq0, tN, 1);
  //------------------------------------------------------

  // set source data
  armas_x_set_values(&A0, unitrand, flags);
  // reduce to tridiagonal matrix
  conf.lb = lb;
  armas_x_trdreduce(&A0, &tauq0, flags, &conf);
  // ----------------------------------------------------------------
  // Q-matrix
  armas_x_trdbuild(&A0, &tauq0, K, flags, &conf);
  armas_x_init(&QQt, N, N);
  armas_x_mult(0.0, &QQt, 1.0, &A0, &A0, ARMAS_TRANSB, &conf);
  armas_x_diag(&d0, &QQt, 0);
  armas_x_madd(&d0, -1.0, ARMAS_NONE);

  nrm = armas_x_mnorm(&QQt, ARMAS_NORM_ONE, &conf);
    
  ok = isFINE(nrm, N*__ERROR);
  printf("%s: [%s] I == Q*Q.T\n", PASS(ok), uplo);
  if (verbose > 0) {
    printf("  || rel error ||: %e [%d]\n", nrm, ndigits(nrm));
  }
  //------------------------------------------------------
  armas_x_release(&A0);
  armas_x_release(&tauq0);
  armas_x_release(&QQt);
  return ok;
}
Example #10
0
 size_t max_formatted_length(T x) const noexcept {
     size_t n = ndigits(x, base_);
     if (x < 0 || any(showpos)) n++;
     return n;
 }
Example #11
0
// test2: M > N and A, U are M-by-M, V = N-by-N
int test2(int M, int N, int verbose)
{
    armas_x_dense_t A, S, Sg, U, U1, V, V1, A0, A1, sD;
    armas_conf_t conf = *armas_conf_default();
    DTYPE n0, n1, n2, nrm_A;
    int err, ok;

    if (M < N) {
        printf("%s [M=%d, N=%d]: test2 requires M >= N\n", PASS(0), M, N);
        return 0;
    }
    armas_x_init(&A, M, N);
    armas_x_init(&A0, M, N);
    armas_x_init(&A1, M, N);
    armas_x_init(&U, M, M);
    armas_x_init(&U1, M, M);
    armas_x_init(&V, N, N);
    armas_x_init(&V1, N, N);
    armas_x_init(&S, N, 1);
    armas_x_init(&Sg, M, N);
    
    armas_x_set_values(&A, unitrand, ARMAS_ANY);
    armas_x_set_values(&Sg, zero, ARMAS_ANY);
    armas_x_mcopy(&A0, &A);
    nrm_A = armas_x_mnorm(&A, ARMAS_NORM_ONE, &conf);

    err = armas_x_svd(&S, &U, &V, &A, ARMAS_WANTU|ARMAS_WANTV, &conf);
    if (err) {
        //printf("error[%d]: %d\n", err, conf.error);
        printf("%s [M=%d, N=%d, err=%d,%d]: A == U*S*V.T, U=[m,m], V=[n,n] \n",
               PASS(0), M, N, err, conf.error);
        return 0;
    }

    // compute: I - U.T*U
    armas_x_mult(0.0, &U1, 1.0, &U, &U, ARMAS_TRANSA, &conf);
    armas_x_diag(&sD, &U1, 0);
    armas_x_madd(&sD, -1.0, ARMAS_ANY);
    n0 = armas_x_mnorm(&U1, ARMAS_NORM_ONE, &conf);
    
    // compute: I - V*V.T
    armas_x_mult(0.0, &V1, 1.0, &V, &V, ARMAS_TRANSA, &conf);
    armas_x_diag(&sD, &V1, 0);
    armas_x_madd(&sD, -1.0, ARMAS_ANY);
    n1 = armas_x_mnorm(&V1, ARMAS_NORM_ONE, &conf);

    // compute: A - U*S*V.T == A - U*diag(Sg)*V.T
    armas_x_diag(&sD, &Sg, 0);
    armas_x_copy(&sD, &S, &conf);

    armas_x_mult(0.0, &A1, 1.0, &U, &Sg, ARMAS_NONE, &conf);
    armas_x_mult(1.0, &A0, -1.0, &A1, &V, ARMAS_NONE, &conf);
    n2 = armas_x_mnorm(&A0, ARMAS_NORM_ONE, &conf) / nrm_A;

    ok = isOK(n2, 10*M);
    printf("%s [M=%d, N=%d]: A == U*S*V.T, U=[m,m], V=[n,n] \n", PASS(ok), M, N);
    if (verbose > 0) {
        printf("  ||A - U*S*V.T||_1: %e [%d]\n", n2, ndigits(n2));
        printf("  ||I - U.T*U||_1  : %e [%d]\n", n0, ndigits(n0));
        printf("  ||I - V*V.T||_1  : %e [%d]\n", n1, ndigits(n1));
    }

    armas_x_release(&A);
    armas_x_release(&A0);
    armas_x_release(&A1);
    armas_x_release(&U);
    armas_x_release(&U1);
    armas_x_release(&V);
    armas_x_release(&V1);
    armas_x_release(&S);
    armas_x_release(&Sg);
    return ok;
}
Example #12
0
int main(int argc, char **argv)
{
  armas_conf_t conf;
  armas_x_dense_t B0, A, B;

  int ok, opt;
  int N = 301;
  int verbose = 1;
  int fails = 0;
  DTYPE alpha = 1.0;
  DTYPE n0, n1;

  while ((opt = getopt(argc, argv, "vC:")) != -1) {
    switch (opt) {
    case 'v':
      verbose++;
      break;
    case 'C':
      Aconstant = STRTOF(optarg);
      break;
    default:
      fprintf(stderr, "usage: trsm [-P nproc] [size]\n");
      exit(1);
    }
  }
    
  if (optind < argc)
    N = atoi(argv[optind]);

  conf = *armas_conf_default();

  armas_x_init(&A, N, N);
  armas_x_init(&B, N, N);
  armas_x_init(&B0, N, N);
  
  // Upper triangular matrix
  armas_x_set_values(&A, one, ARMAS_UPPER);
  if (N < 10) {
    printf("A:\n"); armas_x_printf(stdout, "%8.1e", &A);
  }
    

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_LEFT, &conf);
  if (N < 10) {
    printf("A*B:\n"); armas_x_printf(stdout, "%8.1e", &B);
  }
  armas_x_solve_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_LEFT, &conf);
  if (N < 10) {
    printf("A.-1*B:\n"); armas_x_printf(stdout, "%8.1e", &B);
  }
  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, L|U|N), A, L|U|N)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_RIGHT, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_RIGHT, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, R|U|N), A, R|U|N)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_LEFT|ARMAS_TRANSA, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_LEFT|ARMAS_TRANSA, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, L|U|T), A, L|U|T)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_RIGHT|ARMAS_TRANSA, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_UPPER|ARMAS_RIGHT|ARMAS_TRANSA, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, R|U|T), A, R|U|T)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  // Lower triangular matrix
  armas_x_set_values(&A, one, ARMAS_LOWER);

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_LEFT, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_LEFT, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, L|L|N), A, L|L|N)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_RIGHT, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_RIGHT, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, R|L|N), A, R|L|N)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_LEFT|ARMAS_TRANSA, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_LEFT|ARMAS_TRANSA, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, L|L|T), A, L|L|T)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  armas_x_set_values(&B, one, ARMAS_NULL);
  armas_x_mcopy(&B0, &B);
  armas_x_mult_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_RIGHT|ARMAS_TRANSA, &conf);
  armas_x_solve_trm(&B, alpha, &A, ARMAS_LOWER|ARMAS_RIGHT|ARMAS_TRANSA, &conf);

  n0 = rel_error(&n1, &B, &B0, ARMAS_NORM_ONE, ARMAS_NONE, &conf);
  ok = n0 == 0.0 || isOK(n0, N) ? 1 : 0;
  printf("%6s: B = solve_trm(mult_trm(B, A, R|L|T), A, R|L|T)\n", PASS(ok));
  if (verbose > 0) {
    printf("   || rel error || : %e, [%d]\n", n0, ndigits(n0));
  }
  fails += 1 - ok;

  test_left_right(N, verbose);
  exit(fails);
}
Example #13
0
/*ARGSUSED*/
void
print_partition(struct partition_info *pinfo, int partnum, int want_header)
{
	int		i;
	blkaddr_t	nblks;
	int		cyl1;
	int		cyl2;
	float		scaled;
	int		maxcyl2;
	int		ncyl2_digits;
	char		*s;
	blkaddr_t	maxnblks = 0;
	blkaddr_t	len;

	/*
	 * To align things nicely, we need to know the maximum
	 * width of the number of cylinders field.
	 */
	maxcyl2 = 0;
	for (i = 0; i < NDKMAP; i++) {
		nblks	= (uint_t)pinfo->pinfo_map[i].dkl_nblk;
		cyl1	= pinfo->pinfo_map[i].dkl_cylno;
		cyl2	= cyl1 + (nblks / spc()) - 1;
		if (nblks > 0) {
			maxcyl2 = max(cyl2, maxcyl2);
			maxnblks = max(nblks, maxnblks);
		}
	}
	/*
	 * Get the number of digits required
	 */
	ncyl2_digits = ndigits(maxcyl2);

	/*
	 * Print the header, if necessary
	 */
	if (want_header) {
		fmt_print("Part      ");
		fmt_print("Tag    Flag     ");
		fmt_print("Cylinders");
		nspaces(ncyl2_digits);
		fmt_print("    Size            Blocks\n");
	}

	/*
	 * Print the partition information
	 */
	nblks	= pinfo->pinfo_map[partnum].dkl_nblk;
	cyl1	= pinfo->pinfo_map[partnum].dkl_cylno;
	cyl2	= cyl1 + (nblks / spc()) - 1;

	fmt_print("  %x ", partnum);

	/*
	 * Print the partition tag.  If invalid, print -
	 */
	s = find_string(ptag_choices,
		(int)pinfo->vtoc.v_part[partnum].p_tag);
	if (s == (char *)NULL)
		s = "-";
	nspaces(10 - (int)strlen(s));
	fmt_print("%s", s);

	/*
	 * Print the partition flag.  If invalid print -
	 */
	s = find_string(pflag_choices,
		(int)pinfo->vtoc.v_part[partnum].p_flag);
	if (s == (char *)NULL)
		s = "-";
	nspaces(6 - (int)strlen(s));
	fmt_print("%s", s);

	nspaces(2);

	if (nblks == 0) {
		fmt_print("%6d      ", cyl1);
		nspaces(ncyl2_digits);
		fmt_print("     0         ");
	} else {
		fmt_print("%6d - ", cyl1);
		nspaces(ncyl2_digits - ndigits(cyl2));
		fmt_print("%d    ", cyl2);
		scaled = bn2mb(nblks);
		if (scaled > (float)1024.0 * 1024.0) {
			fmt_print("%8.2fTB    ",
				scaled/((float)1024.0 * 1024.0));
		} else if (scaled > (float)1024.0) {
			fmt_print("%8.2fGB    ", scaled/(float)1024.0);
		} else {
			fmt_print("%8.2fMB    ", scaled);
		}
	}
	fmt_print("(");
	pr_dblock(fmt_print, nblks);
	fmt_print(")");

	nspaces(ndigits(maxnblks/spc()) - ndigits(nblks/spc()));
	/*
	 * Allocates size of the printf format string.
	 * ndigits(ndigits(maxblks)) gives the byte size of
	 * the printf width field for maxnblks.
	 */
	len = strlen(" %") + ndigits(ndigits(maxnblks)) + strlen("d\n") + 1;
	s = zalloc(len);
	(void) snprintf(s, len, "%s%u%s", " %", ndigits(maxnblks), "u\n");
	fmt_print(s, nblks);
	(void) free(s);
}
Example #14
0
/*ARGSUSED*/
void
print_efi_partition(struct dk_gpt *map, int partnum, int want_header)
{
	int		ncyl2_digits = 0;
	float		scaled;
	char		*s;
	uint64_t	secsize;

	ncyl2_digits = ndigits(map->efi_last_u_lba);
	if (want_header) {
	    fmt_print("Part      ");
	    fmt_print("Tag    Flag     ");
	    fmt_print("First Sector");
	    nspaces(ncyl2_digits);
	    fmt_print("Size");
	    nspaces(ncyl2_digits);
	    fmt_print("Last Sector\n");
	}

	fmt_print("  %d ", partnum);
	s = find_string(ptag_choices,
		(int)map->efi_parts[partnum].p_tag);
	if (s == (char *)NULL)
		s = "-";
	nspaces(10 - (int)strlen(s));
	fmt_print("%s", s);

	s = find_string(pflag_choices,
		(int)map->efi_parts[partnum].p_flag);
	if (s == (char *)NULL)
		s = "-";
	nspaces(6 - (int)strlen(s));
	fmt_print("%s", s);

	nspaces(2);

	secsize = map->efi_parts[partnum].p_size;
	if (secsize == 0) {
	    fmt_print("%16llu", map->efi_parts[partnum].p_start);
	    nspaces(ncyl2_digits);
	    fmt_print("  0     ");
	} else {
	    fmt_print("%16llu", map->efi_parts[partnum].p_start);
	    scaled = bn2mb(secsize);
	    nspaces(ncyl2_digits - 5);
	    if (scaled >= (float)1024.0 * 1024) {
		fmt_print("%8.2fTB", scaled/((float)1024.0 * 1024));
	    } else if (scaled >= (float)1024.0) {
		fmt_print("%8.2fGB", scaled/(float)1024.0);
	    } else {
		fmt_print("%8.2fMB", scaled);
	    }
	}
	nspaces(ncyl2_digits);
	if ((map->efi_parts[partnum].p_start+secsize - 1) ==
		UINT_MAX64) {
	    fmt_print(" 0    \n");
	} else {
	    fmt_print(" %llu    \n",
		map->efi_parts[partnum].p_start+secsize - 1);
	}
}