📄 cpio.c
字号:
} Hdr.h_filesize[0] = Actual_size[0]; Hdr.h_filesize[1] = Actual_size[1]; } if (!(One_extent || Multi_extent)) {#endif /* try creating (only twice) */ ans = 0; do { if((f = creat(namep, Hdr.h_mode)) < 0) { ans += 1; }else { ans = 0; break; } }while(ans < 2 && missdir(np) == 0); if(ans == 1) { fprintf(stderr,"Cannot create directory for <%s> (errno:%d)\n", namep, errno); return(0); }else if(ans == 2) { fprintf(stderr,"Cannot create <%s> (errno:%d)\n", namep, errno); return(0); }#ifdef RT }#endif if(Uid == 0) chown(namep, Hdr.h_uid, Hdr.h_gid); return f;}/* Binary Read */bread(b, c)register c;register short *b;{ static nleft = 0; static short *ip; register int rv; register short *p = ip; register int in; printd(stderr,"%s ",Hdr.h_name); printd(stderr,"bread size %d\n", c); c = (c+1)>>1; while(c--) { if(nleft == 0) { in = 0; while((rv=read(Input, &(((char *)Dbuf)[in]), Bufsize - in)) != Bufsize - in) { if(rv <= 0) { Input = chgreel(0, Input); continue; } in += rv; nleft += (rv >> 1); } nleft += (rv >> 1); p = Dbuf; ++Blocks; } *b++ = *p++; --nleft; } ip = p;}/* Read Header */readhdr(b, c)register c;register char *b;{ static nleft = 0; static char *ip; register int rv; register char *p = ip; register int in; printd(stderr,"%s ",Hdr.h_name); printd(stderr,"readhdr size %d \n",c); while(c--) { if(nleft == 0) { in = 0; while((rv=read(Input, &(((char *)Cbuf)[in]), Bufsize - in)) != Bufsize - in) { if(rv <= 0) { Input = chgreel(0, Input); continue; } in += rv; nleft += rv; } nleft += rv; p = Cbuf; ++Blocks; } *b++ = *p++; --nleft; } ip = p;}/* Binary Write */bwrite(rp, c)register short *rp;register c;{ register short *wp = Wp; printd(stderr,"%s ",Hdr.h_name); printd(stderr,"bwrite size = %d \n",c); c = (c+1) >> 1; while(c--) { if(!Wct) {again: if(write(Output, Dbuf, Bufsize)<0) { Output = chgreel(1, Output); goto again; } Wct = Bufsize >> 1; wp = Dbuf; ++Blocks; } *wp++ = *rp++; --Wct; } Wp = wp;}/* Write Header */writehdr(rp, c)register char *rp;register c;{ register char *cp = Cp; printd(stderr,"%s ",Hdr.h_name); printd(stderr,"writehdr size %d \n",c); printd(stderr,"writehdr Hdr.h_filesize %d \n", Hdr.h_filesize); printd(stderr,"writehdr Hdr.h_namesize %d \n", Hdr.h_namesize); while(c--) { if(!Wc) {again: if(write(Output,Cbuf,Bufsize)<0) { Output = chgreel(1,Output); goto again; } Wc = Bufsize; cp = Cbuf; ++Blocks; } *cp++ = *rp++; --Wc; } Cp = cp;}/* Perform Post Linking */postml(namep, np)register char *namep, *np;{ register i; static struct ml { short m_dev; ino_t m_ino; char m_name[2]; } *ml[LINKS]; static mlinks = 0; char *mlp; int ans; for(i = 0; i < mlinks; ++i) { if(mlinks == LINKS) break; if(ml[i]->m_ino==Hdr.h_ino && ml[i]->m_dev==Hdr.h_dev) { if(Verbose) printf("%s linked to %s\n", ml[i]->m_name, np); unlink(namep); if(Option == IN && *ml[i]->m_name != '/') { Fullname[Pathend] = '\0'; strcat(Fullname, ml[i]->m_name); mlp = Fullname; } mlp = ml[i]->m_name; /* try linking (only twice) */ ans = 0; do { if(link(mlp, namep) < 0) { ans += 1; }else { ans = 0; break; } }while(ans < 2 && missdir(np) == 0); if(ans == 1) { fprintf(stderr,"Cannot create directory for <%s> (errno:%d)\n", np, errno); return(0); }else if(ans == 2) { fprintf(stderr,"Cannot link <%s> & <%s>.\n", ml[i]->m_name, np); return(0); } set_time(namep, mklong(Hdr.h_mtime), mklong(Hdr.h_mtime)); return 0; } } if(mlinks == LINKS || !(ml[mlinks] = (struct ml *)malloc(strlen(np) + 2 + sizeof(struct ml)))) { static int first=1; if(first) if(mlinks == LINKS) fprintf(stderr,"Too many links\n"); else fprintf(stderr,"No memory for links\n"); mlinks = LINKS; first = 0; return 1; } ml[mlinks]->m_dev = Hdr.h_dev; ml[mlinks]->m_ino = Hdr.h_ino; strcpy(ml[mlinks]->m_name, np); ++mlinks; return 1;}/* Print Verbose Table of Contents */pentry(namep)register char *namep;{ static short lastid = -1;#include <pwd.h> static struct passwd *pw; struct passwd *getpwuid(); static char tbuf[32]; char *ctime(); printf("%-7o", Hdr.h_mode & 0177777); if(lastid == Hdr.h_uid) printf("%-6s", pw->pw_name); else { setpwent(); if(pw = getpwuid((int)Hdr.h_uid)) { printf("%-6s", pw->pw_name); lastid = Hdr.h_uid; } else { printf("%-6d", Hdr.h_uid); lastid = -1; } } printf("%7ld ", mklong(Hdr.h_filesize)); U.l = mklong(Hdr.h_mtime); strcpy(tbuf, ctime((long *)&U.l)); tbuf[24] = '\0'; printf(" %s %s\n", &tbuf[4], namep);}/* pattern matching functions */nmatch(s, pat)char *s, **pat;{ if(EQ(*pat, "*")) return 1; while(*pat) { if((**pat == '!' && !gmatch(s, *pat+1)) || gmatch(s, *pat)) return 1; ++pat; } return 0;}gmatch(s, p)register char *s, *p;{ register int c; register cc, ok, lc, scc; scc = *s; lc = 077777; switch (c = *p) { case '[': ok = 0; while (cc = *++p) { switch (cc) { case ']': if (ok) return(gmatch(++s, ++p)); else return(0); case '-': ok |= ((lc <= scc) && (scc <= (cc=p[1]))); } if (scc==(lc=cc)) ok++; } return(0); case '?': caseq: if(scc) return(gmatch(++s, ++p)); return(0); case '*': return(umatch(s, ++p)); case 0: return(!scc); } if (c==scc) goto caseq; return(0);}umatch(s, p)register char *s, *p;{ if(*p==0) return(1); while(*s) if (gmatch(s++,p)) return(1); return(0);}/* make needed directories */makdir(namep)register char *namep;{ static status; register pid; if(pid = fork()) while(wait(&status) != pid); else { close(2); execl("/bin/mkdir", "mkdir", namep, 0); exit(2); } return ((status>>8) & 0377)? 1: 0;}/* swap halfwords, bytes or both */swap(buf, ct)register ct;register char *buf;{ register char c; register union swp { long longw; short shortv[2]; char charv[4]; } *pbuf; int savect, n, i; char *savebuf; short cc; savect = ct; savebuf = buf; if(byteswap || bothswap) { if (ct % 2) buf[ct] = 0; ct = (ct + 1) / 2; while (ct--) { c = *buf; *buf = *(buf + 1); *(buf + 1) = c; buf += 2; } if (bothswap) { ct = savect; pbuf = (union swp *)savebuf; if (n = ct % sizeof(union swp)) { if(n % 2) for(i = ct + 1; i <= ct + (sizeof(union swp) - n); i++) pbuf->charv[i] = 0; else for (i = ct; i < ct + (sizeof(union swp) - n); i++) pbuf->charv[i] = 0; } ct = (ct + (sizeof(union swp) -1)) / sizeof(union swp); while(ct--) { cc = pbuf->shortv[0]; pbuf->shortv[0] = pbuf->shortv[1]; pbuf->shortv[1] = cc; ++pbuf; } } } else if (halfswap) { pbuf = (union swp *)buf; if (n = ct % sizeof(union swp)) for (i = ct; i < ct + (sizeof(union swp) - n); i++) pbuf->charv[i] = 0; ct = (ct + (sizeof(union swp) -1)) / sizeof(union swp); while (ct--) { cc = pbuf->shortv[0]; pbuf->shortv[0] = pbuf->shortv[1]; pbuf->shortv[1] = cc; ++pbuf; } }}/* set access and modification times */set_time(namep, atime, mtime)register *namep;long atime, mtime;{ static long timevec[2]; if( (A_symlink) || (!Mod_time)) return; timevec[0] = atime; timevec[1] = mtime; utime(namep, timevec);}/* Request a Reel Change(for multi-volume tapes */chgreel(x, fl){ register f; char str[22]; FILE *devtty; struct stat statb; fprintf(stderr,"errno: %d, ", errno); if (errno == ENOSPC) { fprintf(stderr, "No space left on device\n"); } else { fprintf(stderr,"Can't %s\n", x? "write output": "read input"); } fstat(fl, &statb);#ifndef RT if((statb.st_mode&S_IFMT) != S_IFCHR) exit(2);#else if((statb.st_mode & (S_IFBLK|S_IFREC))==0) exit(2);#endifagain: fprintf(stderr,"If you want to go on, type device/file name when ready\n"); devtty = fopen("/dev/tty", "r"); fgets(str, 20, devtty); str[strlen(str) - 1] = '\0'; if(!*str) exit(2); close(fl); if((f = open(str, x? O_WRONLY: O_RDONLY)) < 0) { fprintf(stderr,"That didn't work"); fclose(devtty); goto again; } return f;}/* Missing Directory */missdir(namep)register char *namep;{ register char *np; register ct = 2; for(np = namep; *np; ++np) if(*np == '/') { /* skip over 'root slash' */ if(np == namep) continue; *np = '\0'; if(STAT(namep, &Xstatb) == -1) { if(Dir) { if((ct = makdir(namep)) != 0) { *np = '/'; return(ct); } }else { fprintf(stderr,"missing 'd' option\n"); return(-1); } } *np = '/'; } if (ct == 2) ct = 0; /* the file already exists */ return ct;}/* get working directory */pwd() { FILE *dir; dir = popen("pwd", "r"); fgets(Fullname, 256, dir); if(pclose(dir)) { perror("cpio pwd"); exit(2); } Pathend = strlen(Fullname); Fullname[Pathend - 1] = '/';}#ifdef RTactsize(file)register int file;{ long tlong; long fsize(); register int tfile; Actual_size[0] = Hdr.h_filesize[0]; Actual_size[1] = Hdr.h_filesize[1]; if (!Extent) return; if (file) tfile = file; else if ((tfile = open(Hdr.h_name,O_RDONLY)) < 0) return; tlong = fsize(tfile); MKSHORT(Hdr.h_filesize,tlong); if (Cflag) bintochar(tlong); if (!file) close(tfile);}#endifmatch_ino(){ short i, found_ino = 0; struct linkbuf *idx, *save_lbp; for (idx = lbp; idx; idx = idx->next_lbp) { save_lbp = idx; if (idx->real_ino == Statb.st_ino && idx->devnum == Statb.st_dev) { /* * Found the link. Set inode field in * this file's header to be the "fake" * inode of the linked-to file. */ Hdr.h_ino = idx->fake_ino; found_ino++; break; } } if (!found_ino) { /* Inode not found on list; create new entry */ idx = (struct linkbuf *) malloc(sizeof(*idx)); if (!idx) { fprintf(stderr, "Out of memory for links\n"); fprintf(stderr, "File link data may be corrupt\n"); return(0); } idx->real_ino = Statb.st_ino; idx->devnum = Statb.st_dev; Hdr.h_ino = idx->fake_ino = next_ino(); if (lbp) { save_lbp->next_lbp = idx; } else { lbp = idx; } } return(0);}/* * Establish next available internal inode. * Inodes are allocated (as in reality) on a per devnum basis. */ino_tnext_ino() { if (internal_ino[devidx]++ > 0777777L) { fprintf(stderr, "Cannot archive < %s > (too many files in archive)\n", Hdr.h_name); exit(2); } return(internal_ino[devidx]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -