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

  1. From: rhg@cpsolv.CPS.COM (Richard H. Gumpertz)
  2. Newsgroups: alt.sources
  3. Subject: shar 3.43 part 1 of 2
  4. Message-ID: <536@cpsolv.CPS.COM>
  5. Date: 8 Sep 90 17:35:34 GMT
  6.  
  7. Submitted-by: rhg@cpsolv.cps.com
  8. Archive-name: shar3.43/part01
  9.  
  10. Enclosed is shar 3.43 which has several bug fixes and improvements over shar
  11. 3.32.  Due to huge volumes of hate mail, Warren Tucker has given up posting
  12. shar.  Nevertheless, I think it is important that this get out to the network,
  13. especially because it responds to several of the criticisms raised.
  14.  
  15. PLEASE DO NOT SEND FLAMES ABOUT WHAT THIS PROGRAM DOES!  Constructive comments,
  16. preferably in the form of SOURCE CODE, will be accepted.  This is a community
  17. effort to produce code that many people find useful; if you don't want it then
  18. don't use it.  The defaults are now such that few systems should choke on the
  19. shar archives produced.
  20.  
  21. Changes since 3.32:
  22.  
  23. 1.  Several bug fixes.
  24.  
  25. 2.  Inverted the meaning of '-x'; the new default is to NOT overwrite.
  26.  
  27. 3.  Added '-c' checking when unpacking so the recipient can force overwrites.
  28.  
  29. 4.  Made '-L' work even with files not being overwritten.
  30.  
  31. 5.  Added '-m' and changed the default behavior to not generate TOUCH commands.
  32.  
  33. 6.  Added '-F'; the default is to suppress the extra 'X' at the beginning of
  34.     each line unless it is needed (i.e., the first character of the line is
  35.     already 'X' or is a non-graphic).
  36.  
  37. 7.  Renamed '-b' and '-t' to '-B' and '-T', respectively.
  38.  
  39. 8.  Added '-b<n>' for use with compression (calls compress with -b<n>).
  40.  
  41. 9.  Renamed the temporary files used in unpacking from shar3_???_.tmp to
  42.     _shar_???_.tmp.
  43.  
  44. 10. Directories may now be passed to shar; a recursive directory walk is
  45.     performed.  This feature may be disabled by compiling with -DNO_WALKTREE.
  46.  
  47. ---- Cut Here and feed the following to sh ----
  48. #!/bin/sh
  49. # This is shar3.43, a shell archive (shar 3.43)
  50. # made 09/08/1990 15:54 UTC by rhg@cpsolv
  51. # Source directory /u/rhg/src/shar
  52. #
  53. # existing files will NOT be overwritten unless -c is specified
  54. #
  55. # This shar contains:
  56. # length  mode       name
  57. # ------ ---------- ------------------------------------------
  58. #  10777 -rw-r--r-- README
  59. #   3090 -rw-r--r-- Makefile
  60. #   7946 -rw-r--r-- shar.1
  61. #   1515 -rw-r--r-- unshar.1
  62. #   1365 -rw-r--r-- uushar.c
  63. #   2882 -rw-r--r-- who@where.c
  64. #  11348 -rw-r--r-- unshar.c
  65. #  36835 -rw-r--r-- shar.c
  66. #
  67. # ============= README ==============
  68. if test -f 'README' -a X"$1" != X"-c"; then
  69.     echo 'x - skipping README (File already exists)'
  70. else
  71. echo 'x - extracting README (Text)'
  72. sed 's/^X//' << 'SHAR_EOF' > 'README' &&
  73. XThis file last revised Sat Aug 4 17:15:01 CDT 1990
  74. X
  75. XHere is shar 3.43, an updated version of shar 3.32, derived from 'shar2'.
  76. XThis offering is the work of many people.  Thanks to wht@n4hgf.Mt-Park.GA.US
  77. X(Warren Tucker), rhg@CPS.COM (Richard H. Gumpertz), colas@avahi.inria.fr
  78. X(Colas Nahaboo), bill@netagw.com (Bill Aten), marks@rex.cs.tulane.edu, and
  79. Xmaaaany others.
  80. X
  81. XThis version's shar:
  82. X1) generates shell code which attempts to create missing directories
  83. X2) generates shell code which will force overwriting of files when passed
  84. X   the '-c' option.
  85. X3) allows entire directories to be archived
  86. X4) handle deviants sun, vax, pyramid, sequent, and SCO XENIX/UNIX
  87. X   automatically; for system V systems I did not catch, add -DSYS5
  88. X   to CFLAGS; for other BSD-like systems, add -DBSD42
  89. X5) if unsharing system's touch is Sys V compatible (allows touch -m),
  90. X   the unshar process restores file dates (-m switch)
  91. X6) An archive name may be specified for inclusion in the header
  92. X   of the shar files (-n switch)
  93. X7) allows automatic generation of "Submitted-by: who@where" &
  94. X   "Archive-name: <name>/part##" headers
  95. X8) uses getopt; no good system library should be without a copy
  96. X   but it is readily available (like look in unshar.c)
  97. X9) includes other chrome-plated bells, whistles, and junque
  98. X
  99. XThis version's unshar:
  100. X1) can change directory before unsharing
  101. X2) can unshar from standard in, from a COLLECTION of shars,
  102. X   from a file containing multiple concatenated shars,
  103. X   or a mixture of shar files and concatenated-shar files.
  104. X3) can pass the '-c' option on to the script being extracted.
  105. X4) does not have a Social Security number.
  106. X
  107. X------------------------ shar  usage -----------------------------------
  108. Xshar 3.43
  109. Xusage: shar [ options ] file ...
  110. X       shar -S [ options ]
  111. X-V  produce "vanilla" shars demanding little of the unshar environment
  112. X-v  verbose messages OFF while executing
  113. X-m  restore file modification dates & times with "touch" commands
  114. X-w  don't check with 'wc -c' after unpack
  115. X-a  generate Submitted-by: & Archive-name: headers
  116. X-nXXX   use XXX as the name of the archive (documentation)
  117. X-s  override automatically determined submitter name
  118. X-x  overwrite existing files without checking if they already exist
  119. X-X  interactively overwrite existing files (NOT FOR NET SHARS)
  120. X-B  treat all files as binary, use uuencode
  121. X-T  treat all files as text (default)
  122. X-C  compress and uuencode all files
  123. X-bXX    pass -bXX (default 12) to compress when compressing (implies -C)
  124. X-p  allow positional parameter options. The options "-B" and "-B"
  125. X    and "-C" may be embedded, and files to the right of the
  126. X    option will be processed in the specified mode
  127. X-M  mixed mode. Determine if the files are text or
  128. X    binary and archive correctly.
  129. X-P  use temp files instead of pipes in the shar file
  130. X-F  force the prefix character on every line (even if not required)
  131. X-c  start the shar with a cut line
  132. X-f  restore by filename only, rather than path
  133. X-dXXX   use XXX to delimit the files in the shar
  134. X-oXXX   (or -o XXX) output to file XXX.01 thru XXX.nn
  135. X-lXX    limit output file size to XXk bytes (but don't split files)
  136. X-LXX    limit output file size to XXk bytes (may split files)
  137. X-S      read files to wrap from stdin, ignoring argument line
  138. X
  139. XThe -S option reads filenames one per line from stdin; input
  140. Xformat must be similar to 'find' output, except that if -p
  141. Xis specified, -B, -T or -C may be used (on lines by themselves)
  142. Xe.g., find . -type f -print | sort | shar -C -l50 -o /tmp/big
  143. X
  144. XThe 'o' option is required if the 'l' or 'L' option is used
  145. XThe 'n' option is required if the 'a' option is used
  146. X
  147. X-a generates sharname/part## headers. If the -a argument contains
  148. Xa '/', then /part is not appended
  149. XThe automatic submitter name is trivial: essentially `whoami`@`uname`
  150. X
  151. X------------------------ unshar usage -----------------------------------
  152. XUnshar has no usage built in.  It has default actions when invoked
  153. Xwith no arguments (read from stdin).
  154. X
  155. XUsage:     unshar [ -d directory ] [ -c ] [ -e | -E exit_line ] [ files ... ]
  156. X
  157. X      The -c flag is passed through to the shell as a parameter to the script
  158. X      It can unshar shar files concatenated in one file, with the
  159. X      the "-e" command, which separates files by recognizing the
  160. X      "exit 0" string at the beginning of a line
  161. X
  162. X      (The -E string option allows you to specify this string, thus
  163. X      -e is equivalent to -E "exit 0")
  164. X
  165. X      The -d flag tells unshar to change directory before unsharing
  166. X
  167. X
  168. X--------------------- history -----------------------------------------
  169. XChanges since 3.11: kudos to rhg@CPS.COM (Richard H. Gumpertz)
  170. X
  171. X1.  The -l switch still specifies a maximum size for the generated
  172. X    shar files, but now it prevents files from spanning shar parts.
  173. X    Shars generated using this method may be unpacked in any order.
  174. X
  175. X2.  The old -l switch functionality is precisely emulated by using the
  176. X    the -L switch.  That is, archived files may be split across parts.
  177. X    Shars generated using this method must still be unpacked in order.
  178. X
  179. X3.  The -C switch causes files to be compressed, then uuencoded.
  180. X    Unpacking reverses the process.
  181. X
  182. X4.  The -P causes the shar to use temp files instead of pipes in
  183. X    the unshar process.
  184. X
  185. X5.  The -f causes files to be resotred by name only (i.e., strip
  186. X    directory portion of input filenames before placing the name
  187. X    into the shar.
  188. X
  189. X
  190. XChanges since 3.20: kudos to colas@avahi.inria.fr (Colas Nahaboo)
  191. X
  192. X1.  The Archived-name: header no longer uses "/part" if there is
  193. X    a "/" in the -n name.  Thus
  194. X        -n xyzzy                     procduces:
  195. X                                     xyzzy/part01
  196. X                                     xyzzy/part02
  197. X
  198. X        -n xyzzy/patch               procduces:
  199. X                                     xyzzy/patch01
  200. X                                     xyzzy/patch02
  201. X
  202. X        -n xyzzy/patch01.            procduces:
  203. X                                     xyzzy/patch01.01
  204. X                                     xyzzy/patch01.02
  205. X2.  The Archive-name part number other than part01 had no leading zero
  206. X    in the number.
  207. X
  208. X3.  The "Submitted-by:" header was missing the hyphen (minus for olde
  209. X    UNIX hackres).
  210. X
  211. X4.  The unshar program may now unshar a whole mailbox or concatenation
  212. X    of shar files.
  213. X
  214. X    -C "string" looks for "string" at the beginning of the line to
  215. X       break the file into individual shar files
  216. X    -c is equivalent to -C "exit 0"
  217. X
  218. X    This of course will only work if there is something in the shar
  219. X    file recognizable to terminate the shar.  Some shars dont have
  220. X    "exit 0" at the end.  However, a clue: most/many .signatures have
  221. X    "--" on a line right before them.
  222. X
  223. X5.  Unshar -d (change directory) no longer makes argv files unreachable.
  224. X    I had never used the feature until the other day.  I guess the
  225. X    author has used in only for unsharing from stdin.
  226. X
  227. XChanges since 3.21: thanks to Adri Verhoef, <a3@rivm.UUCP>
  228. X
  229. X1.  Some vaxen do not run BSD.  I guess I knew this, but -er-
  230. X    here is Adri's note:
  231. X> Hi Warren,
  232. X> 
  233. X>   I encountered a problem trying to get 'shar3.21' to compile on System V
  234. X> on a vax.  Yes, can you believe it?  We run System V Release 3.0 on VAXen!
  235. X> The shar3.21 code assumes that you are BSD if you're on a vax.  This is not
  236. X> always true!  What I did to get the code compiled on System V, was:
  237. X> (+) edit the Makefile and add -DSYS5 to CFLAGS.
  238. X> (+) edit all the C-source files to circumcise compiler warnings
  239. X>     ("SYS5 redefined").
  240. X> 
  241. X
  242. XHe made a suggestion about having a localize.sh edit a distribution
  243. XMakefile, but for now, I'll just suggest you add -DSYS5 to CFLAGS 
  244. Xmanually.
  245. X
  246. X2.  jhd@irfu.se (Jan Dj{rv, sorry about the screwed up character
  247. X    translation, Jan) wrote man pages.  Thanks!
  248. X
  249. XChanges since 3.22: thanks to Dennis Boylan <dennis@nanovx>
  250. X
  251. X1.  The new -S option allows the list of files to be packed
  252. X    to be read from the standard input rather than from the
  253. X    command line.
  254. X
  255. X2.  A few purist checks were made to ensure fork() or malloc()
  256. X    doesn't fail and excite the "if 20 hours of your time is
  257. X    free then why isn't 200?" crowd (who probably will never see
  258. X    this revision anyway :-))
  259. X
  260. XChanges since 3.23:
  261. X
  262. X1.  The -V mode was added.
  263. X
  264. X2.  Altos doesn't like the '@' in filenames.  The filename format
  265. X    was changed.  Thanks to rhg@cps.com.
  266. X
  267. XChanges since 3.24:
  268. X
  269. X1.  Man pages were revised by gam@netcom (Gordon Moffet). Thanks.
  270. X
  271. X2.  When -L was specified, the "Starting ..." message was not
  272. Xproduced on standard error (with or without -v).
  273. X
  274. X3.  When using -X, the 'not for net' warning was printed on standard
  275. Xoutput rather thsn standard error.
  276. X
  277. X4.  marks@rex.cs.tulane.edu reccommends adding -F 5000 to the load
  278. X    line of unshar when using on XENIX 286 to avoid stack overflow
  279. X    core dumps.  I added this information to an excellkent remake
  280. X    of the Makefile by bill@netagw.com.
  281. X
  282. XChanges since 3.25:
  283. X
  284. X1.  Fixed one minor bug with -a/-n.  The period supplied when a
  285. X    slash appears in the -n name was omitted.  This is a hatefully
  286. X    small bug to fix and reissue a whole release, but
  287. X    a) several new names are on the sharlist now and they have
  288. X       only 3.24 to work with,
  289. X    b) this will surely sync us all up, and
  290. X    c) I think it will put shar to bed for a while ("no known bugs
  291. X       at this ti ... bus error core dumped").
  292. X
  293. XChanges since 3.27:
  294. X
  295. X1.  The unshar-time test for a touch -m facility now greps for
  296. X    'mmdd', not '[-amc]', making it more universally successful.
  297. X
  298. X2.  NOTE:  there is still a problem with -n arguments using
  299. X    a 'x/y' construct, but I don't know how to properly generalize
  300. X    it so you'll have to edit shars made with some uses of -a
  301. X    with -n x/y.
  302. X
  303. X3.  This is surely my last work on this.  It does everything
  304. X    I needed and more.  Thanks for all the help and suggestions.
  305. X    It seems as though we didn't precipitate 'death of the shar'
  306. X    after all :-) :-) :-).
  307. X
  308. XChanges since 3.32:
  309. X
  310. X1.  Several bug fixes.
  311. X
  312. X2.  Inverted the meaning of '-x'; the new default is to NOT overwrite.
  313. X
  314. X3.  Added '-c' checking when unpacking so the recipient can force overwrites.
  315. X
  316. X4.  Made '-L' work even with files not being overwritten.
  317. X
  318. X5.  Added '-m' and changed the default behavior to not generate TOUCH commands.
  319. X
  320. X6.  Added '-F'; the default is to suppress the extra 'X' at the beginning of
  321. X    each line unless it is needed (i.e., the first character of the line is
  322. X    already 'X' or is a non-graphic).
  323. X
  324. X7.  Renamed '-b' and '-t' to '-B' and '-T', respectively.
  325. X
  326. X8.  Added '-b<n>' for use with compression (calls compress with -b<n>).
  327. X
  328. X9.  Renamed the temporary files used in unpacking from shar3_???_.tmp to
  329. X    _shar_???_.tmp.
  330. X
  331. X10. Directories may now be passed to shar; a recursive directory walk is
  332. X    performed.  This feature may be disabled by compiling with -DNO_WALKTREE.
  333. SHAR_EOF
  334. chmod 0644 README ||
  335. echo 'restore of README failed'
  336. Wc_c="`wc -c < 'README'`"
  337. test 10777 -eq "$Wc_c" ||
  338.     echo 'README: original size 10777, current size' "$Wc_c"
  339. fi
  340. # ============= Makefile ==============
  341. if test -f 'Makefile' -a X"$1" != X"-c"; then
  342.     echo 'x - skipping Makefile (File already exists)'
  343. else
  344. echo 'x - extracting Makefile (Text)'
  345. sed 's/^X//' << 'SHAR_EOF' > 'Makefile' &&
  346. X#  makefile for shar
  347. X#
  348. X# $Header: /u/rhg/src/shar/Makefile,v 3.43 90/08/13 08:10:03 rhg Exp $
  349. X#
  350. X#+:EDITS:
  351. X#:08-07-1990-21:25-rhg@cps.com-compress man pages if MANEXT ends with .Z
  352. X#:08-05-1990-23:50-rhg@cps.com-add post and compressed.
  353. X#:08-05-1990-12:19-rhg@cps.com-add LIBS and -ldirent
  354. X#:08-04-1990-18:51-rhg@cps.com-add CC, LD; move strip; reorder chgrp/chown.
  355. X#:05-14-1990-17:33-wht@n4hgf-add -F 5000 for XENIX 286
  356. X#:05-14-1990-17:32-bill@netagw.com-expanded Makefile
  357. X#:03-28-1990-14:54-wht@n4hgf-for 3.10, add who@where.c
  358. X
  359. XBINDIR    = /usr/local/bin
  360. XOWNER    = bin
  361. XGROUP    = bin
  362. XBINMODE    = 0755
  363. XMANDIR    = /usr/man/man1
  364. XMANEXT    = 1
  365. XMANOWN    = bin
  366. XMANGRP    = bin
  367. XMANMODE    = 0644
  368. X
  369. XCC    = cc
  370. XLD    = ${CC}
  371. X
  372. X# If you don't have Doug Gwyn's dirent routines or equivalent, then remove
  373. X# -ldirent from LIBS and add -DNO_WALKTREE to CFLAGS.  All you will give up
  374. X# is the ability to shar directorys without using "find ... -type f | shar -S".
  375. X# Note that many systems have the equivalent of the dirent library built into
  376. X# the standard C library.  For such systems, just remove -ldirent from LIBS.
  377. X# (note by RHG@CPS.COM: I don't know which systems listed below are like that.)
  378. X
  379. X# For VAX, PYRAMID, SEQUENT, AT&T 3B1/7300
  380. XCFLAGS    = -O 
  381. XLDFLAGS    =
  382. XLIBS    = -ldirent
  383. X
  384. X# For SUN and SCO XENIX/UNIX 386
  385. X#CFLAGS    = -O 
  386. X#LDFLAGS    =
  387. X#LIBS    =
  388. X
  389. X# For SCO XENIX 286
  390. X#CFLAGS    = -O 
  391. X#LDFLAGS    = -F 5000
  392. X#LIBS    = -ldirent
  393. X
  394. X# For other System V systems I did not catch
  395. X#CFLAGS    = -O -DSYS5
  396. X#LDFLAGS    =
  397. X#LIBS    = -ldirent
  398. X
  399. X# For other BSD-like systems
  400. X#CFLAGS    = -O -DBSD42
  401. X#LDFLAGS    =
  402. X#LIBS    =
  403. X
  404. XSOURCES    = README Makefile shar.1 unshar.1 uushar.c who@where.c unshar.c shar.c
  405. X
  406. XSHAR    = shar.o uushar.o who@where.o
  407. XUNSHAR    = unshar.o
  408. X
  409. Xall: shar unshar
  410. X
  411. Xshar: ${SHAR}
  412. X    ${LD} ${LDFLAGS} ${SHAR} ${LIBS} -o shar
  413. X
  414. Xunshar : ${UNSHAR}
  415. X    ${LD} ${LDFLAGS} ${UNSHAR} ${LIBS} -o unshar
  416. X
  417. Xshar.o : shar.c
  418. X    ${CC} ${CFLAGS} -c shar.c
  419. X
  420. Xunshar.o : unshar.c
  421. X    ${CC} ${CFLAGS} -c unshar.c
  422. X
  423. Xuushar.o : uushar.c
  424. X    ${CC} ${CFLAGS} -c uushar.c
  425. X
  426. Xwho@where.o : who@where.c
  427. X    ${CC} ${CFLAGS} -c who@where.c
  428. X
  429. Xinstall : shar unshar
  430. X    strip shar unshar
  431. X    cp shar unshar ${BINDIR}
  432. X    chmod ${BINMODE} ${BINDIR}/shar ${BINDIR}/unshar
  433. X    chgrp ${GROUP} ${BINDIR}/shar ${BINDIR}/unshar
  434. X    chown ${OWNER} ${BINDIR}/shar ${BINDIR}/unshar
  435. X    if expr "${MANEXT}" : ".*\.Z$$" >/dev/null; then \
  436. X        compress -b14 < shar.1   > ${MANDIR}/shar.${MANEXT}; \
  437. X        compress -b14 < unshar.1 > ${MANDIR}/unshar.${MANEXT}; \
  438. X    else \
  439. X        cp shar.1   ${MANDIR}/shar.${MANEXT}; \
  440. X        cp unshar.1 ${MANDIR}/unshar.${MANEXT}; \
  441. X    fi
  442. X    chmod ${MANMODE} ${MANDIR}/shar.${MANEXT} ${MANDIR}/unshar.${MANEXT}
  443. X    chgrp ${MANGRP}  ${MANDIR}/shar.${MANEXT} ${MANDIR}/unshar.${MANEXT}
  444. X    chown ${MANOWN}  ${MANDIR}/shar.${MANEXT} ${MANDIR}/unshar.${MANEXT}
  445. X
  446. Xclean :
  447. X    rm -f shar unshar *.o core
  448. X
  449. XSETV =    X="`head -1 shar.c`"; \
  450. X    V=`expr "$$X" : 'char \*revision = "\([1-9][0-9]*\.[0-9.a-zA-Z]*\)";$$'`;
  451. X
  452. Xpost : ${SOURCES}
  453. X    ${SETV} test -n "$$V" && ( \
  454. X    rm -f shar$$V.[0-9][0-9]; \
  455. X    shar -l50 -acF -n "shar$$V" -o "shar$$V" ${SOURCES} )
  456. X
  457. Xcompressed : ${SOURCES}
  458. X    ${SETV} test -n "$$V" && \
  459. X    shar -acFm -b16 -n "shar$$V" > "shar$$V" ${SOURCES}
  460. X
  461. SHAR_EOF
  462. chmod 0644 Makefile ||
  463. echo 'restore of Makefile failed'
  464. Wc_c="`wc -c < 'Makefile'`"
  465. test 3090 -eq "$Wc_c" ||
  466.     echo 'Makefile: original size 3090, current size' "$Wc_c"
  467. fi
  468. # ============= shar.1 ==============
  469. if test -f 'shar.1' -a X"$1" != X"-c"; then
  470.     echo 'x - skipping shar.1 (File already exists)'
  471. else
  472. echo 'x - extracting shar.1 (Text)'
  473. sed 's/^X//' << 'SHAR_EOF' > 'shar.1' &&
  474. X.TH SHAR 1 "August 12, 1990"
  475. X.SH NAME
  476. Xshar \- create shell archives
  477. X.SH SYNOPSIS
  478. X.nf
  479. Xshar [ options ] file ...
  480. Xshar \-S [ options ]
  481. X.fi
  482. X.SH DESCRIPTION
  483. XShar
  484. Xcreates "shell archives" (or shar files) which are in text format
  485. Xand can be mailed. These files may be unpacked later by executing them
  486. Xwith /bin/sh. The resulting archive is sent to standard out unless the
  487. X\f2\-o\f1 option is given.  A wide range of features provide extensive
  488. Xflexibility in manufacturing shars and in specifying shar "smartness."
  489. XArchives may be "vanilla" or comprehensive.
  490. XThis manual page reflects shar version 3.43.
  491. X.SS OPTIONS
  492. X.PP
  493. XOptions can be given in any order. Some options depend on each other:
  494. X.nf
  495. X    The \f2\-o\f1 option is required if the \f2\-l\f1 or \f2\-L\f1 option is used.
  496. X    The \f2\-n\f1 option is required if the \f2\-a\f1 option is used.
  497. X    See \f2\-V\f1 below.
  498. X.fi
  499. X.IP "\f2\-V\f1"
  500. XProduce "vanilla" shars which rely only upon the existence of sed and
  501. Xecho in the unsharing environment.  In addition, "if test" must also be
  502. Xsupported if the \f2\-X\f1 option is used.  The \f2\-V\f1 silently
  503. Xdisables options offensive to the "network cop" (or "brown shirt"), but
  504. Xdoes warn you if it is specified with \f2\-B\f1, \f2\-C\f1, \f2\-p\f1
  505. Xor \f2\-M\f1 (any of which does or might require uudecode or compress in
  506. Xthe unsharing environment).
  507. X.IP "\f2\-v\f1"
  508. XVerbose OFF. Disables the inclusion of comments to be output when the archive
  509. Xis unpacked.
  510. X.IP "\f2\-w\f1"
  511. XDo NOT check with 'wc \-c' after unpack. The default is to check.
  512. X.IP "\f2\-n\f1 name"
  513. XName of archive to be included in the header of the shar files.
  514. XSee the \f2\-a\f1 switch.
  515. X.IP "\f2\-a\f1"
  516. XAllows automatic generation of headers:
  517. X.nf
  518. X    Submitted-by: who@where
  519. X    Archive-name: <name>/part##
  520. X.fi
  521. XThe <name> must be given with the \f2\-n\f1 switch.
  522. XIf name includes a '/' "/part" isn't used. Thus:
  523. X.RS 10m
  524. X.nf
  525. X.ta 30m
  526. X\-n xyzzy    produces:
  527. X    xyzzy/part01
  528. X    xyzzy/part02
  529. X
  530. X\-n xyzzy/patch    produces:
  531. X    xyzzy/patch01
  532. X    xyzzy/patch02
  533. X
  534. X\-n xyzzy/patch01.    produces:
  535. X    xyzzy/patch01.01
  536. X    xyzzy/patch01.02
  537. X.RE
  538. X.fi
  539. X.IP ""
  540. XThe who@where can be
  541. Xexplicitly stated with the \f2\-s\f1 switch if the default isn't apropriate.
  542. XWho@where is essentially built as `whoami`@`uname`.
  543. X.IP "\f2\-s\f1 who@where"
  544. XOverride automatically determined submitter name.
  545. X.IP "\f2\-x\f1"
  546. XOverwrite existing files without checking.
  547. XIf neither \f2\-x\f1 nor \f2\-X\f1 is specified, the unpack will
  548. Xcheck for and
  549. Xnot overwrite existing files when unpacking the archive
  550. X(unless '\-c' is passed as a parameter to the script when unpacking).
  551. X.IP "\f2\-X\f1"
  552. XInteractively overwrite existing files
  553. X(DO NOT USE FOR SHARS SUBMITTED TO THE NET).
  554. X.IP "\f2\-B\f1"
  555. XTreat all files as binary, use uuencode prior to packing. This increases the
  556. Xsize of the archive. The recipient must have uudecode in order to unpack.
  557. X(USE OF UUENCODE IS NOT APPRECIATED BY MANY ON THE NET).
  558. X.IP "\f2\-T\f1"
  559. XTreat all files as text (default).
  560. X.IP "\f2\-C\f1"
  561. XCompress and uuencode all files prior to packing. The recipient must have
  562. Xuudecode and uncompress in order to unpack
  563. X(USE OF UUENCODE AND COMPRESS IS NOT APPRECIATED BY MANY ON THE NET).
  564. X.IP "\f2\-m\f1"
  565. XGenerate 'touch' commands to restore the file modification dates when
  566. Xunpacking files from the archive.
  567. X.IP "\f2\-p\f1"
  568. XAllow positional parameter options. The options "\f2\-B\f1" and "\f2\-T\f1"
  569. Xand "\f2\-C\f1" may be embedded, and files to the right of the
  570. Xoption will be processed in the specified mode.
  571. X.IP "\f2\-b\f1 X"
  572. XWhen doing compression, use '\-bX' as a parameter to compress.
  573. XThe \f2\-B\f1 options turns on the \f2\-C\f1 option by default.
  574. X.IP "\f2\-M\f1"
  575. XMixed mode. Determine if the files are text or binary and archive correctly.
  576. XFiles found to be binary are uudecoded prior to packing
  577. X(USE OF UUENCODE IS NOT APPRECIATED BY MANY ON THE NET).
  578. X.IP "\f2\-P\f1"
  579. XUse temporary files instead of pipes in the shar file.
  580. X.IP "\f2\-c\f1"
  581. XStart the shar with a cut line. A line saying 'Cut here' is placed at the
  582. Xstart of each output file.
  583. X.IP "\f2\-f\f1"
  584. XRestore by filename only, rather than path. This option causes only file
  585. Xnames to be used, which is useful when building a shar from several
  586. Xdirectories, or another directory.  Note that if a directory name is passed
  587. Xto shar, the substructure of that directory will be restored whether \f2\-f\f1
  588. Xis specified or not.
  589. X.IP "\f2\-d\f1 XXX"
  590. XUse XXX to delimit the files in the shar instead of SHAR_EOF.
  591. XThis is for those who want to personalize their shar files.
  592. X.IP "\f2\-F\f1"
  593. XForces the prefix character (normally 'X' unless the parameter to the \f2\-d\f1
  594. Xoption starts with 'X') to be prepended to every line even if
  595. Xnot required.  This option may slightly increase the size of the archive,
  596. Xespecially if \f2\-B\f1 or \f2\-C\f1 is used.
  597. X.IP "\f2\-o\f1 XXX"
  598. XSave the archive to files XXX.01 thru XXX.nn instead of standard out.
  599. XMust be used when the \f2\-l\f1 or the \f2\-L\f1 switches are used
  600. X.IP "\f2\-l\f1 XX"
  601. XLimit the output file size to XXk bytes but don't split input files.
  602. X.IP "\f2\-L\f1 XX"
  603. XLimit output file size to XXk bytes and split files if necessary. The archives
  604. Xcreated with this option must be unpacked in correct order.
  605. X.IP "\f2\-S\f1"
  606. XRead list of files to be packed from the standard input rather than
  607. Xfrom the command line.  Input must be in a form similar to
  608. Xthat generated by the find command, one filename per line.  This
  609. Xswitch is especially useful when the command line will not hold
  610. Xthe list of files to be packed. For example:
  611. X.nf
  612. X
  613. Xfind . \-type f \-print | sort | shar \-S \-C \-L50 \-o /tmp/big
  614. X
  615. X.fi
  616. XIf \f2\-p\f1 is specified on the command line, then the 
  617. Xoptions "\f2\-B\f1" and "\f2\-T\f1" and "\f2\-C\f1" may be included
  618. Xin the standard input (on a line separate from filenames).
  619. XThe maximum number of lines of standard input, file names
  620. Xand options, may not exceed 1024.
  621. X.SH EXAMPLES
  622. X.nf
  623. X.ta 37m
  624. Xshar *.c > cprog.shar    # all C prog sources
  625. Xshar \-v *.[ch] > cprog.shar    # non-verbose, .c and .h files
  626. Xshar \-B \-l28 \-oarc.sh *.arc    # all binary .arc files, into
  627. X    # files arc.sh.01 thru arc.sh.NN
  628. Xshar \-f /lcl/src/u*.c > u.sh    # use only the filenames
  629. X.ta
  630. X.fi
  631. X.SH WARNINGS
  632. X.PP
  633. XNo chmod or touch is ever generated for directories created when unpacking.
  634. XThus, if a directory is given to shar, the protection and
  635. Xmodification dates of corresponding unpacked directory
  636. Xmay not match those of the original.
  637. X.PP
  638. XIf a directory is passed to shar, it may be scanned more than once.  Therefore,
  639. Xone should be careful not change the directory while shar is running.
  640. X.PP
  641. XBe careful that the output file(s) are not included in the inputs or shar
  642. Xmay loop until the disk fills up.  Be particularly careful when a directory
  643. Xis passed to shar that the output files are not in that directory
  644. X(or a subdirectory of that directory).
  645. X.PP
  646. XUse of the \f2\-B\f1, \f2\-M\f1 or \f2\-C\f1 may slow the archive process
  647. Xconsiderably, depending on the number of files.
  648. X.PP
  649. XUse of \f2\-X\f1 produces shars which \f2WILL\f1 cause problems
  650. Xwith many unshar procedures.  Use this feature only for archives
  651. Xto be passed among agreeable parties.  Certainly, \f2\-X\f1 is NOT
  652. Xfor shell archives which are to be submitted to Usenet.
  653. XUsage of \f2\-B\f1 or \f2\-C\f1 in net shars will cause you to
  654. Xbe flamed off the earth.
  655. XUsing \f2\-m\f1 or not using \f2\-F\f1 may also get you occasional complaints.
  656. X.SH SEE ALSO
  657. X.PP 
  658. Xunshar(1)
  659. X.SH DIAGNOSTICS
  660. X.PP
  661. XError messages for illegal or incompatible options,
  662. Xfor non-regular, missing or inaccessible files or for (unlikely)
  663. Xmemory allocation failure.
  664. X.SH AUTHORS
  665. X.nf
  666. Xshar3 is a derived work based on the efforts of:
  667. Xdecvax!microsof!uw-beave!jim (James Gosling at CMU)
  668. XMichael A. Thompson, Dalhousie University, Halifax, N.S., Canada
  669. Xdavidsen@sixhub (Bill Davidsen)
  670. Xrhg@CPS.COM (Richard H. Gumpertz)
  671. Xcolas@avahi.inria.fr (Colas Nahaboo)
  672. Xbill@netagw.com (Bill Aten)
  673. Xdennis%nanovx@gatech.edu (Dennis Boylan)
  674. Xwht%n4hgf@gatech.edu (Warren Tucker)
  675. X(other anonymous persons)
  676. X
  677. Xman pages:
  678. Xjhd@irfu.se (Jan Dj{rv)
  679. X.fi
  680. SHAR_EOF
  681. chmod 0644 shar.1 ||
  682. echo 'restore of shar.1 failed'
  683. Wc_c="`wc -c < 'shar.1'`"
  684. test 7946 -eq "$Wc_c" ||
  685.     echo 'shar.1: original size 7946, current size' "$Wc_c"
  686. fi
  687. # ============= unshar.1 ==============
  688. if test -f 'unshar.1' -a X"$1" != X"-c"; then
  689.     echo 'x - skipping unshar.1 (File already exists)'
  690. else
  691. echo 'x - extracting unshar.1 (Text)'
  692. sed 's/^X//' << 'SHAR_EOF' > 'unshar.1' &&
  693. X.TH UNSHAR 1 "August 12, 1990"
  694. X.SH NAME
  695. Xunshar \- unpack a shar file
  696. X.SH SYNOPSIS
  697. Xunshar [ \f2\-d\f1 directory ] [ \f2\-c\f1 ] [ \f2\-e\f1 | \f2\-E\f1 exit_line ] [ file ... ]
  698. X.SH DESCRIPTION
  699. X.PP
  700. XUnshar scans mail messages looking for the start of a shell archive. It
  701. Xthen passes the archive through a copy of the shell to unpack it. It
  702. Xwill accept multiple files. If no files are given, standard input is used.
  703. X.PP
  704. XThe \f2\-d\f1 option tells unshar to change directory before unpacking
  705. Xany files.
  706. X.PP
  707. XThe \f2\-c\f1 option is passed through to 'sh' as a parameter to the unpacked
  708. Xscript.  Many archive shar scripts (including those produced by
  709. Xshar 3.40 and newer)
  710. Xuse this to indicate that existing files should be overwritten.
  711. X.PP
  712. XUnshar can unpack shar files that are concatenated in one file with the
  713. X\f2\-e\f1 option, which separates files by recognizing the "exit 0" string
  714. Xat the beginning of a line. 
  715. XThe \f2\-E\f1 option allows you to specify the string that separates archives
  716. Xif "exit 0" isn't appropriate. (Hint: most .signatures have a "\-\-" on a line
  717. Xright before them). \f2\-e\f1 is equivalent to \f2\-E "exit 0"\f1.
  718. X.SH SEE ALSO
  719. Xshar(1)
  720. X.SH DIAGNOSTICS
  721. XAny message from the shell may be displayed.
  722. X.SH AUTHORS
  723. X.nf
  724. XMichael Mauldin at Carnegie-Mellon University
  725. Xguido@mcvax (Guido van Rossum at CWI, Amsterdam)
  726. Xdavidsen@sixhub.uuxp (Bill Davidsen)
  727. Xwht%n4hgf@gatech.edu (Warren Tucker)
  728. Xrhg@CPS.COM (Richard H. Gumpertz)
  729. Xcolas@avahi.inria.fr (Colas Nahaboo)
  730. X
  731. Xman pages:
  732. Xjhd@irfu.se (Jan Dj{rv)
  733. X.fi
  734. X.PP
  735. SHAR_EOF
  736. chmod 0644 unshar.1 ||
  737. echo 'restore of unshar.1 failed'
  738. Wc_c="`wc -c < 'unshar.1'`"
  739. test 1515 -eq "$Wc_c" ||
  740.     echo 'unshar.1: original size 1515, current size' "$Wc_c"
  741. fi
  742. # ============= uushar.c ==============
  743. if test -f 'uushar.c' -a X"$1" != X"-c"; then
  744.     echo 'x - skipping uushar.c (File already exists)'
  745. else
  746. echo 'x - extracting uushar.c (Text)'
  747. sed 's/^X//' << 'SHAR_EOF' > 'uushar.c' &&
  748. X/* $Header: /u/rhg/src/shar/uushar.c,v 3.43 90/08/13 07:50:57 rhg Exp $ */
  749. X
  750. X#include <stdio.h>
  751. X#include <sys/types.h>
  752. X#include <sys/stat.h>
  753. X
  754. X/* ENC is the basic 1 character encoding function to make a char printing */
  755. X#if 1 /* Richard H. Gumpertz (RHG@CPS.COM), 24 April 1990 */
  756. X#define ENC(c) ((((c) + 077) & 077) + 041)
  757. X#else /* RHG */
  758. X#define ENC(c) ((((c) & 077) + ' ') | ((c & 077) == 0 ? 0100 : 0))
  759. X#endif /* RHG */
  760. X
  761. Xencode (in, out)
  762. X    FILE *in;
  763. X    FILE *out;
  764. X{
  765. X    char  buf[80];
  766. X    int  i, n;
  767. X
  768. X    for (;;)
  769. X    {
  770. X    /* 1 (up to) 45 character line */
  771. X    n = fr (in, buf, 45);
  772. X    putc (ENC (n), out);
  773. X
  774. X    for (i = 0; i < n; i += 3)
  775. X        outdec (&buf[i], out);
  776. X
  777. X    putc ('\n', out);
  778. X    if (n <= 0)
  779. X        break;
  780. X    }
  781. X}
  782. X
  783. X/*
  784. X * output one group of 3 bytes, pointed at by p, on file f.
  785. X */
  786. Xoutdec (p, f)
  787. X    char *p;
  788. X    FILE *f;
  789. X{
  790. X    int  c1, c2, c3, c4;
  791. X
  792. X    c1 = *p >> 2;
  793. X    c2 = (*p << 4) & 060 | (p[1] >> 4) & 017;
  794. X    c3 = (p[1] << 2) & 074 | (p[2] >> 6) & 03;
  795. X    c4 = p[2] & 077;
  796. X    putc (ENC (c1), f);
  797. X    putc (ENC (c2), f);
  798. X    putc (ENC (c3), f);
  799. X    putc (ENC (c4), f);
  800. X}
  801. X
  802. X/* fr: like read but stdio */
  803. Xint
  804. X     fr (fp, buf, cnt)
  805. X    FILE *fp;
  806. X    char *buf;
  807. X    int  cnt;
  808. X{
  809. X    int  c, i;
  810. X
  811. X    for (i = 0; i < cnt; i++)
  812. X    {
  813. X    c = getc (fp);
  814. X    if (c == EOF)
  815. X        return (i);
  816. X    buf[i] = c;
  817. X    }
  818. X    return (cnt);
  819. X}
  820. X/* vi: set tabstop=4 shiftwidth=4: */
  821. SHAR_EOF
  822. chmod 0644 uushar.c ||
  823. echo 'restore of uushar.c failed'
  824. Wc_c="`wc -c < 'uushar.c'`"
  825. test 1365 -eq "$Wc_c" ||
  826.     echo 'uushar.c: original size 1365, current size' "$Wc_c"
  827. fi
  828. # ============= who@where.c ==============
  829. if test -f 'who@where.c' -a X"$1" != X"-c"; then
  830.     echo 'x - skipping who@where.c (File already exists)'
  831. else
  832. echo 'x - extracting who@where.c (Text)'
  833. sed 's/^X//' << 'SHAR_EOF' > 'who@where.c' &&
  834. X/* $Header: /u/rhg/src/shar/who@where.c,v 3.43 90/08/13 07:51:00 rhg Exp $ */
  835. X
  836. X/*+-------------------------------------------------------------------------
  837. X    who@where.c - find out who i am & where i am
  838. X    ...!gatech!kd4nc!n4hgf!wht (wht%n4hgf@gatech.edu)
  839. X--------------------------------------------------------------------------*/
  840. X/*+:EDITS:*/
  841. X/*:04-03-1990-19:55-wht@n4hgf-get rid of complicated who_am_i */
  842. X/*:04-01-1990-13:30-pat@rwing-use utsname.nodename instead of sysname */
  843. X/*:04-02-1990-12:12-wht@n4hgf-sigh... some pwd.h dont declare functions */
  844. X/*:03-29-1990-18:23-wht@n4hgf-add automatic sequent support */
  845. X/*:03-28-1990-15:24-wht@n4hgf-creation */
  846. X
  847. X#include <stdio.h>
  848. X#include <sys/types.h>
  849. X#include <pwd.h>
  850. X
  851. X/* assume system v unless otherwise fixed */
  852. X#if (defined(pyr) || defined(vax) || defined(sequent)) && !defined(BSD42) && !defined(SYS5)
  853. X#define BSD42
  854. X#endif
  855. X#if defined(sun)    /* this miscreant doesn't exactly fit BSD or SYSV */
  856. X#undef BSD42
  857. X#undef SYS5
  858. X#endif
  859. X#if !defined(BSD42) && !defined(sun) && !defined(SYS5)
  860. X#define SYS5
  861. X#endif
  862. X
  863. X#if defined(sun) || defined(BSD42)
  864. X#define strchr    index
  865. X#define strrchr    rindex
  866. X#endif
  867. X
  868. X#if !defined(SYS5) || defined(sun)
  869. X#include <sys/time.h>
  870. Xextern int errno;
  871. X#else
  872. X#include <sys/utsname.h>
  873. X#include <time.h>
  874. X#endif    /* system dependencies */
  875. X
  876. X/*+-------------------------------------------------------------------------
  877. X    who_am_i() - get user name
  878. X--------------------------------------------------------------------------*/
  879. Xchar *
  880. Xwho_am_i()
  881. X{
  882. X    struct passwd *getpwuid();
  883. X    struct passwd *passwd;
  884. X    passwd = getpwuid(getuid());
  885. X    (void)endpwent();
  886. X    if(passwd == (struct passwd *)0)
  887. X        return("???");
  888. X    return(passwd->pw_name);
  889. X
  890. X}    /* end of who_am_i */
  891. X
  892. X/*+-------------------------------------------------------------------------
  893. X    where_am_i() - do uname, gethostname, or read file (/etc/systemid)
  894. X--------------------------------------------------------------------------*/
  895. Xchar *
  896. Xwhere_am_i()
  897. X{
  898. X#if defined(M_SYS5)    /* SCO UNIX or XENIX */
  899. XFILE *fpsid = fopen("/etc/systemid","r");
  900. Xstatic char s20[20];
  901. X    if(!fpsid)
  902. X        return("???");
  903. X    fgets(s20,sizeof(s20),fpsid);
  904. X    fclose(fpsid);
  905. X    s20[strlen(s20) - 1] = 0;
  906. X    return(s20);
  907. X#else /* M_SYS5 */
  908. X#if defined(SYS5)
  909. Xstatic struct utsname where_i_am;
  910. X    uname(&where_i_am);
  911. X    return(where_i_am.nodename);
  912. X#else /* SYS5 */
  913. Xstatic char where_i_am[64];
  914. X    gethostname(where_i_am,sizeof(where_i_am));
  915. X    return(where_i_am);
  916. X#endif /* SYS5 */
  917. X#endif /* M_SYS5 */
  918. X}    /* end of where_am_i */
  919. X
  920. X/*+-------------------------------------------------------------------------
  921. X    who_where(buf)
  922. X--------------------------------------------------------------------------*/
  923. Xchar *
  924. Xwho_where(buf)
  925. Xchar *buf;
  926. X{
  927. Xstatic char ww[64];
  928. X
  929. X    if(!buf)
  930. X        buf = ww;
  931. X    strcpy(buf,who_am_i());
  932. X    strcat(buf,"@");
  933. X    strcat(buf,where_am_i());
  934. X}    /* end of who_where */
  935. X
  936. X/* vi: set tabstop=4 shiftwidth=4: */
  937. X/* end of who@where.c */
  938. SHAR_EOF
  939. chmod 0644 who@where.c ||
  940. echo 'restore of who@where.c failed'
  941. Wc_c="`wc -c < 'who@where.c'`"
  942. test 2882 -eq "$Wc_c" ||
  943.     echo 'who@where.c: original size 2882, current size' "$Wc_c"
  944. fi
  945. # ============= unshar.c ==============
  946. if test -f 'unshar.c' -a X"$1" != X"-c"; then
  947.     echo 'x - skipping unshar.c (File already exists)'
  948. else
  949. echo 'x - extracting unshar.c (Text)'
  950. sed 's/^X//' << 'SHAR_EOF' > 'unshar.c' &&
  951. Xchar *revision = "3.43";
  952. Xchar RCS_ID[] = "$Header: /u/rhg/src/shar/unshar.c,v 3.43 90/08/13 07:50:52 rhg Exp $";
  953. X/****************************************************************
  954. X * unshar.c: Unpackage one or more shell archive files
  955. X *
  956. X * Usage:     unshar [ -c ] [ -e | -E exit_line ] [ -d directory ] [ file ... ]
  957. X *
  958. X * Description:    unshar is a filter which removes the front part
  959. X *        of  a file and passes the rest to the 'sh' command.
  960. X *        It understands phrases like "cut here", and also
  961. X *        knows about shell comment characters and the Unix
  962. X *        commands "echo", "cat", and "sed".
  963. X *
  964. X *        The -c flag is passed through to the shell as a parameter to the script
  965. X *
  966. X *        It can unshar shar files concatenated in one file, with the
  967. X *        the "-e" command, which separates files by recognizing the
  968. X *        "exit 0" string at the beginning of a line
  969. X *
  970. X *        (The -E string option allows you to specify this string, thus
  971. X *        -e is equivalent to -E "exit 0")
  972. X *
  973. X *        The -d flag tells unshar to change directory before unsharing
  974. X *
  975. X * HISTORY
  976. X *  4-Aug-90  Richard H. Gumpertz (rhg@cps.com)
  977. X *        renamed -c and -C to -e and -E and added -c flag (passed through to sh)
  978. X * 19-Apr-90  Colas Nahaboo (colas@mirsa.inria.fr)
  979. X *        added -c and -C flags to read from concatenated archives
  980. X *  1-Feb-85  Guido van Rossum (guido@mcvax) at CWI, Amsterdam
  981. X *        Added missing 'quit' routine;
  982. X *        added -d flag to change to directory first;
  983. X *        added filter mode (read stdin when no arguments);
  984. X *        added 'getopt' to get flags (makes it self-contained).
  985. X * 29-Jan-85  Michael Mauldin (mlm) at Carnegie-Mellon University
  986. X *        Created.
  987. X ****************************************************************/
  988. X/*+:EDITS:*/
  989. X/*:08-04-1990-15:54-rhg@cps.com-changes listed above (-c/-C => -e/-E, new -c)
  990. X/*:05-05-1990-01:37-relay.EU.net!rivm!a3-dont assume vax is running BSD */
  991. X/*:04-19-1990-15:20-wht@n4hgf-fix so -d doesnt make argv files unreachable */
  992. X/*:04-19-1990-15:06-wht@n4hgf-colas@mirsa patches had expanded tabs */
  993. X/*:04-10-1990-22:02-wht@n4hgf-stdin failed sometimes-can not seek on pipe */
  994. X
  995. X#include <stdio.h>
  996. X#define EOL '\n'
  997. X
  998. X#if (defined(pyr) || defined(sun) || defined(BSD42) || \
  999. X defined(vax) || defined(sequent)) && !defined(SYS5)
  1000. X#define strchr    index
  1001. X#undef USE_GETCWD
  1002. Xchar *getwd();
  1003. X#else
  1004. X#define USE_GETCWD
  1005. Xchar *getcwd();
  1006. X#endif
  1007. X
  1008. Xchar *strchr();
  1009. X
  1010. Xextern char *optarg;
  1011. Xextern int optind;
  1012. X
  1013. Xint c_flag = 0;
  1014. Xint continue_reading = 0;
  1015. Xchar *exit_string = "exit 0";
  1016. Xint exit_string_length;
  1017. Xchar argvdir[1024];
  1018. X
  1019. Xmain(argc,argv)
  1020. Xint argc;
  1021. Xchar *argv[];
  1022. X{
  1023. X    int i,ch;
  1024. X    FILE *in;
  1025. X    char s1024[1024];
  1026. X
  1027. X    setbuf(stdout,NULL);
  1028. X    setbuf(stderr,NULL);
  1029. X
  1030. X#ifdef USE_GETCWD
  1031. X    if(!getcwd(argvdir,sizeof(argvdir)))
  1032. X    {
  1033. X        perror("cannot get current directory name");
  1034. X        exit(1);
  1035. X    }
  1036. X#else
  1037. X    argvdir[0] = 0;
  1038. X    if(!getwd(argvdir))
  1039. X    {
  1040. X        if(argvdir[0])
  1041. X            fprintf(stderr,"%s\n",argvdir);
  1042. X        else
  1043. X            printf(stderr,"cannot get current directory name\n");
  1044. X        exit(1);
  1045. X    }
  1046. X#endif
  1047. X
  1048. X
  1049. X    /* Process options */
  1050. X
  1051. X    while((ch = getopt(argc,argv,"cd:eE:")) != EOF)
  1052. X    {
  1053. X        switch(ch)
  1054. X        {
  1055. X        case 'c':
  1056. X            c_flag = 1;
  1057. X            break;
  1058. X        case 'd':
  1059. X            if(chdir(optarg) == -1)
  1060. X            {
  1061. X                fprintf(stderr,"unshar: cannot chdir to '%s'\n",optarg);
  1062. X                exit(2);
  1063. X            }
  1064. X            break;
  1065. X        case 'E':
  1066. X            exit_string = optarg;
  1067. X        case 'e':
  1068. X            continue_reading = 1;
  1069. X            exit_string_length = strlen(exit_string);
  1070. X            break;
  1071. X        default:
  1072. X            quit(2,"Usage: unshar [-c] [-e | -E exit_line] [-d directory] [file ...]\n");
  1073. X        }
  1074. X    }
  1075. X
  1076. X    if(optind < argc)
  1077. X    {
  1078. X        for(i= optind; i < argc; ++i)
  1079. X        {
  1080. X            if(argv[i][0] == '/') {
  1081. X                strcpy(s1024,argv[i]);
  1082. X            } else {
  1083. X                strcpy(s1024,argvdir);
  1084. X                strcat(s1024,"/");
  1085. X                strcat(s1024,argv[i]);
  1086. X            }
  1087. X            if(!(in = fopen(s1024,"r")))
  1088. X            {
  1089. X                perror(s1024);
  1090. X                exit(1);
  1091. X            }
  1092. X            process(s1024,in);
  1093. X            fclose(in);
  1094. X        }
  1095. X    }
  1096. X    else
  1097. X    {
  1098. X        sprintf(s1024,"/tmp/unsh.%05d",getpid());
  1099. X        unlink(s1024);
  1100. X        if(!(in = fopen(s1024,"w+")))
  1101. X        {
  1102. X            fprintf(stderr,"cannot open temp file '%s'\n",s1024);
  1103. X            exit(1);
  1104. X        }
  1105. X        unlink(s1024);    /* don't try this with MSDOS, sports fans */
  1106. X        while(i = fread(s1024,1,sizeof(s1024),stdin))
  1107. X            fwrite(s1024,i,1,in);
  1108. X        rewind(in);
  1109. X        process("standard input",in);
  1110. X        fclose(in);
  1111. X    }
  1112. X
  1113. X    exit(0);
  1114. X}
  1115. X
  1116. X
  1117. Xprocess(name,in)
  1118. Xchar *name;
  1119. XFILE *in;
  1120. X{
  1121. X    char buffer[8196];
  1122. X    char ch;
  1123. X    FILE *shpr,*popen();
  1124. X    long current_position = 0;
  1125. X    char *more_to_read = 0;
  1126. X
  1127. X    while(position(name,in,current_position))
  1128. X    {
  1129. X        printf("%s:\n",name);
  1130. X        if(!(shpr = popen((c_flag ? "sh -s -c" : "sh"),"w")))
  1131. X            quit(1,"unshar: cannot open 'sh' process\n");
  1132. X
  1133. X        if (!continue_reading) {
  1134. X            while((ch = fgetc(in)) != EOF)
  1135. X                fputc(ch,shpr);
  1136. X            pclose(shpr);
  1137. X            break;
  1138. X        } else {
  1139. X            while (more_to_read = fgets(buffer, 8196, in)) {
  1140. X                fputs(buffer, shpr);
  1141. X                if (!strncmp(exit_string, buffer, exit_string_length)) {
  1142. X                    break;
  1143. X                }
  1144. X            }
  1145. X            pclose(shpr);
  1146. X            if (more_to_read)
  1147. X                current_position = ftell(in);
  1148. X            else {
  1149. X                break;
  1150. X            }
  1151. X        }
  1152. X    }
  1153. X}
  1154. X
  1155. X/****************************************************************
  1156. X * position: position 'fil' at the start of the shell command
  1157. X * portion of a shell archive file.
  1158. X ****************************************************************/
  1159. X
  1160. Xposition(fn,fil,start)
  1161. Xchar *fn;
  1162. XFILE *fil;
  1163. Xlong start;                   /* scan file from position */
  1164. X{
  1165. X    char buf[BUFSIZ];
  1166. X    long pos,ftell();
  1167. X
  1168. X    /* Results from star matcher */
  1169. X    static char res1[BUFSIZ],res2[BUFSIZ],res3[BUFSIZ],res4[BUFSIZ];
  1170. X    static char *result[] = 
  1171. X    {
  1172. X        res1,res2,res3,res4         };
  1173. X
  1174. X    fseek(fil, start, 0);
  1175. X
  1176. X    while(1)
  1177. X    { /* Record position of the start of this line */
  1178. X        pos = ftell(fil);
  1179. X
  1180. X        /* Read next line, fail if no more and no previous process */
  1181. X        if(!fgets(buf,BUFSIZ,fil))
  1182. X        {
  1183. X            if(!start)
  1184. X                fprintf(stderr,"unshar: found no shell commands in %s\n",fn);
  1185. X            return(0);
  1186. X        }
  1187. X
  1188. X        /* Bail out if we see C preprocessor commands or C comments */
  1189. X        if(stlmatch(buf,"#include")    || stlmatch(buf,"# include") ||
  1190. X            stlmatch(buf,"#define")    || stlmatch(buf,"# define") ||
  1191. X            stlmatch(buf,"#ifdef")    || stlmatch(buf,"# ifdef") ||
  1192. X            stlmatch(buf,"#ifndef")    || stlmatch(buf,"# ifndef") ||
  1193. X            stlmatch(buf,"/*"))
  1194. X        {
  1195. X            fprintf(stderr,
  1196. X                "unshar: %s looks like raw C code, not a shell archive\n",fn);
  1197. X            return(0);
  1198. X        }
  1199. X
  1200. X        /* Does this line start with a shell command or comment */
  1201. X        if(stlmatch(buf,"#")    || stlmatch(buf,":") ||
  1202. X            stlmatch(buf,"echo ")    || stlmatch(buf,"sed ") ||
  1203. X            stlmatch(buf,"cat ") || stlmatch(buf,"if "))
  1204. X        {
  1205. X            fseek(fil,pos,0);
  1206. X            return(1);
  1207. X        }
  1208. X
  1209. X        /* Does this line say "Cut here" */
  1210. X        if(smatch(buf,"*CUT*HERE*",result) ||
  1211. X            smatch(buf,"*cut*here*",result) ||
  1212. X            smatch(buf,"*TEAR*HERE*",result) ||
  1213. X            smatch(buf,"*tear*here*",result) ||
  1214. X            smatch(buf,"*CUT*CUT*",result) ||
  1215. X            smatch(buf,"*cut*cut*",result))
  1216. X        {
  1217. X            /* Read next line after "cut here", skipping blank lines */
  1218. X            while(1)
  1219. X            {
  1220. X                pos = ftell(fil);
  1221. X
  1222. X                if(!fgets(buf,BUFSIZ,fil))
  1223. X                {
  1224. X                    fprintf(stderr,
  1225. X                        "unshar: found no shell commands after 'cut' in %s\n",fn);
  1226. X                    return(0);
  1227. X                }
  1228. X
  1229. X                if(*buf != '\n') break;
  1230. X            }
  1231. X
  1232. X            /* Win if line starts with a comment character of lower case letter */
  1233. X            if(*buf == '#' || *buf == ':' || (('a' <= *buf) && ('z' >= *buf)))
  1234. X            {
  1235. X                fseek(fil,pos,0);
  1236. X                return(1);
  1237. X            }
  1238. X
  1239. X            /* Cut here message lied to us */
  1240. X            fprintf(stderr,"unshar: %s is probably not a shell archive,\n",fn);
  1241. X            fprintf(stderr,"        the 'cut' line was followed by: %s",buf);
  1242. X            return(0);
  1243. X        }
  1244. X    }
  1245. X}
  1246. X
  1247. X/*****************************************************************
  1248. X * stlmatch  --  match leftmost part of string
  1249. X *
  1250. X * Usage:  i = stlmatch (big,small)
  1251. X *    int i;
  1252. X *    char *small, *big;
  1253. X *
  1254. X * Returns 1 iff initial characters of big match small exactly;
  1255. X * else 0.
  1256. X *
  1257. X * HISTORY
  1258. X * 18-May-82 Michael Mauldin (mlm) at Carnegie-Mellon University
  1259. X *      Ripped out of CMU lib for Rog-O-Matic portability
  1260. X * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  1261. X *    Rewritten for VAX from Ken Greer's routine.
  1262. X *
  1263. X *  Originally from klg (Ken Greer) on IUS/SUS UNIX
  1264. X *****************************************************************/
  1265. X
  1266. Xint stlmatch(big,small)
  1267. Xchar *small,*big;
  1268. X{
  1269. X    register char *s,*b;
  1270. X    s = small;
  1271. X    b = big;
  1272. X    do
  1273. X    {
  1274. X        if(*s == '\0')
  1275. X            return(1);
  1276. X    }  while(*s++ == *b++);
  1277. X    return(0);
  1278. X}
  1279. X
  1280. X/*****************************************************************
  1281. X * smatch: Given a data string and a pattern containing one or
  1282. X * more embedded stars (*) (which match any number of characters)
  1283. X * return true if the match succeeds, and set res[i] to the
  1284. X * characters matched by the 'i'th *.
  1285. X *****************************************************************/
  1286. X
  1287. Xsmatch(dat,pat,res)
  1288. Xregister char *dat,*pat,**res;
  1289. X{
  1290. X    register char *star = 0,*starend,*resp;
  1291. X    int nres = 0;
  1292. X
  1293. X    while(1)
  1294. X    {
  1295. X        if(*pat == '*')
  1296. X        {
  1297. X            star = ++pat;                  /* Pattern after * */
  1298. X            starend = dat;                  /* Data after * match */
  1299. X            resp = res[nres++];              /* Result string */
  1300. X            *resp = '\0';                  /* Initially null */
  1301. X        }
  1302. X        else if(*dat == *pat)              /* Characters match */
  1303. X        {
  1304. X            if(*pat == '\0')              /* Pattern matches */
  1305. X                return(1);
  1306. X            pat++;                      /* Try next position */
  1307. X            dat++;
  1308. X        }
  1309. X        else
  1310. X        {
  1311. X            if(*dat == '\0')              /* Pattern fails - no more */
  1312. X                return(0);                  /* data */
  1313. X            if(star == 0)                  /* Pattern fails - no * to */
  1314. X                return(0);                  /* adjust */
  1315. X            pat = star;                  /* Restart pattern after * */
  1316. X            *resp++ = *starend;              /* Copy character to result */
  1317. X            *resp = '\0';                  /* null terminate */
  1318. X            dat = ++starend;                  /* Rescan after copied char */
  1319. X        }
  1320. X    }
  1321. X}
  1322. X
  1323. X/*****************************************************************
  1324. X * Addendum: quit subroutine (print a message and exit)
  1325. X *****************************************************************/
  1326. X
  1327. Xquit(status,message)
  1328. Xint status;
  1329. Xchar *message;
  1330. X{
  1331. X    fprintf(stderr,message);
  1332. X    exit(status);
  1333. X}
  1334. X
  1335. X/*****************************************************************
  1336. X * Public Domain getopt routine
  1337. X *****************************************************************/
  1338. X
  1339. X/*
  1340. X * get option letter from argument vector
  1341. X */
  1342. Xint opterr = 1;        /* useless, never set or used */
  1343. Xint optind = 1;        /* index into parent argv vector */
  1344. Xint optopt;            /* character checked for validity */
  1345. Xchar *optarg;        /* argument associated with option */
  1346. X
  1347. X#define BADCH    (int)'?'
  1348. X#define EMSG    ""
  1349. X#define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  1350. X        fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  1351. X
  1352. Xgetopt(nargc,nargv,ostr)
  1353. Xint nargc;
  1354. Xchar **nargv,*ostr;
  1355. X{
  1356. X    static char *place = EMSG;    /* option letter processing */
  1357. X    register char *oli;        /* option letter list index */
  1358. X    char *strchr();
  1359. X
  1360. X    if(!*place)
  1361. X    {            /* update scanning pointer */
  1362. X        if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place)
  1363. X            return(EOF);
  1364. X        if(*place == '-')
  1365. X        {    /* found "--" */
  1366. X            ++optind;
  1367. X            return(EOF);
  1368. X        }
  1369. X    }                /* option letter okay? */
  1370. X    if((optopt = (int)*place++) == (int)':' || !(oli = strchr(ostr,optopt)))
  1371. X    {
  1372. X        if(!*place) ++optind;
  1373. X        tell(": illegal option -- ");
  1374. X    }
  1375. X    if(*++oli != ':')
  1376. X    {        /* don't need argument */
  1377. X        optarg = (char *)0;
  1378. X        if(!*place) ++optind;
  1379. X    }
  1380. X    else 
  1381. X    {                /* need an argument */
  1382. X        if(*place) optarg = place;    /* no white space */
  1383. X        else if(nargc <= ++optind)
  1384. X        {    /* no arg */
  1385. X            place = EMSG;
  1386. X            tell(": option requires an argument -- ");
  1387. X        }
  1388. X        else optarg = nargv[optind];    /* white space */
  1389. X        place = EMSG;
  1390. X        ++optind;
  1391. X    }
  1392. X    return(optopt);            /* dump back option letter */
  1393. X}
  1394. X/* vi: set tabstop=4 shiftwidth=4: */
  1395. SHAR_EOF
  1396. chmod 0644 unshar.c ||
  1397. echo 'restore of unshar.c failed'
  1398. Wc_c="`wc -c < 'unshar.c'`"
  1399. test 11348 -eq "$Wc_c" ||
  1400.     echo 'unshar.c: original size 11348, current size' "$Wc_c"
  1401. fi
  1402. true || echo 'restore of shar.c failed'
  1403. echo End of part 1, continue with part 2
  1404. exit 0
  1405. -- 
  1406.   ==========================================================================
  1407.   | Richard H. Gumpertz    rhg@CPS.COM    (913) 642-1777 or (816) 891-3561 |
  1408.   | Computer Problem Solving, 8905 Mohawk Lane, Leawood, Kansas 66206-1749 |
  1409.   ==========================================================================
  1410.