Interfacing with UNIX: Setting File Permissions

CSE230

Interface with

UNIX

Interfacing with UNIX:
Setting File Permissions

•open(const char filename, int flag, …..)
- described in unistd.h(io.h under Windows)
- allows user to specify access permissions in octal for file (rwxrwxrwx)

•Example:
int out_fd;// file descriptor
out_fd=open(fname,______, ______);

More Interfacing:
Running UNIX Commands

•system() - allows users to run UNIX commands from a C program

•returns the exit status of the command that is executed.

•Examples:
printf(“The number of unique users is “);
system(“______| sort | ______| wc - l”);
if (system(“dblspace indata outdata”)) ______)
printf(“system command failed”);

Example

#include <stdio.h>

#include <string.h>

int main()

{

char iexp[]="\"C:\\.....\\iexplore.exe\" ";

char netscape[]="\"C:\\.....\\Netscp.exe\" ";

char cse102[]="c:\\ae\\index102.html";

char cse110[]="c:\\ae\\index110.html";

char cse130[]="c:\\ae\\index130.html";

char cse214[]="c:\\ae\\index214.html";

char cse230[]="c:\\ae\\index230.html";

char response[30];

char command[200];

int course;

while ( 1 )

{

printf("Select browser(ie,net or quit)");

scanf("%s", response);

if (strcmp(response,"quit") == 0)

break;

scanf("%d", &course);

if ( strcmp(response, "ie") == 0 )

strcpy(command,iexp);

else if (strcmp(response,"net") == 0 )

strcpy(command,netscape);

switch (course) {

case 102:{strcat(command,cse102); break;}

case 110:{strcat(command,cse110); break;}

case 130:{strcat(command,cse130); break;}

case 214:{strcat(command,cse214); break;}

case 230:{strcat(command,cse230); break;}

}

if( system(command) != 0)

printf("\n **Error in command**\n");

} /* while */

return 0;

}

More Interfacing: Using Pipes

•popen() - opens a UNIX pipe for input or output

•data from pipe cannot be accessed randomly

•Example:
ifp = popen(______, ______);

read(ifp, ______, ______);
while ( c != 0 ) {

putchar(toupper(c));

read(ifp, ______, ______);

}
pclose(ifp);

More Interfacing:
Environment Variables

int main (int argc, char *argv[], char *env[])

env[] is an array of strings, where each string contains one environment setting:
env[0] = “EDITOR=emacs”
env[1] = “SHELL=/bin/csh”
env[2] = “TERM=vt100”
env[3] = NULL

CGI Environment Variables

•HTTP_USER_AGENT:Name of browser in use.

•QUERY_STRING:Any query information submitted as a result of GET method form.

•CONTENT_LENGTH:Number of bytes of form or query data received.

•REMOTE_ADDR:IP address of browser.

•REMOTE_HOST:Hostname of browser, if known.

•REQUEST_METHOD:Method used in the request (usually GET or POST).

•AUTH_TYPE:The type of authentication used for the request, if any.

Timing C Code

•#include <time.h>

•clock()/CLOCKS_PER_SECOND
returns the number of seconds used by your program in the processor

•time(______)
returns the number of seconds that have elapsed since January 1, 1970

•difftime(______, ______)
returns the difference in two times as a double

Example: Algorithmic Analysis

•Use time functions to measure processing time for various sort or search algorithms.

•Reminders:
- Constructs like loops and function calls add to processing time indirectly
- Time measurements are only approximate and may vary from run to run

Managing C Files

•Each set of functions that are logically related should be stored in a separate C file.

•Compile them together to get one executable.
gcc -o poker ______

•Compile them separately and link later:
gcc -c file1.c
gcc -c file2.c
gcc -c file3.c
gcc -o poker ______

Use a Makefile

•A makefile is a file that keeps track of all source files for a programming project.

•The makefile compiles only updated or modified files each time a compile is requested.

•User specifies compiler options required for each file that is to be compiled.

Example

•Project contains three files:
main.c - has main()
input.c - has data input functions
output.c - has data output functions
info.h - user-defined header file

•Sample makefile (named “makefile”)
program: ______
gcc -o program main.o input.o output.o
main.o: ______
gcc -c main.c
input.o output.o: ______
gcc -c $*.c

Using the makefile

•At the UNIX prompt, you can type the following:
make program
make main
make input
make output
make
make -f ______
// makefile in non-default name

Bigger Example

BASE = /usr/home/esmaili

CC = ______

CFLAGS = -c -p

EFILE = $(BASE)/bin/program

INCLS = -I$(LOC)/include

LOC = /usr/local

OBJS = main.o input.o output.o

$(EFILE): ______
@echo “Linking...”

$(CC) -o $(EFILE) $(OBJS)

$(OBJS): ______
$(CC) $(CFLAGS) $(INCLS) $*.c

More About Make

•To continue a command line or dependency on the next line, end the line with a \

•In order to force a non-updated file to be recompiled, use the UNIX touch command
______

•The make utility compiles a file if any file has a date that is later than all the files that depend on it in the makefile.

