home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / 3b1 / volume02 / ndf / part01 < prev    next >
Encoding:
Internet Message Format  |  1992-07-20  |  17.0 KB

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