linux下常用的基本设置与操作C语言实现
2024-08-27 22:37:10
获取系统时间
int getlocaltime(struct tm *time) { struct timeval tv; time_t second; struct tm *temp; gettimeofday(&tv, NULL); second = tv.tv_sec; temp = localtime(&second); temp->tm_year += ; temp->tm_mon += ; memcpy(time, temp,sizeof(struct tm)); ; }
设置系统时间
int setlocaltime(struct tm *tm) { time_t timesec; struct timeval tv; struct tm temptm; memcpy(&temptm,tm,sizeof(temptm)); temptm.tm_year = tm->tm_year - ; temptm.tm_yday = ; temptm.tm_isdst = -; /* Be sure to recheck dst. */ timesec = mktime(&temptm); tv.tv_sec = timesec; tv.tv_usec = ; settimeofday(&tv,); //设置系统时间 ; }
获取IP地址
void getip(unsigned char* ip_buf,char * name) { struct ifreq temp; struct sockaddr_in *myaddr; ; ; strcpy(temp.ifr_name, name); ))<) { return NULL; } ret = ioctl(fd, SIOCGIFADDR, &temp); close(fd); ) return NULL; myaddr = (struct sockaddr_in *)&(temp.ifr_addr); strcpy(ip_buf, inet_ntoa(myaddr->sin_addr)); printf("getip:%s\n",ip_buf); }
设置IP地址
int setip(char* ip) { struct ifreq temp; struct sockaddr_in *addr; ; ; strcpy(temp.ifr_name, "eth0"); ))<) { ; } addr = (struct sockaddr_in *)&(temp.ifr_addr); addr->sin_family = AF_INET; addr->sin_addr.s_addr = inet_addr(ip); ret = ioctl(fd, SIOCSIFADDR, &temp); close(fd); ) ; ; }
检查网线是否连接
int check_nic(void) { struct ifreq ifr; ); strcpy(ifr.ifr_name, "eth0"); ) { close(skfd); ; } if(ifr.ifr_flags & IFF_RUNNING) { printf("link up\n"); close(skfd); ; // 网卡已插上网线 } else { printf("link down\n"); close(skfd); ; } }
读取本机MAC地址
int read_mac(unsigned char *buf,char *name) { int sock; int ret; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, ); ) { perror("socket error"); ; } memset(&ifr, , sizeof(ifr)); strcpy(ifr.ifr_name, name); ifr.ifr_name[] = '\0'; ret = ioctl(sock,SIOCGIFHWADDR,&ifr); ) { memcpy(buf, ifr.ifr_hwaddr.sa_data, ); printf("HW address is: %02x:%02x:%02x:%02x:%02x:%02x \n", (unsigned ], (unsigned ], (unsigned ], (unsigned ], (unsigned ], (unsigned ]); } else { perror("ioctl error!\n"); } close(sock); return ret; }
判断文件是否存在
int is_file_exist(const char *file_path) { if(file_path == NULL) ; ) ; ; }
最新文章
- collection of vim vim tutorial for beginner
- STL学习之vector
- 关于配置服务器(IIS7)
- AJAX状态值与状态码
- angularjs入门学习【应用剖析中篇】
- 【转】http响应状态代码含义及跳转的类型
- SQL Server 已提交读快照 测试
- EF|CodeFirst数据并发管理
- JS之代理模式
- CentOS7编译安装MySQL5.7.24
- CentOS 7.6安装桌面
- 解决CPC撰写文档报错问题“无法获取“AxforApplication”控件的窗口句柄。不支持无窗口的 ActiveX 控件”
- LINUX系统配置
- 《精通CSS层叠样式表》
- Excel脱拽或者下拉公式时, 保持公式里单元格数字不变
- Redis的认识和基本操作
- Codeforces 578.C Weakness and Poorness
- python虚拟机运行原理
- (二)Fiddler抓取Firefox、Chrome浏览器上的https协议
- Primer 三四五章
热门文章
- CSS3带小图标垂直下拉菜单
- get_called_class--后期静态绑定(";Late Static Binding";)类的名称
- (十三)linux文件系统详解(基于ext2文件系统)【转】
- java 类装载器原理
- MapReduce-从HBase读取数据处理后再写入HBase
- [转]Android:改变Activity切换方式
- 使用log4j2打印Log,log4j不能打印日志信息,log4j2不能打印日志信息,log4j和logj2,idea控制台信息乱码(文末)
- codeforces675D Tree Construction
- hibernate.cfg.xml_属性";connection.url";_指定字符集
- 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它