内容简介:一直都在用这个函数,今天再详细的复习一下getopt函数主要用于命令行的参数解析,如果我们想对一个程序输入多个数据,就需要配置参数,而传递的参数那么多,就需要程序去逐个取得对应的参数值。getopt函数在
一直都在用这个函数,今天再详细的复习一下
0x01 C语言getopt函数介绍
getopt函数主要用于命令行的参数解析,如果我们想对一个程序输入多个数据,就需要配置参数,而传递的参数那么多,就需要程序去逐个取得对应的参数值。
getopt函数在 #include <unistd.h>
头文件中
定义原型
int getopt(int argc,char * const argv[ ],const char * optstring);
函数说明
getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数optstring 则代 表欲处理的选项字符串。此函数会返回在argv 中下一个的选项字母,此字母会对应参数optstring 中的字 母。如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额 外参数。如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符,如果不 希望getopt()印出错信息,则只要将全域变量opterr设为0即可。
返回值
如果找到符合的参数则返回此参数字母,如果参数不包含在参数optstring 的选项字母则返回“?”字符, 分析结束则返回-1。
0x02 C语言getopt函数范例
需求:需要一个程序能对用户传入用户名、密码进行加密、解密。
那么现在,我们要先设置好参数名;
例如:
- 用户名:u,usernme
- 密码:p,password
先写一个传入用户名和密码的程序,再考虑加密、解密。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct user{
char username[100]; // 用户名
char password[100]; // 密码
};
int main(int argc,char **argv){
struct user info;
memset(info.username,0, sizeof(info.username)); // 清空垃圾数据
memset(info.password,0, sizeof(info.password)); // 清空垃圾数据
int ch;
while((ch = getopt(argc,argv,"u:p:e::"))!= -1){ // u后面跟一个“:”代表是必须要参数值 e后面跟两个“:” 代表参数可有可无,不需要参数
switch(ch){
case 'u':
if(strlen(optarg)>99){ //读取用户名,当ch为u的时候,optarg 就代表当前参数值了。
printf("username to long ... \n");
exit(-1);
}
strcpy(info.username,optarg);
break;
case 'p':
if(strlen(optarg)>99){ //读取密码,当ch为p的时候,optarg 就代表当前参数值了。
printf("password to long ... \n");
exit(-1);
}
strcpy(info.password,optarg);
break;
default:
printf("argv %c \n",ch);
}
}
printf("[*] 用户名 :%s ,密码:%s \n",info.username,info.password);
return 0;
}
执行结果:
./untitled -u admin -p admin [*] 用户名 :admin ,密码:admin
0x03 加密函数
既然我们得到了用户名和密码,接下来就是加密了。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <crypt.h>
struct user{
char username[9]; // 用户名
char password[9]; // 密码
char salt[3];
};
void encryption(struct user * info){
strcpy(info->username,crypt(info->username,info->salt));
strcpy(info->password,crypt(info->password,info->salt));
}
int main(int argc,char **argv){
struct user info;
memset(info.username,0, sizeof(info.username)); // 清空垃圾数据
memset(info.password,0, sizeof(info.password)); // 清空垃圾数据
int ch;
while((ch = getopt(argc,argv,"u:p:s:d::"))!= -1){ // u后面跟一个“:”代表是必须要参数值 e后面跟两个“:” 代表参数可有可无,不需要参数
switch(ch){
case 'u':
strncpy(info.username,optarg,8);
break;
case 'p':
strncpy(info.password,optarg,8);
break;
case 's':
strncpy(info.salt,optarg,2);
break;
default:
printf("argv %c \n",ch);
}
}
encryption(&info);
printf("[*] 用户名 :%s ,密码:%s \n",info.username,info.password);
return 0;
}
输出:
liyingzhe@thunderobot:~$ ./untitled -u admin -p admin -s 1o [*] 用户名 :1oAQMMMCT1orzL7NjOD/1g ,密码:1orzL7NjOD/1g
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
哥德尔、艾舍尔、巴赫
[美] 侯世达 / 严勇、刘皓明、莫大伟 / 商务印书馆 / 1997-5 / 88.00元
集异璧-GEB,是数学家哥德尔、版画家艾舍尔、音乐家巴赫三个名字的前缀。《哥德尔、艾舍尔、巴赫书:集异璧之大成》是在英语世界中有极高评价的科普著作,曾获得普利策文学奖。它通过对哥德尔的数理逻辑,艾舍尔的版画和巴赫的音乐三者的综合阐述,引人入胜地介绍了数理逻辑学、可计算理论、人工智能学、语言学、遗传学、音乐、绘画的理论等方面,构思精巧、含义深刻、视野广阔、富于哲学韵味。 中译本前后费时十余年,......一起来看看 《哥德尔、艾舍尔、巴赫》 这本书的介绍吧!