raspberry pi part 6
TRANSCRIPT
www.techvilla.org.in
The linux environment
Passing arguments to programs
Environment variables
Finding out what the time is
Getting information about the user and the host computer
Causing and configuring log messages
Discovering the limits imposed by the system
www.techvilla.org.in
Program Arguments
When a UNIX program written in C runs, it starts at the function main. For UNIX programs, main is declared as:
int main(int argc, char *argv[])
where argc is a count of the program arguments and argv is an array of character strings representing the arguments themselves.
Whenever the operating system starts a new program, the parameters argc and argv are set up and passed to main.
www.techvilla.org.in
Program Arguments
Here's a program, args.c, that examines its own arguments:
#include <stdio.h>
int main(int argc, char *argv[])
{
int arg;
for(arg = 0; arg < argc; arg++) {
if(argv[arg][0] == '−')
printf("option: %s\n", argv[arg]+1);
else
printf("argument %d: %s\n", arg, argv[arg]);
} exit(0);
}
www.techvilla.org.in
Output
When we run this program, it just prints out its arguments and detects options.
$ ./args −i −lr 'hi there' −f fred.c
argument 0: args
option: i
option: lr
argument 3: hi there
option: f
argument 5: fred.c
www.techvilla.org.in
Environment Variables
These are variables that can be used to control the behavior of
shell scripts and other programs.
$ echo $HOME
/home/Anupam
A C program may gain access to environment
variables using the putenv and getenv functions.
#include <stdlib.h>
char *getenv(const char *name);
int putenv(const char *string);
www.techvilla.org.in
Environment variables.
The getenv function searches the environment for a string with the given name and returns the value associated with that name,null otherwise.
The putenv function takes a string of the form name=value and adds it to the current environment. It will fail and return −1 if it can't extend the environment due to lack of available memory.
www.techvilla.org.in
Time and Date
Times are handled using a defined type, a time_t. This is an integer type large enough to contain dates and times in seconds.
On UNIX and Linux systems using a 32−bit time_t type the time will rollover in the year 2038!
You can find the low level time value by calling the time function, which returns the number of seconds since the start of the epoch. It will also write the returned value to a location pointed to by tloc, if this isn't a null pointer.
#include <time.h>
time_t time(time_t *tloc);
www.techvilla.org.in
User Information
All UNIX programs, with the notable exception of init, are started by other programs or by users.
Each program that UNIX runs is run on behalf of a user and has an associated UID.
UID:-unique user identifier
The UID has its own type uid_t defined in sys/types.h.
UID is a small integer.
Normally, users usually have UID values larger than 100.
www.techvilla.org.in
getuid() and getlogin() functions.
The getuid function returns the UID with which the program is associated. This is usually the UID of the user who started the program.
The getlogin function returns the login name associated with the current user.
#include <sys/types.h>
#include <unistd.h>
uid_t getuid(void);
char *getlogin(void);
www.techvilla.org.in
Host Information
gethostname function: The gethostname function writes the machine's network name into
the string name. This string is assumed to be at least namelen characters long. gethostname returns 0 if successful, −1 otherwise.
#include <unistd.h>
int gethostname(char *name, size_t namelen);
For more detailed information about the host computer:
#include <sys/utsname.h>
int uname(struct utsname *name);
The uname function writes host information into the structure pointed to by the name parameter.
www.techvilla.org.in
LOGGING
Many applications need to record their activities. System programs will very often write messages to the console, or a log file.
On a typical Linux installation, the file /var/log/messages contains all
system messages, /var/log/maillog contains other log messages from the mail system and /var/log/debug may
contain debug messages.
You can check your system's configuration in the file /etc/syslog.conf.
www.techvilla.org.in
Syslog function.
The UNIX specification provides an interface for all programs to produce logging messages, using the syslog function:
#include <syslog.h>
void syslog(int priority, const char *message, arguments...);
The syslog function sends a logging message to the logging facility.
Each message has a priority argument
which is a bitwise OR of a severity level and a facility value.
www.techvilla.org.in
Resources and Limits
Programs running on a UNIX system are subject to resource limitations.
physical limits imposed by hardware (such as memory).
limits imposed by system policies (for example, allowed CPU time).
implementation limits (such as the size of an integer or the maximum number of characters allowed in a filename).
The header file limits.h defines many manifest constants that represent the constraints imposed by the operating system.