Thinking in C and *nix (CSC 295/282 2014S) : EBoards

CSC295 2014S, Class 01: Introduction


Overview

Preliminaries

Admin

About the course

Some principles and practices

Three primary approaches

Good programming practices

Good C programmers

Thinking like a *nix programmer

Example: C from K and R

char *
fun (char *t, char *s)
{
  while (*t++ = *s++);
  return t;
} // fun

This function copies s to to. It's mostly strcpy. It returns a pointer to the one after the end-of-string character in the result.

Detour: A C memory problem

x = malloc (...);
foo ();
bar ();
free (x);   // CRASH

Exercises: Some simple tasks

Given a DOS-formatted text file (lines end with \r\n rather than just \n), convert it to a standard text file.

If you know awk, you could write an awk script to do it. (less than a minute, if you know awk and use it regularly)

If you know flex, you can write a flex program. (five minutes)

If you program regularly in C (which we'll assume, write a C program).
(five to ten minutes).

int ch;
while ((ch = getch()) != EOF)
  {
    if (ch != '\r')
      putchar (ch);
  } // while

Two more tools:

Given a standard text file, convert all uppercase letters to lowercase.

Given a standard text file, remove all blank spaces at the end of lines.

Make a list of all misspelled words in a text file.

Given a CSV file in which each line has the form

LastName,FirstName,Assignment,NumericGrade

find the the five highest grades on HW2

Given an HTML file, find the URLs of all images. In case you don't

know HTML, those will typically look like <img ... src="*URL"...> * The img can have any capitalization (img, IMG, Img, iMg, etc.) * There can be other text between the img and the src. (That text cannot include a greater than sign.) * You may find it easier to start this problem by assuming that there's only one image tag on a line.
* But, in the end, you should support multiple image tags on the same line.

Detour: Using the Shell

Some important *nix tools

Homework

This will appear online some time in the near future (probably over the weekend).

Repository: https://github.com/Grinnell-CSC282/hw1

  1. Read chapter 1 of Raymond to get a deeper understanding of the *nix Philosophy.

  2. Find good solutions to any of the tasks we did not solve in class. (That is, write a program or command to solve the task.) Put them in a folder with your name in the GitHub repository.

  3. Make a list of *nix tools and utilities that you've found useful (or even that you've just heard of). Try to categorize and add a short description. Put them in the tools.md file in the repository.

  4. Add some notes about what might be wrong with the C code above.

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.