create.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 696 行 · 第 1/2 页

C
696
字号
                        if (f_verbose) {
                            annorec(stdout, (char *) NULL);
                            printf("%s\n", fname);
                        }
#endif
        donefile:
                        break;

                        /*
                         * File shrunk or gave error, pad out tape to match the size we
                         * specified in the header. ??? Doesn't do that here!  Must be a
                         * FIXME. -- jer
                         */
        padit:
                        abort();
                }

#if defined S_IFLNK && defined __UNIX__
        case S_IFLNK:                           /* Symbolic link */
                {
                        int             size;

                        statbuf->st_size = 0;           /* Force 0 size on symlink */
                        header = start_header(fname, statbuf);
                        if (header == NULL)
                                goto badfile;
                        size = readlink(fname, header->header.linkname, NAMSIZ);
                        if (size < 0)
                                goto badperror;
                        if (size == NAMSIZ)
                        {
                                annorec(stderr, tar);
                                fprintf(stderr,
                                        "%s: symbolic link too long\n", fname);
                                break;
                        }
                        header->header.linkname[size] = '\0';
                        header->header.linkflag = LF_SYMLINK;
                        finish_header(header);          /* Nothing more to do to it */
#ifdef OLDVERBOSE
                        if (f_verbose)
                        {
                                annorec(stdout, (char *) NULL);
                                printf("%s\n", fname);
                        }
#endif
                }
                break;
#endif

        case S_IFDIR:                           /* Directory */
                {
                        DIR     *dirp;
                        struct dirent *d;
                        char    namebuf[NAMSIZ + 2];
                        int     len;

                        /* Build new prototype name */
                        strncpy(namebuf, fname, sizeof(namebuf));
                        len = strlen(namebuf);
                        while (len >= 1 && '/' == namebuf[len - 1])
                                len--;                  /* Delete trailing slashes */
                        namebuf[len++] = '/';           /* Now add exactly one back */

                        /*
                         * Output directory header record with permissions FIXME, do this
                         * AFTER files, to avoid R/O dir problems? If Unix Std format,
                         * don't put / on end of dir name If old archive format, don't
                         * write record at all.
                         */
                        if (!f_oldarch)
                        {
                                statbuf->st_size = 0;   /* Force 0 size on dir */

                                /*
                                 * If people could really read standard archives, this should
                                 * be:          (FIXME) header = start_header(f_standard? p:
                                 * namebuf, statbuf); but since they'd interpret LF_DIR
                                 * records as regular files, we'd better put the / on the
                                 * name.
                                 */
                                header = start_header(namebuf, statbuf);
                                if (header == NULL)
                                        goto badfile;           /* eg name too long */
                                if (f_standard)
                                {
                                        header->header.linkflag = LF_DIR;
                                }
                                finish_header(header);  /* Done with directory header */
                        }
#ifdef OLDVERBOSE
                        if (f_verbose)
                        {
                                annorec(stdout, (char *) NULL);
                                printf("%s\n", fname);
                        }
#endif
                        /*
                         * Hack to remove "./" from the front of all the file names
                         */
                        if (len == 2 && namebuf[0] == '.')
                        {
                                len = 0;
                        }

                        /* Now output all the files in the directory */
                        errno = 0;
                        dirp = opendir(fname);
                        if (!dirp) {
                            if (errno) {
                                perror(fname);
                            } else {
                                annorec(stderr, tar);
                                fprintf(stderr, "%s: error opening directory",
                                                fname);
                            }
                            break;
                        }

                        /* Should speed this up by cd-ing into the dir, FIXME */
                        while (NULL != (d = readdir(dirp)))
                        {
                                /* Skip . and .. */
                                if (d->d_name[0] == '.')
                                {
                                        if (d->d_name[1] == '\0')
                                                continue;
                                        if (d->d_name[1] == '.')
                                        {
                                                if (d->d_name[2] == '\0')
                                                        continue;
                                        }
                                }
                                if (strlen(d->d_name) + len >= NAMSIZ)
                                {
                                        annorec(stderr, tar);
                                        fprintf(stderr, "%s%s: name too long\n",
                                                namebuf, d->d_name);
                                        continue;
                                }
                                strcpy(namebuf + len, d->d_name);
                                dump_file(namebuf);
                        }

                        closedir(dirp);
                }
                break;

        case S_IFCHR:                           /* Character special file */
                type = LF_CHR;
                goto easy;

#ifdef S_IFBLK
        case S_IFBLK:                           /* Block     special file */
                type = LF_BLK;
                goto easy;
#endif

#ifdef S_IFIFO
        case S_IFIFO:                           /* Fifo      special file */
                type = LF_FIFO;
#endif /* S_IFIFO */

easy:
                if (!f_standard)
                        goto unknown;

#if 0
                /* this was a bad kludge, it had the side-effect of causing
                 * skipping of the data part of files when a "list" was
                 * done to work right for special files.  But, Minix needs
                 * this size since it's used to size the device. - JER */
                statbuf->st_size = 0;   /* Force 0 size */
