home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume7 / frag < prev    next >
Text File  |  1989-07-08  |  3KB  |  101 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v07i061: Fragmentation Check
  4. Message-Id: <729@mitisft.Convergent.COM>
  5. Distribution: usa
  6. Organization: Convergent Technologies, San Jose, CA
  7. Reply-To: dold@mitisft.convergent.com (Clarence Dold)
  8.  
  9. Posting-number: Volume 7, Issue 61
  10. Submitted-by: dold@mitisft.convergent.com (Clarence Dold)
  11. Archive-name: frag
  12.  
  13. This turned out to be so short, I should have done it quite some time ago.
  14. A really short, non-error checking tool that accepts one file name,
  15. finds it's inode and invokes a pipe from bcheck to scan the assigned block
  16. numbers.
  17. These block numbers are examined to make sure they are contiguous, else it
  18. prints some info.
  19.  
  20. Do I really need a makefile?
  21. all:    frag
  22.  
  23. frag: frag.o
  24.  
  25. frag.o:
  26.     cc -o frag frag.c
  27.  
  28. ---------------------------------------snip----------------------
  29. /* program to check for fragmentation of one file.
  30.  * Arbitrarily chose 3 block gap as acceptable, to allow for
  31.  * necessary inode gaps in large files.
  32.  * Super User Usage: frag filename
  33.  * CADold - dold@Convergent.COM - Fri Jun  9 19:02:38 PDT 1989
  34.  */
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/gdisk.h>
  39.  
  40. #ifndef AUTHOR
  41. #define AUTHOR
  42. static char *Author=" dold@Convergent.COM";
  43. #endif        /* If there is no author specified, stick my name in the code */
  44.  
  45. char cmd[80] ="/usr/local/bin/bcheck -i ";
  46. main(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50. struct stat statbuf;
  51. int stat(), cont, drive, slice;
  52. FILE *pipe;
  53. char line[BUFSIZ];
  54. long this, last, contig;
  55. long jumps=0L, blocks=0L, restart=0L;
  56.  
  57.  
  58. if (argc != 2){
  59.     printf("Usage: %s filename\n", argv[0]);
  60.     exit(1);
  61. }
  62.  
  63. stat (argv[1], &statbuf);
  64. slice = statbuf.st_dev % GDMAXSLICE;
  65. drive = ((statbuf.st_dev % (GDMAXSLICE * DRIVES)) - slice)/GDMAXSLICE;
  66. cont = statbuf.st_dev / (GDMAXSLICE * DRIVES);
  67.  
  68. sprintf(line, "%d /dev/rdsk/c%dd%ds%d", statbuf.st_ino, cont, drive, slice);
  69. strcat(cmd, line);
  70. if ((pipe = popen(cmd, "r")) != NULL){
  71.     fgets(line,BUFSIZ-1,pipe);
  72.     last = atol(line);
  73.     blocks++;
  74.  
  75.     while (fgets(line,BUFSIZ-1,pipe) != NULL ) {
  76.         this = atol(line);
  77.         if  ( (this -3 > last) || (this < last)  ){
  78.             jumps++;
  79.             contig = blocks - restart;
  80.             restart = blocks;
  81.             printf("Block jump from %ld to %ld, after %ld contiguous blocks\n",
  82.                 last, this, contig);
  83.         }
  84.         blocks++;
  85.         last = this;
  86.     }
  87. } else {
  88.     exit(1);
  89. }
  90. pclose(pipe);
  91. printf("%ld jumps in %ld blocks\n", jumps, blocks);
  92. return(0);
  93. }
  94.  
  95. ---------------------------------------snip----------------------
  96. -- 
  97. ---
  98. Clarence A Dold - dold@tsmiti.Convergent.COM        (408) 434-5293
  99.         ...pyramid!ctnews!tsmiti!dold
  100.         P.O.Box 6685, San Jose, CA 95150-6685    MS#10-007
  101.