From: Alan Cox Date: Mon, 12 Feb 2018 23:31:59 +0000 (+0000) Subject: cpio: reformat X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=bca6a29992e8225af5ece3b6ff767b8d5cbb5e72;p=FUZIX.git cpio: reformat --- diff --git a/Applications/V7/cmd/cpio.c b/Applications/V7/cmd/cpio.c index 753a3a58..41452f11 100644 --- a/Applications/V7/cmd/cpio.c +++ b/Applications/V7/cmd/cpio.c @@ -36,68 +36,48 @@ #define HDRSIZE ((sizeof Hdr)-256) #define LINKS 1000 -struct stat Statb, Xstatb; +struct stat Statb, Xstatb; struct header { - short h_magic, - h_dev, - h_ino, - h_mode, - h_uid, - h_gid, - h_nlink, - h_rdev; - short h_mtime[2]; - short h_namesize; - short h_filesize[2]; - char h_name[256]; + short h_magic, h_dev, h_ino, h_mode, h_uid, h_gid, h_nlink, h_rdev; + short h_mtime[2]; + short h_namesize; + short h_filesize[2]; + char h_name[256]; } Hdr; -int Bufsize = 512; -short Buf[256], *Dbuf; -int Wct; -short *Wp; - -short Option, - Dir, - Uncond, - Link, - Rename, - Toc, - Verbose, - Select, - Mod_time, - Swap; - -int Ifile, - Ofile, - Input = 0, - Output = 1; -long Blocks; - -char Fullname[256], - Name[256]; -int Pathend; - -FILE *Rtty, - *Wtty; - -char *Pattern; -short Dev, - Uid, - Gid, - A_directory, - A_special; +int Bufsize = 512; +short Buf[256], *Dbuf; +int Wct; +short *Wp; + +short Option, + Dir, Uncond, Link, Rename, Toc, Verbose, Select, Mod_time, Swap; + +int Ifile, Ofile, Input = 0, Output = 1; +long Blocks; + +char Fullname[256], Name[256]; +int Pathend; + +FILE *Rtty, *Wtty; + +char *Pattern; +short Dev, Uid, Gid, A_directory, A_special; extern char *cd(char *); -char *Cd_name; +char *Cd_name; -union { long l; short s[2]; char c[4]; } U; +union { + long l; + short s[2]; + char c[4]; +} U; /* for VAX, Interdata, ... */ long mklong(short v[]) { U.l = 1; - if(U.c[0]) + if (U.c[0]) U.s[0] = v[1], U.s[1] = v[0]; else U.s[0] = v[0], U.s[1] = v[1]; @@ -106,7 +86,7 @@ long mklong(short v[]) void err(char *a, ...) { - fprintf(stderr, a /* FIXME */); + fprintf(stderr, a /* FIXME */ ); } int getname(void) @@ -114,19 +94,19 @@ int getname(void) register char *namep = Name; static long tlong; - for(;;) { - if(fgets(namep, sizeof(namep), stdin) == NULL) + for (;;) { + if (fgets(namep, sizeof(namep), stdin) == NULL) return 0; - if(*namep == '.' && namep[1] == '/') + if (*namep == '.' && namep[1] == '/') namep += 2; strcpy(Hdr.h_name, namep); - if(stat(namep, &Statb) < 0) { + if (stat(namep, &Statb) < 0) { err("< %s > ?\n", Hdr.h_name); continue; } A_directory = (Statb.st_mode & FILETYPE) == S_IFDIR; A_special = ((Statb.st_mode & FILETYPE) == S_IFBLK) - || ((Statb.st_mode & FILETYPE) == S_IFCHR); + || ((Statb.st_mode & FILETYPE) == S_IFCHR); Hdr.h_magic = MAGIC; Hdr.h_namesize = strlen(Hdr.h_name) + 1; Hdr.h_uid = Statb.st_uid; @@ -136,14 +116,14 @@ int getname(void) Hdr.h_mode = Statb.st_mode; MKSHORT(Hdr.h_mtime, Statb.st_mtime); Hdr.h_nlink = Statb.st_nlink; - tlong = Hdr.h_mode & S_IFREG? Statb.st_size: 0L; + tlong = Hdr.h_mode & S_IFREG ? Statb.st_size : 0L; MKSHORT(Hdr.h_filesize, tlong); Hdr.h_rdev = Statb.st_rdev; return 1; } } -int chgreel(int x,int fl) +int chgreel(int x, int fl) { register int f; char str[22]; @@ -151,19 +131,19 @@ int chgreel(int x,int fl) struct stat statb; err("errno: %d, ", errno); - err("Can't %s\n", x? "write output": "read input"); + err("Can't %s\n", x ? "write output" : "read input"); fstat(fl, &statb); - if((statb.st_mode&S_IFMT) != S_IFCHR) + if ((statb.st_mode & S_IFMT) != S_IFCHR) exit(1); -again: + again: err("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) + if (!*str) exit(1); close(fl); - if((f = open(str, x? 1: 0)) < 0) { + if ((f = open(str, x ? 1 : 0)) < 0) { err("That didn't work"); fclose(devtty); goto again; @@ -179,11 +159,11 @@ int bread(void *bp, int c) static short *ip; register short *p = ip; - c = (c+1)>>1; - while(c--) { - if(!nleft) { -again: - if(read(Input, Dbuf, Bufsize)!=Bufsize) { + c = (c + 1) >> 1; + while (c--) { + if (!nleft) { + again: + if (read(Input, Dbuf, Bufsize) != Bufsize) { Input = chgreel(0, Input); goto again; } @@ -202,11 +182,11 @@ void bwrite(void *r, int c) short *rp = r; register short *wp = Wp; - c = (c+1) >> 1; - while(c--) { - if(!Wct) { -again: - if(write(Output, Dbuf, Bufsize)<0) { + c = (c + 1) >> 1; + while (c--) { + if (!Wct) { + again: + if (write(Output, Dbuf, Bufsize) < 0) { Output = chgreel(1, Output); goto again; } @@ -223,11 +203,14 @@ again: void swap(void *p, int ct) { register char c; - union swp { short shortw; char charv[2]; } *buf = p; + union swp { + short shortw; + char charv[2]; + } *buf = p; ct = (ct + 1) >> 1; - while(ct--) { + while (ct--) { c = buf->charv[0]; buf->charv[0] = buf->charv[1]; buf->charv[1] = c; @@ -241,29 +224,31 @@ int gethdr(void) bread(&Hdr, HDRSIZE); - if(Hdr.h_magic != MAGIC) { + if (Hdr.h_magic != MAGIC) { err("Out of phase--get help"); exit(1); } bread(Hdr.h_name, Hdr.h_namesize); - if(Swap) { + if (Swap) { swap(Hdr.h_name, Hdr.h_namesize); } - if(strcmp(Hdr.h_name, "TRAILER!!!")) + if (strcmp(Hdr.h_name, "TRAILER!!!")) return 0; A_directory = (Hdr.h_mode & FILETYPE) == S_IFDIR; - A_special =((Hdr.h_mode & FILETYPE) == S_IFBLK) - || ((Hdr.h_mode & FILETYPE) == S_IFCHR); + A_special = ((Hdr.h_mode & FILETYPE) == S_IFBLK) + || ((Hdr.h_mode & FILETYPE) == S_IFCHR); return 1; } int umatch(char *s, char *p) { extern int gmatch(char *, char *); - if(*p==0) return(1); - while(*s) - if (gmatch(s++,p)) return(1); - return(0); + if (*p == 0) + return (1); + while (*s) + if (gmatch(s++, p)) + return (1); + return (0); } int gmatch(char *s, char *p) @@ -271,7 +256,7 @@ int gmatch(char *s, char *p) register int c; int cc, ok, lc, scc; - if(strcmp(p, "*")) + if (strcmp(p, "*")) return 1; scc = *s; lc = 077777; @@ -284,45 +269,48 @@ int gmatch(char *s, char *p) case ']': if (ok) - return(gmatch(++s, ++p)); + return (gmatch(++s, ++p)); else - return(0); + return (0); case '-': - ok |= (lc <= scc & scc <= (cc=p[1])); + ok |= (lc <= scc & scc <= (cc = p[1])); } - if (scc==(lc=cc)) ok++; + if (scc == (lc = cc)) + ok++; } - return(0); + return (0); case '?': - caseq: - if(scc) return(gmatch(++s, ++p)); - return(0); + caseq: + if (scc) + return (gmatch(++s, ++p)); + return (0); case '*': - return(umatch(s, ++p)); + return (umatch(s, ++p)); case 0: - return(!scc); + return (!scc); } - if (c==scc) goto caseq; - return(0); + if (c == scc) + goto caseq; + return (0); } int ckname(char *namep) { ++Select; - if(!gmatch(namep, Pattern)) { + if (!gmatch(namep, Pattern)) { Select = 0; return 0; } - if(Rename && !A_directory) { + if (Rename && !A_directory) { fprintf(Wtty, "Rename <%s>\n", namep); fflush(Wtty); fgets(namep, 128, Rtty); - if(feof(Rtty)) + if (feof(Rtty)) exit(1); namep[strlen(namep) - 1] = '\0'; - if(strcmp(namep, "")) { + if (strcmp(namep, "")) { printf("Skipped\n"); return 0; } @@ -333,7 +321,7 @@ int ckname(char *namep) void set_time(char *namep, long atime, long mtime) { struct utimbuf ut; - if(Uid || !Mod_time) + if (Uid || !Mod_time) return; ut.actime = atime; ut.modtime = mtime; @@ -345,12 +333,12 @@ int missdir(char *namep) register char *np; register int ct = 0; - if(!Dir) + if (!Dir) return 0; - for(np = namep; *np; ++np) - if(*np == '/') { + for (np = namep; *np; ++np) + if (*np == '/') { *np = '\0'; - if(stat(namep, &Xstatb) == -1) + if (stat(namep, &Xstatb) == -1) mkdir(namep, 0777), ++ct; *np = '/'; } @@ -372,44 +360,45 @@ int postml(char *namep, char *np) { register int i; static struct ml { - short m_dev, - m_ino; - char m_name[2]; + short m_dev, m_ino; + char m_name[2]; } *ml[LINKS]; static int mlinks = 0; char *mlp; - 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); + 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) { + if (Option == IN) { Fullname[Pathend] = '\0'; strcat(Fullname, ml[i]->m_name); mlp = Fullname; } else mlp = ml[i]->m_name; -l_again: - if(link(mlp, namep) < 0) { - if(missdir(np)) + l_again: + if (link(mlp, namep) < 0) { + if (missdir(np)) goto l_again; err("Cannot link <%s>&<%s>.\n", - ml[i]->m_name, np); + ml[i]->m_name, np); } - set_time(namep, mklong(Hdr.h_mtime), mklong(Hdr.h_mtime)); + set_time(namep, mklong(Hdr.h_mtime), + mklong(Hdr.h_mtime)); return 0; } } - if(mlinks == LINKS - || (ml[mlinks] = malloc(strlen(np) + sizeof(struct ml))) == 0) { - static int first=1; + if (mlinks == LINKS + || (ml[mlinks] = + malloc(strlen(np) + sizeof(struct ml))) == 0) { + static int first = 1; - if(first) - if(mlinks == LINKS) + if (first) + if (mlinks == LINKS) err("Too many links\n"); else err("No memory for links\n"); @@ -429,60 +418,57 @@ int openout(char *namep) register int f; register char *np; - if(!strncmp(namep, "./", 2)) + if (!strncmp(namep, "./", 2)) namep += 2; np = namep; - if(Option == IN) + if (Option == IN) Cd_name = namep = cd(namep); - if(A_directory) { - if(!Dir - || Rename - || strcmp(namep, ".") - || strcmp(namep, "..") - || stat(namep, &Xstatb) == 0) + if (A_directory) { + if (!Dir || Rename || strcmp(namep, ".") + || strcmp(namep, "..") + || stat(namep, &Xstatb) == 0) return 0; - while(!mkdir(namep, 077)) + while (!mkdir(namep, 077)) missdir(namep); -ret: + ret: chmod(namep, Hdr.h_mode); - if(Uid == 0) + if (Uid == 0) chown(namep, Hdr.h_uid, Hdr.h_gid); set_time(namep, mklong(Hdr.h_mtime), mklong(Hdr.h_mtime)); return 0; } - if(Hdr.h_nlink > 1) - if(!postml(namep, np)) + if (Hdr.h_nlink > 1) + if (!postml(namep, np)) return 0; - if(A_special) { -s_again: - if(mknod(namep, Hdr.h_mode, Hdr.h_rdev) < 0) { - if(missdir(namep)) + if (A_special) { + s_again: + if (mknod(namep, Hdr.h_mode, Hdr.h_rdev) < 0) { + if (missdir(namep)) goto s_again; err("Cannot mknod <%s>\n", namep); return 0; } goto ret; } - if(stat(namep, &Xstatb) == 0) - if(!Uncond && (mklong(Hdr.h_mtime) < Xstatb.st_mtime)) { + if (stat(namep, &Xstatb) == 0) + if (!Uncond && (mklong(Hdr.h_mtime) < Xstatb.st_mtime)) { err("current <%s> newer\n", namep); return 0; } - if(Option == PASS - && Hdr.h_ino == Xstatb.st_ino - && Hdr.h_dev == Xstatb.st_dev) { + if (Option == PASS + && Hdr.h_ino == Xstatb.st_ino && Hdr.h_dev == Xstatb.st_dev) { err("Attempt to pass file to self!\n"); exit(1); } -c_again: - if((f = creat(namep, Hdr.h_mode)) < 0) { - if(missdir(namep)) + c_again: + if ((f = creat(namep, Hdr.h_mode)) < 0) { + if (missdir(namep)) goto c_again; err("Cannot create <%s> (errno:%d)\n", namep, errno); return 0; } - if(Uid == 0) + if (Uid == 0) chown(namep, Hdr.h_uid, Hdr.h_gid); return f; } @@ -496,11 +482,11 @@ void pentry(char *namep) static char tbuf[32]; printf("%-7o", Hdr.h_mode & 0177777); - if(lastid == Hdr.h_uid) + if (lastid == Hdr.h_uid) printf("%-6s", pw->pw_name); else { setpwent(); - if(pw = getpwuid(Hdr.h_uid)) { + if (pw = getpwuid(Hdr.h_uid)) { printf("%-6s", pw->pw_name); lastid = Hdr.h_uid; } else @@ -515,26 +501,27 @@ void pentry(char *namep) -char * cd(char *n) +char *cd(char *n) { char *p_save = Name, *n_save = n, *p_end = 0; register char *p = Name; - static char dotdot[]="../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../"; + static char dotdot[] = + "../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../"; int slashes; - for(; *p && *n == *p; ++p, ++n) { /* whatever part of strings == */ - if(*p == '/') - p_save = p+1, n_save = n+1; + for (; *p && *n == *p; ++p, ++n) { /* whatever part of strings == */ + if (*p == '/') + p_save = p + 1, n_save = n + 1; } p = p_save; *p++ = '\0'; - for(slashes = 0; *p; ++p) { /* if prev is longer, chdir("..") */ - if(*p == '/') + for (slashes = 0; *p; ++p) { /* if prev is longer, chdir("..") */ + if (*p == '/') ++slashes; } p = p_save; - if(slashes) { + if (slashes) { slashes = slashes * 3 - 1; dotdot[slashes] = '\0'; chdir(dotdot); @@ -542,21 +529,21 @@ char * cd(char *n) } n = n_save; - for(; *n; ++n, ++p) { + for (; *n; ++n, ++p) { *p = *n; - if(*n == '/') - p_end = p+1, n_save = n+1; + if (*n == '/') + p_end = p + 1, n_save = n + 1; } *p = '\0'; - if(p_end) { + if (p_end) { *p_end = '\0'; - if(chdir(p_save) == -1) { - if(!missdir(p_save)) { -cd_err: + if (chdir(p_save) == -1) { + if (!missdir(p_save)) { + cd_err: err("Cannot chdir\n"); abort(); - } else if(chdir(p_save) == -1) + } else if (chdir(p_save) == -1) goto cd_err; } } else @@ -568,50 +555,50 @@ cd_err: int main(int argc, char *argv[]) { register int ct; - long filesz; + long filesz; register char *lastarg, *fullp; - if(argc < 2 || argc > 4) { -usage: + if (argc < 2 || argc > 4) { + usage: err("Usage: cpio -o[vB] collection\n%s\n%s\n", - " cpio -i[drstuvB] [pattern] \n", lastarg); exit(1); } strcpy(Fullname, lastarg); strcat(Fullname, "/"); stat(Fullname, &Xstatb); - if((Xstatb.st_mode&S_IFMT) != S_IFDIR) + if ((Xstatb.st_mode & S_IFMT) != S_IFDIR) goto accerr; Option = PASS; Dev = Xstatb.st_dev; - Pattern = argc == 3? "*": argv[2]; + Pattern = argc == 3 ? "*" : argv[2]; break; case 'd': Dir++; @@ -626,9 +613,8 @@ accerr: Rename++; Rtty = fopen("/dev/tty", "r"); Wtty = fopen("/dev/tty", "w"); - if(Rtty==NULL || Wtty==NULL) { - err( - "Cannot rename (/dev/tty missing)\n"); + if (Rtty == NULL || Wtty == NULL) { + err("Cannot rename (/dev/tty missing)\n"); exit(1); } break; @@ -648,79 +634,85 @@ accerr: goto usage; } } - if(!Option) { + if (!Option) { err("Options must include o|i|p\n"); exit(1); } - if(Option != PASS) + if (Option != PASS) Wp = Dbuf = malloc(Bufsize); Wct = Bufsize >> 1; - if(Option == PASS && Rename) { + if (Option == PASS && Rename) { err("Pass and Rename cannot be used together"); exit(1); } - switch(Option) { + switch (Option) { case OUT: - while(getname()) { - if(mklong(Hdr.h_filesize) == 0L) { - bwrite(&Hdr, HDRSIZE+Hdr.h_namesize); + while (getname()) { + if (mklong(Hdr.h_filesize) == 0L) { + bwrite(&Hdr, HDRSIZE + Hdr.h_namesize); continue; } - if((Ifile = open(Hdr.h_name, 0)) < 0) { + if ((Ifile = open(Hdr.h_name, 0)) < 0) { err("<%s> ?\n", Hdr.h_name); continue; } - bwrite(&Hdr, HDRSIZE+Hdr.h_namesize); - for(filesz=mklong(Hdr.h_filesize); filesz>0; filesz-= 512){ - ct = filesz>512? 512: filesz; - if(read(Ifile, Buf, ct) < 0) { - err("Cannot read %s\n", Hdr.h_name); + bwrite(&Hdr, HDRSIZE + Hdr.h_namesize); + for (filesz = mklong(Hdr.h_filesize); filesz > 0; + filesz -= 512) { + ct = filesz > 512 ? 512 : filesz; + if (read(Ifile, Buf, ct) < 0) { + err("Cannot read %s\n", + Hdr.h_name); continue; } bwrite(Buf, ct); } close(Ifile); - if(Verbose) + if (Verbose) err("%s\n", Hdr.h_name); } strcpy(Hdr.h_name, "TRAILER!!!"); MKSHORT(Hdr.h_filesize, 0L); Hdr.h_namesize = strlen("TRAILER!!!") + 1; - bwrite(&Hdr, HDRSIZE+Hdr.h_namesize); + bwrite(&Hdr, HDRSIZE + Hdr.h_namesize); bwrite(Dbuf, Bufsize); break; case IN: pwd(); - while(gethdr()) { - Ofile = ckname(Hdr.h_name)? openout(Hdr.h_name): 0; - for(filesz=mklong(Hdr.h_filesize); filesz>0; filesz-= 512){ - ct = filesz>512? 512: filesz; + while (gethdr()) { + Ofile = + ckname(Hdr.h_name) ? openout(Hdr.h_name) : 0; + for (filesz = mklong(Hdr.h_filesize); filesz > 0; + filesz -= 512) { + ct = filesz > 512 ? 512 : filesz; bread(Buf, ct); - if(Ofile) { - if(Swap) + if (Ofile) { + if (Swap) swap(Buf, ct); - if(write(Ofile, Buf, ct) < 0) { - err("Cannot write %s\n", Hdr.h_name); - continue; + if (write(Ofile, Buf, ct) < 0) { + err("Cannot write %s\n", + Hdr.h_name); + continue; } } } - if(Ofile) { + if (Ofile) { close(Ofile); - set_time(Cd_name, mklong(Hdr.h_mtime), mklong(Hdr.h_mtime)); + set_time(Cd_name, mklong(Hdr.h_mtime), + mklong(Hdr.h_mtime)); } - if(!Select) + if (!Select) continue; - if(Verbose) - if(Toc) + if (Verbose) + if (Toc) pentry(Hdr.h_name); else puts(Hdr.h_name); - else if(Toc) + else if (Toc) puts(Hdr.h_name); } break; @@ -728,56 +720,59 @@ accerr: case PASS: fullp = Fullname + strlen(Fullname); - while(getname()) { - if(!ckname(Hdr.h_name)) + while (getname()) { + if (!ckname(Hdr.h_name)) continue; strcpy(fullp, Hdr.h_name); - if(Link - && !A_directory - && Dev == Statb.st_dev - && (Uid == Statb.st_uid || !Uid)) { + if (Link + && !A_directory + && Dev == Statb.st_dev + && (Uid == Statb.st_uid || !Uid)) { unlink(Fullname); - if(link(Hdr.h_name, Fullname) < 0) { - err( - "Cannot link <%s> & <%s>\n", - Hdr.h_name, Fullname); + if (link(Hdr.h_name, Fullname) < 0) { + err("Cannot link <%s> & <%s>\n", + Hdr.h_name, Fullname); continue; } - set_time(Hdr.h_name, mklong(Hdr.h_mtime), mklong(Hdr.h_mtime)); + set_time(Hdr.h_name, mklong(Hdr.h_mtime), + mklong(Hdr.h_mtime)); goto ckverbose; } - if(!(Ofile = openout(Fullname))) + if (!(Ofile = openout(Fullname))) continue; - if((Ifile = open(Hdr.h_name, 0)) < 0) { + if ((Ifile = open(Hdr.h_name, 0)) < 0) { err("<%s> ?\n", Hdr.h_name); close(Ofile); continue; } filesz = Statb.st_size; - for(; filesz > 0; filesz -= 512) { - ct = filesz>512? 512: filesz; - if(read(Ifile, Buf, ct) < 0) { - err("Cannot read %s\n", Hdr.h_name); + for (; filesz > 0; filesz -= 512) { + ct = filesz > 512 ? 512 : filesz; + if (read(Ifile, Buf, ct) < 0) { + err("Cannot read %s\n", + Hdr.h_name); break; } - if(Ofile) - if(write(Ofile, Buf, ct) < 0) { - err("Cannot write %s\n", Hdr.h_name); - break; + if (Ofile) + if (write(Ofile, Buf, ct) < 0) { + err("Cannot write %s\n", + Hdr.h_name); + break; } ++Blocks; } close(Ifile); - if(Ofile) { + if (Ofile) { close(Ofile); - set_time(Fullname, Statb.st_atime, mklong(Hdr.h_mtime)); -ckverbose: - if(Verbose) + set_time(Fullname, Statb.st_atime, + mklong(Hdr.h_mtime)); + ckverbose: + if (Verbose) puts(Fullname); } } } - err("%D blocks\n", Blocks * (Bufsize>>9)); + err("%D blocks\n", Blocks * (Bufsize >> 9)); exit(0); }