SlideShare a Scribd company logo
UNIX and “C” For more info check out the Unix man pages (i.e., do “man -k topic-you-wish-to-search”)  -or- Unix in a Nutshell -and C Language Reference Manual (K&R). This document gives a starter in how to program in C.  There’s very little here that’s UNIX specific – all the functions described here will run on any operating system.
Remember printf? Sends text to standard output (stdout). void main() { printf(“Hello World \n”); } printf is a routine that is part of the C standard library (i.e., libc) libc is linked into your program by default. Other libs (i.e., libm -- math -- is not)
What about Input? Scanf -- reads text from standard input (i.e., stdin) void main() { char buffer[32]; int i; scanf(“%s %d”, buffer, &i); } /* Note, we use buffer and not &buffer */
What are stdin, stdout, stderr? File descriptors...or more precisely a pointer to type FILE. These FILE descriptors are setup when your program is run. So, then what about regular user files...
File I/O Operations fopen -- opens a file fclose -- close a file fprintf -- “printf” to a file. fscanf -- read input from a file. ...and many other routines..
fopen #include<stdio.h> void main() { FILE *myfile; myfile = fopen( “myfile.txt”, “w”); } 2nd arg is mode: w -- create/truncate file for writing w+ -- create/truncate for writing and reading r -- open for reading r+ -- open for reading and writing
fclose #include<stdio.h> #include<errno.h> void main() { FILE *myfile; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fclose( myfile ); /* could check for error here, but usually not needed */ }
fscanf #include<stdio.h> #include<errno.h> void main() { FILE *myfile; int i, j, k; char buffer[80]; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fscanf( myfile, “%d %d %d %s”, &i, &j, &k, buffer); fclose( myfile ); /* could check for error here, but usually not needed */ }
fprintf #include<stdio.h> #include<errno.h> void main() { FILE *myfile; int i, j, k; char buffer[80]; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fscanf( myfile, “%d %d %d %s”, &i, &j, &k, buffer); fprintf( myfile, “%d %d %d %s, i, j, k, buffer ); fclose( myfile ); /* could check for error here, but usually not needed */ }
Pipes They to are realized as a file descriptor which links either ouput to input or input to output. recall doing shell commands of the form: > ls -al | grep “Jan  1” | more “ |” is implemented as a libc call to “popen” Ex: let’s send e-mail from a C program... First, how do you “sendmail”???
To send mail use “sendmail” sendmail:  is a unix command that allow the transmission and delivery of mail.  Note – everything so far in this document has applied to “C” in general – but sendmail is a UNIX specific command. extremely complicated program and it is full of security holes (i.e., never run sendmail on your unix machine). To use sendmail: > /usr/lib/sendmail -t To: jbreecher@clarku.edu From: bogus Subject: test This is a test!!. . /* NOTE: the “.\n” here is needed to terminate */ >
Putting it all together with pipes #include<stdio.h> #include<errno.h> void main() { FILE *mailpipe; if( NULL == (mailpipe = popen( “usr/lib/sendmail -t”, “w”))) { perror(“popen failed in main”); exit(-1); } fprintf( mailpipe, “To: chrisc@cs.rpi.edu \n” ); fprintf( mailpipe, “From: bogus \n” ); fprintf( mailpipe, “Subject: test \n” ); fprintf( mailpipe, “This is a test. \n” ); fprintf( mailpipe, “.\n” ); pclose( mailpipe ); /* could check for error here, but usually not needed */ }
Other useful commands... fgets( char *buffer, int maxsize, FILE *f); retrieves a whole line at a time up to newline or EOF. sscanf - does scanf on a string buffer. sprintf - does printf into a string buffer. You will use these in assignment 1... And speaking that, let’s cover that now!!

More Related Content

PPT
Talk Unix Shell Script 1
PPT
Talk Unix Shell Script
PPT
Unix Basics
PPT
Airlover 20030324 1
PPT
Using Unix
PDF
Unix Commands
PPTX
Unix shell scripting basics
PDF
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Talk Unix Shell Script 1
Talk Unix Shell Script
Unix Basics
Airlover 20030324 1
Using Unix
Unix Commands
Unix shell scripting basics
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...