#endif
                header = start_header(fname, statbuf);
                if (header == NULL)
                        goto badfile;           /* eg name too long */

                header->header.linkflag = type;
                if (type != LF_FIFO)
                {
                        to_oct((long) major(statbuf->st_rdev), 8,
                                header->header.devmajor);
                        to_oct((long) minor(statbuf->st_rdev), 8,
                                header->header.devminor);
                }

                finish_header(header);
#ifdef OLDVERBOSE
                if (f_verbose)
                {
                        annorec(stdout, (char *) NULL);
                        printf("%s\n", fname);
                }
#endif
                break;

        default:
unknown:
                annorec(stderr, tar);
                fprintf(stderr,
                        "%s: Unknown file type; file ignored.\n", fname);
                break;
        }

        return 1;                                       /* Success */
}


/*
 * Make a header block for the file  name  whose stat info is  st .
 * Return header pointer for success, NULL if the name is too long.
 */
union record   *start_header( char *name, struct stat *st )
{
        union record *header;

        hstat[0] = *st;                         /* save stat for verbose-mode listing */

        header = (union record *) findrec();
        bzero(header->charptr, sizeof(*header));        /* XXX speed up */
        strcpy(header->header.name, name);
        if (header->header.name[NAMSIZ - 1])
        {
                annorec(stderr, tar);
                fprintf(stderr, "%s: name too long\n", name);
                return NULL;
        }
#ifdef MSDOS
        to_oct((long) (st->st_mode & ~(S_IFMT | DOSUMASK)),
                8, header->header.mode);
#else
        to_oct((long) (st->st_mode & ~S_IFMT),
                8, header->header.mode);
#endif
        to_oct((long) st->st_uid, 8, header->header.uid);
        to_oct((long) st->st_gid, 8, header->header.gid);
        to_oct((long) st->st_size, 1 + 12, header->header.size);
        to_oct((long) st->st_mtime, 1 + 12, header->header.mtime);
        /* header->header.linkflag is left as null */

        /* Fill in new Unix Standard fields if desired. */
        if (f_standard) {
                header->header.linkflag = LF_NORMAL;    /* New default */
                strcpy(header->header.magic, TMAGIC);   /* Mark as Unix Std */
#ifndef NONAMES
                finduname(header->header.uname, st->st_uid);
                findgname(header->header.gname, st->st_gid);
#else
                /* don't leave garbage in if no name - JER */
                header->header.uname[0] = '\0';
                header->header.gname[0] = '\0';
#endif
        }
        return header;
}

/*
 * Finish off a filled-in header block and write it out.
 */
void finish_header( union record *header )
{
    int    i, sum;
    char  *p;

    bcopy(CHKBLANKS, header->header.chksum, sizeof(header->header.chksum));

    sum = 0;
    p = header->charptr;
    for (i = sizeof(*header); --i >= 0;) {
        /*
        * We can't use unsigned char here because of old compilers, e.g. V7.
        */
        sum += 0xFF & *p++;
    }

    /*
    * Fill in the checksum field.  It's formatted differently from the other
    * fields:  it has [6] digits, a null, then a space -- rather than
    * digits, a space, then a null. We use to_oct then write the null in
    * over to_oct's space. The final space is already there, from
    * checksumming, and to_oct doesn't modify it.
    *
    * This is a fast way to do: (void) sprintf(header->header.chksum, "%6o",
    * sum);
    */
    to_oct((long) sum, 8, header->header.chksum);
    header->header.chksum[6] = '\0';        /* Zap the space */

    head = header;
    if (f_verbose) {
        print_header(header->header.name);
    }
    
    userec(header);
    return;
}


/*
 * Quick and dirty octal conversion.
 * Converts long "value" into a "digs"-digit field at "where",
 * including a trailing space and room for a null.  "digs"==3 means
 * 1 digit, a space, and room for a null.
 *
 * We assume the trailing null is already there and don't fill it in.
 * This fact is used by start_header and finish_header, so don't change it!
 *
 * This should be equivalent to:
 *      (void) sprintf(where, "%*lo ", digs-2, value);
 * except that sprintf fills in the trailing null and we don't.
 */
void to_oct( long value, int digs, char *where)
{

    --digs;                                         /* Trailing null slot is left alone */
    where[--digs] = ' ';            /* Put in the space, though */

    /* Produce the digits -- at least one */
    do {
        where[--digs] = '0' + (value & 7);              /* one octal digit */
        value >>= 3;
    } while (digs > 0 && value != 0);

    /* Leading spaces, if necessary */
    while (digs > 0) {
        where[--digs] = ' ';
    }

}


/*
 * Write the EOT block(s).
 */
void write_eot( void )
{
        union record   *p;

        p = findrec();
        bzero(p->charptr, RECORDSIZE);
        userec(p);
        /* FIXME, only one EOT block should be needed. */
        p = findrec();
        bzero(p->charptr, RECORDSIZE);
        userec(p);
}

⌨️ 快捷键说明

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