Wednesday, July 30, 2008

Date & Time Management System using C

Introduction
It’s my first post here. I will discuss on Date & Time Management Functions of C. Though if someone is only interested about getting the time and date, then it can be done by some trivial function but the goal of this detail discussion is understanding the bios as well as decreasing the abstraction. I had to sum up this data when I was working on an algorithm. Hopefully some of you will find this useful.
Here you will find explanations of each run-time library functions that supports the capture, conversion and display of time, from which all date and date measures are derived. The examples provided here demonstrate the proper use of these functions and detail the relationship that exist between the global variable daylight, timezone and tzname[], and environment string TZ.
Computer Time
All system date and time measures available under DOS are derived from either the 8253 timer (oscillator) chip, or a real-time (battery-supported) if one is installed and operating. The 8253 timer oscillates at 1,193,180 Hz with a built-in divisor of 65,536 and emits a BIOS interrupt 0x08 18.2 times per second, or about every 0.055 seconds.
Universal Date & Time
To maintain compatibility with UNIX, several DOS run-time library functions and data structures are available that permit the underlying DOS clock device driver measures of time to support universal (GMT) time as well as standard and daylight saving time.
The non-Standard DOS-specific global variables (timezone, daylight and tzname[]) and environment string (TZ=) are used to implement universal GMT based time and to provide a complement of UNIX-compatible functions with within the DOS. These global variables assume default values based on an assumed TZ=string setting of TZ=PST8PDTwhere PST : A three character standard time zone abbreviation –PST – Pacific Standard Time MST – Mountain Standard Time CST – Central Standard Time EST – Eastern Standard Time 8 : The number of whole hours, measured from the prime meridian; a positive value of 0 to 12 if west longitude, and 0 to –12 if east longitude.PDT : A three-character abbreviation used to designate dayligyt saving time is in effect for this time zone, it stands for Pacific Daylight Time it is also can be of 4 types PDT, MDT, CDT, EDT
timezone : the number of seconds difference between the local time zone and the GMT is as follows : PST : 8 hrsMST : 7 hrsCST : 6 hrsEST : 5 hrsdaylight : A flag that is set to zero if daylight saving time is not in effect otherwise 1tzname[] : Contains the three-character daylight saving time descriptive string or a null stringfunction tzset() sets these values
Terminology & Data Types
TicksWhenever we speak of ticks, we are referring to the count of interrupts that are generated by the 8285 timer chip or real-time clock, and accumulated by the BIOS. For example if you have a tick count of 66,733, using the BIOS constant, the measure of time would be calculated as follows
1-hour : 66733-(1*((1,193,180/65,536)*60*60))=1190
Standard C defines two data-object types, clock_t and time_t, for representing data in and one data structure, tm.
clock_tThe standard C clock_t type is defined as typedef long clock_t;and represents the elapsed process time in seconds divided by the object-like macro CLK_TCK or CLOCKS_PER_SEC which is set to 1000 for DOS in
time_t It is defined as typedef long time_t;A time_t type represents the number of elapsed seconds since midnight relative to prime meridian (GMT) January 1, 1970, for compatibility with UNIX, whereas all DOS dates are originated at January 1, 1980.
struct tmThis is a special structure used in date time management functions. Template of this structure :
Code: struct tm { int tm_sec; //seconds after the minute int tm_min; //minutes after the hour int tm_hour; //hours since midnight int tm_mday; //day of the month int tm_mon; //months since January int tm_year; //years since 1900 int tm_wday; //days since Sunday int tm_yday; //days since January 1 int tm_isdst; //daylight saving time flag};Microsoft C defines the dosdate_t and dostime_t data structure in , the timeb data structure in .
The structure template is given by :
Code: struct dosdate_t { unsigned char hour; unsigned char minute; unsigned char second; unsigned char hsecond;};Code: struct dostime_t{ unsigned char day; unsigned char month; unsigned int year; unsigned char dayofweek;};Code: struct timeb { time_t time; //GMT in seconds unsigned short millitm; //millisecond fraction short timezone; //GMT local time in minute short dstflag; //=1 if DST in effect};Run-Time Library Functions For Date & Time Management
Here is a list of library functions that are used for Date and Time Management in C :asctime() biostime() _bios_timeofday() ctime() difftime()_dos_getdate() _dos_gettime() _dos_settime() _dos_setdate()dostounix() ftime() getdate() gettime() gmtime()localtime() mktime() stime() _strdate() _strtime()strftime() time() TDate(class) TTime(class) tzset()unixtodos()

Here are the example of few - asctime() - convert the standard tm structure to a string
Spicification #includechar *asctime(const struct tm *pointer)Argumentspointer : pointer to the Standard C date and time structure, tmReturn Value A pointer to a static character string that is always 26 characters and in the formatSun Jul 06 09:30:50 1980\n\0Example Code: C
#include#include#includeint main(){ struct tm t; char str[80]; /* Sample loading of tm structure */ t.tm_sec=1; //seconds t.tm_min=30; //minutes t.tm_hour=9; //hour t.tm_mday=22; //day of the month t.tm_mon=11; //month t.tm_year=56; //year does not include century t.tm_wday=4; //day of the week t.tm_yday=0; //does not show in asctime t.tm_isdst=0; //is Daylight SavTime, does not show in asctime /* converts structure to null terminates string */ strcpy(str, asctime(&t)); printf("%s\n",str);
return 0;}biostime() - Reads or set the BIOS timer (interrupt 0x1A)
Specification#includelong biostime (int cmd, long newtime);Arguments & Return ValueIf cmd equals 0, biostime returns the current value of the timer. If cmd=1, the timer is set to the long value in newtime.Example Code: c
#include#include#include#include
int main(void){ long bios_time; clrscr(); printf("The number of clock ticks since midnight : \n"); printf("The number of seconds since midnight : \n"); printf("The number of minutes since midnight : \n"); printf("The number of hours since midnight : \n"); printf("Press any key to stop...."); while(!kbhit()) { bios_time=biostime(0,0L);
gotoxy(50,1); printf("%lu",bios_time);
gotoxy(50,2); printf("%.4f",bios_time/_BIOS_CLK_TCK);
gotoxy(50,3); printf("%.4f",bios_time/_BIOS_CLK_TCK/60);
gotoxy(50,4); printf("%.4f",bios_time/_BIOS_CLK_TCK/3600);
} return 0;}clock() - Determines processor time
Specification#includeclock_t clock(void);
Return Value Clock can be used to determine the time interval between two events. On success clock returns the processor time elapsed since the beginning of the program invocation. On error clock returns –1.Example
Code: c
#include#include#include#include
void main(void){ long int bios_start,bios_end; float elap_bios,elap_clock; clock_t clock_start,clock_end;
_bios_timeofday(_TIME_GETCLOCK,&bios_start); clock_start=clock(); printf("\n_bios = %ld\t\tclcok = %ld",bios_start,clock_start); clock_end=clock(); _bios_timeofday(_TIME_GETCLOCK,&bios_end); printf("\n_bios = %ld\t\tclock = %ld",bios_end,clock_end);
elap_bios=(bios_end-bios_start)/18.2F; elap_clock=(clock_end-clock_start)/(float) CLK_TCK; printf("\nelap_bios = %f sec\nelap_clock = %f sec",elap_bios,elap_clock); exit(0);}_bios_timeofday() - Get/set the system timer clock ticks
Specification #includeunsigned _bios_timeofday(unsigned service, long *ticks)Argumentsservice : use either the _TIME_GETCLOCK or _TIME_SETCLOCK object like macros found in ticks : pointer to a long that is used either to return the clock tick count or to serve as the value to set the clock tick count.Return ValueWhen the _TIME_GETCLOCK service is used, a value of 0 is returned if midnight has been passed since the last get or set time was performed; otherwise a value pf 1 is returned.When _TIME_SETCLOCK service is used, there is no return value.Example See previous example
ctime() - converts date and time to a string
Specification#includechar *ctime(const time_t *timer);Argumentstimer : pointer to a time value in time_t format that was acquired using function time().Return Valuectime returns a pointer to the character string containing the date and time.ExampleCode: c
#include#include
int main(void){ time_t t; time(&t); printf("Todays date and time is : %s",ctime(&t)); return 0;}time() - get system time in time_t format
Specification#includetime_t time(time_t *timer);Argumentstimer : pointer to a time_t type variable that is assigned the number of seconds elapsed since 00:00:00 GMT January 1, 1970.If timer is null no value is stored.Return Value The elapsed time in seconds identical to that placed in timer.Example See previous example
References
C Encyclopedia, Compiler Help and various websites (credits to google)

Good places to check for latest virus information

These are just some of the places that you should visit frequently to keep up to date on information about the latest virus, trojan, backdoor, worm, etc. Some of these places will have removal tools (if available) or important information on what you can do to remove the threat if you have been infected. It is a good idea to check them regularly, along with updating the definitions of any virus program you may have installed. Symantec Security Response:
Code:
http://securityresponse.symantec.com/Symantec Security Response (Search):
Code:
http://www.symantec.com/avcenter/vinfodb.htmlSymantec Security Response (Removal Tools):
Code:
http://securityresponse.symantec.com...ools.list.htmlCA Virus Information Center:
Code:
http://www3.ca.com/securityadvisor/v...o/default.aspxUS-CERT Current Activity:
Code:
http://www.us-cert.gov/current/CERT/CC Computer Virus Resources (good list of sites to check for different things):
Code:
http://www.cert.org/other_sources/viruses.html

Basic BIOS password క్రాక్:

This is a password hack but it clears the BIOS such that the next time you start the PC, the CMOS does not ask for any password. Now if you are able to bring the DOS prompt up, then you will be able to change the BIOS setting to the default. To clear the CMOS do the following:Get DOS prompt and type:
Code:

DEBUG hit Enter

n-o 70 2e hit Enter

-o 71 ff hit Enter

-q hit Enter

exit hit Enter

Restart the computer. It works on most versions of the AWARD BIOS.
Accessing information on the hard disk
When you turn on the host machine, enter the CMOS setup menu (usually you have to press F2, or DEL, or CTRL+ALT+S during the boot sequence) and go to STANDARD CMOS SETUP, and set the channel to which you have put the hard disk as TYPE=Auto, MODE=AUTO, then SAVE & EXIT SETUP. Now you have access to the hard disk.
Standard BIOS backdoor passwords The first, less invasive, attempt to bypass a BIOS password is to try on of these standard manufacturer's backdoor passwords:
AWARD BIOSAWARD SW, AWARD_SW, Award SW, AWARD PW, _award, awkward, J64, j256, j262, j332, j322, 01322222, 589589, 589721, 595595, 598598, HLT, SER, SKY_FOX, aLLy, aLLY, Condo, CONCAT, TTPTHA, aPAf, HLT, KDD, ZBAAACA, ZAAADA, ZJAAADC, djonet, %øåñòü ïpîáåëîâ%, %äåâÿòü ïpîáåëîâ%
AMI BIOSAMI, A.M.I., AMI SW, AMI_SW, BIOS, PASSWORD, HEWITT RAND, Oder
Other passwords you may try (for AMI/AWARD or other BIOSes)
LKWPETER, lkwpeter, BIOSTAR, biostar, BIOSSTAR, biosstar, ALFAROME, Syxz, Wodj
Note that the key associated to "_" in the US keyboard corresponds to "?" in some European keyboards (such as Italian and German ones), so -- for example -- you should type AWARD?SW when using those keyboards. Also remember that passwords are Case Sensitive. The last two passwords in the AWARD BIOS list are in Russian.
Flashing BIOS via software
If you have access to the computer when it's turned on, you could try one of those programs that remove the password from the BIOS, by invalidating its memory. However, it might happen you don't have one of those programs when you have access to the computer, so you'd better learn how to do manually what they do. You can reset the BIOS to its default values using the MS-DOS tool DEBUG (type DEBUG at the command prompt. You'd better do it in pure MS-DOS mode, not from a MS-DOS shell window in Windows). Once you are in the debug environment enter the following commands:
AMI/AWARD BIOS
Code: O 70 17O 71 17QPHOENIX BIOS
Code: O 70 FFO 71 17QGENERICInvalidates CMOS RAM.Should work on all AT motherboards(XT motherboards don't have CMOS)
Code: O 70 2EO 71 FFQNote that the first letter is a "O" not the number "0". The numbers which follow are two bytes in hex format.
Flashing BIOS via hardware If you can't access the computer when it's on, and the standard backdoor passwords didn't work, you'll have to flash the BIOS via hardware. Please read the important notes at the end of this section before to try any of these methods. Using the jumpers
The canonical way to flash the BIOS via hardware is to plug, unplug, or switch a jumper on the motherboard (for "switching a jumper" I mean that you find a jumper that joins the central pin and a side pin of a group of three pins, you should then unplug the jumper and then plug it to the central pin and to the pin on the opposite side, so if the jumper is normally on position 1-2, you have to put it on position 2-3, or vice versa). This jumper is not always located near to the BIOS, but could be anywhere on the motherboard. To find the correct jumper you should read the motherboard's manual.
Once you've located the correct jumper, switch it (or plug or unplug it, depending from what the manual says) while the computer is turned OFF. Wait a couple of seconds then put the jumper back to its original position. In some motherboards it may happen that the computer will automatically turn itself on, after flashing the BIOS. In this case, turn it off, and put the jumper back to its original position, then turn it on again. Other motherboards require you turn the computer on for a few seconds to flash the BIOS.
If you don't have the motherboard's manual, you'll have to "brute force" it... trying out all the jumpers. In this case, try first the isolated ones (not in a group), the ones near to the BIOS, and the ones you can switch (as I explained before). If all them fail, try all the others. However, you must modify the status of only one jumper per attempt, otherwise you could damage the motherboard (since you don't know what the jumper you modified is actually meant for). If the password request screen still appear, try another one.
If after flashing the BIOS, the computer won't boot when you turn it on, turn it off, and wait some seconds before to retry.
Removing the battery
If you can't find the jumper to flash the BIOS or if such jumper doesn't exist, you can remove the battery that keeps the BIOS memory alive. It's a button-size battery somewhere on the motherboard (on elder computers the battery could be a small, typically blue, cylinder soldered to the motherboard, but usually has a jumper on its side to disconnect it, otherwise you'll have to unsolder it and then solder it back). Take it away for 15-30 minutes or more, then put it back and the data contained into the BIOS memory should be volatilized. I'd suggest you to remove it for about one hour to be sure, because if you put it back when the data aren't erased yet you'll have to wait more time, as you've never removed it. If at first it doesn't work, try to remove the battery overnight.
Important note: in laptop and notebooks you don't have to remove the computer's power batteries (which would be useless), but you should open your computer and remove the CMOS battery from the motherboard.
Short-circuiting the chip
Another way to clear the CMOS RAM is to reset it by short circuiting two pins of the BIOS chip for a few seconds. You can do that with a small piece of electric wire or with a bent paper clip. Always make sure that the computer is turned OFF before to try this operation.
Here is a list of EPROM chips that are commonly used in the BIOS industry. You may find similar chips with different names if they are compatible chips made by another brand. If you find the BIOS chip you are working on matches with one of the following you can try to short-circuit the appropriate pins. Be careful, because this operation may damage the chip. CHIPS P82C206 (square)
Short together pins 12 and 32 (the first and the last pins on the bottom edge of the chip) or pins 74 and 75 (the two pins on the upper left corner).
Code: gnd 74 __________________5v 75-- CHIPS 1 * P82C206 ___________________ gnd 5v 12 32OPTi F82C206 (rectangular) Short together pins 3 and 26 (third pin from left side and fifth pin from right side on the bottom edge).
Code: 80 51 ______________81 - - 50 OPTi F82C206 100-________________-31 1 30 3 26Dallas DS1287, DS1287ABenchmarq bp3287MT, bq3287AMT The Dallas DS1287 and DS1287A, and the compatible Benchmarq bp3287MT and bq3287AMT chips have a built-in battery. This battery should last up to ten years. Any motherboard using these chips should not have an additional battery (this means you can't flash the BIOS by removing a battery). When the battery fails, the RTC chip would be replaced.
CMOS RAM can be cleared on the 1287A and 3287AMT chips by shorting pins 12 and 21.The 1287 (and 3287MT) differ from the 1287A in that the CMOS RAM can't be cleared. If there is a problem such as a forgotten password, the chip must be replaced. (In this case it is recommended to replace the 1287 with a 1287A). Also the Dallas 12887 and 12887A are similar but contain twice as much CMOS RAM storage.
Code: __________ 1 - * U - 24 5v 2 - - 23 3 - - 22 4 - - 21 RCL (RAM Clear) 5 - - 20 6 - - 19 7 - - 18 8 - - 17 9 - - 16 10 - - 15 11 - - 14gnd 12 -__________- 13NOTE: Although these are 24-pin chips,the Dallas chips may be missing 5 pins,these are unused pins.Most chips have unused pins,though usually they are still present.
Dallas DS12885SBenchmarq bq3258SHitachi HD146818APSamsung KS82C6818A This is a rectangular 24-pin DIP chip, usually in a socket. The number on the chip should end in 6818. Although this chip is pin-compatible with the Dallas 1287/1287A, there is no built-in battery.Short together pins 12 and 24.
Code: 5v 24 20 13 _______________________________ DALLAS > DS12885S __________________________________ 1 12 gndMotorola MC146818AP Short pins 12 and 24. These are the pins on diagonally opposite corners - lower left and upper right. You might also try pins 12 and 20.
Code: __________ 1 - * U - 24 5v 2 - - 23 3 - - 22 4 - - 21 5 - - 20 6 - - 19 7 - - 18 8 - - 17 9 - - 16 10 - - 15 11 - - 14gnd 12 -__________- 13Replacing the chip
If nothing works, you could replace the existing BIOS chip with a new one you can buy from your specialized electronic shop or your computer supplier. It's a quick operation if the chip is inserted on a base and not soldered to the motherboard, otherwise you'll have to unsolder it and then put the new one. In this case would be more convenient to solder a base on which you'll then plug the new chip, in the eventuality that you'll have to change it again. If you can't find the BIOS chip specifically made for your motherboard, you should buy one of the same type (probably one of the ones shown above) and look in your motherboard manufacturer's website to see if there's the BIOS image to download. Then you should copy that image on the chip you bought with an EPROM programmer.
Important
Whether is the method you use, when you flash the BIOS not only the password, but also all the other configuration data will be reset to the factory defaults, so when you are booting for the first time after a BIOS flash, you should enter the CMOS configuration menu (as explained before) and fix up some things.
Also, when you boot Windows, it may happen that it finds some new device, because of the new configuration of the BIOS, in this case you'll probably need the Windows installation CD because Windows may ask you for some external files. If Windows doesn't see the CD-ROM try to eject and re-insert the CD-ROM again. If Windows can't find the CD-ROM drive and you set it properly from the BIOS config, just reboot with the reset key, and in the next run Windows should find it. However most files needed by the system while installing new hardware could also be found in C:\WINDOWS, C:\WINDOWS\SYSTEM, or C:\WINDOWS\INF .
Key Disk for Toshiba laptops
Some Toshiba notebooks allow to bypass BIOS by inserting a "key-disk" in the floppy disk drive while booting. To create a Toshiba Keydisk, take a 720Kb or 1.44Mb floppy disk, format it (if it's not formatted yet), then use a hex editor such as Hex Workshop to change the first five bytes of the second sector (the one after the boot sector) and set them to 4B 45 59 00 00 (note that the first three bytes are the ASCII for "KEY" :) followed by two zeroes). Once you have created the key disk put it into the notebook's drive and turn it on, then push the reset button and when asked for password, press Enter. You will be asked to Set Password again. Press Y and Enter. You'll enter the BIOS configuration where you can set a new password.
Key protected cases
A final note about those old computers (up to 486 and early Pentiums) protected with a key that prevented the use of the mouse and the keyboard or the power button. All you have to do with them is to follow the wires connected to the key hole, locate the jumper to which they are connected and unplug it.
more later...

Why must we Defrag the computer?

As everyone know once in a while everyone must defrag their computer. But do you really know why? Here i will show you basically why you must do it. When you start up a program the RAM (Randomly Access Memory) must find the files to the program you wanna start up. If the files aren't together the RAM will use longer time to find the files to the CPU. So this will basically say that your programs will startup slower than it should do. This is why you must defrag your computer it will Increase the performance.

How do we start defrag?

There are several ways you can do it on.

> START
> > All Programs
> > > Accessories
> > > > System Tools
> > > > > Defragment

> START
> > RUN
> > > dfrg.msc

> START
> > RUN
> > > cmd
> > > > defrag

Now you get up list of commands you may use.
Lets take an Example :

Defrag C: -a = Here you will Analyze and see if its needed to defrag this Volum.
Defrag C: -f = Here you will start defraging Volume C:

You must always type in the Volume you wanna defrag.

What are all the colors how can we understand them?

Red (Fragmented Files)

So what does Fragmented Files means? Here i am going to show you what it really is. Every time you download a Software or install something. The data so the file have will be written down on the hard disk it tries to find available space and it are just finding an space so is free. When you haven't defrag for a while the files will flow over everywhere and it will be much more harder to find the files for the computer. So Basically the defrag is taking away the Fragmented files and make them to Contextual files.

Blue (Contextual files)

This is files so are stored together. They are converted from Fragmented files to Contextual files. This means that the computer have easy access to the files. So this will get better for the RAM to find the files and send it to the CPU and the programs will startup faster.

Green (Files so can't be moved) "Sorry i don't know the English word" :/

This is files so cannot be moved this is since the files are in use by the system (This can basically mean WINDOWS files)

White (Free Space)

This is free space on your computer to more white you have to more space is available on your computer so you can use to store data on.