home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1579 < prev    next >
Internet Message Format  |  1990-12-28  |  16KB

  1. From: wht@n4hgf.Mt-Park.GA.US (Warren Tucker)
  2. Newsgroups: alt.sources
  3. Subject: shar 3.32 part 2/2
  4. Message-ID: <164@n4hgf.Mt-Park.GA.US>
  5. Date: 14 Jul 90 18:01:36 GMT
  6.  
  7. Submitted-by: wht@n4hgf.Mt-Park.GA.US
  8. Archive-name: shar332/part02
  9.  
  10. #!/bin/sh
  11. # This is part 02 of shar332
  12. # ============= unshar.c ==============
  13. echo "x - extracting unshar.c (Text)"
  14. sed 's/^X//' << 'SHAR_EOF' > unshar.c &&
  15. Xchar *revision = "3.30";
  16. X/****************************************************************
  17. X * unshar.c: Unpackage one or more shell archive files
  18. X *
  19. X * Usage:     unshar [-c] [-C exit_line] [ -d directory ] [ file ] ...
  20. X *
  21. X * Description:    unshar is a filter which removes the front part
  22. X *        of a file and passes the rest to the 'sh' command.
  23. X *        It understands phrases like "cut here", and also
  24. X *        knows about shell comment characters and the Unix
  25. X *        commands "echo", "cat", and "sed".
  26. X *
  27. X *      It can unshar shar files concatenated in one file, with the
  28. X *      the "-c" command, which separates files by recognizing the
  29. X *      "exit 0" string at the beginning of a line
  30. X *
  31. X *      (The -C string option allows you to specify this string, thus
  32. X *      -c is equivalent to -C "exit 0")
  33. X *
  34. X *      The -d flag tells unshar to change directory before unsharing
  35. X *
  36. X * HISTORY
  37. X * 19-Apr-90  Colas Nahaboo (colas@mirsa.inria.fr)
  38. X *      added -c and -C flags to read from concatenated archives
  39. X *  1-Feb-85  Guido van Rossum (guido@mcvax) at CWI, Amsterdam
  40. X *    Added missing 'quit' routine;
  41. X *    added -d flag to change to directory first;
  42. X *    added filter mode (read stdin when no arguments);
  43. X *    added 'getopt' to get flags (makes it self-contained).
  44. X * 29-Jan-85  Michael Mauldin (mlm) at Carnegie-Mellon University
  45. X *    Created.
  46. X ****************************************************************/
  47. X/*+:EDITS:*/
  48. X/*:05-05-1990-01:37-relay.EU.net!rivm!a3-dont assume vax is running BSD */
  49. X/*:04-19-1990-15:20-wht@n4hgf-fix so -d doesnt make argv files unreachable */
  50. X/*:04-19-1990-15:06-wht@n4hgf-colas@mirsa patches had expanded tabs */
  51. X/*:04-10-1990-22:02-wht@n4hgf-stdin failed sometimes-can not seek on pipe */
  52. X
  53. X#include <stdio.h>
  54. X#define EOL '\n'
  55. X
  56. X#if (defined(pyr) || defined(sun) || defined(BSD42) || \
  57. X defined(vax) || defined(sequent)) && !defined(SYS5)
  58. X#define strchr    index
  59. X#undef USE_GETCWD
  60. Xchar *getwd();
  61. X#else
  62. X#define USE_GETCWD
  63. Xchar *getcwd();
  64. X#endif
  65. X
  66. Xchar *strchr();
  67. X
  68. Xextern char *optarg;
  69. Xextern int optind;
  70. X
  71. Xint continue_reading = 0;
  72. Xchar *exit_string = "exit 0";
  73. Xint exit_string_length;
  74. Xchar argvdir[1024];
  75. X
  76. Xmain(argc,argv)
  77. Xint argc;
  78. Xchar *argv[];
  79. X{
  80. X    int i,ch;
  81. X    FILE *in;
  82. X    char s1024[1024];
  83. X
  84. X    setbuf(stdout,NULL);
  85. X    setbuf(stderr,NULL);
  86. X
  87. X#ifdef USE_GETCWD
  88. X    if(!getcwd(argvdir,sizeof(argvdir)))
  89. X    {
  90. X        perror("cannot get current directory name");
  91. X        exit(1);
  92. X    }
  93. X#else
  94. X    argvdir[0] = 0;
  95. X    if(!getwd(argvdir))
  96. X    {
  97. X        if(argvdir[0])
  98. X            fprintf(stderr,"%s\n",argvdir);
  99. X        else
  100. X            printf(stderr,"cannot get current directory name\n");
  101. X        exit(1);
  102. X    }
  103. X#endif
  104. X
  105. X
  106. X    /* Process options */
  107. X
  108. X    while((ch = getopt(argc,argv,"d:cC:")) != EOF)
  109. X    {
  110. X        switch(ch)
  111. X        {
  112. X        case 'd':
  113. X            if(chdir(optarg) == -1)
  114. X            {
  115. X                fprintf(stderr,"unshar: cannot chdir to '%s'\n",optarg);
  116. X                exit(2);
  117. X            }
  118. X            break;
  119. X        case 'C':
  120. X            exit_string = optarg;
  121. X        case 'c':
  122. X            continue_reading = 1;
  123. X            exit_string_length = strlen(exit_string);
  124. X            break;
  125. X        default:
  126. X            quit(2,"Usage: unshar [-d directory] [file] ...\n");
  127. X        }
  128. X    }
  129. X
  130. X    if(optind < argc)
  131. X    {
  132. X        for(i= optind; i < argc; ++i)
  133. X        {
  134. X            if(argv[i][0] == '/') {
  135. X                strcpy(s1024,argv[i]);
  136. X            } else {
  137. X                strcpy(s1024,argvdir);
  138. X                strcat(s1024,"/");
  139. X                strcat(s1024,argv[i]);
  140. X            }
  141. X            if(!(in = fopen(s1024,"r")))
  142. X            {
  143. X                perror(s1024);
  144. X                exit(1);
  145. X            }
  146. X            process(s1024,in);
  147. X            fclose(in);
  148. X        }
  149. X    }
  150. X    else
  151. X    {
  152. X        sprintf(s1024,"/tmp/unsh.%05d",getpid());
  153. X        unlink(s1024);
  154. X        if(!(in = fopen(s1024,"w+")))
  155. X        {
  156. X            fprintf(stderr,"cannot open temp file '%s'\n",s1024);
  157. X            exit(1);
  158. X        }
  159. X        unlink(s1024);    /* don't try this with MSDOS, sports fans */
  160. X        while(i = fread(s1024,1,sizeof(s1024),stdin))
  161. X            fwrite(s1024,i,1,in);
  162. X        rewind(in);
  163. X        process("standard input",in);
  164. X        fclose(in);
  165. X    }
  166. X
  167. X    exit(0);
  168. X}
  169. X
  170. X
  171. Xprocess(name,in)
  172. Xchar *name;
  173. XFILE *in;
  174. X{
  175. X    char buffer[8196];
  176. X    char ch;
  177. X    FILE *shpr,*popen();
  178. X    long current_position = 0;
  179. X    char *more_to_read = 0;
  180. X
  181. X    while(position(name,in,current_position))
  182. X    {
  183. X        printf("%s:\n",name);
  184. X        if(!(shpr = popen("sh","w")))
  185. X            quit(1,"unshar: cannot open 'sh' process\n");
  186. X
  187. X        if (!continue_reading) {
  188. X            while((ch = fgetc(in)) != EOF)
  189. X                fputc(ch,shpr);
  190. X            pclose(shpr);
  191. X            break;
  192. X        } else {
  193. X            while (more_to_read = fgets(buffer, 8196, in)) {
  194. X                fputs(buffer, shpr);
  195. X                if (!strncmp(exit_string, buffer, exit_string_length)) {
  196. X                    break;
  197. X                }
  198. X            }
  199. X            pclose(shpr);
  200. X            if (more_to_read)
  201. X                current_position = ftell(in);
  202. X            else {
  203. X                break;
  204. X            }
  205. X        }
  206. X    }
  207. X}
  208. X
  209. X/****************************************************************
  210. X * position: position 'fil' at the start of the shell command
  211. X * portion of a shell archive file.
  212. X ****************************************************************/
  213. X
  214. Xposition(fn,fil,start)
  215. Xchar *fn;
  216. XFILE *fil;
  217. Xlong start;                   /* scan file from position */
  218. X{
  219. X    char buf[BUFSIZ];
  220. X    long pos,ftell();
  221. X
  222. X    /* Results from star matcher */
  223. X    static char res1[BUFSIZ],res2[BUFSIZ],res3[BUFSIZ],res4[BUFSIZ];
  224. X    static char *result[] = 
  225. X    {
  226. X        res1,res2,res3,res4         };
  227. X
  228. X    fseek(fil, start, 0);
  229. X
  230. X    while(1)
  231. X    { /* Record position of the start of this line */
  232. X        pos = ftell(fil);
  233. X
  234. X        /* Read next line, fail if no more and no previous process */
  235. X        if(!fgets(buf,BUFSIZ,fil))
  236. X        {
  237. X            if(!start)
  238. X                fprintf(stderr,"unshar: found no shell commands in %s\n",fn);
  239. X            return(0);
  240. X        }
  241. X
  242. X        /* Bail out if we see C preprocessor commands or C comments */
  243. X        if(stlmatch(buf,"#include")    || stlmatch(buf,"# include") ||
  244. X            stlmatch(buf,"#define")    || stlmatch(buf,"# define") ||
  245. X            stlmatch(buf,"#ifdef")    || stlmatch(buf,"# ifdef") ||
  246. X            stlmatch(buf,"#ifndef")    || stlmatch(buf,"# ifndef") ||
  247. X            stlmatch(buf,"/*"))
  248. X        {
  249. X            fprintf(stderr,
  250. X                "unshar: %s looks like raw C code, not a shell archive\n",fn);
  251. X            return(0);
  252. X        }
  253. X
  254. X        /* Does this line start with a shell command or comment */
  255. X        if(stlmatch(buf,"#")    || stlmatch(buf,":") ||
  256. X            stlmatch(buf,"echo ")    || stlmatch(buf,"sed ") ||
  257. X            stlmatch(buf,"cat ") || stlmatch(buf,"if "))
  258. X        {
  259. X            fseek(fil,pos,0);
  260. X            return(1);
  261. X        }
  262. X
  263. X        /* Does this line say "Cut here" */
  264. X        if(smatch(buf,"*CUT*HERE*",result) ||
  265. X            smatch(buf,"*cut*here*",result) ||
  266. X            smatch(buf,"*TEAR*HERE*",result) ||
  267. X            smatch(buf,"*tear*here*",result) ||
  268. X            smatch(buf,"*CUT*CUT*",result) ||
  269. X            smatch(buf,"*cut*cut*",result))
  270. X        {
  271. X            /* Read next line after "cut here", skipping blank lines */
  272. X            while(1)
  273. X            {
  274. X                pos = ftell(fil);
  275. X
  276. X                if(!fgets(buf,BUFSIZ,fil))
  277. X                {
  278. X                    fprintf(stderr,
  279. X                        "unshar: found no shell commands after 'cut' in %s\n",fn);
  280. X                    return(0);
  281. X                }
  282. X
  283. X                if(*buf != '\n') break;
  284. X            }
  285. X
  286. X            /* Win if line starts with a comment character of lower case letter */
  287. X            if(*buf == '#' || *buf == ':' || (('a' <= *buf) && ('z' >= *buf)))
  288. X            {
  289. X                fseek(fil,pos,0);
  290. X                return(1);
  291. X            }
  292. X
  293. X            /* Cut here message lied to us */
  294. X            fprintf(stderr,"unshar: %s is probably not a shell archive,\n",fn);
  295. X            fprintf(stderr,"        the 'cut' line was followed by: %s",buf);
  296. X            return(0);
  297. X        }
  298. X    }
  299. X}
  300. X
  301. X/*****************************************************************
  302. X * stlmatch  --  match leftmost part of string
  303. X *
  304. X * Usage:  i = stlmatch (big,small)
  305. X *    int i;
  306. X *    char *small, *big;
  307. X *
  308. X * Returns 1 iff initial characters of big match small exactly;
  309. X * else 0.
  310. X *
  311. X * HISTORY
  312. X * 18-May-82 Michael Mauldin (mlm) at Carnegie-Mellon University
  313. X *      Ripped out of CMU lib for Rog-O-Matic portability
  314. X * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  315. X *    Rewritten for VAX from Ken Greer's routine.
  316. X *
  317. X *  Originally from klg (Ken Greer) on IUS/SUS UNIX
  318. X *****************************************************************/
  319. X
  320. Xint stlmatch(big,small)
  321. Xchar *small,*big;
  322. X{
  323. X    register char *s,*b;
  324. X    s = small;
  325. X    b = big;
  326. X    do
  327. X    {
  328. X        if(*s == '\0')
  329. X            return(1);
  330. X    }  while(*s++ == *b++);
  331. X    return(0);
  332. X}
  333. X
  334. X/*****************************************************************
  335. X * smatch: Given a data string and a pattern containing one or
  336. X * more embedded stars (*) (which match any number of characters)
  337. X * return true if the match succeeds, and set res[i] to the
  338. X * characters matched by the 'i'th *.
  339. X *****************************************************************/
  340. X
  341. Xsmatch(dat,pat,res)
  342. Xregister char *dat,*pat,**res;
  343. X{
  344. X    register char *star = 0,*starend,*resp;
  345. X    int nres = 0;
  346. X
  347. X    while(1)
  348. X    {
  349. X        if(*pat == '*')
  350. X        {
  351. X            star = ++pat;                  /* Pattern after * */
  352. X            starend = dat;                  /* Data after * match */
  353. X            resp = res[nres++];              /* Result string */
  354. X            *resp = '\0';                  /* Initially null */
  355. X        }
  356. X        else if(*dat == *pat)              /* Characters match */
  357. X        {
  358. X            if(*pat == '\0')              /* Pattern matches */
  359. X                return(1);
  360. X            pat++;                      /* Try next position */
  361. X            dat++;
  362. X        }
  363. X        else
  364. X        {
  365. X            if(*dat == '\0')              /* Pattern fails - no more */
  366. X                return(0);                  /* data */
  367. X            if(star == 0)                  /* Pattern fails - no * to */
  368. X                return(0);                  /* adjust */
  369. X            pat = star;                  /* Restart pattern after * */
  370. X            *resp++ = *starend;              /* Copy character to result */
  371. X            *resp = '\0';                  /* null terminate */
  372. X            dat = ++starend;                  /* Rescan after copied char */
  373. X        }
  374. X    }
  375. X}
  376. X
  377. X/*****************************************************************
  378. X * Addendum: quit subroutine (print a message and exit)
  379. X *****************************************************************/
  380. X
  381. Xquit(status,message)
  382. Xint status;
  383. Xchar *message;
  384. X{
  385. X    fprintf(stderr,message);
  386. X    exit(status);
  387. X}
  388. X
  389. X/*****************************************************************
  390. X * Public Domain getopt routine
  391. X *****************************************************************/
  392. X
  393. X/*
  394. X * get option letter from argument vector
  395. X */
  396. Xint opterr = 1;        /* useless, never set or used */
  397. Xint optind = 1;        /* index into parent argv vector */
  398. Xint optopt;            /* character checked for validity */
  399. Xchar *optarg;        /* argument associated with option */
  400. X
  401. X#define BADCH    (int)'?'
  402. X#define EMSG    ""
  403. X#define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  404. X        fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  405. X
  406. Xgetopt(nargc,nargv,ostr)
  407. Xint nargc;
  408. Xchar **nargv,*ostr;
  409. X{
  410. X    static char *place = EMSG;    /* option letter processing */
  411. X    register char *oli;        /* option letter list index */
  412. X    char *strchr();
  413. X
  414. X    if(!*place)
  415. X    {            /* update scanning pointer */
  416. X        if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  417. X            return(EOF);
  418. X        if(*place == '-')
  419. X        {    /* found "--" */
  420. X            ++optind;
  421. X            return(EOF);
  422. X        }
  423. X    }                /* option letter okay? */
  424. X    if((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt)))
  425. X    {
  426. X        if(!*place) ++optind;
  427. X        tell(": illegal option -- ");
  428. X    }
  429. X    if(*++oli != ':')
  430. X    {        /* don't need argument */
  431. X        optarg = (char *)0;
  432. X        if(!*place) ++optind;
  433. X    }
  434. X    else 
  435. X    {                /* need an argument */
  436. X        if(*place) optarg = place;    /* no white space */
  437. X        else if(nargc <= ++optind)
  438. X        {    /* no arg */
  439. X            place = EMSG;
  440. X            tell(": option requires an argument -- ");
  441. X        }
  442. X        else optarg = nargv[optind];    /* white space */
  443. X        place = EMSG;
  444. X        ++optind;
  445. X    }
  446. X    return(optopt);            /* dump back option letter */
  447. X}
  448. X/* vi: set tabstop=4 shiftwidth=4: */
  449. SHAR_EOF
  450. # ============= uushar.c ==============
  451. echo "x - extracting uushar.c (Text)"
  452. sed 's/^X//' << 'SHAR_EOF' > uushar.c &&
  453. X#include <stdio.h>
  454. X#include <sys/types.h>
  455. X#include <sys/stat.h>
  456. X
  457. X/* ENC is the basic 1 character encoding function to make a char printing */
  458. X#define ENC(c) ((((c) & 077) + ' ') | ((c & 077) == 0 ? 0100 : 0))
  459. X
  460. Xencode (in, out)
  461. X    FILE *in;
  462. X    FILE *out;
  463. X{
  464. X    char  buf[80];
  465. X    int  i, n;
  466. X
  467. X    for (;;)
  468. X    {
  469. X    /* 1 (up to) 45 character line */
  470. X    n = fr (in, buf, 45);
  471. X    putc (ENC (n), out);
  472. X
  473. X    for (i = 0; i < n; i += 3)
  474. X        outdec (&buf[i], out);
  475. X
  476. X    putc ('\n', out);
  477. X    if (n <= 0)
  478. X        break;
  479. X    }
  480. X}
  481. X
  482. X/*
  483. X * output one group of 3 bytes, pointed at by p, on file f.
  484. X */
  485. Xoutdec (p, f)
  486. X    char *p;
  487. X    FILE *f;
  488. X{
  489. X    int  c1, c2, c3, c4;
  490. X
  491. X    c1 = *p >> 2;
  492. X    c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  493. X    c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  494. X    c4 = p[2] & 077;
  495. X    putc (ENC (c1), f);
  496. X    putc (ENC (c2), f);
  497. X    putc (ENC (c3), f);
  498. X    putc (ENC (c4), f);
  499. X}
  500. X
  501. X/* fr: like read but stdio */
  502. Xint
  503. X     fr (fp, buf, cnt)
  504. X    FILE *fp;
  505. X    char *buf;
  506. X    int  cnt;
  507. X{
  508. X    int  c, i;
  509. X
  510. X    for (i = 0; i < cnt; i++)
  511. X    {
  512. X    c = getc (fp);
  513. X    if (c == EOF)
  514. X        return (i);
  515. X    buf[i] = c;
  516. X    }
  517. X    return (cnt);
  518. X}
  519. X/* vi: set tabstop=4 shiftwidth=4: */
  520. SHAR_EOF
  521. # ============= who@where.c ==============
  522. echo "x - extracting who@where.c (Text)"
  523. sed 's/^X//' << 'SHAR_EOF' > who@where.c &&
  524. X/*+-------------------------------------------------------------------------
  525. X    who@where.c - find out who i am & where i am
  526. X    ...!gatech!kd4nc!n4hgf!wht (wht%n4hgf@gatech.edu)
  527. X--------------------------------------------------------------------------*/
  528. X/*+:EDITS:*/
  529. X/*:04-03-1990-19:55-wht@n4hgf-get rid of complicated who_am_i */
  530. X/*:04-01-1990-13:30-pat@rwing-use utsname.nodename instead of sysname */
  531. X/*:04-02-1990-12:12-wht@n4hgf-sigh... some pwd.h dont declare functions */
  532. X/*:03-29-1990-18:23-wht@n4hgf-add automatic sequent support */
  533. X/*:03-28-1990-15:24-wht@n4hgf-creation */
  534. X
  535. X#include <stdio.h>
  536. X#include <sys/types.h>
  537. X#include <pwd.h>
  538. X
  539. X/* assume system v unless otherwise fixed */
  540. X#if (defined(pyr) || defined(vax) || defined(sequent)) && !defined(BSD42) && !defined(SYS5)
  541. X#define BSD42
  542. X#endif
  543. X#if defined(sun)    /* this miscreant doesn't exactly fit BSD or SYSV */
  544. X#undef BSD42
  545. X#undef SYS5
  546. X#endif
  547. X#if !defined(BSD42) && !defined(sun) && !defined(SYS5)
  548. X#define SYS5
  549. X#endif
  550. X
  551. X#if defined(sun) || defined(BSD42)
  552. X#define strchr    index
  553. X#define strrchr    rindex
  554. X#endif
  555. X
  556. X#if !defined(SYS5) || defined(sun)
  557. X#include <sys/time.h>
  558. Xextern int errno;
  559. X#else
  560. X#include <sys/utsname.h>
  561. X#include <time.h>
  562. X#endif    /* system dependencies */
  563. X
  564. X/*+-------------------------------------------------------------------------
  565. X    who_am_i() - get user name
  566. X--------------------------------------------------------------------------*/
  567. Xchar *
  568. Xwho_am_i()
  569. X{
  570. X    struct passwd *getpwuid();
  571. X    struct passwd *passwd;
  572. X    passwd = getpwuid(getuid());
  573. X    (void)endpwent();
  574. X    if(passwd == (struct passwd *)0)
  575. X        return("???");
  576. X    return(passwd->pw_name);
  577. X
  578. X}    /* end of who_am_i */
  579. X
  580. X/*+-------------------------------------------------------------------------
  581. X    where_am_i() - do uname, gethostname, or read file (/etc/systemid)
  582. X--------------------------------------------------------------------------*/
  583. Xchar *
  584. Xwhere_am_i()
  585. X{
  586. X#if defined(M_SYS5)    /* SCO UNIX or XENIX */
  587. XFILE *fpsid = fopen("/etc/systemid","r");
  588. Xstatic char s20[20];
  589. X    if(!fpsid)
  590. X        return("???");
  591. X    fgets(s20,sizeof(s20),fpsid);
  592. X    fclose(fpsid);
  593. X    s20[strlen(s20) - 1] = 0;
  594. X    return(s20);
  595. X#else /* M_SYS5 */
  596. X#if defined(SYS5)
  597. Xstatic struct utsname where_i_am;
  598. X    uname(&where_i_am);
  599. X    return(where_i_am.nodename);
  600. X#else /* SYS5 */
  601. Xstatic char where_i_am[64];
  602. X    gethostname(where_i_am,sizeof(where_i_am));
  603. X    return(where_i_am);
  604. X#endif /* SYS5 */
  605. X#endif /* M_SYS5 */
  606. X}    /* end of where_am_i */
  607. X
  608. X/*+-------------------------------------------------------------------------
  609. X    who_where(buf)
  610. X--------------------------------------------------------------------------*/
  611. Xchar *
  612. Xwho_where(buf)
  613. Xchar *buf;
  614. X{
  615. Xstatic char ww[64];
  616. X
  617. X    if(!buf)
  618. X        buf = ww;
  619. X    strcpy(buf,who_am_i());
  620. X    strcat(buf,"@");
  621. X    strcat(buf,where_am_i());
  622. X}    /* end of who_where */
  623. X
  624. X/* vi: set tabstop=4 shiftwidth=4: */
  625. X/* end of who@where.c */
  626. SHAR_EOF
  627. exit 0
  628.  
  629. --------------------------------------------------------------------
  630. Warren Tucker, TuckerWare emory!n4hgf!wht or wht@n4hgf.Mt-Park.GA.US
  631. Sforzando (It., sfohr-tsahn'-doh).   A direction to perform the tone
  632. or chord with special stress, or marked and sudden emphasis.
  633.