/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stat.h>

#define FNM "z.z"
#define N 3

static void stat_size(const char *fnm)
{
   struct stat buf;
   stat(fnm, &buf);
   printf("stat size = %d bytes\n", (int)buf.st_size);
}

static void gen_file(const char *fnm, const char *rfm, const char *xtra, int n)
{
   FILE *fp;
   int i, j;
   remove(fnm);
   if(xtra != NULL)
      fp = fopen(fnm, "w", rfm, xtra);
   else
      fp = fopen(fnm, "w", rfm);
   for(i = 1; i <= n; i++)
   {
      for(j = 1; j <= i; j++)
      {
         fputc('0' + i, fp);
      }
      fputc('\n', fp);
   }
   fclose(fp);
}

static void count(const char *fnm, const char *mode, const char *ctx)
{
   FILE *fp;
   int n;
   fp = fopen(fnm, mode, ctx);
   n = 0;
   while(fgetc(fp) != EOF)
   {
      n++;
   }
   fclose(fp);
   printf("read mode=%s %s : %d bytes\n", mode, ctx, n);
}

static void test(const char *rfm, const char *xtra)
{
   if(xtra != NULL)
      printf("%s %s\n", rfm, xtra);
   else
      printf("%s\n", rfm);
   gen_file(FNM, rfm, xtra, N);
   stat_size(FNM);
   count(FNM, "r", "ctx=rec");
   count(FNM, "r", "ctx=stm");
   count(FNM, "rb", "ctx=rec");
   count(FNM, "rb", "ctx=stm");
}

int main(int argc,char *argv[])
{
   test("rfm=var", NULL);
   test("rfm=stmlf", NULL);
   test("rfm=stm", NULL);
   test("rfm=fix", "mrs=3");
   test("rfm=fix", "mrs=4");
   return 0;
}
