home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sources / 3b1 / 79 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  17.2 KB

  1. Path: sparky!uunet!comp-sources-3b1
  2. From: dave@galaxia.network23.com (David H. Brierley)
  3. Newsgroups: comp.sources.3b1
  4. Subject: v02i026:  Berkeley style df command for the 3b1, Part01/01
  5. Date: 21 Jul 1992 22:28:18 -0400
  6. Organization: UUNET Communications
  7. Lines: 579
  8. Sender: dhb@ftp.UU.NET
  9. Approved: dave@galaxia.network23.com
  10. Message-ID: <14ih42INNd80@ftp.UU.NET>
  11. NNTP-Posting-Host: ftp.uu.net
  12. X-Checksum-Snefru: e11676cc 0dc24940 8bb79820 b9e61c38
  13.  
  14. Submitted-by: dave@galaxia.network23.com (David H. Brierley)
  15. Posting-number: Volume 2, Issue 26
  16. Archive-name: ndf/part01
  17.  
  18. This is the README file for the Dave Brierley collection of miscellaneous
  19. source programs.  The collection is posted in seven separate pieces but
  20. all of the source came from the "misc" directory on my machine.  As a result
  21. of this, there is only one README file and only one Makefile, although each
  22. of the seven postings will contain a copy of these two files.  I suggest
  23. that you obtain (or save) as many of these postings as you are interested
  24. in, unpack them all in a single directory, and then compile the sources.
  25. Note that some of the programs are actually shell scripts and therefore do
  26. not need compilation.
  27.  
  28. ndf.c
  29.  Almost emulates the BSD df command.  I say "almost" because I chose to
  30.  display the numbers in terms of blocks instead of kbytes since everything
  31.  else on the machine displays sizes in blocks.  Supports the "-i" option
  32.  to displays inode information.  Also supports specifying any random
  33.  directory as an argument and it will figure out what file system it is.
  34.  
  35. #! /bin/sh
  36. # This is a shell archive.  Remove anything before this line, then unpack
  37. # it by saving it into a file and typing "sh file".  To overwrite existing
  38. # files, type "sh file -c".  You can also feed this as standard input via
  39. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  40. # will see the following message at the end:
  41. #        "End of archive 1 (of 1)."
  42. # Contents:  MANIFEST Makefile README ndf.c
  43. # Wrapped by dave@galaxia on Tue Jul 21 10:38:47 1992
  44. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  45. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  46.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  47. else
  48. echo shar: Extracting \"'MANIFEST'\" \(220 characters\)
  49. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  50. X   File Name        Archive #    Description
  51. X-----------------------------------------------------------
  52. X MANIFEST                   1    
  53. X Makefile                   1    
  54. X README                     1    
  55. X ndf.c                      1    
  56. END_OF_FILE
  57. if test 220 -ne `wc -c <'MANIFEST'`; then
  58.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  59. fi
  60. # end of 'MANIFEST'
  61. fi
  62. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  63.   echo shar: Will not clobber existing file \"'Makefile'\"
  64. else
  65. echo shar: Extracting \"'Makefile'\" \(374 characters\)
  66. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  67. XCFLAGS    = -O
  68. X
  69. Xall:        bsdln disktest email ndf techo wclipper
  70. X
  71. Xbsdln:        bsdln.o
  72. X        $(CC) bsdln.o
  73. X        mv a.out bsdln
  74. X
  75. Xdisktest:    disktest.o
  76. X        $(CC) disktest.o
  77. X        mv a.out disktest
  78. X
  79. Xemail:        email.o
  80. X        $(CC) email.o
  81. X        mv a.out email
  82. X
  83. Xndf:        ndf.o
  84. X        $(CC) ndf.o
  85. X        mv a.out ndf
  86. X
  87. Xtecho:        techo.o
  88. X        $(CC) techo.o
  89. X        mv a.out techo
  90. X
  91. Xwclipper:    wclipper.o
  92. X        $(CC) wclipper.o
  93. X        mv a.out wclipper
  94. X
  95. END_OF_FILE
  96. if test 374 -ne `wc -c <'Makefile'`; then
  97.     echo shar: \"'Makefile'\" unpacked with wrong size!
  98. fi
  99. # end of 'Makefile'
  100. fi
  101. if test -f 'README' -a "${1}" != "-c" ; then 
  102.   echo shar: Will not clobber existing file \"'README'\"
  103. else
  104. echo shar: Extracting \"'README'\" \(4280 characters\)
  105. sed "s/^X//" >'README' <<'END_OF_FILE'
  106. XThis is the README file for the Dave Brierley collection of miscellaneous
  107. Xsource programs.  The collection is posted in seven separate pieces but
  108. Xall of the source came from the "misc" directory on my machine.  As a result
  109. Xof this, there is only one README file and only one Makefile, although each
  110. Xof the seven postings will contain a copy of these two files.  I suggest
  111. Xthat you obtain (or save) as many of these postings as you are interested
  112. Xin, unpack them all in a single directory, and then compile the sources.
  113. XNote that some of the programs are actually shell scripts and therefore do
  114. Xnot need compilation.
  115. X
  116. XINSTALLATION INSTRUCTIONS:
  117. X1. Compile all the source programs by typing "make all".  Edit the Makefile
  118. X   if you did not unpack all seven of the pieces.  Note that the Makefile
  119. X   does not contain explicit commands for using the shared library.  I always
  120. X   depend on "ccc" to do this for me.
  121. X2. Copy the resultant executables to your favorite bin directory.  I always
  122. X   use "/usr/local/bin".
  123. X3. Copy the various shell scripts to the bin directory and make then executable
  124. X   using the chmod command.  The shell scripts are all distributed with a
  125. X   suffix of ".sh".  I suggest you remove this suffix when you install it, but
  126. X   that is entirely up to you.
  127. X
  128. X
  129. XDescription of programs included in this package.  There is a line of dashes
  130. Xseparating each of the seven pieces.
  131. X
  132. Xbsdln.c
  133. X A version of the ln command that follows the BSD behaviour.  I.e. if the
  134. X target file exists, the command will fail.  The standard 3b1 version will
  135. X remove the target.
  136. X
  137. X--------------------------------------------------------------------------
  138. X
  139. Xinstall.sh
  140. X A shell script version of the BSD install command.
  141. X
  142. Xnull.sh
  143. X Zero out one or more files.
  144. X
  145. Xtolower.sh
  146. X Convert file names to lower case.  Requires ksh.
  147. X
  148. Xtoupper.sh
  149. X Convert file names to upper case.  Requires ksh.
  150. X
  151. X--------------------------------------------------------------------------
  152. X
  153. Xndf.c
  154. X Almost emulates the BSD df command.  I say "almost" because I chose to
  155. X display the numbers in terms of blocks instead of kbytes since everything
  156. X else on the machine displays sizes in blocks.  Supports the "-i" option
  157. X to displays inode information.  Also supports specifying any random
  158. X directory as an argument and it will figure out what file system it is.
  159. X
  160. X--------------------------------------------------------------------------
  161. X
  162. Xdisktest.c
  163. X A program used by the format script to test the floppy disk to be doubly
  164. X sure that it is usable.
  165. X
  166. Xformat.sh
  167. X Format a floppy disk.  Asks questions to determine desired parameters, such
  168. X as number of cylinders and number of sectors.  Will optionally run an
  169. X intensive surface test of the floppy (see disktest.c), build a file system
  170. X structure, and make the floppy bootable.  The main processing is done in a
  171. X loop so you can format multiple floppies easily.
  172. X
  173. Xnewfs.sh
  174. X Reads the VHB from the floppy and then builds a file system structure on it.
  175. X
  176. X--------------------------------------------------------------------------
  177. X
  178. Xtecho.c
  179. X A version of echo that allows direct access to termcap capabilities.  A lot
  180. X easier to use than intermixing calls to echo with calls to tput.
  181. X
  182. Xtecho.1
  183. X Hey, I actually wrote a man page for this one!
  184. X
  185. X--------------------------------------------------------------------------
  186. X
  187. Xwclipper.c
  188. X A program to read in the wtmp file and output the tail end of it.  Useful
  189. X if you want to maintain an N-day history of who has been using your system.
  190. X The output size can be specified in terms of days or kbytes.
  191. X
  192. Xwtmp.fix.sh
  193. X A shell script to control the operation of wclipper.
  194. X
  195. X--------------------------------------------------------------------------
  196. X
  197. Xemail.c
  198. X A version of the "email" program.  This program should be installed as
  199. X /usr/bin/email and it will be used by pcmgr (or by smgr) when you click
  200. X on the envelope icon.  It provides a safe interface to the mail program
  201. X by making sure the uid is set correctly and by doing a chdir to the
  202. X users home directory (both of which are already being done by pcmgr).
  203. X This version also provides a unique feature of reading an "rc" file
  204. X from the users home directory (~/.email.rc), which can be used to specify
  205. X what mail program to use.  See the sample provided.
  206. X
  207. Xemail.rc
  208. X Sample .email.rc file.
  209. END_OF_FILE
  210. if test 4280 -ne `wc -c <'README'`; then
  211.     echo shar: \"'README'\" unpacked with wrong size!
  212. fi
  213. # end of 'README'
  214. fi
  215. if test -f 'ndf.c' -a "${1}" != "-c" ; then 
  216.   echo shar: Will not clobber existing file \"'ndf.c'\"
  217. else
  218. echo shar: Extracting \"'ndf.c'\" \(8245 characters\)
  219. sed "s/^X//" >'ndf.c' <<'END_OF_FILE'
  220. X/***************************************************************************
  221. X *
  222. X * Program: Berkeley style df program
  223. X *
  224. X * Author: David H. Brierley, October 1987
  225. X *       Galaxia Systems Ltd.
  226. X *       {cbosgd,gatech,necntc,uiucdcs}!rayssd!galaxia!dave
  227. X *
  228. X * Function:
  229. X *    Provides a Berkeley style df command for non-Berkeley systems.
  230. X *    Standard output includes the total size of the file system, the
  231. X *    number of used and available block, and the percent used.  The
  232. X *    -i option requests similar information for the inode table.
  233. X *    Additional arguments may be supplied which specify which file
  234. X *    system(s) is to be displayed.  The name may be specified either
  235. X *    as a device name or a directory name within the mounted file
  236. X *    system.
  237. X *
  238. X * Note: This program was written for the ATT Unix PC (7300).  Since
  239. X *    it is relatively straight forward it should be easy to port
  240. X *    to other systems if you want to.  Send me mail if you do port
  241. X *    it so that I can include the changes into my official source.
  242. X *
  243. X * Note: This program does not contain any Berkeley code (or any other
  244. X *    licensed code for that matter).  The only reason I call the
  245. X *    program "a Berkeley style df" is that the output is similar
  246. X *    to the output produced by the df command from Berkeley.
  247. X *
  248. X * Copyright notice:
  249. X *    Copyright 1987 David H. Brierley
  250. X *    Permission is granted to freely copy, modify, and use this software
  251. X *    in any way that you see fit, subject to the following restrictions:
  252. X *    1. This software may not be used for commercial gain without the
  253. X *       express written permission of the author.
  254. X *    2. The author assumes no liability for any consequences arising
  255. X *       from the use of this software.
  256. X *    3. Derivative works must be clearly labelled as such and must not
  257. X *       be misrepresented as the original software.
  258. X *    4. This notice must be retained in all copies and derivative works.
  259. X *
  260. X ***************************************************************************/
  261. X
  262. X#ifndef    lint
  263. Xstatic char *SccsId    = "@(#) SCCS/s.df.c: version 1.2 8/15/90";
  264. X#endif
  265. X
  266. X#include <stdio.h>
  267. X#include <string.h>
  268. X#include <sys/types.h>
  269. X#include <sys/stat.h>
  270. X#include <mnttab.h>
  271. X#include <sys/filsys.h>
  272. X#include <sys/param.h>
  273. X#include <sys/ino.h>
  274. X
  275. X#define MNTTAB        "/etc/mnttab"
  276. X#define INOPERBLK    (512 / sizeof (struct dinode))
  277. X
  278. X/*
  279. X * table for holding info about mounted file systems
  280. X */
  281. Xstruct mtable {
  282. X    char            m_device[16];
  283. X    char            m_filsys[16];
  284. X    dev_t           m_dev;
  285. X};
  286. X
  287. X/* declare external routines */
  288. Xextern void     exit ();
  289. Xextern void     perror ();
  290. Xextern long     lseek ();
  291. X
  292. Xmain (argc, argv)
  293. Xint             argc;
  294. Xchar           *argv[];
  295. X{
  296. X    struct mtable   m_info[NMOUNT];
  297. X    int             m_size;
  298. X    int             optch;
  299. X    int             iflag;
  300. X    int             n;
  301. X    extern int      optind;
  302. X
  303. X    iflag = 0;
  304. X    while ((optch = getopt (argc, argv, "i")) != EOF) {
  305. X    switch (optch) {
  306. X    case 'i':
  307. X        iflag = 1;
  308. X        break;
  309. X    default:
  310. X        (void) printf ("df: unknown option '%c'\n", optch);
  311. X        exit (1);
  312. X    }
  313. X    }
  314. X
  315. X    /*
  316. X     * read the mount table
  317. X     */
  318. X    m_size = read_mtab (m_info);
  319. X
  320. X    (void) printf ("DeviceName   Blocks   used  avail  %%used");
  321. X    if (iflag) {
  322. X    (void) printf ("  iused  ifree %%iused");
  323. X    }
  324. X    (void) printf (" Mounted on\n");
  325. X
  326. X    /*
  327. X     * if no arguments, print the mount table
  328. X     */
  329. X    if (optind == argc) {
  330. X    for (n = 0; n < m_size; n++) {
  331. X        display (&m_info[n], iflag);
  332. X    }
  333. X    exit (0);
  334. X    }
  335. X
  336. X    /*
  337. X     * process the specified file systems
  338. X     */
  339. X    for (; optind < argc; optind++) {
  340. X    fscheck (argv[optind], m_info, m_size, iflag);
  341. X    }
  342. X
  343. X    return (0);
  344. X
  345. X}
  346. X
  347. X/*
  348. X * Routine: read_mtab
  349. X *
  350. X * Read the mount table and extract info about each file
  351. X * system.  The file system is also "stat"ed to extract
  352. X * the device number.
  353. X *
  354. X */
  355. X
  356. Xread_mtab (info)
  357. Xstruct mtable   info[];
  358. X{
  359. X    int             n;
  360. X    int             fd;
  361. X    struct mnttab   mtab;
  362. X    struct stat     sbuf;
  363. X
  364. X#define    L_DEV        (sizeof(mtab.mt_dev))
  365. X#define L_FILSYS    (sizeof(mtab.mt_filsys))
  366. X
  367. X    if ((fd = open (MNTTAB, 0)) == -1) {
  368. X    perror ("df");
  369. X    (void) printf ("Unable to open file %s\n", MNTTAB);
  370. X    exit (1);
  371. X    }
  372. X
  373. X    n = 0;
  374. X    while (read (fd, &mtab, sizeof (mtab)) == sizeof (mtab)) {
  375. X    (void) strcpy (info[n].m_device, "/dev/");
  376. X    (void) strncat (info[n].m_device, mtab.mt_dev, L_DEV);
  377. X    info[n].m_device[L_DEV] = '\0';
  378. X    (void) strncpy (info[n].m_filsys, mtab.mt_filsys, L_FILSYS);
  379. X    info[n].m_filsys[L_FILSYS] = '\0';
  380. X    if (stat (info[n].m_device, &sbuf) == -1) {
  381. X        continue;
  382. X    }
  383. X    info[n].m_dev = sbuf.st_rdev;
  384. X    n++;
  385. X    }
  386. X
  387. X    (void) close (fd);
  388. X    return (n);
  389. X
  390. X}
  391. X
  392. X/*
  393. X * Routine: display
  394. X *
  395. X * Display information about the specified file system.
  396. X *
  397. X */
  398. X
  399. Xdisplay (mtbl, iflag)
  400. Xstruct mtable  *mtbl;
  401. Xint             iflag;
  402. X{
  403. X    int             tot;
  404. X    int             used;
  405. X    int             avail;
  406. X    double          pct;
  407. X    struct filsys   fsys;
  408. X    int             fd;
  409. X    int             type;
  410. X
  411. X    /* sync 3 times on the console if you want me,    */
  412. X    /* twice on the pipe if the answer is no.        */
  413. X    sync ();
  414. X    sync ();
  415. X
  416. X    if ((fd = open (mtbl -> m_device)) == -1) {
  417. X    perror ("df");
  418. X    (void) printf ("%s: unable to open device\n", mtbl -> m_device);
  419. X    return;
  420. X    }
  421. X
  422. X    /*
  423. X     * Read and validate the super-block
  424. X     */
  425. X    if (lseek (fd, 512L, 0) == -1L) {
  426. X    perror ("df");
  427. X    (void) printf ("%s: unable to seek to super-block\n", mtbl -> m_device);
  428. X    (void) close (fd);
  429. X    return;
  430. X    }
  431. X
  432. X    if (read (fd, &fsys, sizeof (fsys)) != sizeof (fsys)) {
  433. X    perror ("df");
  434. X    (void) printf ("%s: i/o error reading super-block\n", mtbl -> m_device);
  435. X    (void) close (fd);
  436. X    return;
  437. X    }
  438. X    (void) close (fd);
  439. X
  440. X    if (sbcheck (&fsys) == -1) {
  441. X    (void) printf ("%s: invalid super-block\n", mtbl -> m_device);
  442. X    return;
  443. X    }
  444. X
  445. X    type = 1;
  446. X    if (fsys.s_magic == FsMAGIC) {
  447. X    type = fsys.s_type;
  448. X    }
  449. X
  450. X    /*
  451. X     * calculate the numbers and print them out
  452. X     */
  453. X    tot = fsys.s_fsize * type;
  454. X    avail = fsys.s_tfree * type;
  455. X    used = tot - avail;
  456. X    if (tot > 0) {
  457. X    pct = ((double) used / (double) tot) * 100.0;
  458. X    }
  459. X    (void) printf ("%-12s", mtbl -> m_device);
  460. X    (void) printf ("%7d%7d%7d%6.0f%%", tot, used, avail, pct);
  461. X    if (iflag) {
  462. X    tot = ((fsys.s_isize - 2) * type) * INOPERBLK;
  463. X    avail = fsys.s_tinode;
  464. X    used = tot - avail;
  465. X    if (tot > 0) {
  466. X        pct = ((double) used / (double) tot) * 100.0;
  467. X    }
  468. X    (void) printf ("%7d%7d%6.0f%%", used, avail, pct);
  469. X    }
  470. X    (void) printf (" %s\n", mtbl -> m_filsys);
  471. X
  472. X}
  473. X
  474. X/*
  475. X * Routine: sbcheck
  476. X *
  477. X * Perform various sanity checks on the fields in the super block.
  478. X * This is to prevent things like reading a non-filesystem format
  479. X * floppy.
  480. X *
  481. X */
  482. X
  483. Xsbcheck (fsys)
  484. Xstruct filsys  *fsys;
  485. X{
  486. X
  487. X    if (fsys -> s_isize & 0x8000) {
  488. X    return (-1);
  489. X    }
  490. X    if (fsys -> s_fsize < 0) {
  491. X    return (-1);
  492. X    }
  493. X    if (fsys -> s_isize >= fsys -> s_fsize) {
  494. X    return (-1);
  495. X    }
  496. X    if (fsys -> s_nfree > NICFREE) {
  497. X    return (-1);
  498. X    }
  499. X    if (fsys -> s_ninode > NICINOD) {
  500. X    return (-1);
  501. X    }
  502. X    if (fsys -> s_tfree >= fsys -> s_fsize) {
  503. X    return (-1);
  504. X    }
  505. X    if (fsys -> s_tfree < 0) {
  506. X    return (-1);
  507. X    }
  508. X    if (fsys -> s_tinode >= fsys -> s_fsize) {
  509. X    return (-1);
  510. X    }
  511. X
  512. X    return (0);
  513. X
  514. X}
  515. X
  516. X/*
  517. X * Routine: fscheck
  518. X *
  519. X * Check to see if the specified path name is part of a mounted
  520. X * filesystem or is a special device file.  If so, print out the
  521. X * info.  If not, print out an error message.
  522. X *
  523. X */
  524. X
  525. Xfscheck (DirName, m_info, m_size, iflag)
  526. Xchar           *DirName;
  527. Xstruct mtable   m_info[];
  528. Xint             m_size;
  529. Xint             iflag;
  530. X{
  531. X    struct stat     sbuf;
  532. X    struct mtable   fake;
  533. X    int             n;
  534. X
  535. X    if (stat (DirName, &sbuf) == -1) {
  536. X    perror ("df");
  537. X    (void) printf ("%s: unable to access file\n", DirName);
  538. X    return;
  539. X    }
  540. X
  541. X    if ((sbuf.st_mode & S_IFMT) == S_IFBLK) {
  542. X    (void) strcpy (fake.m_device, DirName);
  543. X    (void) strcpy (fake.m_filsys, "");
  544. X    display (&fake, iflag);
  545. X    return;
  546. X    }
  547. X
  548. X    if ((sbuf.st_mode & S_IFMT) == S_IFCHR) {
  549. X    (void) strcpy (fake.m_device, DirName);
  550. X    (void) strcpy (fake.m_filsys, "");
  551. X    display (&fake, iflag);
  552. X    return;
  553. X    }
  554. X
  555. X    for (n = 0; n < m_size; n++) {
  556. X    if (sbuf.st_dev == m_info[n].m_dev) {
  557. X        display (&m_info[n], iflag);
  558. X        return;
  559. X    }
  560. X    }
  561. X
  562. X    (void) printf ("%s: mounted on unknown device\n", DirName);
  563. X
  564. X}
  565. END_OF_FILE
  566. if test 8245 -ne `wc -c <'ndf.c'`; then
  567.     echo shar: \"'ndf.c'\" unpacked with wrong size!
  568. fi
  569. # end of 'ndf.c'
  570. fi
  571. echo shar: End of archive 1 \(of 1\).
  572. cp /dev/null ark1isdone
  573. MISSING=""
  574. for I in 1 ; do
  575.     if test ! -f ark${I}isdone ; then
  576.     MISSING="${MISSING} ${I}"
  577.     fi
  578. done
  579. if test "${MISSING}" = "" ; then
  580.     echo You have the archive.
  581.     rm -f ark[1-9]isdone
  582. else
  583.     echo You still need to unpack the following archives:
  584.     echo "        " ${MISSING}
  585. fi
  586. ##  End of shell archive.
  587. exit 0
  588. -- 
  589. David H. Brierley
  590. Home: dave@galaxia.network23.com; Work: dhb@quahog.ssd.ray.com
  591. Send comp.sources.3b1 submissions to comp-sources-3b1@galaxia.network23.com
  592. %% Can I be excused, my brain is full. **
  593.