一个简单的小程序模拟一条 linux命令 生成的一种方法。
my_bin.c
#include "stdio.h"
#define MAX_SIZE 10
#define TRUE 1
#define FALSE 0
void usage()
{
printf("********************* usage *********************\n");
/*
* usage:
*/
printf("\n");
return ;
}
int main(int argc, void **argv)
{
char c;
printf("Welcome to my bin.\n");
while (TRUE)
{
c = getchar();
if ('\n' == c)
continue;
switch (c)
{
case 'p':
{
printf("hello\n\n");
break;
}
case 'h':
{
usage();
break;
}
case 'q':
{
printf("bye bye\n\n");
return 0;
break;
}
default:
{
printf("your input is wrong!\n");
usage();
break;
}
}
}
return 0;
}
makefile文件:
CC=gcc
INSTDIR=/usr/local/bin
my_bin : my_bin.o
$(CC) -o my_bin my_bin.o
my_bin.o : my_bin.c public.h
$(CC) -c my_bin.c
install : my_bin
@if [ -d $(INSTDIR) ]; then \
cp my_bin $(INSTDIR); \
chmod a+x $(INSTDIR)/my_bin; \
echo "install to $(INSTDIR) success"; \
else \
echo "sorry, $(INSTDIR) is not exist"; \
fi
clean:
-rm my_bin my_bin.o $(INSTDIR)/my_bin
编译安装之前:
linux:/mnt/hgfs/vmware-share # my_bin
bash: /usr/local/bin/my_bin: 没有那个文件或目录
编译并且安装:
linux:/mnt/hgfs/vmware-share # make install
gcc -c my_bin.c
gcc -o my_bin my_bin.o
install to /usr/local/bin/ success
运行结果:
linux:/mnt/hgfs/vmware-share # my_bin
Welcome to my bin.
p
hello
a
your input is wrong!
********************* usage *********************
h
********************* usage *********************
q
bye bye
linux:/mnt/hgfs/vmware-share #