Warning! This site is under development.

EBoard 14: Wrapping Up

  • This class will not be recorded, at least not by me.

Approximate overview

  • Administrative stuff
  • More fun with getopt
  • What I hope you’ve learned in this course
  • What you might explore next
  • EOCEs
  • Final comments

Administrivia

  • Beware! Friday the 13th falls on a Friday this month.
  • If you choose to attend Block Party, please be responsible.

Building standardized libraries

  • At some point, most language designers decided to add some way of
    • (a) accessing the values entered on the command line (e.g., argc and argv in C) and
    • (b) providing information to process the command line.
  • In C, getopt is the primary library and getopt_long is an extended version of it.
  • Other languages have better libraries.

Here’s the declaration of getopt

int getopt(int argc, char * const argv[],
           const char *optstring);

extern char *optarg;
extern int optind, opterr, optopt;

What’s going on here?

  • argc is the number of options (the length of argv)
  • argv is an array of strings which represent what was typed on the command line (spaces generally separate the strings)
  • optstring is a list of legal options.
    • a letter means we accept “-a”
    • a letter followed by a colon means we accept “-a arg”
    • a letter followed by a question mark means optional argument
  • getopt returns an int which represents the next option
    • for example, if you ask for arguments from [“-a”,”-b”,”-c”], the first call will return ‘a’, the second will return ‘b’, the third will return ‘c’, and the fourth will return -1 to represent the end of input.
    • getopt has somewhat hidden state.
    • That’s why it’s an int and not char.
  • What’s the const in the declaration of argv?
    • Something is immutable. Maybe the strings. Maybe the array. The array is mutable!
  • What’s the const in the declaration of optstring?
    • The string is not mutable.
  • What’s optarg? Is the argument for flags that have arguments or optional arguments.
  • What’s optopt? A way to report errors back to the caller. (It’s C, since we only get to return one value, we “return” other values in globals.)
    • When there’s error, getopt always returns ‘?’
    • The illegal character is stored in optind.
  • What’s opterr? How you tell getopt whether or not you want it to print errors. By default, it’s a number that means “print errors”. If you set it to 0, it doesn’t print errors.
  • What’s optind? The index last used (or next used); the state of getopt.
    • You have access to it because you may want to change the state of the system. E.g., you might want to start further down the array.

A sample getopt program

Stolen from the getopt man page.

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

int
main (int argc, char *argv[])
{
  int flags, opt;
  int nsecs, tfnd;

  nsecs = 0;
  tfnd = 0;
  flags = 0;
  while ((opt = getopt (argc, argv, "nt:")) != -1) 
    {
      switch (opt) 
        {
          case 'n':
            flags = 1;
            break;
          case 't':
            nsecs = atoi (optarg);
            tfnd = 1;
            break;
          default: /* '?' */
            fprintf (stderr, "Usage: %s [-t nsecs] [-n] name\n",
                    argv[0]);
            exit (EXIT_FAILURE);
        } // switch
    } // while

  printf ("flags=%d; tfnd=%d; nsecs=%d; optind=%d\n",
         flags, tfnd, nsecs, optind);

  if (optind >= argc) 
    {
      fprintf (stderr, "Expected argument after options\n");
      exit (EXIT_FAILURE);
    }

  exit (EXIT_SUCCESS);
} // main

Another example program

Our goal: Understand what getopt does in a variety of situations. We’ll print out the status after each call to getopt and not much more.

  int opt = 0;
  while ((opt = getopt (argc, argv, "abc:d:ef?")) != -1) 
    {
      fprintf (port, "Status\n");
      fprintf (port, "  opt:    '%c' (%d)\n", opt, opt);
      fprintf (port, "  optopt: '%c' (%d)\n", optopt, optopt);
      fprintf (port, "  optind: %d\n", optind);
      fprintf (port, "  optarg: \"%s\"\n", optarg);
      fprintf (port, "  argv: ");
      fprintstrings (port, argv, argc);
      fprintf (port, "\n\n");
    }

