Redirection

• Command > file More Commands „ Redirects command output to a file Unix User Settings „ If file already created, then overwrites the file Makefile ¾ >> use to append „ E.g. ls > dump.txt GDB More Preprocessor • Command < file Extern keyword in C „ Command is run and its input is taken from file (instead of stdin) „ See example: redirection.c CIT 593 „ a.out < inputfile > outputfile

CIT593 2

grep Shell Variables

• Search text within a file • Predefined Variables examples „ HOME ¾ Contains user home directory path • grep searchterm filename/directorypath „ PATH ¾ Contains all the search paths for your shell „ Can use regular expression terms such (*), (.), (\) „ SHELL • E.g. grep world* filename ¾ Tells which is your default shell • In order use these variables you need to put $ sign in „ Prints lines containing all words starting with world front of the variable name. E.g. in file „ echo $HOME -> prints your home directory path (even if you • E.g . grep world** are not in yyy)our home directory) ¾ echo is shell command to print „ Prints lines containing all words starting with world ¾ If you do not put the $ sign then it will just print “HOME” in files within the directory – \ before $HOME will just print home (so \ removes special meaning from a single character)

CIT593 3 CIT593 4

1 .cshrc or .bashsrc file Customizing .cshrc Example

• Are files for customizing shells # if you want to add directories to your search path, add them before # the "." in the line below. For example, „ .cshsrc for csh or tcsh shell # # set path = ( $path ~ ~/bin ~/scripts . ) „ .bashrc for shell

set path = ($path /usr/java/jdk1.5.0/bin .) • These files contains commands, variable # to be automatically logged out after 30min of inactivity: definitions and aliases used any time the type # set autologout=(30) shell is run. unset autologout

„ When one logs in, for e. g. C shell starts by reading #####DIANA######## the .cshrc file, and sets up any variables and if ($?prompt) set prompt = "palsetia%c04:“ aliases alias c clear alias p pine

CIT593 5 CIT593 6

After editing .cshrc/.bashrc Core Dump

• At the prompt type: source .cshrc • A core dump is the name often given to the recorded state of the working memory of a „ This will allow the changes to take place effectively at a specific time „ generally when the program that has terminated abnormally „ Otherwise, the changes will take place next time you login ¾ Because .cshrc script is executed once i.e. • Is a simply a binary file with the sequence of bytes or executed when user logs in words containing the memory image of a particular process.

• Name of file is “core” without any extensions and the file appears in the current working directory

CIT593 7 CIT593 8

2 Core Dump (contd..) Core Dump (contd..)

Abnormal Program Termination Issue with core file „ Often buffer overflows, where a allocates too „ Core file is often very large and takes up a lot of disk space little memory for incoming or computed data „ One can avoid exceeding the disk quota with a large core file „ Access to null pointers, a common coding error when an by limiting the core dump size unassigned memory reference variable is accessed

Uses of core dump csh/tcsh shell „ A runtime error such as “” does not „ limit coredumpsize 0 describe as to where does the problem lie „ limit coredumpsize 10240 # Limit core dumps to „ Core dump can help reveal where the fault is occurring 10 megs ¾ Programmer can try to interpret the file but needs enough knowledge of structure of the programs memory use „ unlimit coredumpsize # Allow unlimited-sized ¾ Special Programs such as GDB or dump coredumps analyzers can relieve the programmer from reading core file which is in hex bytes

CIT593 9 CIT593 10

make Example: makefile (with targets) make is a compilation tool #is a comment =gcc „ make compiles, assembles, and links your source LIBS=-lm #needed to compile Math.h files OPTIONS=-Wall „ Think of it as the lcs file in PennSim simulator NAME1= hypo tenuse command „ The file you write is called “makefile” without any NAME2=main file extensions NAME3= hypo ¾ makefile is read by program make all: „ Then at prompt type : make targetname $(COMPILER) $(LIBS) $(OPTIONS) $(NAME1).c $(NAME2).c -o $(NAME3)

# To remove or .o files generated # Type: make clean clean: rm -f $(NAME3)

CIT593 11 CIT593 12

3 makefile: Rule Syntax makefile: Rule Syntax (contd..)

A rule tells make two things: • The targets are named sections (like a LABEL) to tell make what to execute „ when the targets are out of date „ There can be more than one target „ how to update them when necessary. „ When use make, specify the target you want ¾ If targetname is not provided, then the make executes In general, a rule looks like this: the first target in the file targets : prerequisites „ The command lines start with a tab character (‘\t’) „ The first command may appear on the line after the <‘\t’>command prerequisites, with a tab character, or may appear on the same line, with a semicolon „ You may split a long line by inserting a backslash followed ... or like this: by a newline ¾ But this is not required, as make places no limit on the targets : prerequisites ; command command ... length of a line in a makefile

CIT593 13 CIT593 14

Makefile (prerequites and commands) GDB Debugger

#is a comment GDB stands for GNU Debugger COMPILER=gcc LIBS=-lm #needed to compile Math.h „ GDB is a Unix terminal debugger, which can OPTIONS=-Wall prerequisites execute your program in a sandbox and allow you NAME1=hypotenuse to analyze it during runtime NAME2=main NAME3= hypo „ Most helpful to catch segmentation fault error ¾ all: hypotenuse.c main.c hypotenuse.h Can pin-point exactly where in your code it $(COMPILER) $(LIBS) $(OPTIONS) $(NAME1).c $(NAME2).c -o $(NAME3) happening „ # To remove executable or .o files generated Follow the GDB tutorial on how to use GDB tool # Type: make clean clean: If any of the prerequisites are rm -f $(NAME3) missing make will not execute the target command

CIT593 15 CIT593 16

4 More Preprocessor: #ifdef Example

#ifdef MACRO #define DEBUG //#define DEBUG controlled text int main() { int main() { #endif int x; int x; .. .. #ifdef DEBUG #ifdef DEBUG The block is called conditional group printf(“The value of x printf(“The value of x The controlled text will be included in the %\n”, x); %d\n”, x); output of the preprocessor if and only if #endif #endif MACRO is defined …. …. } „ Conditional succeeds if MACRO is designed. Print statement is Print statement is not executed as DEBUG } executed as DEBUG is defined is not defined CIT593 17 CIT593 18

More Preprocessor: #ifndef Example

#ifndef MACRO #ifndef EXTENDED controlled text #define MAX_LEN 50 #endif #else #define MAX_LEN 75 Checks whether a macro is define or not #endif If macro is not define then the lines of code immediately following the condition are If EXTENDED is not defined then MAX_ LEN is passed on the compiler defined to be 50 else is defined to be 75

CIT593 19 CIT593 20

5 Another Example with ifndef Extern in C

„ Note that scope of global variable is limited to the file it is declared in. //In packets_main.c

#include “packets.h” „ The keyword “extern” in C indicates the existence of global variable or function that is defined in another file

#ifndef TEST _MAIN „ Ifil1In file1.c int main(){ int x = 1; //global (declared outside of all functions) … „ In file2.c } extern int x; //tells the compiler to look for definition of x elsewhere #endif int main(){ „ Allows for testing with different main than the users main printf(“%d\n”,x); x++; „ E. g. packets. h will define the macro TEST_ MAIN printf(“%d\n”,x); „ Allows other implemented functions in packets_main.c to be still } accessed by another program with giving compiler errors of multiple definitions of mains. „ If you forget to put extern it will still work as the linker will resolve the reference across multiple files, however the intent then is unclear and can lead to error prone in case of typos.

CIT593 21 CIT593 22

6