What's hot (20)

PDF
Introduction to shell scripting
PDF
Shell scripting
PPT
Shell Scripts
PPT
Chap06
PPTX
Bash Shell Scripting
PPT
Unix Shell Scripting Basics
PDF
Quick start bash script
PPT
Shell Scripting
PPT
Unix And Shell Scripting
PPT
Unix Shell Scripting Basics
RTF
Unix lab manual
PDF
Unix 1st sem lab programs a - VTU Karnataka
PPTX
Unix - Shell Scripts
PPTX
Unix shell scripting
PDF
Shell scripting
PPTX
Bash Shell Scripting
PPTX
Unix shell scripts
PPTX
Easiest way to start with Shell scripting
PPT
Linux shell scripting
PPTX
Pipes and filters
Introduction to shell scripting
Shell scripting
Shell Scripts
Chap06
Bash Shell Scripting
Unix Shell Scripting Basics
Quick start bash script
Shell Scripting
Unix And Shell Scripting
Unix Shell Scripting Basics
Unix lab manual
Unix 1st sem lab programs a - VTU Karnataka
Unix - Shell Scripts
Unix shell scripting
Shell scripting
Bash Shell Scripting
Unix shell scripts
Easiest way to start with Shell scripting
Linux shell scripting
Pipes and filters
Ad

Similar to Unix And C (20)

PPTX
File handling in c
PPT
Unit5 C
PPT
7.0 files and c input
PDF
C for Java programmers (part 1)
PPTX
File Handling in C Programming for Beginners
PPT
file.ppt
PPT
C-Programming Chapter 5 File-handling-C.ppt
PPT
File handling-dutt
PPTX
Lecture 04 Programming C for Beginners 001
DOCX
Satz1
PPT
CInputOutput.ppt
PPT
Mesics lecture files in 'c'
PPTX
C-Programming File-handling-C.pptx
PPTX
C-Programming File-handling-C.pptx
PPTX
PPS PPT 2.pptx
DOCX
Linux 系統程式--第一章 i/o 函式
PPT
Csdfsadf
PPT
PDF
Chapter 13.1.10
File handling in c
Unit5 C
7.0 files and c input
C for Java programmers (part 1)
File Handling in C Programming for Beginners
file.ppt
C-Programming Chapter 5 File-handling-C.ppt
File handling-dutt
Lecture 04 Programming C for Beginners 001
Satz1
CInputOutput.ppt
Mesics lecture files in 'c'
C-Programming File-handling-C.pptx
C-Programming File-handling-C.pptx
PPS PPT 2.pptx
Linux 系統程式--第一章 i/o 函式
Csdfsadf
Chapter 13.1.10
Ad

More from Dr.Ravi (18)

PDF
Corporate Overview
PDF
Excel For The Ceo
PDF
Project Specs Pf
PDF
Pf Day5
PDF
Assignments Programming Fundamentals
PDF
Hdd Chssc
PDF
Chssc Day3
PDF
Chssc Day1
PDF
Pf Day3
PDF
Ldd Pf
PDF
Chssc Assignments
PDF
Chssc Day4
PDF
Chssc Day2
PPT
Unix Lec2
PDF
Unix Book
PPT
Unix Basics 04sp
PDF
Wicked Cool Shell Scripts
PPT
SAP INTRO
Corporate Overview
Excel For The Ceo
Project Specs Pf
Pf Day5
Assignments Programming Fundamentals
Hdd Chssc
Chssc Day3
Chssc Day1
Pf Day3
Ldd Pf
Chssc Assignments
Chssc Day4
Chssc Day2
Unix Lec2
Unix Book
Unix Basics 04sp
Wicked Cool Shell Scripts
SAP INTRO

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Spectroscopy.pptx food analysis technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Spectral efficient network and resource selection model in 5G networks
Spectroscopy.pptx food analysis technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Unlocking AI with Model Context Protocol (MCP)
The Rise and Fall of 3GPP – Time for a Sabbatical?
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Electronic commerce courselecture one. Pdf
sap open course for s4hana steps from ECC to s4
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