Creating Your Own Libraries

•Compile your function(s) in a file:
gcc -c input.c
gcc -c output.c

•Archive the files into a library:
ar ruv our_lib.a ______

•Prepare the library for the loader:
ranlib our_lib.a

•Use your library:
gcc -o program ______

C Compiler Flags

-ccompile only

-o nameput executable output in name

-pgenerate code for profiler

-vverbose mode

-Einvoke preprocessor only

-I dirlook for include files in dir

-Ouse code optimization

-D name=def Insert ______at top of each .c file

RCS

•RCS = Revision Control System

•Allows the programmer to
- store and register multiple revisions of program code
- compare and merge revisions
- create a complete history of revisions for project management and control

•Very important when multiple programmers work on one project

•RCS Basics“Check out” the most recent revision to make changes and then check it in again.
co -l main.c<-- ______
ci ______<-- creates main.c,1.2

•Determine the different between the working version and the most recent revision
rcsdiff main.c

RCS Basics

•Make a directory for RCS within your project directory:
cd $HOME/CSE230/project3
mkdir ______

•“Check in” (register) each file of the project
ci ______
-This creates a file called main.c,v in the RCS subdirectory

RCS Basics (cont’d)

•“Check out” the most recent revision to make changes and then check it in again.
co -l main.c<-- ______
ci ______<-- creates main.c,1.2

•Determine the different between the working version and the most recent revision
rcsdiff main.c

Keywords

•Add keywords to your file and check the file in.

•Upon check-out, the file will have each keyword expanded with useful information.

•Examples:
$Author$$Locker$
$Date$$Log$
$Header$ $Revision$

Keyword Example

(before Check-in)

#include <stdio.h>

/*
$Author$

$Date$

$Log$

$Revision$

*/

int main() {
...

}

Keyword Example

(after Check-in and Check-out)

#include <stdio.h>

/*

$Author: Esmaili $

$Date: 2006/10/1114:34:12 $

$Log: ______$

#Revision 1.1 2006/10/11 14:34:12 Esmaili

#Initial version of program

$Revision: ______$

*/

int main() {
...

}

RCS Command: ci

•Stores the contents of the specified working file into its corresponding RCS file.

•If RCS file doesn’t exist, you are prompted for a description of the file for the log.

•If the RCS file exists, the version number is incremented and you are prompted to enter a description of the changes for the log.

ci Examples

ci ______main.c
Checks in code and returns a read-only copy of latest revision

ci ______main.c
Checks in code and returns a locked copy of latest revision

ci ______main.c
Checks in code as version 2.1

ci ______main.c
Starts another revision level

RCS Command: co

•Retrieves a previously checked-in revision.

•If you are going to edit the file, be sure to lock the file so others cannot change it during your revision.

•If you intend to review or compile a revision (but not change it), you can retrieve a read-only copy of the file.

co Examples

co main.c
Checks out code without a lock (read-only copy)

co ______main.c
Checks out main.c and locks file from outside access

co ______main.c
Checks out latest revision at or before v2.1 and send to standard output

Other RCS commands

ident main.c
Extracts keyword/value symbols from file (useful before compiling)

______main.c
Administrative command to set up default RCS attributes for file

______main.c
Displays log messages, number of lines added/removed, date of last check-in, etc for the file

Debuggers

•Program debuggers allow the programmer to run programs in a protected environment in order to find logic errors.

•Key features:
- breakpoints
- watches
- analysis of core dump

•Some debuggers in UNIX: sdb, dbx, gdb

gdb Basics

•Compiling for gdb:
gcc -g main.c

•Running debugger:
gdb a.out

•Exiting debugger:
quit

•Getting help within debugger:
help

Sample program for gdb

1 #include <stdio.h>

2 int main(void)

3 {

4 int a = ______, *p = &a;

5 static int b[3] = {1, 2, 3}, *q = ______;

6

7 *q++ += ______;

8 (*p)++;

9 printf("*q is %d, *p is %d, b[] is %d %d %d\n",

10 *q, *p, *b, *(b+1), b[2]);

11 return 0;

12 }

sparky: gdb a.out

(gdb) list 5,8

5 static int b[3] = {1, 2, 3}, *q = &b[1];

6

7 *q++ += b[2];

8 (*p)++;

(gdb) break 7

Breakpoint 1 at 0x10698: file main.c, line 7.

(gdb) run

Starting program: /users/home/esmaili/a.out

Breakpoint 1, main () at main.c:7

7 *q++ += b[2];

(gdb) display b

1: b = {1, 2, 3}

(gdb) next

8 (*p)++;

1: b = {______, ______, ______}

(gdb) continue

Continuing.

*q is 3, *p is 6, b[] is ______

Program exited normally.

(gdb) quit

sparky:

Other useful commands

jump - jump to specified line #

step - run until next source line

until - run until code advances to any line past current line

frame - get information about current stack frame

whatis - displays data type of given expression

watch - set a watchpoint on expr
(tells you when expr changes)

1