[comp.sources.misc] v07i102: which volume am i on?

allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (07/28/89)

Posting-number: Volume 7, Issue 102
Submitted-by: jjb@otter.UUCP (John Burns)
Archive-name: vol

[For System V.  Maybe this should become part of C news for those of us who
 don't have the most ultra-modern systems?  ++bsa]

#! /bin/sh
# This file was wrapped with "dummyshar".  "sh" this file to extract.
# Contents:  vol.c
echo extracting 'vol.c'
if test -f 'vol.c' -a -z "$1"; then echo Not overwriting 'vol.c'; else
sed 's/^X//' << \EOF > 'vol.c'
X/*
X * This is a little program that prints out the volume on which
X * the specified file resides.
X */
X
Xstatic char RCSid[] = "$Header: vol.c,v 1.1 89/07/20 11:50:03 anon Exp $";
X/*
X * $Log:	vol.c,v $
X * Revision 1.1  89/07/20  11:50:03  11:50:03  jjb ()
X * Initial revision
X * 
X
X * vol will print out the name of the file system on which the
X * current or named directory exists.  It will do the same for all
X * the parents of those directories also.
X
X*/
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/stat.h>
X#include <string.h>
X
X
X
Xstatic struct mnt_entry{
X	char name[64];
X	dev_t devno;
X}mount_table[32];
Xstatic int mtp = 0;
X
Xextern char *getcwd(/*char *,int*/);
Xextern int   stat (/*char *, struct stat * */);
X
Xmain(argc,argv)
Xchar *argv[];
X{
X	char name[64];
X	/*
X	 * pick up /etc/mnttab and
X	 *  store data locally
X	 */
X	build_mount_table();
X
X	if(argc == 1){
X		process(getcwd(name,sizeof(name)));
X	}
X	else
X		while(--argc){
X
X			process(*++argv);
X		}
X}
Xstatic process(name)
X{
X	char cwd[4096];
X	struct stat buf;
X	char *match();
X
X
X	/*
X	 * get the device number from
X	 * the stat call as a way of identifying
X	 * the volume
X	 */
X
X	if( stat(name,&buf) == -1 ){
X		perror(cwd);
X	}
X	else {
X		fprintf(stderr,"%s is on %s\n",name,match(buf.st_dev));
X	}
X
X}
Xbuild_mount_table()
X{
X	char line[2048];
X	struct stat buf;
X
X
X	FILE *mount;
X
X	mount = fopen("/etc/mnttab","r");
X	if(mount == NULL){
X		perror("/etc/mnttab");
X		return;
X	}
X
X	while(fgets(line,sizeof(line)-1,mount)){
X
X		register struct mnt_entry *p = &(mount_table[mtp++]);
X		char filsys[64];
X
X		sscanf(line,"%s %s",p->name,filsys);
X
X		strcat(filsys,"/.");
X		if(stat(filsys,&buf) == -1){
X			perror(filsys) ;
X			p->devno = 0 ;
X			continue;
X		}
X		p->devno = buf.st_dev;
X	}
X}
Xchar * match(id)
Xdev_t id;
X{
X	int i;
X	static char q[32];
X
X	for(i = 0 ; i < mtp ; i++){
X		if(id == mount_table[i].devno)return(mount_table[i].name);
X	}
X
X	sprintf(q,"%X",id);
X	return q;
X}
EOF
chars=`wc -c < 'vol.c'`
if test $chars !=     1909; then echo 'vol.c' is $chars characters, should be     1909 characters!; fi
fi
exit 0