Upcoming Events

Cloud Connect
Santa Clara
Feb 13-16, 2012

Cloud Connect brings together the entire cloud eco-system to better understand the transformation we're experiencing and promises to be the defining event of the cloud computing industry. Learn about the latest cloud technologies and platforms from thought leaders in Cloud Connect’s comprehensive conference.

Register Now!

More Events »

Subscribe to Newsletter

  • Keep up with all of the latest news and analysis on the fast-moving IT industry with Network Computing newsletters.
Sign Up


Dependable Make Files: Listings

Listing 1: A make file dependency generator lists the header files required by each object-file target.

.

A. Listing of the Perl script:

1   #!/usr/bin/perl
2   # @(#) makedepend  List make target dependencies
3   # Author: Dale Mensch, July 1993
4   # Modified by: Becca Thomas, January 1994
5  
6   # CONFIGURATION PARAMETERS:
7  
8   # Using GNU make?
9   $have_gmake = 0;                        # 0 = No, 1 = Yes
10  
11  # Choose the source-file suffix:
12  $source_suffix = '.c';                  # Possibilities: .C, .cpp, .cc
13  
14  # Choose the object-file suffix:
15  $object_suffix = '.o';                  # Another possibility: .xo
16  
17  # List directories containing header files that don't change:
18  $ignore_include_dirs = '/usr/include';  # Don't need to list these
19  
20  # Location of standard header files:
21  $add_include_dirs = '-I/usr/include';   # If not included automatically
22  
23  # Command that runs preprocessor, includes headers, excludes comments:
24  $cpp_cmd = 'cc -E';
25  
26  # MAIN PROGRAM:
27  
28  # Check correct command line usage:
29  die "Usage: $0 target [target...]\n" unless (@ARGV);# At least one
30  
31  # Open pipe to read make (or gmake) output:
32  if ($have_gmake) {
33      open(MAKE, "gmake -n -W $ARGV[0] |");       # -W outdates target
34  } else {
35      # Rename target because if non-existent it's treated as out-of-date:
36      ($sample_target = $ARGV[0]) =~ s/$source_suffix$/$object_suffix/;
37      if (-e $sample_target) {    # If the sample target exists
38          $saved_target = "$sample_target"."~";   # Backup copy name
39          rename($sample_target, $saved_target);  # Squirrel away
40      }
41      open(MAKE, "make -n $sample_target |");     # Make treats as dated
42  }
43  
44  # Determine necessary include directives:
45  while (<MAKE>) {    # For each make-command output line
46      chop;           # Remove trailing newline
47      @cmd_line = split(/[ \t]+/, $_);            # Tokenize at whitespace
48  
49      while (@cmd_line) {
50          $make_token = shift (@cmd_line);        # For each token
51          if ($make_token =~ /^-I|^-D/) {         # Include/define?
52              $directive_token = $make_token;     # Yes, save it
53              if (! $is_directive{$directive_token}) {    # Not seen
54                  $is_directive{$directive_token} .= $directive_token;
55                  # Add to catenated string of include directories:
56                  $cpp_directives .= " ";         # (Catenation saves
57                  $cpp_directives .= $directive_token;# original order)
58              }
59          }
60      }   # End of while ($make_token = ...
61  }       # End of while (<MAKE>) {
62  
63  # Add any necessary include directories:
64  $cpp_directives .= " $add_include_dirs ";
65  
66  # For each source file named on command line
67  while (@ARGV) {                         # While there are arguments
68      undef %includes;                    # Reset array before next file
69      $source_file = shift;               # Save name for later use
70      if (! -r $source_file) {            # Can file be read?
71          print STDERR "Source file \"$source_file\" not readable.\n";
72          next;   # Try another source file, if available
73      }
74      # Preprocessor names all include directives:
75      open(CPP, "$cpp_cmd $cpp_directives $source_file |");
76  
77      # Create list of all included files:
78      while (<CPP>) {
79          s#//.*$##;                      # Delete C++ "//" comment
80          if (/^(#|#line)\s*\d+\s+["<](\S+)[">]\s+.*$/ &&
81            ! m.$ignore_include_dirs. ) { # Grab relevant includes
82                  $includes{$2} .= $2 unless $includes{$2};   # Save name
83          }   # End of if (/^ ...
84      }       # End of while (<CPP>) {
85  
86      # Report target name:
87      $_ = $source_file;
88      s/$source_suffix$/$object_suffix/;  # Create target name
89      print "$_ :";                       # Dependency or rules line
90  
91      # List the prerequisites for the target:
92      foreach $dependency (sort keys(%includes)) {
93          print " \\\n";  # continuation line
94          print "\t$includes{$dependency}";# Important: tab first
95      }
96      print "\n\n";                       # Empty lines between targets
97  }
98  rename($saved_target, $sample_target) if $saved_target;

B. Sample prototype Makefile:

# Make file to build program
#
CFLAGS = -g # for symbolic debugging
#
OBJS = module1.o module2.o
#
program: $(OBJS)
cc -o $@ $(OBJS)
#
# Object file dependencies:
#

C. Completed make file with object-file dependencies included:

# Make file to build program
#
CFLAGS = -g # for symbolic debugging
#
OBJS = module1.o module2.o
#
program: $(OBJS)
cc -o $@ $(OBJS)
#
# Object file dependencies:
#
module1.o : \
./module1.h \
module1.c

module2.o : \
module2.c

Listing 2: The zloop Bourne shell script lets you operate on selected compressed files.

A. Listing of the zloop program:

1  #! /bin/sh
2  # @(#) zloop  Loop through compressed files, running command on each
3  # Author: Jerry Peek, April 1990, updated July 1993
4  # Modified by Kees Hendrikse, February 1994
5  
6  Myname=`basename $0`    # Name of this program
7  Usage="Usage: $Myname 'command line to run' file.{Z|z|gz|zip}..."
8  
9  # Check command-line arguments:
10  case $# in
11      0|1) echo $Usage 1>&2 ; exit 1 ;;
12  esac
13  
14  # Save command quoting white space:
15  Cmd="$1"; shift         # shift away command argument
16  
17  for File                # For all remaining arguments (files)
18  do
19      case $File in
20          *.Z)    Zcat=zcat ;;
21          *.z)    Zcat=pcat ;;
22          *.gz)   Zcat=gzcat ;;
23          *.zip)  Zcat="unzip -c" ;;
24          *)      echo "File type of \"$File\" not recognized." >&2
25                  continue ;;
26      esac
27  
28      # Display a header, run the command, report any errors:
29      echo "
30  ==== $Myname: $Zcat $File | $Cmd ====" 1>&2
31      eval $Zcat $File \| $Cmd
32      Status=$?
33      case "$Status" in
34          0)  ;;
35          *)  echo "Note command that returned $Status exit status:
36              '$Zcat $File | $Cmd'" 1>&2 ;;
37      esac
38  done

