Listing 1

/*
&cstruse
cstr - Produce compilable C STRing from a text file.

Syntax:
      cstr file.str

Where:
  file.str
      is the name of a file containing text. The filename need not end
      in ".str", but it cannot end in ".c" (since the output would
      destroy the input).

Descriptionn:
      From a text file, cstr produces a C file containing a character
      array named after the input file and initialized with its text.
      If the first line of the text file is of the form:

      &name size

      it will be taken to indicate the name and the dimension(in bytes)
      of the character array. The "size" specification is optional.
*/
/*------------------------------------------------------------------*/
/* cstr - produce compilable C STRing from a text file.             */
/*                                                                  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE 512                 /* largest input line expected */
#define MAX_NAME 128                 /* largest file name expected  */

main(argc,argv)
char    **argv;
{
   int   i;
   FILE  *fin, *fout;
   char  foutname[MAX_NAME+1];
   char  sname[MAX_NAME+1];

   fprintf(stderr, "cstr 1.0\n");
   if(argc < 2)                     /* if not command-line args...  */
      usage();
   for(i = 1; i < argc; ++i) {      /* for each input file...       */
      if( !makeout(argv[i], foutname, sname)) {
         printf("%s: Input filename ('%s') must not end in '.c'\n",
                 argv[0],argv[i]);
         exit(EXIT_FAILURE);         /*  defined in <stdlib.h>     */
      }
      fin  = fopen(argv[i], "r");
      if(!fin) {
         printf{"%s: Can't open '%s" for input\n", argv[0],argv[i]);
         exit(EXIT_FAILURE);
      }
      fout = fopen(foutname, "w");
      if(!fout) {
         printf("%s: Can't open '%s' for output\n", argv[0],argv[i]);
         exit(EXIT_FAILURE);
      }
      cstr(fin, fout, sname);
      fclose(fin);
      fclose(fout);
   }
}
/*  usage - display correct usage and exit.  */

usage()
{
/*  extern char cstruse[]; */

/*  fputs(cstruse,stderr); */
   exit(EXIT_FAILURE)                 /* defined in <stdlib.h> */
}

/*  makeout - make output name from input name.  */

static
int     makeout(in, out, sname)
char    *in, *out, *sname;
{
   char c;
   char *t = "c";
   while((*sname++=*out++=c=*in++) != '\0')
      if(c == '.')
         if (!strcmp(*in, *t))
            return(0);
         else
            break;
   if{c != '.')  {         /* output has '.', even if input didn't. */
      *out++ = '.';
      *sname = '\0';
   }
   else
      *--sname = '\0';
   *out++ = 'c';
   *out++ = '\0';
   return(1);
}

/*  cstr - do the actual work...  */

static
int     cstr(fin, fout, sname)
FILE    *fin, *fout;
char    *sname;
{
   char    line[MAX_LINE+1], *maxdimp;
   int     maxdim = 0, c, i;
   
   if((c = fgetc(fin)) == '&')  {    /* if input specifies name and */
                               /*   dimension...              */
      fgets(line, MAX_LINE, fin);
      maxdimp = strpbrk(line, "\t\n");
      if(*maxdimp)
         *maxdimp++ = '\0';
      maxdim = atoi(maxdimp);
   }
   else {
      ungetc(c, fin);
      strcpy(line, sname);
   }
   fprintf(fout, "char %s[", line);
   if(maxdim)
      fprintf(fout, "%d", maxdim);
   fprintf(fout, "] =\n      {");
   
   for(i = 0; (c=fetc(fin)) !=EOF; ++i) {
      if(!(i%12))
         fprintf(fout,"\n ");
      if(c == '\n')
         fprintf(fout," '\\n',");
      else if(c == '\t')
         fprintf(fout," '\\t',");
      else if(c == '\\')
         fprintf(fout," '\\\\',");
      else
         fprintf(fout," '%c',",c);
   }
   if (!(i%12) {
      fprintf(fout, "\n  ");
      fprintf{fout,"   '\\0'\n  };\n");
   }
   else
      fprintf{fout,"   '\\0'\n  };\n");
   fprintf(stderr, "Did %d chars\n", i);
}
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/