16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C Libraries<br />

}<br />

switch (pid=fork()){<br />

case 0: /* child exec's shell */<br />

execl("/bin/sh","sh","-c",command,0);<br />

/* fall through if exec fails */<br />

case -1:<br />

/* could not fork, print error message*/<br />

perror(myname);<br />

exit(1);<br />

default: /* parent waits for child to finish */<br />

while ((wval=wait(&status)) != pid)<br />

if (wval == -1) return -1;<br />

}<br />

return status;<br />

Buffer control<br />

#include <br />

char outbuf[BUFSIZ];<br />

main()<br />

{<br />

int c; /* for no buffering */<br />

}<br />

setbuf(stdout, outbuf); /*set outbuf to NULL*/<br />

while ((c=fgetc(stdin)) != EOF)<br />

fputc(c, stdout);<br />

The C Preprocessor<br />

#include<br />

#include "filename"<br />

or<br />

#include <br />

for including files of text into a program<br />

from current directory<br />

from directory "/usr/include"<br />

cc -I../include prog.c to change default directory redefine for defining constants<br />

#define NUMLINES 60<br />

#define<br />

#define min(a,b)<br />

for defining powerful in-line macros<br />

((a) > (b) ? (a) : (b))<br />

#if, #ifdef, #ifndef & #undef for managing conditional compilation<br />

#define DEBUG<br />

#ifdef DEBUG<br />

printf("MyProg Version 1.0 (debug)\n"),<br />

#else<br />

printf("Myprog Version 1.0 (production)\n");<br />

#endif<br />

#undef __TURBOC__<br />

#ifndef __TURBOC__<br />

system("grep name * > names");<br />

#endif<br />

#if COLUMNS > 80<br />

/* code for wide printers */<br />

#else<br />

/* code for narrow printers */<br />

#endif<br />

cc -DLINELENGTH=80 prog.c to define constants<br />

74

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!