Advanced Linux Programming 2
Writing Good GNU/Linux Software
1, Interaction With the Parameters
getopt_long.c
1, Interaction With the Parameters
getopt_long.c
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
void print_usage(FILE *stream, int exit_code)
{
fprintf(stream, "Usage:..\n");
exit(exit_code);
}
int main(int argc, char* argv[])
{
int next_option;
/** A string listing valid short options letters. */
const char* const short_options = "ho:v";
/** A array describing valid long options. */
const struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "output", 1, NULL, 'o' },
{ "verbose", 0, NULL, 'v' },
{ NULL, 0, NULL, 0 }
};
const char* output_filename = NULL;
do {
next_option = getopt_long(argc, argv, short_options,
long_options, NULL);
switch(next_option)
{
case 'h': /* -h or --help */
print_usage(stdout, 0);
case 'o': /* -o or --output */
output_filename = optarg;
printf("output file=%s\n", output_filename);
break;
case 'v': /* -v or --verbose */
/* do something */
break;
case '?': /* Invalid option */
print_usage(stderr, 1);
case -1: /* Done with options */
break;
default:
abort();
}
} while(next_option != -1);
return 0;
}tom@debian:~/project/linuxc/ch02$ ./getopt_long tom@debian:~/project/linuxc/ch02$ ./getopt_long -h Usage:.. tom@debian:~/project/linuxc/ch02$ ./getopt_long -o log.txt output file=log.txt tom@debian:~/project/linuxc/ch02$ ./getopt_long --verbose tom@debian:~/project/linuxc/ch02$ ./getopt_long -? ./getopt_long: invalid option -- ? Usage:.. tom@debian:~/project/linuxc/ch02$2, Printing the Execution Environment
#include <stdio.h>
/** The ENVIRON variable contains the environment. */
extern char** environ;
int main()
{
char** var;
for(var=environ; *var!=NULL; ++var)
{
printf("%s\n", *var);
}
return 0;
}3, Interact with environ variables#include <stdio.h>
#include <stdlib.h>
int main()
{
char* server_name = getenv("SERVER_NAME");
if(server_name == NULL)
{
server_name = "localhost";
}
printf("SERVER_NAME: %s\n", server_name);
return 0;
}tom@debian:~/project/linuxc/ch02$ ./client SERVER_NAME: localhost tom@debian:~/project/linuxc/ch02$ export SERVER_NAME="sina.com" tom@debian:~/project/linuxc/ch02$ ./client SERVER_NAME: sina.com tom@debian:~/project/linuxc/ch02$4, Do as the kernel does
1. Allocate the buffer.5, Writing and Using libraries
2. Open the file.
3. Read from the file into the buffer.
4. Close the file.
5. Return the buffer.
tom@debian:~/project/linuxc/ch02$ echo "int f(){return 3;}" > test.c
tom@debian:~/project/linuxc/ch02$ echo "int main() {return f();}" > app.c
tom@debian:~/project/linuxc/ch02$ gcc -c test.c
tom@debian:~/project/linuxc/ch02$ ar cr libtest.a test.o
tom@debian:~/project/linuxc/ch02$ gcc -c app.c
tom@debian:~/project/linuxc/ch02$ gcc -o app app.o -L. -ltest
tom@debian:~/project/linuxc/ch02$ ./app
tom@debian:~/project/linuxc/ch02$ echo $?
3tom@debian:~/project/linuxc/ch02$ gcc -c -fPIC test.c tom@debian:~/project/linuxc/ch02$ gcc -shared -fPIC -o libtest.so test.o tom@debian:~/project/linuxc/ch02$ gcc -o app app.c -L. -ltest tom@debian:~/project/linuxc/ch02$ sudo cp libtest.so /usr/lib/ tom@debian:~/project/linuxc/ch02$ ldd app linux-gate.so.1 => (0xb77c3000) libtest.so => /usr/lib/libtest.so (0xb77a5000) libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb764a000) /lib/ld-linux.so.2 (0xb77c4000) tom@debian:~/project/linuxc/ch02$ ./app tom@debian:~/project/linuxc/ch02$ echo $? 3
标签: Linux


0 条评论:
发表评论
订阅 帖子评论 [Atom]
<< 主页