4.9 "C" A Programming Language |
Most of the UNIX operating system
is written in the "C" programming language. Most of the tools
(and games) that run on the system are also written in "C".
Many other utilities have a flavor of "C", such as the "C"
shell 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.
Using a text editor, create the file 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"); }
Compile and run the program:
(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 0700. Write on your lab what happens and
explain why. I want you to turn in your explanation of what happened when you
set your umask
to 0700 and what you
needed to do to get thing to work again.
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] = $*
Turn in a copy of this output marked with:
your name
TABER CIS137
Lab 4.9: C Programming
Include your written explanation of what
happened when you set umask
to 0777 and
what was needed to get your account to function again.
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.
Instructor: Louis Taber, louis.taber.at.pima at gmail dot com (520) 206-6850
My web site in Cleveland, OH
The Pima Community College web site
4.9 "C" A Programming Language |