#include <stdio.h>
#define progname "rot"

#define MAXSIZE 256
char x[MAXSIZE*MAXSIZE];
short asize ;    /* pic dimensions */
short header=1;/* set if pic file contains a header char */

FILE *fp, *fq = NULL;            /* input,output picture channels */
 
main(argc,argv)
char **argv;
{

  argv++;
  while ((argc>1)&&((*argv)[0] == '-'))
  {

    switch((*argv)[1]) {
    case 'x':
      sscanf(*argv+2,"%hd",&asize);
      if(asize<0) error("picture size");
      header=0;
      break;
    default:
      error("unknown flag");
      break;
    }
    --argc;argv++;
  }
 
  if (argc<3)
    error("missing arguments");
  else
  {
    if((fp = fopen(*argv++,"r")) == NULL)
      error("can't access input file");
    --argc;

    if((fq = fopen(*argv,"w")) == NULL)
      error("can't access output file");
  }
  read_pic();
  printf("pic read\n");
  fclose(fp);
  rotate();
  fclose(fq);
}
 
read_pic()
{
long i,area;
char *p;

  if(header) asize=(getc(fp))&0377;
  area=asize*asize;
  for(i=0,p=x;i<area;i++)
     *p++ = getc(fp);
}

rotate()
{
short c;
char *p;

  if(header) putc(asize,fq);

  for(c=0,p=x;c<asize;c++)
    write_col(p++);
}

write_col(p)
char *p;
{
short r;

  for(r=0;r<asize;r++,p+=asize)
    putc(*p,fq);
}

error(s)
char *s;
{
  fprintf(stderr,"%s: %s\n",progname,s);
  exit(1);
}
 
