home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d07xx / d0764.lha / CLI-Tools / Blocks.c < prev    next >
C/C++ Source or Header  |  1992-11-21  |  3KB  |  119 lines

  1. /* Blocks
  2.  *
  3.  * by Thies Wellpott
  4.  *
  5.  * Usage: Blocks <file>|<bytes>
  6.  * calculates number of blocks used by file or bytes including file header
  7.  * and file list blocks
  8.  *
  9.  * compile (SAS C V5.10a):
  10.  *   LC -cisu -rr -v -O Blocks
  11.  * link:
  12.  *   BLink FROM LIB:c.o,Blocks.o TO Blocks SC SD ND LIB LIB:lcr.lib
  13.  *
  14.  *
  15.  * HISTORY
  16.  *
  17.  * V1.00  28.6.92
  18.  *   first version
  19.  * V1.10  29.6.92
  20.  *   feature added: you may give a number (of bytes) as argument
  21.  *
  22.  *
  23.  * TODO
  24.  *
  25.  * - accept device name and calculated number of blocks for that device
  26.  *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <proto/exec.h>
  33. #include <proto/dos.h>
  34. #include <exec/memory.h>
  35. #include <dos/dos.h>
  36.  
  37. #define VERSION "1.10"
  38. #define DATE "29.06.92"
  39.  
  40.  
  41. /* for DOS-command Version (OS 2.0) */
  42. char version[] = "$VER: Blocks V"VERSION" ("DATE")";
  43. BPTR lock = NULL;
  44. struct FileInfoBlock *fib = NULL;   /* must be longword-aligned!! */
  45.  
  46.  
  47. void close_all(char *s, int err)
  48. /* close/free all and exit */
  49. {
  50.    if (lock) UnLock(lock);
  51.    if (fib) FreeMem(fib, sizeof(struct FileInfoBlock));
  52.  
  53.    if (s) printf("%s!\n", s);
  54.    exit(err);
  55. }
  56.  
  57.  
  58. void usage(void)
  59. /* print usage and exit */
  60. {
  61.    printf("\2334m%s\2330m by Thies Wellpott\n\n", &version[6]);
  62.    printf("Usage: Blocks <file>|<bytes>\n");
  63.  
  64.    exit(5);
  65. }
  66.  
  67.  
  68. int get_blocks(long size, int bpb)
  69. /* returns number of blocks used by size bytes with bpb bytes per block */
  70. {
  71.    int data_blk, list_blk;
  72.  
  73.    data_blk = (size + bpb - 1) / bpb;  /* number of data blocks */
  74.    list_blk = (data_blk - 1) / 72;     /* number of file list blocks */
  75.  
  76.    return(1 + list_blk + data_blk);    /* file header + list + data blocks */
  77. }
  78.  
  79.  
  80. void main(int argc, char *argv[])
  81. {
  82.    int i, flag=1;
  83.    long size;
  84.  
  85.    if ((argc != 2) || (*argv[1] == '?'))
  86.       usage();
  87.  
  88.    size = strlen(argv[1]);
  89.    for (i = 0; i < size; i++)
  90.       if ((argv[1][i] < '0') || (argv[1][i] > '9'))
  91.       {
  92.      flag = 0;
  93.      break;
  94.       }
  95.  
  96.    if (flag)
  97.    {
  98.       size = atoi(argv[1]);
  99.       printf("%d byte(s) need\n", size);
  100.    }
  101.    else
  102.    {
  103.       if (!(fib = AllocMem(sizeof(struct FileInfoBlock), MEMF_CLEAR)))
  104.      close_all("Out of memory", 20);
  105.       if (!(lock = Lock(argv[1], ACCESS_READ)))
  106.      close_all("Can`t find file", 20);
  107.       if (!(Examine(lock, fib)))
  108.      close_all("Can`t examine file", 20);
  109.       size = fib->fib_Size;
  110.       printf("%s has %d byte(s) and needs\n", argv[1], size);
  111.    }
  112.  
  113.    printf("NFS (488 bytes per block): %d block(s)\n", get_blocks(size, 488));
  114.    printf("FFS (512 bytes per block): %d block(s)\n", get_blocks(size, 512));
  115.  
  116.    close_all(NULL, 0);
  117. }
  118.  
  119.