home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / vol < prev    next >
Encoding:
Text File  |  1989-07-27  |  2.8 KB  |  141 lines

  1. Newsgroups: comp.sources.misc
  2. organization: Hewlett-Packard Laboratories, Bristol, UK.
  3. subject: v07i102: which volume am i on?
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Reply-To: jjb@otter.UUCP (John Burns)
  6.  
  7. Posting-number: Volume 7, Issue 102
  8. Submitted-by: jjb@otter.UUCP (John Burns)
  9. Archive-name: vol
  10.  
  11. [For System V.  Maybe this should become part of C news for those of us who
  12.  don't have the most ultra-modern systems?  ++bsa]
  13.  
  14. #! /bin/sh
  15. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  16. # Contents:  vol.c
  17. echo extracting 'vol.c'
  18. if test -f 'vol.c' -a -z "$1"; then echo Not overwriting 'vol.c'; else
  19. sed 's/^X//' << \EOF > 'vol.c'
  20. X/*
  21. X * This is a little program that prints out the volume on which
  22. X * the specified file resides.
  23. X */
  24. X
  25. Xstatic char RCSid[] = "$Header: vol.c,v 1.1 89/07/20 11:50:03 anon Exp $";
  26. X/*
  27. X * $Log:    vol.c,v $
  28. X * Revision 1.1  89/07/20  11:50:03  11:50:03  jjb ()
  29. X * Initial revision
  30. X * 
  31. X
  32. X * vol will print out the name of the file system on which the
  33. X * current or named directory exists.  It will do the same for all
  34. X * the parents of those directories also.
  35. X
  36. X*/
  37. X#include <stdio.h>
  38. X#include <sys/types.h>
  39. X#include <sys/stat.h>
  40. X#include <string.h>
  41. X
  42. X
  43. X
  44. Xstatic struct mnt_entry{
  45. X    char name[64];
  46. X    dev_t devno;
  47. X}mount_table[32];
  48. Xstatic int mtp = 0;
  49. X
  50. Xextern char *getcwd(/*char *,int*/);
  51. Xextern int   stat (/*char *, struct stat * */);
  52. X
  53. Xmain(argc,argv)
  54. Xchar *argv[];
  55. X{
  56. X    char name[64];
  57. X    /*
  58. X     * pick up /etc/mnttab and
  59. X     *  store data locally
  60. X     */
  61. X    build_mount_table();
  62. X
  63. X    if(argc == 1){
  64. X        process(getcwd(name,sizeof(name)));
  65. X    }
  66. X    else
  67. X        while(--argc){
  68. X
  69. X            process(*++argv);
  70. X        }
  71. X}
  72. Xstatic process(name)
  73. X{
  74. X    char cwd[4096];
  75. X    struct stat buf;
  76. X    char *match();
  77. X
  78. X
  79. X    /*
  80. X     * get the device number from
  81. X     * the stat call as a way of identifying
  82. X     * the volume
  83. X     */
  84. X
  85. X    if( stat(name,&buf) == -1 ){
  86. X        perror(cwd);
  87. X    }
  88. X    else {
  89. X        fprintf(stderr,"%s is on %s\n",name,match(buf.st_dev));
  90. X    }
  91. X
  92. X}
  93. Xbuild_mount_table()
  94. X{
  95. X    char line[2048];
  96. X    struct stat buf;
  97. X
  98. X
  99. X    FILE *mount;
  100. X
  101. X    mount = fopen("/etc/mnttab","r");
  102. X    if(mount == NULL){
  103. X        perror("/etc/mnttab");
  104. X        return;
  105. X    }
  106. X
  107. X    while(fgets(line,sizeof(line)-1,mount)){
  108. X
  109. X        register struct mnt_entry *p = &(mount_table[mtp++]);
  110. X        char filsys[64];
  111. X
  112. X        sscanf(line,"%s %s",p->name,filsys);
  113. X
  114. X        strcat(filsys,"/.");
  115. X        if(stat(filsys,&buf) == -1){
  116. X            perror(filsys) ;
  117. X            p->devno = 0 ;
  118. X            continue;
  119. X        }
  120. X        p->devno = buf.st_dev;
  121. X    }
  122. X}
  123. Xchar * match(id)
  124. Xdev_t id;
  125. X{
  126. X    int i;
  127. X    static char q[32];
  128. X
  129. X    for(i = 0 ; i < mtp ; i++){
  130. X        if(id == mount_table[i].devno)return(mount_table[i].name);
  131. X    }
  132. X
  133. X    sprintf(q,"%X",id);
  134. X    return q;
  135. X}
  136. EOF
  137. chars=`wc -c < 'vol.c'`
  138. if test $chars !=     1909; then echo 'vol.c' is $chars characters, should be     1909 characters!; fi
  139. fi
  140. exit 0
  141.