Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Gnu-Linux Code Examples

Syslog hello

1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Syslog hello */ #include <stdio.h> #include <unistd.h> #include <syslog.h> int main(void) { openlog("slog", LOG_PID|LOG_CONS, LOG_USER); syslog(LOG_INFO, "A different kind of Hello world ... "); closelog(); return 0; }
syslog() Function in C
The syslog() function sends a message to an implementation-specific logging facility, which loads it in an appropriate system log, writes it to the system console, forwards it to a list of users, or forwards it to the logging facility on another host over the network. The logged message includes a message header and a message body. The message header consists of a facility indicator, a severity indicator, a timestamp, a tag string, and optionally the process ID. The process ID is surrounded by square brackets. The code point values for the square brackets are taken from code page IBM-1047. The value for the left square bracket is 0xAD. The value for the right square bracket is 0xBD. The message body is generated from the message and following arguments in the same manner as if these were arguments to the printf() function, except that occurrences of %m in the format string pointed to by the message argument are replaced by the error message string associated with the current value of errno. A trailing newline character is added if needed.
Syntax for syslog() Function in C
#include <syslog.h> void syslog(int priority, const char *message, ... /* argument */);
priority
Values of the priority argument are formed by ORing together a severity level values and an option facility value. If no facility value is specified, the current default facility value is used. Possible values of severity level include:
LOG_ALERT
A condition that should be corrected immediately, such as a corrupted system database.
LOG_CRIT
Critical conditions, such as hard device errors.
LOG_DEBUG
Messages that contain information normally of use only when debugging a program.
LOG_EMERG
A Panic condition. This is normally broadcast to all processes.
LOG_ERR
Errors.
LOG_INFO
Informational messages.
LOG_NOTICE
Conditions that are not error conditions, but that may require special handling.
LOG_WARNING
Warning messages.
message
specifier the message syslog() returns no values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* send a message to the control log by syslog() function code example */ #include <stdio.h> #include <syslog.h> // #include <stdlib.h> is needed for the resolution of EXIT_SUCCESS #include <stdlib.h> // #include <time.h> is needed for the clock() function and the macro CLOCKS_PER_SEC #include <time.h> // #include <unistd.h> and #include <sys/types.h> are needed for the functions uid_t getuid(void); and uid_t geteuid(void); //getuid() returns the real user ID of the calling process. //geteuid() returns the effective user ID of the calling process. //These functions are always successful. #include <unistd.h> #include <sys/types.h> #define RANGE (100000) int main() { { const clock_t start = clock(); unsigned int i; for (i = 0; i < RANGE; i++){ printf ("Program started by Real User %u (Effective User %u)\n", getuid(), geteuid()); } printf("\n"); const clock_t end = clock(); const float seconds = (float) (end - start) / CLOCKS_PER_SEC; printf("printf: Seconds elapsed %f\n", seconds); } { const clock_t start = clock(); setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("HappyCodings", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); unsigned int i; for (i = 0; i < RANGE; i++){ syslog (LOG_NOTICE, "Program started by Real User %u (Effective User %u)", getuid(), geteuid()); } closelog (); const clock_t end = clock(); const float seconds = (float) (end - start) / CLOCKS_PER_SEC; printf("syslog: Seconds elapsed %f\n", seconds); } return EXIT_SUCCESS; }
#include Directive in C
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include: • Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. • User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor.
Syntax for #include Directive in C
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. */ // C program to illustrate file inclusion // <> used to import system header file #include <stdio.h> // " " used to import user-defined file #include "process.h" // main function int main() { // add function defined in process.h add(10, 20); // mult function defined in process.h multiply(10, 20); // printf defined in stdio.h printf("Process completed"); return 0; }
main() Function in C
In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed. Its first function or entry point of the program from where program start executed, program's execution starts from the main. So main is an important function in c , c++ programming language.
Syntax for main() Function in C
void main() { ......... // codes start from here ......... }
void
is a keyword in C language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value. In place of void we can also use int return type of main() function, at that time main() return integer type value.
main
is a name of function which is predefined function in C library. • An operating system always calls the main() function when a programmers or users execute their programming code. • It is responsible for starting and ends of the program. • It is a universally accepted keyword in programming language and cannot change its meaning and name. • A main() function is a user-defined function in C that means we can pass parameters to the main() function according to the requirement of a program. • A main() function is used to invoke the programming code at the run time, not at the compile time of a program. • A main() function is followed by opening and closing parenthesis brackets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* basic c program by main() function example */ #include <stdio.h> #include <conio.h> main() { printf (" It is a main() function "); int fun2(); // jump to void fun1() function printf ("\n Finally exit from the main() function. "); } void fun1() { printf (" It is a second function. "); printf (" Exit from the void fun1() function. "); } int fun2() { void fun1(); // jump to the int fun1() function printf (" It is a third function. "); printf (" Exit from the int fun2() function. "); return 0; }
openlog() Function in C
Open the system control log. The openlog() function optionally opens a connection to the logging facility, and sets process attributes that affect subsequent calls to the syslog() function. openlog() opens or reopens a connection to Syslog in preparation for submitting messages. openlog() function opens the syslogd system log and provides for more specialized processing of the messages sent by syslog() and vsyslog().
Syntax for openlog() Function in C
#include <syslog.h> void openlog(const char *ident, int logopt, int facility);
ident
A string that you want to prepend to every message. The argument ident is a string that is prefixed to every message.
logopt
A bit field specifying logging options. logopt is a bit field indicating logging options. Current values of logopt are:
LOG_CONS
Write messages to the system console if they cannot be sent to the logging facility. This option is safe to use in processes that have no controlling terminal, since the syslog() function forks before opening the console.
LOG_NDELAY
Open the connection to the logging facility immediately. Normally the open is delayed until the first message is logged. This is useful for programs that need to manage the order in which file descriptors are allocated..
LOG_NOWAIT
Do not wait for child processes that have been forked to log messages onto the console. This option should be used by processes that enable notification of child termination using SIGCHLD, since the syslog() function may otherwise block waiting for a child whose exit status has already been collected.
LOG_ODELAY
Delay open until syslog() is called.
LOG_PID
Log the processID with each message. This is useful for identifying specific processes. In the message header, the processID is surrounded by square brackets. The code point values for the square brackets are taken from code page IBM-1047. The value for the left square bracket is 0xAD. The value for the right square bracket is 0xBD.
facility
encodes a default facility to be assigned to all messages that do not have an explicit facility already encoded. The initial default facility is as follows:
LOG_AUTH
Authorization system.
LOG_AUTHPRIV
Same as LOG_AUTH, but logged to a file readable only by selected individuals.
LOG_CRON
Clock daemon.
LOG_DAEMON
System daemons (such as routed) that aren't explicitly provided for by other facilities.
LOG_FTP
File transfer protocol daemon.
LOG_KERN
Messages generated by the kernel. These can't be generated by any user processes.
LOG_LPR
Line printer spooling system.
LOG_MAIL
Mail system.
LOG_NEWS
Network news system.
LOG_SYSLOG
Messages generated internally by syslogd.
LOG_USER
Messages generated by random user processes. This is the default facility identifier if none is specified.
LOG_UUCP
The UUCP system.
LOG_LOCAL0 through LOG_LOCAL7
Reserved for local use. openlog() returns no values. No errors are defined.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/* open the syslogd system log and provides for more specialized processing of the messages sent by syslog() and vsyslog() by openlog() function code example */ #include <stdio.h> #include <syslog.h> // #include <stdlib.h> is needed for the resolution of EXIT_SUCCESS #include <stdlib.h> // #include <time.h> is needed for the clock() function and the macro CLOCKS_PER_SEC #include <time.h> // #include <unistd.h> and #include <sys/types.h> are needed for the functions uid_t getuid(void); and uid_t geteuid(void); //getuid() returns the real user ID of the calling process. //geteuid() returns the effective user ID of the calling process. //These functions are always successful. #include <unistd.h> #include <sys/types.h> #define RANGE (100000) int main() { { const clock_t start = clock(); unsigned int i; for (i = 0; i < RANGE; i++){ printf ("Program started by Real User %u (Effective User %u)\n", getuid(), geteuid()); } printf("\n"); const clock_t end = clock(); const float seconds = (float) (end - start) / CLOCKS_PER_SEC; printf("printf: Seconds elapsed %f\n", seconds); } { const clock_t start = clock(); setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("HappyCodings", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); unsigned int i; for (i = 0; i < RANGE; i++){ syslog (LOG_NOTICE, "Program started by Real User %u (Effective User %u)", getuid(), geteuid()); } closelog (); const clock_t end = clock(); const float seconds = (float) (end - start) / CLOCKS_PER_SEC; printf("syslog: Seconds elapsed %f\n", seconds); } return EXIT_SUCCESS; }
closelog() Function in C
The closelog() function shall close any open file descriptors allocated by previous calls to openlog() or syslog(). closelog closes the current Syslog connection, if there is one. This includes closing the /dev/log socket, if it is open. closelog also sets the identification string for Syslog messages back to the default, if openlog was called with a non-NULL argument to ident. The default identification string is the program name taken from argv[0]. If you are writing shared library code that uses openlog to generate custom syslog output, you should use closelog to drop the GNU C Library's internal reference to the ident pointer when you are done. closelog does not flush any buffers. You do not have to call closelog before re-opening a Syslog connection with openlog. Syslog connections are automatically closed on exec or exit.
Syntax for closelog() Function in C
#include <syslog.h> void closelog( void );
closelog() neither accepts an input nor returns a result. The system control log is closed for this process. No errors are defined.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* shall close any open file descriptors allocated by previous calls to openlog() or syslog() by closelog() function code example */ #define _GNU_SOURCE /* for basename( ) in <string.h> */ #include <syslog.h> #include <string.h> int count = 0; char *host = "some-machine"; int main(int argc, char *argv[]) { openlog(basename(argv[0]), LOG_PID, LOG_LOCAL3); syslog(LOG_WARNING, "%d connection attempts from %s", count, host); syslog(LOG_AUTHPRIV|LOG_ERR, "intruder alert!"); syslog(LOG_ERR, "can't open configuration file: %m"); closelog( ); return(0); }


Get current Display Mode. Character with chosen attribute. Scroll the window up. Scroll the window down. Read the character & it's attribute. Positioning the Cursor. Selecting...
C program code to 2d example insertion sort [linked list]. Insert a node directly at the right place in the linked list. Insert some numbers into the linked list. Print the list. Free the list
C programming code example for Number Conversions: Binary to decimal, decimal to binary, decimal to octal, decimal to hex, octal to decimal, octal to binary, binary to octal,
Check if string is a 'Palindrome' without using the "Built-In function". "Take a string" as input & store it in the array 'string[]'. Then store the same string into the another array 'reverse_s'