4.9 "C" A Programming Language |
csh
, awk
, and perl
.
The interface to the operating system
is easily accessed with the "C" language. The operating system
calls look like "C" function calls. The system calls
are documented in section 2 of the manual pages. Library calls
are documented in section 3 of the manual pages.
This lab will also be used to look at the effects of umask
on protection bits when a file is created.
hello.c
with the
following "C" program:
/* Program to print "hello, world" */ /* Kernighan & Richie "The C Programming Language" */ /* Copyright 1978 page 6 */ #include <stdio.h> main() { printf("hello, world\n"); }
(cc is the standard C Compiler)username@gort ~ $
cc hello.c
or
(gcc is the gnu C Compiler)
username@gort ~ $
gcc hello.c
The standard output of the compiler is a.out. You can use
the option -o
to put the executable in another file.
username@gort ~ $
ls -l a.out
-rwxrwxrwx 1 ltaber 15165 Oct 13 14:30 a.out
username@gort ~ $
a.out
(run the program)
hello, world (resulting output)
umask
on
file protection bits during file creation.
You may have noted above that the protection bits that you saw
on your a.out
file did not match my example. This is
because you may have a different umask
setting. To
look at your current setting try:
username@gort ~ $
umask
Your output should be an octal number that is the current setting for
umask
. If a bit is set in your umask
it will prevent
that bit from being set when a file is created. Set your umask
to zero.
username@gort ~ $
umask 0
Recompile your program and look at the protection bits.
It should now match my example. Now try a more restrictive setting:
username@gort ~ $
umask 077
Recompile your program and look at the protection bits for a.out
again. This will only allow you to read, write, or execute
the program. Your results should look like this:
username@gort ~ $
ls -l a.out
-rwx------ 1 ltaber 15165 Oct 13 14:30 a.out
Try a umask
setting of 0777. Write on your lab what happens and
explain why.
args.c
with the following C program:
/* Program to print arguments to a program */ /* L Taber October 13, 1992 PCC */ #include <stdio.h> main(argc, argv ) int argc; char * argv[ ]; { int i; printf("argc = %d File name = %s\n", argc, argv[0]); for ( i=1; i<argc; i++) printf(" arg[%d] = %s\n" ,i,argv[i]); printf("\n"); }
Compile and test the program.
test1
":
#!/bin/bash # # L Taber Argument passing # test script a.out $@ a.out $* a.out "$@" a.out "$*" a.out '$@' a.out '$*'
username@gort ~ $
test1 "A B" 'c d' e
The output should look as follows:
argc = 6 File name = a.out arg[1] = A arg[2] = B arg[3] = c arg[4] = d arg[5] = e argc = 6 File name = a.out arg[1] = A arg[2] = B arg[3] = c arg[4] = d arg[5] = e argc = 4 File name = a.out arg[1] = A B arg[2] = c d arg[3] = e argc = 2 File name = a.out arg[1] = A B c d e argc = 2 File name = a.out arg[1] = $@ argc = 2 File name = a.out arg[1] = $*
your name
TABER CIS137
Lab 4.9: C Programming
and the results of a umask
of zero
Please turn your lab to Louis Taber or to Pima Community
College employee
in room A-115 of the Santa Rita Building.
Ask them to place it in the dark blue folder in
Louis Taber's mailbox.4.9 "C" A Programming Language |