TPS: What experiments would you like to run?

  • ./getopt-experiment -a - see what happens with just a flag
  • ./getopt-experiment foo -a - see what happens if the flag isn’t first
  • ./getopt-experiment -abd yeah - Combine arguments together
  • ./getopt-experiment -c foo bar baz - Give multiple arguments
  • ./getopt-experiment foo bar baz -a - Flag after the arguments
  • ./getopt-experiment -f - no optional argument
  • ./getopt-experiment -f foo - optional argument [NOT WORKING AS EXPECTED]
  • ./getopt-experiment -c foo -c bar - repeat an argument
  • ./getopt-experiment -cfilename - argument attached
  • ./getopt-experiment -abcfilename - argument attached
  • ./getopt-experiment foo -b bar -a baz -a - Flag mixed into arguments
  • ./getopt-experiment foo -q bar -r baz -a - Illegal flags
  • ./getopt-experiment foo bar baz - Flag after the arguments
  • ./getopt-experiment -d - missing required argument
  • ./getopt-experiment - nothing at all
  • ./getopt-experiment -- -a -b -c cat - special dashdash flag
  • ./getopt-experiment - -a -b -c dog - special dashdash flag

What I hope you’ve learned in this course

Some big picture issues

  • We all have new things to learn.
  • We all make mistakes.
  • You should be at the stage of your career in which you can learn by reading man pages, or mediocre blog posts, or even more mediocre documentation, or …
  • Programmers often learn by trying.
  • Things change
    • In my day, every byte mattered. And humans could optimize better than computers.
    • Today …
      • A few extra bytes are usually irrelevant (unless you have a huge data set).
      • Humans can’t optimize for, say, pipelined architectures.
      • Even for simpler architectures, optimzers are generally better.
    • Still, there are small things that can have big consequences. Don’t be Shlemeil the Painter.
  • Real programmers go meta (not Meta)
    • Write code rather than do things by hand
    • Write code code to write code, when appropriate
    • https://xkcd.com/1205/
  • Have fun!

Programming Languages

  • “Knowing” a programming language requires more than knowing the basics.
    • In C, macros, preprocessor, command-line, stupid tricks, etc.
  • Programming languages encourage particular models of thought
    • In C, you should know how things are laid out and have an inclination about how things are implemented.
    • Among other things, you need to understand strings (and arrays and …)
    • More generally, C requires you to think “low level”
    • You’ll find that other languages also encourage/discourage particular models of thought.
  • One of the ongoing debate topics: How much do you trust the programmer?

Linux

  • Linux also encourages a particular way of thinking
    • Small tools that do one thing well
    • Combined in multiple ways
  • Learn to use key features, such as pipes, other redirection (e.g., backticks), history, etc.
  • Learn the basic tools available to you.
  • Go meta.
  • Use Make
  • Learn a scripting language
    • Three decades ago: AWK + sed
    • One point five decades ago: Perl
    • Today: probably Python

Software development

  • Test, test, test. (And some testing strategies.)
  • Decompose.
  • Learn and use tools.
  • Learn and use libraries.

Particular things

  • Some aspects of C.
  • Some Linux tools.

What you might explore next

  • An editor.
    • I like vi.
    • My hacker son likes vim. (perhaps too much)
    • emacs is/was beautiful.
  • Tmux
    • Persistent sessions!
  • More on awk and sed.
  • The C standard.
  • Replacements for C
    • Rust
  • New scripting languages …
    • Perl.
    • Python.
    • Ruby.
  • Modern Make alternatives. (More generally, the build tool for your language.)

End-of-course evaluations

Evaluation forms may be found at https://grinnell.smartevals.com.

End-of-course ratings enable you to give responsible feedback for your professors, and the information you provide enters into future contract reviews. The agree/disagree responses will be tallied to produce frequency reports. The instructor will be able to review your unidentified comments within the electronic course evaluation system. Please note that the scale starts with “Strongly Disagree” at the top. Be careful not to inadvertently reverse your responses. Please provide comments but do not write your name in the comment boxes. Instructors receive the unidentified, completed forms only after grades have been submitted to the registrar.

Note that Sam’s form has a few extra questions.

Final comments

No, you don’t have a final.

  • Uniqueness
  • Bye