> Online tutorial : dos.h in c language
Showing posts with label dos.h in c language. Show all posts
Showing posts with label dos.h in c language. Show all posts

unixtodos() in c


 unixtodos()

 Converts date and time from UNIX to DOS format

 Declaration:

   void unixtodos(long time, struct date *d, struct time *t);

 Remarks:

unixtodos converts the UNIX-format time given in time to DOS format and
fills in the date and time structures *d and *t.

 Return Value:
None


 Example:

 #include <stdio.h>
 #include <dos.h>

 char *month[] = {"---", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

 #define SECONDS_PER_DAY 86400L  /* the number of seconds in one day */

 struct date dt;
 struct time tm;

 int main(void)
 {
    unsigned long val;

 /* get today's date and time */
    getdate(&dt);
    gettime(&tm);
    printf("today is %d %s %d\n", dt.da_day, month[dt.da_mon], dt.da_year);

 /*convert date and time to unix format (num of seconds since Jan 1, 1970*/
    val = dostounix(&dt, &tm);
 /* subtract 42 days worth of seconds */
    val -= (SECONDS_PER_DAY * 42);

 /* convert back to dos time and date */
    unixtodos(val, &dt, &tm);
    printf("42 days ago it was %d %s %d\n",
           dt.da_day, month[dt.da_mon], dt.da_year);
    return 0;
 }

sound() in c

sound(),nosound()
sound turns the PC speaker on at the specified frequency
nosound turns the PC speaker off
Declaration:
void sound(unsigned frequency);
void nosound(void);
Remarks:
sound turns on the PC's speaker at a given frequency.
nosound turns the speaker off after it has been turned on by a call to sound.
frequency specifies the frequency of the sound in hertz (cycles per second).
Return Value:
None
Example :


 #include <dos.h>

 int main(void)
 {
    sound(7);
    delay(10000);
    nosound();
    return 0;
 }




setdate() in c

setdate()
Gets or sets DOS system date
Declaration:
void setdate(struct date *datep);
Remarks:
setdate sets the system date to the date in *datep.
Return Value:
getdate: None
On success, returns 0
Otherwise, returns a non-zero value and
sets errno to EINVAL (Invalid date)

Program



#include <stdio.h>
#include <process.h>
#include <dos.h>

int main(void)
{
   struct date reset;
   struct date save_date;

   getdate(&save_date);
   printf("Original date:\n");
   system("date");

   reset.da_year = 2001;
   reset.da_day = 1;
   reset.da_mon = 1;
   setdate(&reset);

   printf("Date after setting:\n");
   system("date");

   setdate(&save_date);
   printf("Back to original date:\n");
   system("date");

   return 0;
}

getdate() in c

getdate()
Gets or sets DOS system date

Declaration:
void getdate(struct date *datep);
Remarks:
getdate fills in the date structure *datep with the system's current date.
Return Value:
getdate: None
On success, returns 0
Otherwise, returns a non-zero value and
sets errno to EINVAL (Invalid date)
Program
#include <stdio.h>
#include<dos.h>
int main(void)
{
struct date d;
getdate(&d);
printf("The current year is: %d\n", d.da_year);
printf("The current day is: %d\n", d.da_day);
printf("The current month is: %d\n", d.da_mon);
return 0;
}


poke() and pokeb() in c

poke(),pokeb()
poke stores an integer value at the memory location segment:offset
pokeb stores a byte value at the memory location segment:offset
Declaration:
void poke(unsigned segment, unsigned offset, int value);
void pokeb(unsigned segment, unsigned offset, char value);
Remarks:
poke stores the integer value at the memory location segment:offset.
pokeb stores the byte value at the memory location segment:offset.

If you call either of these routines when DOS.H has been included, the routine will be treated as a macro that expands to inline code.
If you don't include DOS.H, or if you do include it and #undef poke (or pokeb), you'll get the function rather than the macro.
Return Value:
None
Example:
#include<stdio.h>
#include <dos.h>
int main(void)
{
clrscr();
cprintf("Make sure the scroll lock key is off and press any key\r\n");
getch();
poke(0x0000,0x0417,16);
cprintf("The scroll lock is now on\r\n");
return 0;
}

keep() in c

keep()
Exits and remains resident
Declaration:
void keep(unsigned char status, unsigned size);
Remarks:
Both _dos_keep and keep use DOS function 0x31 to return to DOS with the exit status in status.
The current program remains resident.
These functions set the program to size paragraphs in length, and free the remainder of the memory of the program.
You can use _dos_keep and keep when installing TSR programs.
Return Value:
None
Program

#include
/* The clock tick interrupt */
#define INTR 0x1C
/* Screen attribute (blue on grey) */
#define ATTR 0x7900

#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif

/* reduce heaplength and stacklength to make a smaller program in memory */
extern unsigned _heaplen = 1024;
extern unsigned _stklen = 512;

void interrupt ( *oldhandler)(__CPPARGS);

typedef unsigned int (far *s_arrayptr);

void interrupt handler(__CPPARGS)
{
s_arrayptr screen[80];
static int count;

/* For a color screen the video memory is at B800:0000.
For a monochrome system use B000:000 */
screen[0] = (s_arrayptr) MK_FP(0xB800,0);

/* increase the counter and keep it within 0 to 9 */
count++;
count %= 10;

/* put the number on the screen */
screen[0][79] = count + '0' + ATTR;

/* call the old interrupt handler */
oldhandler();
}

int main(void)
{

/* get the address of the current clock
tick interrupt */
oldhandler = _dos_getvect(INTR);

/* install the new interrupt handler */
_dos_setvect(INTR, handler);

/* _psp is the starting address of the
program in memory. The top of the stack
is the end of the program. Using _SS and
_SP together we can get the end of the
stack. You may want to allow a bit of
saftey space to insure that enough room
is being allocated ie:
(_SS + ((_SP + safety space)/16) - _psp)
*/
_dos_keep(0, (_SS + (_SP/16) - _psp));
return 0;
}

/* NOTE:
This is an interrupt service routine.
You can NOT compile this program with Test
Stack Overflow turned on and get an
executable file which will operate
correctly. Due to the nature of this
function the formula used to compute
the number of paragraphs may not
necessarily work in all cases. Use with
care! Terminate Stay Resident (TSR)
programs are complex and no other support
for them is provided. Refer to the
MS-DOS technical documentation
for more information. */

#include
/* The clock tick interrupt */
#define INTR 0x1C
/* Screen attribute (blue on grey) */
#define ATTR 0x7900

#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif

/* reduce heaplength and stacklength to make a smaller program in memory */
extern unsigned _heaplen = 1024;
extern unsigned _stklen = 512;

void interrupt ( *oldhandler)(__CPPARGS);

typedef unsigned int (far *s_arrayptr);

void interrupt handler(__CPPARGS)
{
s_arrayptr screen[80];
static int count;

/* For a color screen the video memory is at B800:0000.
For a monochrome system use B000:000 */
screen[0] = (s_arrayptr) MK_FP(0xB800,0);

/* increase the counter and keep it within 0 to 9 */
count++;
count %= 10;

/* put the number on the screen */
screen[0][79] = count + '0' + ATTR;

/* call the old interrupt handler */
oldhandler();
}

int main(void)
{

/* get the address of the current clock
tick interrupt */
oldhandler = getvect(INTR);

/* install the new interrupt handler */
setvect(INTR, handler);

/* _psp is the starting address of the
program in memory. The top of the stack
is the end of the program. Using _SS and
_SP together we can get the end of the
stack. You may want to allow a bit of
saftey space to insure that enough room
is being allocated ie:
(_SS + ((_SP + safety space)/16) - _psp)
*/
keep(0, (_SS + (_SP/16) - _psp));
return 0;
}

getdta() in c

getdta() gets disk-transfer address
Declaration:
char far *getdta(void);
Remarks:
getdta returns the current setting of the disk transfer address (DTA).
Return Value:
getdta returns a far pointer to the current DTA
Program


#include <dos.h>
#include <stdio.h>

int main(void)
{
   char far *dta;

   dta = getdta();
   printf("The current disk transfer address is: %Fp\n", dta);
   return 0;
}

_dos_freemem() in c

_dos_freemem() Frees a previously allocated DOS memory block
Declaration:
unsigned _dos_freemem(unsigned segx);
Remarks:
_dos_freemem frees a memory block allocated by a previous call to _dos_allocmem.
segx is the segment address of the block.
Return Value:
On success, both functions return 0 On error,_dos_freemem returns the DOS error code and sets errno to
ENOMEM (Bad memory pointer) freemem returns -1 and sets errno to ENOMEM (Insufficient memory)

Program



 #include <dos.h>
#include <stdio.h>

int main(void)
{
  unsigned int size, segp, err, maxb;
  size = 64; /* (64 x 16) = 1024 bytes */
  err = _dos_allocmem(size, &segp);
  if (err == 0)
    printf("Allocated memory at segment: %x\n", segp);
  else {
    perror("Unable to allocate block");
    printf("Maximum no. of paragraphs available is %u\n", segp);
    return 1;
  }
  if (_dos_setblock(size * 2, segp, &maxb) == 0)
    printf("Expanded memory block at segment: %X\n", segp);
  else {
    perror("Unable to expand block");
    printf("Maximum no. of paragraphs available is %u\n", maxb);
  }
  _dos_freemem(segp);
  return 0;
}

freemem() in c

Frees a previously allocated DOS memory block
Declaration:
int freemem(unsigned segx);
Remarks:
freemem frees a memory block allocated by a previous call to allocmem.
segx is the segment address of the block.

Return Value:
On success, both functions return 0
On error,
_dos_freemem returns the DOS error code and sets errno to ENOMEM (Bad memory pointer)
freemem returns -1 and sets errno to ENOMEM (Insufficient memory)

program



#include <dos.h>
#include <alloc.h>
#include <stdio.h>

int main(void)
{
   unsigned int size, segp;
   int stat;

   size = 64; /* (64 x 16) = 1024 bytes */
   stat = allocmem(size, &segp);
   if (stat < 0)
      printf("Allocated memory at segment: %x\n", segp);
   else
      printf("Failed: maximum number of\
             paragraphs available is %u\n", stat);
   freemem(segp);

   return 0;
}