June:12(Tue)
Also review running programs in the background using & .$ man getcwd $ man chdir $ man strerror $ man getenv $ man 3 sleep
search() program files
as follows:
search.h
containing
the declaration of function search()
and the definition of the 'not found' value -1.
search.c,
#include
your new
search.h .
typedef to give a name such as "index_t" to the
type of values that
search()
returns and its third argument.
Use this "index_t"
in search.h
and search.c .
search_demos.c
from ~mcguire/public_html/teaching/361/exercises/4/e/ ,
and
in it,
#include
your new
search.h .
search.h ,
search.c ,
search_demos.c , and
Makefile ,
as well as a demonstration
in which you execute the following:
$ gcc -MM search.c $ gcc -MM search_demo.c $ touch search.h $ make $ ./search_demosIf make doesn't recompile everything, then you need to fix things.
open()s.
Handle errors as in lowercase_sys.c presented in lectures.
Still don't .
#include <stdio.h>
Demo as before plus with .../0 and .../_ and no argument. Display exit-status values.
$
my command interpreter is /bin/bash
$
/home/mcguire/public_html/teaching/361/exercises/2.6
$
my home directory is /home/mcguire
$
$
/home/mcguire
$
Where is Wa/bin/bash?
$
Note that you need to handle shell variables embedded in 'words',
as indicated.
system() to
execute the user's command if it
is "echo ...".
system() to
execute the user's command if it
is "pwd".
getcwd()
to determine
the current working directory,
and display it.
system() to
execute the user's command if it
is "cd directory".
chdir()
to change
the current working directory
to be directory .
getenv("HOME")
(plus a little more processing),
if the user executes cd
with no argument.
perror() --
but don't
abort your program.
system() to
execute the user's command if it
is "size filename".
stat() to the
filename,
and then display the specified file's size.
perror() —
but don't
abort your program.
/home/mcguire/public_html/teaching/361/exercises/3/h/0
/home/mcguire/public_html/teaching/361/exercises/3/h/1
/home/mcguire/public_html/teaching/361/exercises/3/h/2
/home/mcguire/public_html/teaching/361/exercises/6/c/1
stdio.h —
and, indeed, doesn't #include it —
yet manages to put the
characters hello\n (where \n
means a newline character)
in the file specified by the
program's first argument.
Use the system calls of Sections 8.2 and 8.3 .
Your program should behave appropriately
if the number of arguments is wrong or
if
accessing the file fails; again,
use strerror().
Also,
your program needs to close the file explicitly
(rather than having the file closed implicitly when the program ends).
Here (again) are macros for outputting strings:
#define fdputs(fd,s) write((fd), (s), strlen(s)) #define fdputsq(fd,sq) write((fd), (sq), sizeof (sq) - 1)Copy these into your program here.
In your demonstration here, do the following:
struct
to represent a DVD (movie),
with data members
for the movie's title,
number of minutes,
and price (a floating-point number).
structs.
qsort()
and appropriate comparison functions which you write
to
sort your list of
DVDs by titles, lengths, and prices,
displaying the contents of the array each time.
qsort() uses it,
longer movies will precede shorter ones.
typedef
for const struct pointers.
If you don't own 10 DVDs, obtain 10 titles by including movies you've seen recently that are available on DVD, and look up their lengths and prices (via Amazon.com or something).
[ ]";
instead,
use "*"s
at proper places.
Inside the function,
use pointer arithmetic;
your function
should contain no array references
(i.e. using brackets, "[ ]"),
and you should not change (increment or decrement)
any variables other than pointer variables.
Instead of such operations on integral variables,
decrement and increment and compare and subtract etc.
pointer variables.
main()
of Exercise C of Exercise-Set #6
works properly with this new version of your function.
main(),
output the values of
sizeof data ,
sizeof *data ,
alen(data) ,
and all the contents of
data[]
where data is defined as follows:
char * data[] =
{
"negative", "zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen",
"twenty", "twenty-one", "twenty-two", "twenty-three",
"infinity",
"click", "cling", "clock", "clung", "flick", "fling", "flock", "flung"
};
qsort()
and auxiliary code —
particularly scmp() —
as shown in
lecture (and as also available online)
to sort
data[]
alphabetically (lexicographically),
just as (most of)
argv[]
was sorted in
qsorts.c presented in lecture.
data[] again, after that sorting.
scmp()
except that it invokes your function
of Part 1 above
instead of strcmp().
qsort() and this new function
to sort data[]
in rhyming dictionary order.
scmp()
rather than just the one
from
Part 1 above
(or
Exercise C of Exercise-Set #6) alone
because
qsort() expects you to pass it
a function that can handle, e.g.,
&data[0] and &data[1] as arguments.
data[] again after it's been sorted
this second way.
(Copyright © 2007 by Hugh McGuire -- for thoughts about this, see: http://www.csis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html )