intmy_init(void) { int irqs[NR_IRQS]; unsignedlong stime;
printk(KERN_INFO "Initializing module...\n");
/* get IRQ statistics for CPU 0 */ kstat_irqs_cpu(0, irqs, &stime);
/* print IRQ statistics */ printk(KERN_INFO "IRQ statistics for CPU 0:\n"); for (int i = 0; i < NR_IRQS; i++) { if (irqs[i] > 0) { printk(KERN_INFO "IRQ %d: count=%d\n", i, irqs[i]); } }
voidfunc() { printf("Entering func...\n"); longjmp(env, 1); printf("This line will not be executed.\n"); }
intmain() { int ret = setjmp(env); if (ret == 0) { printf("Jumping to func...\n"); func(); } else { printf("Returning from longjmp with value %d\n", ret); } return EXIT_SUCCESS; }
上述程序定义了一个名为 env 的 jmp_buf 类型变量,用于保存当前执行状态。在主函数中,通过调用 setjmp 函数将当前状态保存到 env 中,并返回 0。然后调用 func 函数,该函数打印一条消息并调用 longjmp 函数恢复之前保存的状态,这里传入参数值为 1。由于 longjmp 函数会导致程序跳转到 setjmp 函数继续执行,因此后面的 printf 语句会输出 "Returning from longjmp with value 1"。
/* calculate elapsed time in jiffies and convert to ticks */ end = jiffies; ticks = jiffies_delta_to_clock_t(end - start); printk(KERN_INFO "Elapsed time: %ld ticks\n", (long)ticks);
/* calculate elapsed time in jiffies and convert to milliseconds */ end = jiffies; msecs = jiffies_delta_to_msecs(end - start); printk(KERN_INFO "Elapsed time: %lu ms\n", msecs);
/* calculate elapsed time in jiffies_64 and convert to ticks */ end = jiffies_64; ticks = jiffies64_to_clock_t(end - start); printk(KERN_INFO "Elapsed time: %ld ticks\n", (long)ticks);
/* calculate elapsed time in jiffies_64 and convert to milliseconds */ end = jiffies_64; msecs = jiffies64_to_msecs(end - start); printk(KERN_INFO "Elapsed time: %llu ms\n", msecs);
intmain() { int result; int port = 0; // 串行端口 0 // 从硬件端口中输入 result = inport(port); printf("Word read from port %d = 0x%X\n", port, result); return0; }
4. insline
4.1 函数说明
函数声明
函数功能
void insline(void);
在当前光标位置插入一行空行,原光标所在行及其下方的所有行都会向下移动一行 【在 Turbo C 等早期 C 编译器的图形库或控制台操作库中使用】
4.2 演示示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<conio.h>
intmain() { clrscr(); cprintf("INSLINE inserts an empty line in the text window\r\n"); cprintf("at the cursor position using the current text\r\n"); cprintf("background color. All lines below the empty one\r\n"); cprintf("move down one line and the bottom line scrolls\r\n"); cprintf("off the bottom of the window.\r\n"); cprintf("\r\nPress any key to continue:"); gotoxy(1, 3); getch(); // 在文本窗口中插入一个空行 insline(); getch(); return0; }
5. installuserdriver
5.1 函数说明
函数声明
函数功能
int installuserdriver(char *name, int (*detect)(void));
/* 删除文件,成功返回0,失败返回非0。 */ intdelete_file(char near *filename) { unionREGSregs; int ret; regs.h.ah = 0x41; regs.x.dx = (unsigned) filename; ret = intdos(®s, ®s);
// 如果设置了进位标志,则出现错误 return(regs.x.cflag ? ret : 0); }
intmain(void) { int err; err = delete_file("NOTEXIST.$$$"); if (!err) printf("Able to delete NOTEXIST.$$$\n"); else printf("Not Able to delete NOTEXIST.$$$\n"); return0; }
10. intdosx
10.1 函数说明
函数声明
函数功能
int intdosx(union REGS *inregs, union REGS *outregs, struct SREGS *segregs);
它主要作用是触发一个 DOS 软件中断(通常是中断号 0x21),并通过寄存器传递参数和获取返回值,从而实现对 DOS 系统功能的调用。
/* 删除文件,成功返回0,失败返回非0。 */ intdelete_file(char far *filename) { unionREGSregs; structSREGSsregs; int ret; regs.h.ah = 0x41; regs.x.dx = FP_OFF(filename); sregs.ds = FP_SEG(filename); ret = intdosx(®s, ®s, &sregs);
// 如果设置了进位标志,则出现错误 return(regs.x.cflag ? ret : 0); }
intmain(void) { int err; err = delete_file("NOTEXIST.$$$"); if (!err) printf("Able to delete NOTEXIST.$$$\n"); else printf("Not Able to delete NOTEXIST.$$$\n"); return0; }
11. intr
11.1 函数说明
函数声明
函数功能
void intr(int intr_num, struct REGPACK *preg);
它是一个在早期 DOS 环境下用于进行软件中断调用的函数
参数:
int intr_num :要调用的中断号。在 DOS 和 BIOS 系统中,不同的中断号对应着不同的功能。例如,中断号 0x10 通常用于 BIOS 的视频服务,中断号 0x21 用于 DOS 系统功能调用。
printf("Enter directory to change to: "); gets(directory); reg.r_ax = 0x3B << 8; // 将3Bh转换为AH reg.r_dx = FP_OFF(directory); reg.r_ds = FP_SEG(directory); intr(0x21, ®); if (reg.r_flags & CF) printf("Directory change failed\n"); getcwd(directory, 80); printf("The current directory is: %s\n", directory); return0; }
12. ioctl
12.1 函数说明
函数声明
函数功能
int ioctl(int fd, int cmd, ...) ;
控制 I/O 设备
参数:
fd :文件描述符
cmd : 交互协议,设备驱动将根据 cmd 执行对应操作
… : 可变参数arg,依赖 cmd 指定长度以及类型
12.2 演示示例
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<stdio.h> #include<dir.h> #include<io.h>
intmain(void) { int stat = ioctl(0, 8, 0, 0); if (!stat) printf("Drive %c is removable.\n", getdisk() + 'A'); else printf("Drive %c is not removable.\n", getdisk() + 'A'); return0; }
printf("The hypotenuse of a right triangle whose legs are %lf and %lf is %lf\n", x, y, result); printf("The hypotenuse of a right triangle whose legs are %f and %f is %f\n", xf, yf, resultf); printf("The hypotenuse of a right triangle whose legs are %Lf and %Lf is %Lf\n", xL, yL, resultL);
intmain(void) { // 在硬件问题中断上安装我们的处理程序 harderr(handler); clrscr(); printf("Make sure there is no disk in drive A:\n"); printf("Press any key ....\n"); getch(); printf("Trying to access drive A:\n"); printf("fopen returned %p\n", fopen("A:temp.dat", "w")); return0; }
intmain(void) { int gdriver = DETECT, gmode, errorcode; int midx, midy, mode; char numname[80], modename[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
midx = getmaxx() / 2; midy = getmaxy() / 2;
mode = getgraphmode(); sprintf(numname, "%d is the current mode number.", mode); sprintf(modename, "%s is the current graphics mode.", getmodename(mode));
intmain() { int gdriver = DETECT, gmode, errorcode; structpalettetypepal; char psize[80], pval[20]; int i, ht; int y = 10;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
getpalette(&pal);
sprintf(psize, "The palette has %d modifiable entries.", pal.size);
outtextxy(0, y, psize); if (pal.size != 0) { ht = textheight("W"); y += 2*ht; outtextxy(0, y, "Here are the current values:"); y += 2*ht; for (i=0; i < pal.size; i++, y+=ht) { sprintf(pval, "palette[%02d]: 0x%02X", i, pal.colors[i]); outtextxy(0, y, pval); } }
intmain() { int gdriver = DETECT, gmode, errorcode; structtextsettingstypetextinfo; int midx, midy, ht; char fontstr[80], dirstr[80], sizestr[80]; char hjuststr[80], vjuststr[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
midx = getmaxx() / 2; midy = getmaxy() / 2;
// 获取有关当前图形文本字体的信息 gettextsettings(&textinfo);
sprintf(fontstr, "%s is the text style.", font[textinfo.font]); sprintf(dirstr, "%s is the text direction.", dir[textinfo.direction]); sprintf(sizestr, "%d is the text size.", textinfo.charsize); sprintf(hjuststr, "%s is the horizontal justification.", hjust[textinfo.horiz]); sprintf(vjuststr, "%s is the vertical justification.", vjust[textinfo.vert]);
intmain() { int gdriver = DETECT, gmode, errorcode; structviewporttypeviewinfo; int midx, midy, ht; char topstr[80], botstr[80], clipstr[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
midx = getmaxx() / 2; midy = getmaxy() / 2;
// 获取有关当前视区的信息 getviewsettings(&viewinfo);
sprintf(topstr, "(%d, %d) is the upper left viewport corner.", viewinfo.left, viewinfo.top); sprintf(botstr, "(%d, %d) is the lower right viewport corner.", viewinfo.right, viewinfo.bottom); sprintf(clipstr, "Clipping is turned %s.", clip[viewinfo.clip]);
for (int i = WHITE; i >= 0; i--) { setbkcolor(i); bkcolor = getbkcolor(); // 获取当前背景颜色 if (i == WHITE) setcolor(BLACK); else setcolor(WHITE); itoa(bkcolor, bkname, 10); strcat(bkname," is the current background color.");
for (int i = WHITE; i > 0; i--) { color = getcolor(); itoa(color, colname, 10); strcat(colname, " is the current drawing color."); outtextxy(midx, midy, colname); getch(); cleardevice(); setcolor(i - 1); }
intmain(void) { int gdriver = DETECT, gmode, errorcode; int midx, midy, mode; char numname[80], modename[80];
initgraph(&gdriver, &gmode, "");
errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
midx = getmaxx() / 2; midy = getmaxy() / 2;
// 获取当前图形模式 mode = getgraphmode(); sprintf(numname, "%d is the current mode number.", mode); sprintf(modename, "%s is the current graphics mode", getmodename(mode));
// the names of the line styles supported char *lname[] = { "SOLID_LINE", "DOTTED_LINE", "CENTER_LINE", "DASHED_LINE", "USERBIT_LINE" };
intmain() { int gdriver = DETECT, gmode, errorcode; structlinesettingstypelineinfo; int midx, midy; char lstyle[80], lpattern[80], lwidth[80];
initgraph(&gdriver, &gmode, ""); errorcode = graphresult(); if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); }
midx = getmaxx() / 2; midy = getmaxy() / 2;
// 取当前线型、模式和宽度 getlinesettings(&lineinfo);
sprintf(lstyle, "%s is the line style.", lname[lineinfo.linestyle]); sprintf(lpattern, "0x%X is the user-defined line pattern.", lineinfo.upattern); sprintf(lwidth, "%d is the line thickness.", lineinfo.thickness);
int fscanf(FILE *stream, char *format[,argument...]);
从一个流中执行格式化输入
注意: fscanf 遇到空格和换行时结束。它与 fgets 有区别,fgets 遇到空格不结束。
16.2 演示示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<stdlib.h> #include<stdio.h>
intmain(void) { int i; printf("Input an integer: "); if (fscanf(stdin, "%d", &i)) printf("The integer is: %d\n", i); else { fprintf(stderr, "Error reading an integer from stdin.\n"); exit(1); } return0; }
16.3 运行结果
17. fseek
17.1 函数说明
函数声明
函数功能
int fseek(FILE *stream, long offset, int fromwhere);
stream = fopen("STU.FIL", "w+"); // 获取当前文件指针的位置 fgetpos(stream, &filepos); fprintf(stream, "This is a test"); // 展示当前文件指针的位置 showpos(stream);
/* set a new file position, display it */ if (fsetpos(stream, &filepos) == 0) showpos(stream); else { fprintf(stderr, "Error setting file pointer.\n"); exit(1); }
if ((stream = fopen("STU.FIL", "w+")) == NULL) { fprintf(stderr, "Cannot open output file.\n"); exit(1); } fprintf(stream, "This is a test"); fflush(stream);
// get information about the file fstat(fileno(stream), &statbuf); fclose(stream);
if (statbuf.st_mode & S_IFCHR) printf("Handle refers to a device.\n"); if (statbuf.st_mode & S_IFREG) printf("Handle refers to an ordinary file.\n"); if (statbuf.st_mode & S_IREAD) printf("User has read permission on file.\n"); if (statbuf.st_mode & S_IWRITE) printf("User has write permission on file.\n"); // 不知道为啥,我这里文件的驱动号是空 printf("Drive letter of file: %c\n", statbuf.st_dev); printf("Size of file in bytes: %ld\n", statbuf.st_size); printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime)); return0; }
19.3 运行结果
20. ftell
20.1 函数说明
函数声明
函数功能
long ftell(FILE *stream);
获取文件指针当前位置相对于文件首的偏移字节数
20.2 演示示例
1 2 3 4 5 6 7 8 9 10
#include<stdio.h>
intmain(void) { FILE *stream = fopen("temp.txt", "w+"); fprintf(stream, "This is a test"); printf("The file pointer is at byte %ld\n", ftell(stream)); fclose(stream); return0; }
20.3 运行结果
21. fwrite
21.1 函数说明
函数声明
函数功能
int fwrite(const void *ptr, int size, int nitems, FILE *stream);
intmain(void) { char ch; printf("Enter a character followed by <Enter>: "); // read the character from stdin ch = fgetchar(); printf("The character read is: '%c'\n", ch); return0; }
11.3 运行结果
12. fgetpos
12.1 函数说明
函数声明
函数功能
int fgetpos(FILE *stream);
依据当前文件的句柄,获取当前访问指针位置信息
12.2 演示示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include<string.h> #include<stdio.h>
intmain(void) { FILE *stream; charstring[] = "This is a test"; fpos_t filepos; stream = fopen("STU.FIL", "w+"); fwrite(string, strlen(string), 1, stream); // report the file pointer position fgetpos(stream, &filepos); printf("The file pointer is at byte %lld\n", filepos); fclose(stream); return0; }