Archive for November, 2008
How to find my processor type and speed in Linux
Nov 11th
The Linux provides proc interface and provides a file /proc/cpuinfo which contains all the information about the micro processor the current system is using. For example, the file in my system shows:
[neo@techpulp ~]# cat /proc/cpuinfo processor : 0 vendor_id : AuthenticAMD cpu family : 15 model : 72 model name : AMD Turion(tm) 64 X2 stepping : 2 cpu MHz : 800.000 cache size : 512 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level More >
How to move to last used directory in bash
Nov 11th
The “cd -” is used to switch between current and last used directories in bash command line. Following example shows a sample session.
[neo@techpulp dir1]# cd ../dir2 [neo@techpulp dir2]# pwd /home/neo/dir2 [neo@techpulp dir2]# cd - [neo@techpulp dir1]# pwd /home/neo/dir1 [neo@techpulp dir1]# cd - [neo@techpulp dir2]# pwd /home/neo/dir2
How to get the names of all files in a directory in C
Nov 10th
The opendir(), readir() and closedir() library functions supported by C library can be used to get the names of files presented in a directory. The readdir() function should be invoked repeatedly to get all the files until it returns NULL. It returns a pointer to a structure dirent.
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};
The following example program takes a directory name as command line arguments and prints the names More >
Print IP address in dotted-decimal format without converting to string in C
Nov 10th
Few binary operations can be used to print a binary IP address in human-readable dotted-decimal format without using inet_ntoa() function that requires a string buffer. The following example shows how to do it.
[neo@techpulp ~]# cat pip.c
#include <stdio.h>
#define IP_QUAD(ip) (ip)>>24,((ip)&0x00ff0000)>>16,((ip)&0x0000ff00)>>8,((ip)&0x000000ff)
int main(int argc, char *argv[])
{
unsigned long my_ip = 0xa0653b20;
printf("My IP Address (0x%x) is %d.%d.%d.%d\n", my_ip, IP_QUAD(my_ip));
return 0;
}
[neo@techpulp ~]#
In the above program the macro IP_QUAD uses right-shift operator to extract each byte of the integer from MSB to LSB to expand it in to four arguments. This macro can be used pretty easily with “%u.%u.%u.%u” More >
How to write a function that takes variable number of arguments in C
Nov 10th
The C language supports function that takes variable number of arguments using standard argument interface (stdarg.h). The following are the functions used to handle variable number of arguments in a function.
#include <stdarg.h> void va_start(va_list ap, last); type va_arg(va_list ap, type); void va_end(va_list ap); void va_copy(va_list dest, va_list src);
The prototype of a function which takes varying number of arguments will be similar to the following.
int max_of(int count,int num1,...);
It is declared similar to any other function but a “…” is followed by fixed arguments representing the start of variable number of arguments. The varying number of arguments supplied to such function can More >
Parse date and time represented in custom string in C
Nov 10th
The C library function strptime() can be used to convert date and time represented in custom string format to a broken-down representation in struct tm. Here is the declaration of strptime() function.
char *strptime(const char *s, const char *format, struct tm *tm);
The following example parses a date and time string “2008-12-31 21:45:10” and converts first in to broken-down time format struct tm. Then converts it back to string format in another representation.
[neo@techpulp ~]# cat parse_time.c
#include <stdio.h>
#include <time.h>
char *times[] = {
"2008-12-31 21:45:10",
NULL
};
int main(int argc, char *argv[])
{
struct tm tm;
char buf[255];
strptime("2008-12-31 21:45:10", "%Y-%m-%d %H:%M:%S", &tm); More > How to get custom formatted date and time string in C
Nov 10th
The C library function strftime() can be used to print date and time in string format. This function converts a broken-down date and time represented by struct tm according to the given format specification. The other library functions time() and localtime() can be used to get the time in broken-down format. The following example get the current time and converts in to a string format and prints it on the screen.
[neo@techpulp ~]# cat mytime.c
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
time_t t;
struct tm *tm;
char buf[255];
time(&t);
tm = localtime(&t);
strftime(buf, sizeof(buf), "%d %b %Y %H:%M", tm);
puts(buf);
return More > How to display date and time in various formats in Linux
Nov 8th
The date command can be used to print the current system’s date and time. It can also be used to print the date and time in custom format using the various options provided by it.
If a date command is used without any arguments, it prints the date and time in the following format which is default.
[neo@techpulp ~]# date Sat Nov 8 11:10:48 IST 2008 [neo@techpulp ~]#
You can provide a custom format string to date command as command-line argument. The following example prints the date in “dd-mm-yyyy hh:mm” format.
[neo@techpulp ~]# date +"%d-%m-%Y %H:%M" 08-11-2008 11:13 [neo@techpulp ~]#
As shown in the above example, More >
How to set date and time under linux using command line
Nov 8th
The date command can be used to set the date and time of a Linux system. It can also be used to print the current date and time.
You can use date command without any arguments to print the current date and time as shown below.
[neo@techpulp ~]# date Sat Nov 8 10:52:28 PST 2008 [neo@techpulp ~]#
Now let us see how to set the date and time using date command. The date command expect the date and time in [MMDDhhmm[[CC]YY][.ss]] format. Remember you should have super-user privileges to do this. The following example sets the date and time to Dec 31, 2008 10:00.
[root@techpulp ~]# More >


Recent Comments