?? ship.c
字號:
{ d |= b << k; k += 7; } else if ((b -= LOWSZ) < LOWSZ) { d |= (b + 0x40) << k; k += 7; } else { d |= b << k; k += 6; } if (k >= 8) { cputc(d, c); d >>= 8; k -= 8; } } decb = d; decn = k; } else { ulg d; /* disperses bytes */ d = k = 0; while ((b = *s++) != 0) if ((b = invsafe[b]) < 85) { d += m[k] * b; if (++k == 5) { cputc(d, c); d >>= 8; cputc(d, c); d >>= 8; cputc(d, c); d >>= 8; cputc(d, c); d = k = 0; } } if (--k > 0) { while (--k) { cputc(d, c); d >>= 8; } cputc(d, c); } }}void unship(v, g, o)char **v; /* arguments */int g; /* number of arguments */int o; /* overwrite flag *//* Extract from the files named in the arguments the files that were encoded by ship. If an argument is "-", then stdin is read. */{ int b; /* state of line loop */ cfile *c; /* output binary file */ FILE *f; /* output file */ char *h; /* name of current ship file */ char l[LNSZ]; /* line buffer on input */ int n; /* next argument to use for input */ char *p; /* modifies line buffer */ char *q; /* scans continuation line */ char *r; /* name of output binary file */ FILE *s; /* current ship file */ int z; /* true if zero files received */ /* Build inverse table */ mkinv(); /* No input or output files initially */ s = NULL; c = NULL; h = r = NULL; /* Loop on input files' lines */ z = 1; /* none received yet */ n = 0; /* start with file zero */ b = 2; /* not in body yet */ while (1) /* return on end of last file */ { /* Get next line from list of files */ while (s == NULL || fgets(l, LNSZ, s) == NULL) { if (s != NULL) fclose(s); if (n >= g) { if (c != NULL) err(SE_PART, r); else if (z) err(SE_NONE, ""); return; } if (v[n][0] == '-') if (v[n][1]) err(SE_ARG, v[n]); else { h = "stream stdin"; s = stdin; } else { h = v[n]; if ((s = fopen(h, "r")) == NULL) err(SE_FIND, h); } n++; b &= ~1; /* not in middle of line */ } /* Strip control characters and leading blank space, if any */ for (q = l; *q && *q <= ' ' && *q != '\n'; q++) ; for (p = l; *q; q++) if (*q >= ' ' || *q == '\n') *p++ = *q; *p = 0; /* Based on current state, end or start on terminator. States are: b == 0: at start of body or body terminator line b == 1: in middle of body line b == 2: at start of non-body line b == 3: in middle of non-body line b == 4: at information line */ switch (b) { case 0: if ((!fast && strcmp(l, "$\n") == 0) || (fast && strcmp(l, "$ f\n") == 0)) { b = 4; break; } /* fall through to case 1 */ case 1: decode((unsigned char *)l, c); b = l[strlen(l) - 1] != '\n'; break; case 2: if (strcmp(l, "$\n") == 0 || strcmp(l, "$ f\n") == 0) { fast = l[1] == ' '; b = 4; break; } /* fall through to case 3 */ case 3: b = l[strlen(l)-1] == '\n' ? 2 : 3; break; case 4: /* Possible information lines are ship, more, cont, and end */ if (l[b = strlen(l) - 1] != '\n') err(SE_FORM, h); l[b] = 0; if (strncmp(l, "ship ", 5) == 0) { /* get name, open new output file */ if (c != NULL) err(SE_FORM, h); if ((r = malloc(b - 4)) == NULL) err(SE_MEM, ""); strcpy(r, l + 5); if (strcmp(r, "-") == 0) f = stdout;#ifndef VMS /* shouldn't have explicit version #, so VMS won't overwrite */ else if (!o && (f = fopen(r, "r")) != NULL) { fclose(f); err(SE_OVER, r); }#endif /* !VMS */ else if ((f = fopen(r, "w")) == NULL) err(SE_FULL, r); if ((c = chook(f)) == NULL) err(SE_MEM, ""); b = decb = decn = 0; ccnt = 0; } else if (strcmp(l, "more") == 0) { /* check if currently writing */ if (c == NULL) err(SE_FORM, h); b = 2; } else if (strncmp(l, "cont ", 5) == 0) { /* check name and file offset */ if (c == NULL) err(SE_FORM, h); for (q = l + 5; *q && *q != ' '; q++) ; if (*q == 0 || atol(l + 5) != ccnt + 4 + (decn != 0) || strcmp(q + 1, r)) err(SE_CONT, r); b = 0; } else if (strcmp(l, "end") == 0) { /* check crc, close output file */ if (c == NULL) err(SE_FORM, h); if (c->n != 4 || c->b != ~c->c) err(SE_CRC, r); if (ferror(c->f) || fclose(c->f)) err(SE_FULL, r); if (noisy) fprintf(stderr, "%s received\n", r); z = 0; free((voidp *)c); c = NULL; b = 2; } else { for (q = l; *q && *q != ' '; q++) ; *q = 0; fprintf(stderr, "%s: unsupported keyword '%s' ignored\n", warname, l); b = 4; } break; } }}void help(){ int i; static char *text[] = {"Usage:"," ship [-f] [-q] [-nnn] [-m address] [-s subject] files...",""," ships the files to stdout. -m sends the output via the mailer to"," address. -nnn splits the output into pieces of nnnK bytes or less."," if -nnn is used without -m, the output goes to the files partxxxx,"," where xxxx is 0001, 0002, etc. If -0 is specified, the output goes"," entirely to the file part0001. -f uses a fast method with slightly"," less performance. If no files are given, stdin is used. The special"," filename '-' also takes input from stdin. Files shipped from stdin"," are unshipped to stdout. This can be used to document a shipment."," When mailing, -s gives a subject line prefix. -q inhibits messages.",""," ship -u [-o] [-q] files..."," unship [-o] [-q] files...",""," extracts the contents of the mail messages in files... -o allows"," existing files to be overwritten. -u is implied if the name of the"," executable is unship. If no files are given, the input is from"," stdin. If any of the files were shipped from stdin, then they are"," extracted to stdout." }; puts(SHIPVER); for (i = 0; i < sizeof(text)/sizeof(char *); i++) { printf(text[i]); putchar('\n'); } exit(0);}void main(argc, argv)int argc; /* number of arguments */char **argv; /* table of argument strings */{ FILE *f; /* input file */ ulg k; /* number of k requested per part */ char *p; /* temporary variable */ int o; /* overwrite flag */ int r; /* temporary variable */ int s; /* true if no names given */ /* No temporary file yet (for err()) */ *mname = 0; /* No subject prefix yet */ *mprefix = 0; /* See if help requested */ if (argc > 1 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "-?") == 0)) help(); /* Unship */ if ((p = strrchr(argv[0], PATHCUT)) == NULL) p = argv[0]; else p++; r = 0; /* (make some compilers happier) */ if ((r = strncmp(p, "unship", 6)) == 0 || (r = strncmp(p, "UNSHIP", 6)) == 0 || (argc > 1 && strcmp(argv[1], "-u") == 0)) { errname = "unship error"; warname = "unship warning"; r = r ? 2 : 1; /* next arg */ o = 0; /* disallow overwriting */ if (r < argc && strcmp(argv[r], "-o") == 0) { r++; o = 1; /* allow overwriting */ } if (r < argc && strcmp(argv[r], "-q") == 0) { r++; noisy = 0; /* inhibit messages */ } if (r < argc) unship(argv + r, argc - r, o); /* unship files in args */ else { char *a[1]; /* short list of names (one) */ a[0] = "-"; unship(a, 1, o); /* no args--do stdin */ } } /* Ship */ else { mail = 0; /* not mailing */ fast = 0; /* use base 85 encoding */ s = 1; /* no names given yet */ strcpy(sname, "-"); /* output to stdout */ sfile = stdout; slns = slmax = k = 0; for (r = 1; r < argc; r++) /* go through args */ if (argv[r][0] == '-') /* option or stdin */ if (argv[r][1]) /* option */ { if (argv[r][1] == 'm') /* mail output */ { mail = 1; mdest = NULL; /* next arg is mail address */ } else if (argv[r][1] == 's') /* next arg is subject prefix */ mprefix = NULL; else if (argv[r][1] == 'f') /* fast arithmetic encoding */ { fast = 1; if (k) /* recompute slmax if needed */ slmax = 4 + (k * 1024L) / 81; } else if (argv[r][1] == 'q') /* quiet operation */ noisy = 0; else /* option is number of lines */ { /* Check numeric option */ for (p = argv[r] + 1; *p; p++) if (*p < '0' || *p > '9') break; if (*p || slmax) err(SE_ARG, argv[r]); /* Zero means infinity, else convert */ if ((k = atol(argv[r] + 1)) == 0) slmax = -1L; else slmax = 4 + (k * 1024L) / (fast ? 81 : 77); } } else /* input file is stdin */ { if (mail && mdest == NULL) err(SE_ARG, "- (no mail destination given)"); s = 0; if (mail && !*mprefix) strcpy(mprefix, "(stdin)"); ship("-", stdin); } else /* not option or stdin */ if (mail && mdest == NULL) /* arg is mail address */ mdest = argv[r]; else if (mprefix == NULL) /* arg is subject prefix */ mprefix = argv[r]; else /* arg is file to ship */ { s = 0; if ((f = fopen(argv[r], "r")) == NULL) err(SE_FIND, argv[r]); if (mail && !*mprefix) { int i; for (i = 0, p = nopath(argv[r]); i < 8 && *p; p++) if ((*p >= '0' && *p <= '9') || (*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || *p == '.' || *p == '_') mprefix[i++] = *p; mprefix[i] = 0; } ship(argv[r], f); fclose(f); } if (s) /* no names--act as filter */ if (mail && mdest == NULL) err(SE_ARG, "-m (no mail destination given)"); else if (mprefix == NULL) err(SE_ARG, "-s (no subject prefix given)"); else { if (mail && !*mprefix) strcpy(mprefix, "(stdin)"); ship("-", stdin); } endship(1); /* clean up */ if (noisy && (mail || slmax)) fprintf(stderr, "file%s%s %s\n", strcmp("part0001", sname) ? "s part0001.." : " ", sname, mail ? "mailed" : "written"); } /* Done */ exit(0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -