Finding the block size of a file on tape - block.c (0/1)

Finding the block size of a file on tape - block.c (0/1)

Post by Greg Ko » Thu, 19 Feb 1998 04:00:00



I found this program to find the block size of a file on tape and I
need to know how to use it.  It reads the whole file telling me:

read #xxxx of yyyyy bytes returned zzzzzz

I assume xxxx is the block number,  yyyyy is the block size and zzzzz
is the number of bytes in the block.  What I dont know is how this
helps me.  yyyyy and zzzzz are the same for every block except the
last one for every block size I use.  If I have the right block size,
should the last Block be padded to equel the block size?  If you have
to tell it the block size, why would you need it to find the block
size?  

Greg Koch
remove the spam deflector

 
 
 

Finding the block size of a file on tape - block.c (0/1)

Post by Greg Ko » Thu, 19 Feb 1998 04:00:00


Guess I can't include attachments here.  I'll try again.  I got a
program that finds the block size of a file on tape.  It tells me the
block number, block size, and how many bytes in the block.  I have to
tell it the block size ahead of time or it uses the default block
size.  How does this help me find the block size of the file?  Should
the last block be padded to equel the block size or something.  I'll
include the program below.  Thank you for any help.

/*
 * block.c - determine size of blocks read from magnetic tape or
networks.
 * Copyright (C) 1985, 1986, 1987, 1991  Robert M. Toxen, Fly-By-Day
 * Consulting, 6 Ruthellen Road, Framingham, MA 01701, 508-877-4991.
 * Permission to use is granted if all of the following are complied
with:
 *   1. This copyright is not removed.
 *   2. The program is not altered to prohibit printing out the
copyright
 *      notice.
 *   3. This program is not sold or bought for any price.
 *   4. No liability by the copyright holder of any kind, including
special,
 *      puntitive, or consequential is assumed.  Use at your own risk.
Use
 *      of this product constitutes agreement to this lack of
liability by
 *      the copyright holder.  This program is offered "as is".
 *
 * To compile on U*x: cc -o block -O block.c
 * This program has not been tested on 16-bit machines (sizeof (int)
== 2).
 * This program assumes 2's compliment arithmetic.
 */
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#define R       register

int     dflag;          /* display data */
int     cflag;          /* continue on error */
long    block = 32767;  /* typical maximum tape record size (sizeof
short
 -1)
*/
char    *malloc();
char    *buf;
char    *progname = "block";
FILE    *diag = stdout;
int     errno;
char    *wname;
int     wfd = -1;

main(argc, argv)
int     argc;
char    **argv;
{
        long    i;
        long    atol();

        if (argc > 0)
                progname = *argv;
        argc--;
        argv++;
        fprintf(stderr, "%s: Copyright (C) 1985, 1986, 1987, 1991
Robert M.
Toxen, 508-877-4991.\n", progname);
        while (argc > 0 && **argv == '-') {
                if (!strcmp(*argv, "-d")) {
                        dflag++;
                        diag = stderr;  /* to stderr since data to
stdout */
                        fprintf(stderr,
                          "block: data to stdout, messages to
stderr\n");
                } else if (!strcmp(*argv, "-b")) {
                        argc--;
                        argv++;
                        i = atol(*argv);
                        if (i < 1)
                                error("non-positive block size '%s'",
*argv);
                        block = i;
                        fprintf(stderr, "block: block size will be
%ld\n",
                          block);
                } else if (!strcmp(*argv, "-w")) {
                        argc--;
                        argv++;
                        wname = *argv;
                        wfd = creat(wname, 0600);
                        if (wfd < 0)
                                error("cannot open '%s' for writing",
*argv);
                        fprintf(stderr, "block: writing tape to
'%s'\n",
wname);
                } else if (!strcmp(*argv, "-c")) {
                        cflag++;
                        fprintf(stderr, "block: will continue on
error\n");
                } else {
                        fprintf(stderr, "block: usage:\n");
                        fprintf(stderr,
"\tblock [-b block_size] [-c] [-d] [-w ofile] [ifile]\n");
                        fprintf(stderr,
                          "-b specifies block size, default %ld\n",
                          block);
                        fprintf(stderr, "-c prevents exit on read
error\n");
                        fprintf(stderr,
"-d outputs printf of data to stdout, messages to stderr\n");
                        fprintf(stderr,
                          "-w copies input to file ofile\n");
                        fprintf(stderr,
"if ifile is listed, it is opened for reading, else stdin\n");
                        error("Unknown flag '%c'", **argv);
                }
                argc--;
                argv++;
        }
        if (argc > 1)
                error("too many args");
        if (argc) {
                close(0);
                if (open(*argv, 0) != 0)
                        error("cannot open '%s'", *argv);
        }
        buf = malloc(block+2);
        if (buf == NULL)
                error("cannot malloc %d bytes", block);
        doit();

}

doit()
{
        R long  i;
        static long oldi = -2;
        int record;

        record = 0;
        for (;;) {
                for (i=0; i<block+1; i++)
                        buf[i] = '\0';
                errno = 0;
                i = read(0, buf, block);
                if (dflag)
                        printf("read '%.*s'\n", strlen(buf), buf);
                if (i < 0) {
                        fprintf(diag,
                          "read #%d returned %ld, errno=%d\n",
                          record, i, errno);
                        perror("read");
                        sleep(10);      /* Slow down, don't skid. */
                } else {
                        if (wfd >= 0) {
                                errno = 0;
                                if (write(wfd, buf, i) != i)
                                        perror("cannot write ofile");
                        }
                        if (!(record % 50) || i != oldi) {
                                fprintf(diag,
                                  "\nread #%d of %d bytes returned
%ld\n",
                                  record, block, i);
                                fflush(diag);
                        } else
                                fprintf(diag, ".");
                }
                if (i <= 0)
                        if (i == 0 || !cflag) {
                                if (wfd >= 0)
                                        close(wfd);
                                exit(i);
                        }
                oldi = i;
                record++;
        }

}

error(f, a, b, c)
char    *f;
char    *a;
char    *b;
char    *c;
{
        fprintf(stderr, "%s: ", progname);
        fprintf(stderr, f, a, b, c);
        fprintf(stderr, "\n");
        exit(1);

}

Greg Koch
remove the spam deflector
g<remove>k...@usa.net

 
 
 

1. Finding tape block size

I agree with everything Doug said. However, I would like to add that
while mtio(4) says that an error will be returned, many device drivers
will not return one. The only source (Tahoe) I have available to look
at (Lions is at home) says things like:

/*
 * If we were reading raw tape and the only error was that the
 * record was too long, then we don't consider this an error.
 */
--

                unknown mode: sane

2. Question about sending input data to the Internet

3. Non blocking socket blocks; says 'read would block' ?

4. KRN Offline Newsreading

5. HELP: scsi tape and tape block size - how to user control

6. Our netra i5 died...

7. Appropriate size of dump tape blocks for high density tape

8. How to shutdown in Runlevel 5?

9. EXT2: Defualt block-size & super-block pos.

10. SCO tar error on block size (not block factor)

11. Does TRU-64 support devices with block sizes > 512 bytes/block ?

12. File - How to cut a file block by block ?

13. block size vs. blocking factor