|
|
1.1 ! root 1: /* ntar.c ! 2: * ! 3: * ! 4: * A Non-Toxic ARchive program. ! 5: * ! 6: * ! 7: * version 1.8, 12/5/80 ! 8: * ! 9: * ! 10: */ ! 11: ! 12: #include "stdio.h" ! 13: #define TRUE 1 ! 14: #define FALSE 0 ! 15: #define BLK 512 /* input buffer size */ ! 16: #define HSIZE 128 /* max size of headers and trailers */ ! 17: #define FSIZE 128 /* max size of filenames (with paths) */ ! 18: ! 19: ! 20: FILE *rch = stdin; /* input channel */ ! 21: FILE *wch = stdout; /* output channel */ ! 22: FILE *ech = stderr; /* error channel */ ! 23: int rargc; /* command line pointers */ ! 24: char **rargv; ! 25: int func, list; /* control variables for requested functions */ ! 26: int ibp = 0; /* input buffer pointer */ ! 27: int backup = 0; /* re-read char count */ ! 28: char fn[FSIZE]; /* filename buffer */ ! 29: char ifn[FSIZE]; /* command line file names buffer */ ! 30: int fnp = 0; /* filename (after path) pointer */ ! 31: ! 32: char head[HSIZE] = { ! 33: "**************************************************start file: "}; ! 34: char tail[HSIZE] = { ! 35: "**************************************************end file: "}; ! 36: ! 37: ! 38: char whatstring[] = ! 39: {"@(#)ntar.c 1.8"}; ! 40: main(argc,argv) ! 41: int argc; ! 42: char ** argv; ! 43: { ! 44: register int i; ! 45: register char *cp, *p; ! 46: ! 47: extern int null(), wrch(); ! 48: ! 49: while (--argc && ((++argv)[0][0] == '-')) ! 50: ! 51: switch (argv[0][1]) { ! 52: ! 53: case 'p': ! 54: case 'g': ! 55: if (!func) ! 56: func = argv[0][1]; /* save function key */ ! 57: else ! 58: error("conflicting or duplicate functions"); ! 59: break; ! 60: ! 61: case 'l': ! 62: list++; ! 63: break; ! 64: ! 65: case 'd': ! 66: if (--argc < 0) opterr(); ! 67: p = (++argv)[0]; ! 68: for (fnp=0; ((fnp<(FSIZE-1))&&(fn[fnp++]=(*p++))); ) ! 69: ; ! 70: if (fnp < (FSIZE-1)) fn[fnp-1] = '/'; ! 71: break; ! 72: ! 73: case 'h': ! 74: if (--argc < 0) opterr(); ! 75: p = (++argv)[0]; ! 76: cp = head; ! 77: for (i=0; (i<(HSIZE-1))&&(*cp++ = *p++); i++) ! 78: ; ! 79: break; ! 80: ! 81: case 't': ! 82: if (--argc < 0) opterr(); ! 83: p = (++argv)[0]; ! 84: cp = tail; ! 85: for (i=0; (i<(HSIZE-1))&&(*cp++ = *p++); i++) ! 86: ; ! 87: break; ! 88: ! 89: default: opterr(); ! 90: } ! 91: ! 92: if (!func && !list) { ! 93: print("no function (-p, -g, or -l)\n"); ! 94: exit(1); } ! 95: ! 96: rargc = ++argc; /* use rargc, rargv instead of originals */ ! 97: rargv = --argv; ! 98: ! 99: if (--rargc > 0) ! 100: getfile(); /* open first file */ ! 101: ! 102: if (!func) /* then list only */ ! 103: ! 104: while (find(head,null)) ! 105: pname(); ! 106: ! 107: else if (func == 'p') { ! 108: ! 109: while (wrch(gch()) != EOF) ; /* copy text */ ! 110: fclose(wch); } ! 111: ! 112: else { /* do get function */ ! 113: ! 114: while (find(head, wrch)) { ! 115: ! 116: for (i=fnp; ((i<(FSIZE-1)) && ! 117: ((fn[i++]=gch())!='\n')); ) ; ! 118: fn[i-1] = 0; /* terminate filename string */ ! 119: if (list) { print(fn); print("\n"); } ! 120: if ((i = creat(fn, 0644)) < 0) ! 121: error("can't create ", fn); ! 122: else wch = fdopen(i, "w"); ! 123: if (!find(tail, wrch)) ! 124: error("missing tail for ", &fn[fnp]); ! 125: if ((!match(&fn[fnp])) || (gch()!='\n')) ! 126: error("wrong tail name for ", &fn[fnp]); ! 127: fclose(wch); ! 128: wch = stdout; /* reset to standard output */ ! 129: }} ! 130: ! 131: exit(0); ! 132: } ! 133: ! 134: ! 135: opterr() ! 136: { ! 137: print("usage: ntar [-h `header'] [-t `trailer'] [-d path] [-l] [-g] [-p] [file ...]\n"); ! 138: exit(-1); ! 139: } ! 140: find(str, f) ! 141: char *str; ! 142: int (*f)(); ! 143: { int ch; ! 144: ! 145: while (!match(str)) ! 146: do { ! 147: if ((ch = gch()) == EOF) return FALSE; ! 148: (*f)(ch); } ! 149: while (ch != '\n'); ! 150: ! 151: return TRUE; ! 152: } ! 153: ! 154: ! 155: match(str) ! 156: char *str; ! 157: { int i; ! 158: ! 159: for (i = 1; gch() == *str++; i++) ! 160: if (!*str) return TRUE; ! 161: ibp = ibp - i; ! 162: backup = backup + i; ! 163: return FALSE; ! 164: } ! 165: ! 166: ! 167: gch() ! 168: { ! 169: static int ib[BLK]; /* input buffer */ ! 170: ! 171: if (ibp < 0) ibp = ibp + BLK; /* wrap around */ ! 172: if (ibp == BLK) ibp = 0; ! 173: ! 174: if (!backup) { ! 175: ! 176: while ((ib[ibp++] = getc(rch)) == EOF) { ! 177: ! 178: fclose(rch); /* close input */ ! 179: ! 180: if (ifn[0]) { ! 181: wrstr(tail); /* write matching tail */ ! 182: wrstr(ifn); ! 183: wrch('\n'); ! 184: ifn[0] = 0; } ! 185: ! 186: if (--rargc <= 0) ! 187: return EOF; /* done if no more files */ ! 188: else ! 189: getfile(); /* otherwise get the next */ ! 190: ! 191: ibp--; } ! 192: ! 193: return ib[ibp-1]; } ! 194: ! 195: else if (ib[ibp++] != EOF) { ! 196: backup--; ! 197: return ib[ibp-1]; } ! 198: ! 199: else return EOF; ! 200: } ! 201: ! 202: getfile() ! 203: { register int i; ! 204: register char *p; ! 205: ! 206: if ((rch=fopen((++rargv)[0],"r")) == NULL) ! 207: error("can't open ", rargv[0]); ! 208: ! 209: if (func == 'p') { /* put? write name if so */ ! 210: wrstr(head); ! 211: wrstr(rargv[0]); /* header */ ! 212: wrch('\n'); ! 213: for (p=rargv[0],i=0; ifn[i++]=(*p++); ) ; ! 214: if (list) { ! 215: print(ifn); /* give name */ ! 216: print("\n"); }} ! 217: } ! 218: ! 219: error(st1, st2) ! 220: char *st1, *st2; ! 221: { ! 222: print(st1); ! 223: print(st2); ! 224: print("\n"); ! 225: exit(-1); ! 226: } ! 227: ! 228: ! 229: print(str) ! 230: char *str; ! 231: { ! 232: while (*str) ! 233: putc(*str++, ech); ! 234: ! 235: } ! 236: ! 237: ! 238: pname() ! 239: { ! 240: while (putc(gch(), ech) != '\n'); ! 241: } ! 242: ! 243: ! 244: wrstr(str) ! 245: char *str; ! 246: { ! 247: while (*str) ! 248: wrch(*str++); ! 249: } ! 250: ! 251: ! 252: wrch(ch) ! 253: int ch; ! 254: { ! 255: return (ch != EOF ? putc(ch,wch) : ch); ! 256: } ! 257: ! 258: ! 259: ! 260: null(ch) ! 261: char ch; ! 262: {}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.