Last update Nov 28, 2002
This function is exactly the same as dos_abs_disk_read.
This function is exactly the same as dos_abs_disk_write.
int dos_abs_disk_read(int drive, int num_sec, long start_sec, char *buffer);Description
Transfers control directly to BIOS to perform disk read. Use 0 to specify drive A, 1 for B, and so on to 25. The number of sectors to read is num_sec. start_sec defines the first sector for operation. Argument buffer is the operation's destination memory address. The buffer must be large enough to hold the requested sectors.
Return Value
DOS Windows 3.x Win32
See Also
Example
/* Example for dos_abs_disk_read Also demonstrates absread (which is just like it), atoi, toupper, and isprint DISKDUMP.C Reads logical sector 1 from drive A and prints a hex and ascii dump of it. */ #include <stdio.h> #include <dos.h> #include <ctype.h> #include <stdlib.h> static unsigned char buffer[512]; void main(int argc, const char *const argv[]) { int i, drive, sector; unsigned char *p; unsigned code; if (argc < 3) goto usage_error; drive = toupper(*argv[1]) - 'A'; if (drive < 0 || drive > 25) goto usage_error; sector = atoi(argv[2]); code = dos_abs_disk_read(drive, 1, sector, (char *)buffer); if (code != 0) { fprintf(stderr, "Error, dos code %02x, bios code %02x\n", code & 0xff, code >> 8); exit(EXIT_FAILURE); } printf("Drive %c, logical sector %d:\n", drive + 'A', sector); for (p = buffer; p < buffer + sizeof buffer; p += 16) { for (i = 0; i < 16; i += 1) printf("%02x ", p[i]); printf(" "); for (i = 0; i < 16; i += 1) if (isprint(p[i])) printf("%c", p[i]); else printf("."); printf("\n"); } exit(EXIT_SUCCESS); usage_error: fprintf(stderr, "Usage: DISKDUMP drive-letter sector-number\n"); exit(EXIT_FAILURE); }Output
Drive C, logical sector 0: eb 3c 90 4d 53 44 4f 53 35 2e 30 00 02 10 01 00 .<. MSDOS5.0..... 02 00 02 00 00 f8 00 01 20 00 40 00 20 00 00 00 ........ .@. ... e0 f7 0f 00 80 00 29 c7 5e 63 1c 4d 53 2d 44 4f .......^ c. MS-DO 53 5f 36 20 20 20 46 41 54 31 36 20 20 20 fa 33 S_6 FAT16 .3 c0 8e d0 bc 00 7c 16 07 bb 78 00 36 c5 37 1e 56 .....|... x. 6.7. V 16 53 bf 3e 7c b9 0b 00 fc f3 a4 06 1f c6 45 fe .S.>|......... E. 0f 8b 0e 18 7c 88 4d f9 89 47 02 c7 07 3e 7c fb ....|. M.. G...>|. cd 13 72 79 33 c0 39 06 13 7c 74 08 8b 0e 13 7c .. ry3.9..| t....| 89 0e 20 7c a0 10 7c f7 26 16 7c 03 06 1c 7c 13 .. |..|.&.|...|. 16 1e 7c 03 06 0e 7c 83 d2 00 a3 50 7c 89 16 52 ..|...|.... P|.. R 7c a3 49 7c 89 16 4b 7c b8 20 00 f7 26 11 7c 8b |. I|.. K|. ..&.|. 1e 0b 7c 03 c3 48 f7 f3 01 06 49 7c 83 16 4b 7c ..|.. H.... I|.. K| 00 bb 00 05 8b 16 52 7c a1 50 7c e8 92 00 72 1d ...... R|. P|... r. b0 01 e8 ac 00 72 16 8b fb b9 0b 00 be e6 7d f3 ..... r........}. a6 75 0a 8d 7f 20 b9 0b 00 f3 a6 74 18 be 9e 7d .u... ..... t...} e8 5f 00 33 c0 cd 16 5e 1f 8f 04 8f 44 02 cd 19 ._. 3...^.... D... 58 58 58 eb e8 8b 47 1a 48 48 8a 1e 0d 7c 32 ff XXX... G. HH...| 2. f7 e3 03 06 49 7c 13 16 4b 7c bb 00 07 b9 03 00 .... I|.. K|...... 50 52 51 e8 3a 00 72 d8 b0 01 e8 54 00 59 5a 58 PRQ.:. r.... T. YZX 72 bb 05 01 00 83 d2 00 03 1e 0b 7c e2 e2 8a 2e r..........|.... 15 7c 8a 16 24 7c 8b 1e 49 7c a1 4b 7c ea 00 00 .|..$|.. I|. K|... 70 00 ac 0a c0 74 29 b4 0e bb 07 00 cd 10 eb f2 p.... t)......... 3b 16 18 7c 73 19 f7 36 18 7c fe c2 88 16 4f 7c ;..| s.. 6.|.... O| 33 d2 f7 36 1a 7c 88 16 25 7c a3 4d 7c f8 c3 f9 3.. 6.|..%|. M|... c3 b4 02 8b 16 4d 7c b1 06 d2 e6 0a 36 4f 7c 8b ..... M|..... 6O|. ca 86 e9 8a 16 24 7c 8a 36 25 7c cd 13 c3 0d 0a .....$|. 6%|..... 4e 6f 6e 2d 53 79 73 74 65 6d 20 64 69 73 6b 20 Non-System disk 6f 72 20 64 69 73 6b 20 65 72 72 6f 72 0d 0a 52 or disk error.. R 65 70 6c 61 63 65 20 61 6e 64 20 70 72 65 73 73 eplace and press 20 61 6e 79 20 6b 65 79 20 77 68 65 6e 20 72 65 any key when re 61 64 79 0d 0a 00 49 4f 20 20 20 20 20 20 53 59 ady... IO SY 53 4d 53 44 4f 53 20 20 20 53 59 53 00 00 55 aa SMSDOS SYS.. U.
/* Example for dos_alloc Also demonstrates dos_free, _MK_FP */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main () { unsigned newseg; void __far *pointer; newseg = dos_alloc (20); if (newseg == 0) { perror ("dos_alloc failed"); exit (EXIT_FAILURE); } pointer = _MK_FP (newseg, 0); printf("Memory allocated successfully at %Fp\n", pointer); if (dos_free (newseg) == -1) { perror ("Unable to free memory\n"); exit (EXIT_FAILURE); } }
Description
Allocates a DOS memory segment,
using the DOS system call 0x48. The allocated memory block has
the number of paragraphs specified by size. (There
are 16 bytes in a paragraph.) segp points to the word
that contains the segment descriptor of the allocated memory block.
Do not use the _dos_allocmem and malloc functions in the
same program.
Return Value
Returns 0 if the memory is successfully allocated. If unsuccessful, -1
DOS error code is returned and the word pointed to by segp is set
to the size (in paragraphs) of the largest available block.
In addition, the global variable _doserrno is set and the
global variable errno is set to ENOMEM, for not enough memory.
Compatibility
DOS, Windows 3.x
See Also
_dos_freemem
Example
/* Example for _dos_allocmem Also demonstrates _dos_freemem */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { unsigned largest; int size = 64; unsigned segp; largest = _dos_allocmem(size, &segp); if (largest != -1) { printf("Address of segment: %p\n", segp); printf("Allocated memory at segment:% x\n", &segp); _dos_freemem( segp); } else printf("Unable to allocate memory\n"); }
/* Example for _dos_commit Also demonstrates _dos_close, _dos_creat, _dos_read and _dos_write */ #include <dos.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void main() { int fd; int result; unsigned count; char buf[32] = "Hello from Digital Mars\0" ; char newbuf[50]; fd = 0; result = _dos_creat ("temp.dat", _A_NORMAL, &fd); if (result != 0) perror ("_dos_creat failed"); else { _dos_write (fd, buf, strlen (buf), &count); _dos_commit (fd); _dos_read (fd, newbuf, 50, &count); _dos_close (fd); } }
The attribute byte is the same as described in the reference manual for MS-DOS. Use the following attribute values defined in dos.h:
/* Example for _dos_creat Also demonstrates _dos_close */ #include <dos.h> #include <stdio.h>> #include <stdlib.h> void main() { int handle; int result; result = _dos_creat ("temp.dat", _A_NORMAL, &handle); if (result != 0) perror ("_dos_creat failed"); else { printf("File successfully created\n"); _dos_close( handle); } }
/* Example for _dos_creatnew Also demonstrates _dos_close */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { int handle; int result; result = _dos_creatnew ("temp.dat", _A_NORMAL, &handle); if (result != 0) perror ("_dos_creatnew failed"); else { printf("File created\n"); _dos_close(handle); } }
File createdIf the file temp.dat exists:
_dos_creatnew failed: File exists
int exterror | AX register contents (extended error) |
char eclass | BH register contents (error class) |
char action | BL register contents (action) |
char locus | CH register contents (error locus) |
/* Example for _dos_exterr Also demonstrates _dos_close, _dos_creat and _dos_creatnew */ #include <dos.h> #include <io.h> #include <stdio.h> #include <stdlib.h> int main() { int handle; int result; struct _DOSERROR p; /* These calls in this order should */ /* guarantee an error occurs */ result = _dos_creat("temp.dat", _A_NORMAL, &handle); result = _dos_creatnew("temp.dat", _A_NORMAL, &handle); if (result != 0) { _dosexterr(&p); printf("Exterror = %d\n", p.exterror); printf("Class = %d\n", p.errclass); printf("Action = %d\n", p.action); printf("Locus = %d\n", p.locus); perror("Create new file error"); } else printf("No file error"); _dos_close(handle); return 0; }
/* Example for _dos_findfirst and _dos_findnext */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void list_name( struct find_t fileinfo) { int result; result = _dos_findnext (& fileinfo); if (result == 0) { printf("% s\n", fileinfo. name); list_name (fileinfo); } else printf("List done\n"); } void main() { int result; struct find_t ffblk; result = _dos_findfirst ("*.dat", _A_NORMAL, &ffblk); if (result == 0) { printf("% s\n", ffblk. name); list_name (ffblk); } else printf("No files match *.dat\n"); }
struct _dosdate_t { unsigned char day; /* day of month( 1-31)*/ unsigned char month; /* month (1-12)*/ unsigned int year; /* year (1980-2099)*/ unsigned char dayofweek; /* 0-6 (0= Sunday)*/ }
/* Example for _dos_getdate and _dos_setdate */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { struct _dosdate_t date; unsigned int oldyear; _dos_getdate (& date); printf("Date : %2d/%2d/%d\n", date.month, date.day, date.year); oldyear = date.year; date.year = 1990; _dos_setdate (& date); _dos_getdate (& date); printf("Date : %2d/%2d/%d\n", date.month, date.day, date.year); date.year = oldyear; _dos_setdate (& date); _dos_getdate (& date); printf("Date : %2d/%2d/%d\n", date.month, date.day, date.year); }Output
Date : 6/22/1994 Date : 6/22/1990 Date : 6/22/1994
struct _diskfree_t { unsigned total_clusters; /* on disk*/ unsigned avail_clusters; /* on disk*/ unsigned sectors_per_clusters; unsigned bytes_per_sector; }
/* Example for _dos_getdiskfree */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { unsigned ret; struct diskfree_t free; ret = _dos_getdiskfree (0, &free); if (ret != 0) perror("_dos_getdiskfree"); else { printf("% u avaliable clusters\n", free. avail_clusters); printf("out of %u clusters.\n", free. total_clusters); printf("% u sectors per cluster,\n", free. sectors_per_cluster); printf("% u bytes per sector.\n", free. bytes_per_sector); } }
/* Example for dos_getdiskfreespace DISKFREE. C */ #include <stdio.h> #include <dos.h> #include <stdlib.h> void main (int argc, char *argv[]) { int drive; if (argc < 2) drive = 0; else drive = toupper (* argv[ 1]) -'A' + 1; if (drive < 0 || drive > 26) { fprintf(stderr, "Usage: DISKFREE [drive-letter]\ n"); exit (EXIT_FAILURE); } printf(" Free space is %ld bytes\ n", dos_getdiskfreespace (drive)); }
/* Example for _dos_getdrive */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { unsigned driveno; _dos_getdrive (& driveno); printf("The current drive is %c\n", driveno-1+ 'a'); }
/* Example for _dos_getfileattr, and _dos_setfileattr Also demonstrates _dos_creat */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { int handle, result; unsigned attrib; result = _dos_creat ("temp.dat", _A_RDONLY, &handle); if (result == 0) { _dos_getfileattr ("temp.dat", &attrib); if (attrib & _A_RDONLY) printf("File is read only\n"); else printf("File is not read only\n"); if (attrib & _A_ARCH) printf("File has archive bit set\n"); else printf("File does not have archive bit set\n"); _dos_setfileattr ("temp.dat", _A_NORMAL); _dos_getfileattr ("temp.dat", &attrib); if (attrib & _A_RDONLY) printf("File is read only\n"); else printf("File is not read only\n"); if (attrib & _A_ARCH) printf("File has archive bit set\n"); else printf("File does not have archive bit set\n"); _dos_close (handle); } else perror ("Error creating file"); }
/* Example for _dos_getftime and _dos_setftime Also demonstrates _dos_open and _dos_close */ #include <dos.h> #include <fcntl.h> #include <io.h> #include <stdio.h> #include <stdlib.h> void main() { int handle, result; int d, m, y, h, mi, s; unsigned date, time, oldtime; result = _dos_open ("temp.dat", _O_RDONLY, &handle); if (result == 0) { _dos_getftime (handle, &date, &time); d = date & 0x01F; m = (date >> 5) & 0x0F; y = (( date >> 9) & 0x7F) + 1980; s = (time & 0x1F)* 2; mi = (time >> 5) & 0x3F; h = (time >> 11) & 0x1F; printf("temp.dat date = %2d/% 2d/% 4d\n", m, d, y); printf("temp.dat time = %2d:% 2d:% 2d\n", h, mi, s); oldtime = time; time = 6; _dos_setftime (handle, date, time); _dos_getftime (handle, &date, &time); s = (time & 0x1F)* 2; mi = (time >> 5) & 0x3F; h = (time >> 11) & 0x1F; printf("\ntemp.dat time = %2d:% 2d:% 2d\n\n", h, mi, s); time = oldtime; _dos_setftime (handle, date, time); _dos_getftime (handle, &date, &time); d = date & 0x01F; m = (date >> 5) & 0x0F; y = (( date >> 9) & 0x7F) + 1980; s = (time & 0x1F)* 2; mi = (time >> 5) & 0x3F; h = (time >> 11) & 0x1F; printf("temp.dat date = %2d/% 2d/% 4d\n", m, d, y); printf("temp.dat time = %2d:% 2d:% 2d\n", h, mi, s); _dos_close( handle); } else perror ("Error creating file"); }
struct dos_time_t { unsigned char hour; /* hours (0-23)*/ unsigned char minute; /* minutes (0-59)*/ unsigned char second; /* seconds (0-59) */ unsigned char hsecond; /* seconds/ 100 (0-99)*/ }
/* Example for _dos_gettime, _dos_settime */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { struct _dostime_t time; char oh; _dos_gettime(& time); printf("Time is %02d:% 02d:% 02d\n", time.hour, time. minute, time. second); oh = time.hour; time.hour = 4; _dos_settime(& time); _dos_gettime(& time); printf("Time is %02d:% 02d:% 02d\n", time.hour, time. minute, time. second); time.hour = oh; _dos_settime(& time); _dos_gettime(& time); printf("Time is %02d:% 02d:% 02d\n", time.hour, time. minute, time. second); }
/* Example for _dos_lock Also demonstrates _dos_open, _dos_close LOCKER. C Locks 10 bytes of this file starting at offset 25, making them inaccessable to LOCKEE. EXE. */ #include <dos.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <share.h> #include <io.h> static void error (char *message) { perror (message); exit (EXIT_FAILURE); } void main () { int handle; if (_dos_open ("locker. c", _O_RDONLY|_SH_DENYNO, &handle) != 0) error ("Open failed"); if (_dos_lock (handle, 0, 25L, 10L) != 0) error ("Lock failed (is SHARE running?)"); printf("LOCKER. C locked. Run LOCKEE. EXE in another Windows DOS box now.\n" "Press any key when ready to unlock: "); getch (); if (_dos_lock (handle, 1, 25L, 10L) != 0) error ("Unlock failed"); printf("\nFile unlocked. Try LOCKEE. EXE again.\n"); _dos_close (handle); } /* Second part of example for _dos_lock Also demonstrates _dos_open, _dos_seek, _dos_close LOCKEE. C Prints the first 100 characters of LOCKER. C. If a character can't be accessed, a question mark is printed. */ #include <dos.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <share.h> static void error (char *message) { perror (message); exit (EXIT_FAILURE); } int main () { int handle, i; char c; unsigned actual; if (_dos_open ("LOCKER. C", _O_RDONLY|_SH_DENYNO, &handle) != 0) error ("Open failed"); for (i = 0; i < 100; i += 1) { if (_dos_seek (handle, (unsigned long) i, 0) == -1) error ("Seek error"); if (_dos_read (handle, &c, 1, &actual) != 0) printf("?"); else { if (actual == 0) break; printf("% c", c); } } _dos_close (handle); }
---DOS box 1: C:\SC\EXAMPLES> locker LOCKER. C locked. Run LOCKEE. EXE in another Windows DOS box now. Press any key when ready to unlock: ---DOS box 2: C:\SC\EXAMPLES> lockee /* Example for _dos_l?????????? so demonstrates _dos_open, _dos_close LOCKER. C Loc ---DOS box 1: C:\SC\EXAMPLES> locker LOCKER. C locked. Run LOCKEE. EXE in another Windows DOS box now. Press any key when ready to unlock: File unlocked. Try LOCKEE. EXE again. ---DOS box 2: C:\SC\EXAMPLES> lockee /* Example for _dos_lock Also demonstrates _dos_open, _dos_close LOCKER. C Loc
/* Example for _dos_open, _dos_close */ #include <dos.h> #include <fcntl.h> #include <io.h> #include <stdio.h> #include <stdlib.h> void main() { char *fname; unsigned int mode; int handle; int result; mode = O_RDONLY; fname = "temp.dat"; result = _dos_open( fname, mode, &handle); if (result != 0) perror ("Read_only open failed"); else { printf("\nFile %s opened for reading\n", fname); _dos_close( handle); } mode = O_WRONLY; fname = "CON"; result = _dos_open( fname, mode, &handle); if (result != 0) perror ("Error opening console"); else { printf("\nFile %s opened for writing\n", fname); _dos_close( handle); } }
See dos_commit
/* Example for _dos_seek Also demonstrates _dos_open, _dos_read _DOS_SEE. C */ #include <dos.h> #include <fcntl.h> #include <share.h> #include <stdlib.h> #include <stdio.h> static void error (char *message) { perror (message); exit (EXIT_FAILURE); } void main () { int handle; char buffer[20]; unsigned actual; if (_dos_open ("_DOS_SEE. C", _O_RDONLY|_SH_DENYNO, &handle) != 0) error ("Open failed"); if (_dos_seek (handle, -20, 2) == -1) error ("Couldn't seek from end"); if (_dos_read (handle, buffer, 20, &actual) != 0) error ("Couldn't read from end"); printf("The 20 bytes at the end are\n\n%. 20s\n\n", buffer); if (_dos_seek (handle, 20, 0) == -1) error ("Couldn't seek from beginning"); if (_dos_read (handle, buffer, 20, &actual) != 0) error ("Couldn't read from beginning"); printf("20 bytes 20 from the beginning are\n\n%. 20s\n\n", buffer); if (_dos_seek (handle, 20, 1) == -1) error ("Couldn't seek from the current position"); if (_dos_read (handle, buffer, 20, &actual) != 0) error ("Couldn't read from the current position"); printf("20 bytes 20 from the current position are\n\n%. 20s\n\n", buffer); }
The 20 bytes at the end are \n\n", buffer); } 20 bytes 20 from the beginning are Also de 20 bytes 20 from the current position are _D
/* Example for _dos_setblock Also demonstrates _dos_allocmem and _dos_freemem */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { unsigned newseg; unsigned maxsize; int result; result = _dos_allocmem( 10,& newseg); if (result == 0) { printf("Memory allocated at %u\n", newseg); result = _dos_setblock( 20, newseg, &maxsize); if (result == 0) { printf("Memory successfully expanded\n"); result = _dos_freemem (newseg); if (result == 0) printf("Memory successfully freed\n"); else perror ("Error freeing memory"); } else { printf("_dos_setblock failed: "); printf("only %u available at %u\n", maxsize, newseg); result = _dos_freemem (newseg); if (result == 0) printf("Memory successfully freed\n"); else perror ("Error freeing memory"); } } else perror ("_dos_allocmem failed"); }
struct _dosdate_t { unsigned char day; /* day of month( 1-31) */ unsigned char month; /* month (1-12) */ unsigned int year; /* year (1980-2099) */ unsigned char dayofweek; /* day of week (0 = Sunday) */ }
See _dos_getdate
/* Example for _dos_setdrive */ #include <dos.h> #include <stdio.h> #include <stdlib.h> void main() { unsigned numdrives; _dos_setdrive (1,& numdrives); printf(" Total number of drives is:% d\ n", numdrives); printf(" Current drive is now a:\ n"); }
See _dos_getfileattr
See _dos_getftime