Thinking in C and *nix (CSC 282 2015S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [Teaching & Learning] - [Calendar]
Current: [Outline] [EBoard] [Lab] [Assignment]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines]
Reference: [EBook] - [ISO] [GNU Coding Standards] [GCC Documentation] - [TAoUP] [Make3]
Previous Offerings: [CSC 295 2013S] [CSC 295 2014S]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
Code by NK.
#!/bin/sh
# first argument is where from, second argument is where too.
for file in $(find $1 | grep .jpg) ; do
echo "copying $file to $2"
cp $file $2
done
Observation 1: $1 and $2 refer to command-line arguments.
Question: How do we figure out what find $1 | grep .jpg does?
man grep - search for lines that match a patternman find - when used as find dir, lists all the files in
a directory.Conclusion: The command find $1 | grep .jpg finds all the jpg
files within a directory.
Question: Might it find thigns other than jpg files. We might
have a filename with .jpg in the middle. (or just any character
and jpg). use \.jpg$.
Other improvements: find can actually have some parameters that
specify what files to find.
find . -name '*.jpg'
Note time cmd tells you how long a command takes
Detour: How do pipes work?
On the design and implementation of larger programs written in C.
Simple model of C programs: Put everything in one file. (Alternate: Put things in separate files.)
Traditionally, a C project includes
#include parta.c, but that has the slow compilation
issue.The 161 model: .c -> executable
The actual steps:
For our silly program
When compiling partb, how does the compiler know about parta?
We've just seen that it doesn't have to. Maybe it's smart.
Why do we not get the warning that 'foo' is not declared?
The C compiler trusts the user.
What should we do?
Quick hack:
extern double foo(double);
Normal strategy: For every .c file, you create a .h file that gives such declarations.
Here's parta.h
#ifndef __PARTA_H__
#define __PARTA_H__
/**
* parta.h
* Amazing mathemtical utils by NK and cmapsdfay.
*
* Released under GPL 3.0. You should have received a
* license with this software. If not, got to
* Richard Stallman's abode and ask for a copy.
*/
extern double foo(double);
#endif __PARTA_H__