380 likes | 609 Views
Programming Tools, C Programming in UNIX, Make, and SCCS. What we’ll be looking at…. C compiler make utility sccs (Source Code Control System) rcs (Revision Control System) programming examples. Programming in C. UNIX was developed in C provides easy access for system calls
E N D
Programming Tools, C Programming in UNIX, Make, and SCCS
What we’ll be looking at… • C compiler • make utility • sccs (Source Code Control System) • rcs (Revision Control System) • programming examples
Programming in C • UNIX was developed in C • provides easy access for system calls • and a variety of libraries are available • you use the editor of your choice (like vi) • create source code file ending with “.c”
C Programming Example /* Program name: Hello World Created: August 1, 1999 Author: Author’s name*/ #include <stdio.h> main () { printf (“Hello World\n”); }
C Programming Example /* Program name: Hello World Created: August 1, 1999 Author: Author's name */ #include <stdio.h> main () { char *name; printf("Please enter your name: "); scanf("%s", name); printf ("Hello, %s!\n", name); }
C Programming Example /* program - tabs.c convert tabs in standard input to spaces in standard output while keeping columns */ #include <stdio.h> #define TABSIZE 8 main() { char ch; /* character read from stdin */ int posn = 0; /* column position of character */ int inc; /* column increment to tab stop */ while ((ch = getchar() ) != EOF) switch(ch) { case ‘\t’: /* ch is a tab */ inc = findstop(posn); posn += inc; for ( ; inc > 0; inc--) putchar(‘ ‘); break;
example (cont.) case ‘\n’: /* ch is a newline */ putchar(ch); posn = 0; break; default: /* ch is anything else */ putchar(ch); posn++; break; } } /*------------------------------------------------------------------*/ /* function to compute size of increment to next tab stop */ findstop() int col; /* column position of tab character */ { return (TABSIZE - (col % TABSIZE)); } /*--- eof - tab.c ---*/
#include statement • angle brackets <stdio.h> • ie. #include <stdio.h> • look for header file in standard directory • /usr/include on most systems • quotes “/xxx/yyy/zzz.h” • gets header file from directory you specify • ie. #include “/alex/cprogs/ledg.h”
Compiling • gcc tab.c (We are using a freeware C compiler called gcc) • there are 4 processes called in turn by gcc • preprocessor • compiler -- creates assembler code • assembler -- creates object - .o file • linker (link editor) -- creates executable image • a.out (by default) • or you can specify with -o switch
other switches…. • -l (“el” not “one”) • specify other libraries to search • -O • optimize program • -o • give executable the name of your choice • -c • suppress linking (link editor) phase
cheap debug tool... • display debug info from program to stderr • an example for the tab.c program might be: • fprintf(stderr, “before function is called, posn is %d\n”, posn) • remember posn was declared variable in the program • imbed as many “print” statements as you need • any where you need them.... • don’t forget to remove ALL of the debug print statements when you are done!!! • ....or at least comment them out!
lint • no, it isn’t something from your pocket… • it is a C program verifier • checks program for bugs and possible portability problems • lint is VERY strict (unlike the C compiler) • not all bugs are catastrophic • but if lint complains you should try to fix your code before compiling….
debuggers • you can use whichever debugger you wish • or is available on your system • some choices might be: • adb, sdb, debug, or dbx • they tend to be command line oriented and interactive • can be used to look into a core file as well!
Controlling Processes • fork() - creates a new child process • wait() - cause parent process to wait for child to finish running before it resumes execution • exit() - cause a process to exit • nice() - change the priority of a process • kill() - send a signal to a process
Filesystem Access • stat() - get status information from an inode • access() - check file access permissions • creat() - create a new file • open() - open an existing file • read() - read a file • write() - write a file • close() - close a file • unlink() - unlink a file (delete name reference to inode) • chmod() - change file access permissions • chown() - change file ownership
make utility • large programs tend to have many source and header files that depend on one another in complex ways • make automates the process of determining which modules need to be compiled due to their dependency relationships • looks at dependency lines in file makefile in the working directory
make (cont…) • at it’s simplest • dependency lines indicate relationships among files, specifying a target file that depends on one or more prerequisite files • if any prerequisite is newer than a target then update target based on construction commands that follow the dependency line • make usually stops when it encounters an error during the construction process
a simple makefile target: prerequisite-list construction-commands for example….. to compile the program “mytab” mytab: tab.c gcc -o mytab tabs.c
...a more complicated makefile form: size.o length.o gcc -o form size.o length.o size.o: size.c form.h gcc -c size.c length.o: length.c form.h gcc -c length.c form.h: num.h table.h cat num.h table.h > form.h
another example... # # makefile for compute # compute: compute.o calc.o gcc -o compute compute.o calc.o compute.o: compute.c compute.h gcc -c -O compute.c calc.o: calc.c gcc -c calc.c clean: note this is on next line @- rm *.o
executing make • make<cr> (on aries you’ll find make in the /usr/ccs/bin directory) • uses makefile in the current working directory • if program is current, then make does nothing except tell you so • you can directly refer to any label in the makefile • ie. make clean • or. make compute (the default for this example)
touch • If you perform a successful make, you will sometimes want to repeat the entire process again for testing purposes • If you get the message “file” is up to date then it is telling you that nothing has changed and it doesn’t need to run again • Touch will change the last modification time of any file ie; touch -c tabs.c • By default if the specified file doesn’t exist it is created with a zero size. To prevent use the –c option
Source Code Management • helps keep track of projects involving many files over long periods of time • helps keep track of versions • for both source code and documentation • a must have when more than one person is working on the project
Source Code Management • UNIX systems include two utilities for managing and tracking changes to files • SCCS, the Source Code Control System • included with SVR4 • RCS, the Revision Control System • provided as add-on by many manufacturers • they can be used on any text file • but are usually used to manage source code and software documentation
SCCS • when you change a SCCS file and record the changes in SCCS, the set of changes is referred to as a delta • each delta has an associated: • version number • in SCCS the version number is known as a: • SCCS Identification String (SID)
SID • consists of either 2 or 4 numbers • first two (which are always used) • release • level • default for initial file creation is 1.1 • you have control over the numbering • you can skip level numbers • and change release numbers
SID (continued…) • the second two numbers represent: • branch • sequence number • default to 1.1 • so you could see something like 2.3.1.1 • used when changes are made to an intermediate version of a file • branching away from the sequential development
SCCS commands…. • sccs - a front end to the SCCS utility. • automatically prepends SCCS/s. to any filename arguments • great stuff if you are using the SCCS subdirectory! • Note: you’ll need to know where the sccs commands such as admin live. I have the path included for some examples but not all in the following slides. On aries this path is /usr/bin
SCCS commands…. • Basic setup and editing • admin • create or add new SCCS files • change options for SCCS files • sccs admin -itab.c SCCS/s.tabs.c • get • retrieve text version of SCCS files • -e switch gets an editable text file! • sccs get –e SCCS/s.tabs.c • Will give you a tabs.c file that can be edited in your current directory • Also creates a p.tabs.c that goes away when the file is returned to the library
SCCS commands…. • delta • incorporate changes to one or more SCCS files • i.e., append a new delta • normally removes the original text file • unget • cancel a previous get -e • don’t create a new delta
SCCS commands…. • Fixing deltas • cdc • change delta comments • comb • combine consecutive deltas into a single delta • produces script that must be run to actually accomplish the task • rmdel -r • remove a delta from SCCS files
SCCS commands…. • Information • help • online help facility • prs • print formatted info about SCCS files • sact • report on editing activity on SCCS files • what • search for @(#) pattern and print text that follows
SCCS commands…. • More commands • sccsdiff • show differences between any two SCCS files sccs sccsdiff -r1.1 -r1.2 SCCS/s.tab.c • val • validate an SCCS file
Creating a SCCS file • admin -inamefilename • name: file SCCS will encode • filename: name of SCCS encoded file • always start with s. • for example: admin -ihello s.hello • creates encoded file in the current working directory • or: admin -hello SCCS/s.hello • assuming you’ve created a subdirectory called SCCS to store the encoded files separate from the working directory
Retrieving a SCCS file • get • retrieve text version of SCCS file • use -e option to retrieve an editable file • if you are using the SCCS subdirectory: • sccs get -e SCCS/s.hello - and - • -r option • change major version number (say 1.5 to 2.1) • retrieve earlier version (which causes a branch)
Recording changes to SCCS file • delta • incorporate changes (add a delta) to one or more SCCS files • used to store changes made in file retrieved by get -e command • example: • sccs delta hello -or- • Sccs delta SCCS/s.hello • prompts for comment • end with <CTRL>D on line by itself
Controlling access to SCCS • use admin to establish list of users who are allowed to make deltas. • by default the list is empty • which means anyone can make deltas • if the list is not empty only those on the list can make deltas • done on a ‘per file’ basis - each file under control of SCCS has a separate list
Controlling access (continued) • examples: • adding users to the list • sccs admin -ainstructor1 -astudent1 SCCS/s.hello • sccs admin -astudent2 -astudent3 hello • erasing users from the list • sccs admin -einstructor2 SCCS/s.hello • lock and unlock releases • sccs admin -fl2 hello (locks release 2 of hello) • sccs admin -fla hello (unlocks all releases)