Unix And C

  • 1. UNIX and “C” For more info check out the Unix man pages (i.e., do “man -k topic-you-wish-to-search”) -or- Unix in a Nutshell -and C Language Reference Manual (K&R). This document gives a starter in how to program in C. There’s very little here that’s UNIX specific – all the functions described here will run on any operating system.
  • 2. Remember printf? Sends text to standard output (stdout). void main() { printf(“Hello World \n”); } printf is a routine that is part of the C standard library (i.e., libc) libc is linked into your program by default. Other libs (i.e., libm -- math -- is not)
  • 3. What about Input? Scanf -- reads text from standard input (i.e., stdin) void main() { char buffer[32]; int i; scanf(“%s %d”, buffer, &i); } /* Note, we use buffer and not &buffer */
  • 4. What are stdin, stdout, stderr? File descriptors...or more precisely a pointer to type FILE. These FILE descriptors are setup when your program is run. So, then what about regular user files...
  • 5. File I/O Operations fopen -- opens a file fclose -- close a file fprintf -- “printf” to a file. fscanf -- read input from a file. ...and many other routines..
  • 6. fopen #include<stdio.h> void main() { FILE *myfile; myfile = fopen( “myfile.txt”, “w”); } 2nd arg is mode: w -- create/truncate file for writing w+ -- create/truncate for writing and reading r -- open for reading r+ -- open for reading and writing
  • 7. fclose #include<stdio.h> #include<errno.h> void main() { FILE *myfile; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fclose( myfile ); /* could check for error here, but usually not needed */ }
  • 8. fscanf #include<stdio.h> #include<errno.h> void main() { FILE *myfile; int i, j, k; char buffer[80]; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fscanf( myfile, “%d %d %d %s”, &i, &j, &k, buffer); fclose( myfile ); /* could check for error here, but usually not needed */ }
  • 9. fprintf #include<stdio.h> #include<errno.h> void main() { FILE *myfile; int i, j, k; char buffer[80]; if( NULL == (myfile = fopen( “myfile.txt”, “w”))) { perror(“fopen failed in main”); exit(-1); } fscanf( myfile, “%d %d %d %s”, &i, &j, &k, buffer); fprintf( myfile, “%d %d %d %s, i, j, k, buffer ); fclose( myfile ); /* could check for error here, but usually not needed */ }
  • 10. Pipes They to are realized as a file descriptor which links either ouput to input or input to output. recall doing shell commands of the form: > ls -al | grep “Jan 1” | more “ |” is implemented as a libc call to “popen” Ex: let’s send e-mail from a C program... First, how do you “sendmail”???
  • 11. To send mail use “sendmail” sendmail: is a unix command that allow the transmission and delivery of mail. Note – everything so far in this document has applied to “C” in general – but sendmail is a UNIX specific command. extremely complicated program and it is full of security holes (i.e., never run sendmail on your unix machine). To use sendmail: > /usr/lib/sendmail -t To: [email protected] From: bogus Subject: test This is a test!!. . /* NOTE: the “.\n” here is needed to terminate */ >
  • 12. Putting it all together with pipes #include<stdio.h> #include<errno.h> void main() { FILE *mailpipe; if( NULL == (mailpipe = popen( “usr/lib/sendmail -t”, “w”))) { perror(“popen failed in main”); exit(-1); } fprintf( mailpipe, “To: [email protected] \n” ); fprintf( mailpipe, “From: bogus \n” ); fprintf( mailpipe, “Subject: test \n” ); fprintf( mailpipe, “This is a test. \n” ); fprintf( mailpipe, “.\n” ); pclose( mailpipe ); /* could check for error here, but usually not needed */ }
  • 13. Other useful commands... fgets( char *buffer, int maxsize, FILE *f); retrieves a whole line at a time up to newline or EOF. sscanf - does scanf on a string buffer. sprintf - does printf into a string buffer. You will use these in assignment 1... And speaking that, let’s cover that now!!