B. Using zloop to print some compressed files:

$ ls ch*
ch01.Z   ch02.Z   ch03.Z   ch04.Z   ch05.Z
$ zloop 'pr | lp -d ast' ch0[1-3].Z

==== zloop: zcat ch01.Z | pr | lp -d ast ====
request id is ast-1904 (standard input)

==== zloop: zcat ch02.Z | pr | lp -d ast ====
request id is ast-1905 (standard input)

==== zloop: zcat ch03.Z | pr | lp -d ast ====
request id is ast-1906 (standard input)
$ []

C. Discarding the headers from the last example:

$ zloop 'pr | lp -d ast' ch0[1-3].Z 2>/dev/null
request id is ast-1907 (standard input)
request id is ast-1908 (standard input)
request id is ast-1909 (standard input)
$ []

D. Running grep to search for a text in compressed files:

$ zloop 'grep -i "System V"' ch0[12].Z

==== zloop: zcat ch01.Z | grep -i "System V" ====
Note command that returned 1 exit status:
		'/usr/bin/zcat ch01.Z | grep -i "System V"'

==== zloop: zcat ch02.Z | grep -i "System V" ====
1  #ifdef TERMIO                   /* System V or BSD4.3+ */
$ []
Print This Page


e-mail Send as e-mail

Research and Reports

Hypervisor Derby
August 2011

Network Computing: August 2011

TechWeb Careers