home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / binutils-1.8.x.tar.gz / binutils-1.8.x.tar / binutils / hp-bin / chatr.c next >
C/C++ Source or Header  |  1989-03-03  |  5KB  |  215 lines

  1. /* Change Attributes program for GNU.
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <a.out.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <sys/file.h>
  22. #include "ioutil.h"
  23.  
  24. #define forward extern
  25.  
  26. enum boolean { false, true };
  27. typedef enum boolean boolean;
  28.  
  29. boolean change_to_shared_p;
  30. boolean change_to_demand_loaded_p;
  31. boolean silent_p;
  32. int failures;
  33.  
  34. char *input_filename;
  35. char output_filename [20];
  36. int output_descriptor;
  37.  
  38. void
  39. main (argc, argv)
  40.      int argc;
  41.      char *argv[];
  42. {
  43.   register int argi;
  44.   register char **argp;
  45.   forward void usage ();
  46.   forward void process_file ();
  47.   forward void file_abort_handler ();
  48.   extern char *mktemp ();
  49.  
  50.   iou_set_program_name (argv);
  51.   argi = 1;
  52.   argp = (& (argv [1]));
  53.  
  54.   change_to_shared_p = false;
  55.   change_to_demand_loaded_p = false;
  56.   silent_p = false;
  57.  
  58.   while ((argi < argc) && (((*argp) [0]) == '-'))
  59.     {
  60.       switch ((*argp) [1])
  61.     {
  62.     case 'n':
  63.       change_to_shared_p = true;
  64.       break;
  65.     case 'q':
  66.       change_to_demand_loaded_p = true;
  67.       break;
  68.     case 's':
  69.       silent_p = true;
  70.       break;
  71.     default:
  72.       usage ();
  73.     }
  74.       if (((*argp) [2]) != '\0')
  75.     usage ();
  76.       argi += 1;
  77.       argp += 1;
  78.     }
  79.  
  80.   if (change_to_shared_p && change_to_demand_loaded_p)
  81.     iou_error ("conflicting options: -n and -q");
  82.  
  83.   if ((! change_to_shared_p) && (! change_to_demand_loaded_p) && silent_p)
  84.     exit (0);
  85.  
  86.   if (argi == argc)
  87.     exit (255);
  88.  
  89.   strcpy (output_filename, "/tmp/chatrXXXXXX");
  90.   if (output_filename != (mktemp (output_filename)))
  91.     iou_error ("mktemp failure");
  92.  
  93.   failures = 0;
  94.   for (; (argi < argc); argi += 1)
  95.     {
  96.       input_filename = (*argp++);
  97.       output_descriptor =
  98.     (iou_open (output_filename, (O_RDWR | O_CREAT | O_TRUNC), 0666));
  99.       iou_abort_handler_bind (process_file, file_abort_handler);
  100.       iou_close (output_descriptor);
  101.     }
  102.   iou_unlink (output_filename);
  103.   exit (failures);
  104. }
  105.  
  106. void
  107. usage ()
  108. {
  109.   fprintf (stderr, "usage: %s [-n] [-q] [-s] file ...\n", iou_program_name);
  110.   iou_error ();
  111. }
  112.  
  113. void
  114. file_abort_handler ()
  115. {
  116.   failures += 1;
  117.   return;
  118. }
  119.  
  120. void
  121. file_error (message)
  122.      char *message;
  123. {
  124.   char buffer [256];
  125.  
  126.   sprintf (buffer, "%s: \"%s\"", message, input_filename);
  127.   iou_error (buffer);
  128. }
  129.  
  130. void
  131. file_copy (input_descriptor, output_descriptor)
  132.      int input_descriptor;
  133.      int output_descriptor;
  134. {
  135.   char buffer [8192];
  136.   register int bytes_read;
  137.  
  138.   while (1)
  139.     {
  140.       bytes_read = (iou_read (input_descriptor, buffer, 8192));
  141.       if (bytes_read == 0) break;
  142.       iou_write (output_descriptor, buffer, bytes_read);
  143.     }
  144.   return;
  145. }
  146.  
  147. void
  148. process_file ()
  149. {
  150.   int input_descriptor;
  151.   struct exec input_exec;
  152.   struct exec output_exec;
  153.  
  154.   input_descriptor = (iou_open (input_filename, O_RDONLY));
  155.  
  156.   if ((iou_read (input_descriptor, (& input_exec), (sizeof (input_exec)))) !=
  157.       (sizeof (input_exec)))
  158.     file_error ("unable to read file header");
  159.  
  160.   switch (N_MAGIC (input_exec))
  161.     {
  162.     case NMAGIC:
  163.       if (change_to_shared_p)
  164.     file_error ("file not demand load executable");
  165.       break;
  166.  
  167.     case ZMAGIC:
  168.       if (change_to_demand_loaded_p)
  169.     file_error ("file not shared executable");
  170.       break;
  171.  
  172.     default:
  173.       file_error ("file not executable format");
  174.     }
  175.  
  176.   if (! silent_p)
  177.     {
  178.       printf ("%s:\n", input_filename);
  179.       if (change_to_shared_p || change_to_demand_loaded_p)
  180.     printf ("   current values:\n");
  181.       printf
  182.     ("         %s executable\n",
  183.      (((N_MAGIC (input_exec)) == NMAGIC) ? "shared" : "demand loaded"));
  184.       fflush (stdout);
  185.     }
  186.  
  187.   if ((! change_to_shared_p) && (! change_to_demand_loaded_p))
  188.     return;
  189.  
  190.   output_exec = input_exec;
  191.   N_SET_MAGIC (output_exec, (change_to_shared_p ? NMAGIC : ZMAGIC));
  192.  
  193.   iou_write (output_descriptor, (& output_exec), (sizeof (output_exec)));
  194.   iou_lseek (input_descriptor, (N_TXTOFF (input_exec)), 0);
  195.   iou_lseek (output_descriptor, (N_TXTOFF (output_exec)), 0);
  196.   file_copy (input_descriptor, output_descriptor);
  197.  
  198.   /* Now copy the temporary output file back into the input file. */
  199.   iou_close (input_descriptor);
  200.   iou_lseek (output_descriptor, 0, 0);
  201.   input_descriptor = (iou_open (input_filename, (O_WRONLY | O_TRUNC), 0777));
  202.   file_copy (output_descriptor, input_descriptor);
  203.   iou_close (input_descriptor);
  204.  
  205.   if (! silent_p)
  206.     {
  207.       printf ("   new values:\n");
  208.       printf
  209.     ("         %s executable\n",
  210.      (((N_MAGIC (output_exec)) == NMAGIC) ? "shared" : "demand loaded"));
  211.       fflush (stdout);
  212.     }
  213.   return;
  214. }
  215.