随笔分类 - Linux编程
摘要:#include #include int main() { time_t now; struct tm *w; time(&now); w=localtime(&now); printf("%04d/%02d/%02d\n%02d:%02d:%02d\n",w->tm_year+1900, ...
阅读全文
摘要:#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <stdio.h>int main(){ pid_t pid; char *m; int n; pid=fork(); if(pid==0) { m="this is child"; n=5; } else { m="this is fath...
阅读全文
摘要:#include <stdio.h>#include <termios.h>int main(){ char s;// FILE *in;// FILE *out; struct termios initial_settings,new_settings;// in=fopen("/dev/tty","r");// out=fopen("/dev/tty","w"); tcgetattr(fileno(stdin),&initial_settings);//保存原来的设置 new_s
阅读全文
摘要:#include <sys/utsname.h>#include <unistd.h>#include <stdio.h>int main(){ char computer[256]; struct utsname uts; if(gethostname(computer,256)!=0 || uname(&uts)<0) { exit(1); } printf("Computer host name is %s\n",computer); printf("Syste...
阅读全文
摘要:#include <sys/types.h>#include <stdio.h>#include <pwd.h>#include <unistd.h>int main(){ uid_t uid; gid_t gid; struct passwd *pw; uid =getuid(); gid=getgid(); printf("User is %s\n",getlogin()); printf("User IDs:uid=%d,gid=%d\n",uid,gid); pw=getpw...
阅读全文
摘要:#include <stdio.h>int main(){ char tmpname[L_tmpnam]; char *filename; FILE *tmpfp; filename=tmpnam(tmpname); printf("tmp file is : %s\n",filename); tmpfp=tmpfile(); if(tmpfp) printf("open a tmp file ok\n"); else ...
阅读全文
摘要:#include <time.h>#include <stdio.h>int main(){ time_t the_time; (void)time(&the_time); printf("The date is: %s",ctime(&the_time)); return 0;}函数原型:#include <time.h>char *ctime(const time_t *timeval);注:上一篇获得的时间是标准格林威治时间。
阅读全文
摘要:#include <time.h>#include <stdio.h>int main(){ struct tm *tm_ptr; time_t the_time; (void) time(&the_time); tm_ptr=gmtime(&the_time); printf("Raw time is %ld\n",the_time); printf("gmtime gives:\n"); printf("date: %02d/%02d/%02d\n", tm_ptr->tm_year,
阅读全文
摘要:#include <time.h>#include <stdio.h>#include <unistd.h>int main(){ int i; time_t the_time; for(i=1;i<=5;i++) { the_time=time((time_t*)0); printf("the time is %ld\n",the_time); sleep(2); } exit(0);}时间是从1970年1月1日开始的。函数原型...
阅读全文
摘要:#include <stdlib.h>#include <stdio.h>#include <string.h>int main(int argc,char **argv){ char *var,*value; if(argc==1||argc>3) { exit(1); } var=argv[1]; value=getenv(var); if(value) printf("Variable %s has value %s\n",var,...
阅读全文
摘要:#include <stdio.h>#include <unistd.h>int main(int argc,char **argv){ int opt; while((opt=getopt(argc,argv,"if:lr"))!=-1) { switch(opt) { case 'i': case 'l': case 'r': ...
阅读全文
摘要:#include <stdio.h>#include <stdlib.h>extern char **environ;int main(){ char **env=environ; while(*env) { printf("%s\n",*env); env++; } exit(0);}主要是environ变量,定义如下#include <stdlib>extern char **environ;
阅读全文
摘要:#include <stdio.h>#include <stdlib.h>int main(){ int c; FILE *in,*out; in=fopen("file.in","r"); out=fopen("file.out","w"); while((c=fgetc(in))!=EOF) fputc(c,out); exit(0);}
阅读全文
摘要:#include <unistd.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <stdlib.h>int main(){ char block[1024]; int in,out; int nread; in=open("file.in",O_RDONLY); out=open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); while ((nread=
阅读全文
摘要:#include <unistd.h>#include <stdlib.h>int main(){ char buffer[128]; int nread; nread=read(0,buffer,128); if(nread==-1) write(2,"A read error has occurred\n",26); if((write(1,buffer,nread))!=nread) write(2,"A write error has occurred\n"...
阅读全文
摘要:#include <unistd.h>#include <stdlib.h>int main(){ if((write(1,"here is some data\n",18))!=18) write(2,"write error\n",12); exit(0);}原型:#include <unistd.h>size_t write(int fildes,const void *buf,size_t nbytes);write()的第一个参数:0标准输入,1标准输出,2标准错误 第二个参数:写入的数据 第三个参数:写入如
阅读全文