home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / tools / filetodata.c next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  5.8 KB  |  219 lines

  1. /*
  2. ** filetodata.c -- This program converts a file to an object file containing
  3. **           the file contents as data.
  4. **           The object file contains 2 variables:
  5. **              1) The contents of the file, labeled as <name>data
  6. **              2) A variable indicating the size of the contents,
  7. **             labeled as <name>size.
  8. **
  9. **           The 1st argument is the file to convert.
  10. **           The 2nd argument is the string to substitute for <name>
  11. **             in the above label definitions.
  12. **           The 3rd argument (optional) is the output object file
  13. **             name.  It defaults to filedata.o
  14. **
  15. ** Copyright 1993 by Hamish Macdonald.
  16. **
  17. ** This file is subject to the terms and conditions of the GNU General Public
  18. ** License.  See the file README.legal in the main directory of this archive
  19. ** for more details.
  20. **
  21. */
  22.  
  23. #include <stddef.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <sys/types.h>
  28. #include <sys/file.h>
  29. #include <sys/stat.h>
  30. #include "linux/a.out.h"
  31. #include <unistd.h>
  32.  
  33. #ifndef EXIT_FAILURE
  34. #define EXIT_FAILURE 1
  35. #endif
  36. #ifndef EXIT_SUCCESS
  37. #define EXIT_SUCCESS 0
  38. #endif
  39.  
  40. void usage(void)
  41. {
  42.     fprintf (stderr, "Usage:\n"
  43.          "\tfiletodata file name [outputfilename]\n");
  44.     exit (EXIT_FAILURE);
  45. }
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.     int fd, objfd;
  50.     struct exec outex;
  51.     char *filename;
  52.     char *objname = "filedata.o";
  53.     char *labname;
  54.     unsigned long datasize, filesize;
  55.     unsigned char *buf;
  56.     struct nlist sym[2];
  57.     unsigned long strsize;
  58.     struct stat s;
  59.  
  60.     /* print the greet message */
  61.     puts("Amiga Linux filetodata version 1.2");
  62.     puts("Copyright 1993 by Hamish Macdonald\n");
  63.  
  64.     if (argc < 3 || argc > 4) {
  65.     usage();
  66.     exit (EXIT_FAILURE);
  67.     }
  68.  
  69.     filename = argv[1];
  70.     labname = argv[2];
  71.     if (argc > 3)
  72.     objname = argv[3];
  73.  
  74.     /* open the input file and determine its size */
  75.     if ((fd = open (filename, O_RDONLY)) == -1) {
  76.     fprintf (stderr, "Unable to open input file %s\n", filename);
  77.     exit (EXIT_FAILURE);
  78.     }
  79.  
  80.  
  81.     if (fstat(fd, &s) == -1) {
  82.     fprintf (stderr, "stat on input file %s failed\n", filename);
  83.     exit (EXIT_FAILURE);
  84.     }
  85.  
  86.     /* record size file (rounded up to longword boundary) */
  87.     filesize = s.st_size;
  88.     datasize = (filesize + sizeof (unsigned long) - 1)
  89.         & ~(sizeof(unsigned long) -1);
  90.  
  91.     /* setup exec header for object file */
  92.     N_SET_MAGIC (outex, OMAGIC);
  93.     N_SET_MACHTYPE (outex, M_68020);
  94.     N_SET_FLAGS (outex, 0);
  95.     outex.a_text   = 0;
  96.     outex.a_data   = datasize + sizeof (unsigned long);
  97.     outex.a_bss    = 0;
  98.     outex.a_syms   = 2 * sizeof (struct nlist);
  99.     outex.a_entry  = 0;
  100.     outex.a_trsize = 0;
  101.     outex.a_drsize = 0;
  102.  
  103.     /* open the output file */
  104.     if ((objfd = open (objname, O_WRONLY | O_CREAT | O_TRUNC)) == -1) {
  105.     fprintf (stderr, "Unable to open objectfile %s\n", objname);
  106.     exit (EXIT_FAILURE);
  107.     }
  108.  
  109.     /* write exec header to object file */
  110.     if (write (objfd, (void *)&outex, sizeof(outex)) != sizeof(outex)) {
  111.     fprintf (stderr, "Unable to write exec header to %s\n",
  112.          objname);
  113.     exit (EXIT_FAILURE);
  114.     }
  115.  
  116.     /* allocate buffer to hold text and data */
  117.     if (!(buf = calloc (datasize, sizeof (char)))) {
  118.     fprintf (stderr, "Failed to allocate memory for buffer\n");
  119.     exit (EXIT_FAILURE);
  120.     }
  121.  
  122.     /* read contents of input file */
  123.     if (read (fd, buf, filesize) != filesize) {
  124.     fprintf (stderr, "Failed to read contents of input file\n");
  125.     free (buf);
  126.     exit (EXIT_FAILURE);
  127.     }
  128.  
  129.     /* close the input file */
  130.     close (fd);
  131.  
  132.     /* seek to data offset in object file */
  133.     if (lseek (objfd, N_DATOFF(outex), L_SET) == -1) {
  134.     fprintf (stderr, "Failed to seek to data in object file\n");
  135.     free (buf);
  136.     exit (EXIT_FAILURE);
  137.     }
  138.  
  139.     /* write the contents */
  140.     if (write (objfd, buf, datasize) != datasize) {
  141.     fprintf (stderr, "Failed to write data to object file\n");
  142.     free (buf);
  143.     exit (EXIT_FAILURE);
  144.     }
  145.  
  146.     /* write the filesize variable value */
  147.     if (write (objfd, &filesize, sizeof(filesize)) != sizeof(filesize)) {
  148.     fprintf (stderr, "Failed to write filesize to object file\n");
  149.     free (buf);
  150.     exit (EXIT_FAILURE);
  151.     }
  152.     /*
  153.      * setup symbol table
  154.      */
  155.  
  156.     /* sym entry for initdata */
  157.     sym[0].n_un.n_strx = sizeof(strsize);
  158.     sym[0].n_type      = N_DATA | N_EXT;
  159.     sym[0].n_other     = 0;
  160.     sym[0].n_desc      = 0;
  161.     sym[0].n_value     = 0;
  162.  
  163.     /* sym entry for initsize */
  164.     sym[1].n_un.n_strx = sizeof(strsize) + strlen(labname) + 6;
  165.     sym[1].n_type      = N_DATA | N_EXT;
  166.     sym[1].n_other     = 0;
  167.     sym[1].n_desc      = 0;
  168.     sym[1].n_value     = datasize;
  169.  
  170.     /* seek to symbol offset in object file */
  171.     if (lseek (objfd, N_SYMOFF(outex), L_SET) == -1) {
  172.     fprintf (stderr, "Failed to seek to symbols in object file\n");
  173.     free (buf);
  174.     exit (EXIT_FAILURE);
  175.     }
  176.  
  177.     /* write symbols */
  178.     if (write (objfd, sym, sizeof(sym)) != sizeof(sym)) {
  179.     fprintf (stderr, "Failed to write symbols to object file\n");
  180.     free (buf);
  181.     exit (EXIT_FAILURE);
  182.     }
  183.  
  184.     /* calculate string table size */
  185.     strsize = sizeof(strsize) + strlen(labname) * 2 + 12;
  186.  
  187.     /* seek to string table offset in object file */
  188.     if (lseek (objfd, N_STROFF(outex), L_SET) == -1) {
  189.     fprintf (stderr, "Failed to seek to symbols in object file\n");
  190.     free (buf);
  191.     exit (EXIT_FAILURE);
  192.     }
  193.  
  194.     if (write (objfd, &strsize, sizeof(strsize)) != sizeof(strsize)) {
  195.     fprintf (stderr, "Failed to write str table size to object file\n");
  196.     free (buf);
  197.     exit (EXIT_FAILURE);
  198.     }
  199.  
  200.     /* write strings to the string table */
  201.     write (objfd, "_", 1);
  202.     write (objfd, labname, strlen(labname));
  203.     write (objfd, "data", 5);
  204.     write (objfd, "_", 1);
  205.     write (objfd, labname, strlen(labname));
  206.     write (objfd, "size", 5);
  207.  
  208.     /* close the object file */
  209.     close (objfd);
  210.  
  211.     /* free the buffer */
  212.     free (buf);
  213.  
  214.     printf( "Converted input file %s to object file %s\n", filename,
  215.        objname);
  216.  
  217.     exit (EXIT_SUCCESS);
  218. }
  219.