Skip to content

Instantly share code, notes, and snippets.

@pinebright
Last active February 16, 2018 12:14
Show Gist options
  • Save pinebright/7fd406d217c4aa8baf1f497b16abd540 to your computer and use it in GitHub Desktop.
Save pinebright/7fd406d217c4aa8baf1f497b16abd540 to your computer and use it in GitHub Desktop.
[C]同じ実行ファイルをシンボリックリンク名で使い分けるサンプルコード ref: https://qiita.com/pinebright/items/6eb5254e6d01bdebb46b
$ gcc main.c
$ ln -s a.out test_a
$ ln -s a.out test_b
$ ./test_a
test_a: called
$ ./test_b
test_b: called
#include <stdio.h>
#include <string.h>
struct command_list {
char *command;
int (*func)(int argc, char *argv[]);
};
static int test_a(int argc, char *argv[])
{
printf("test_a: called\n");
return (0);
}
static int test_b(int argc, char *argv[])
{
printf("test_b: called\n");
return (0);
}
static struct command_list list[] = {
{ .command = "test_a", .func = test_a },
{ .command = "test_b", .func = test_b },
{ .command = NULL, .func = NULL }
};
int main(int argc, char *argv[])
{
int i;
int ret = 0;
for (i = 0; list[i].command != NULL; i++) {
if (strstr(argv[0], list[i].command) != NULL) {
ret = list[i].func(argc, argv);
}
}
return (ret);
}
$ gcc main.c
$ ln -s a.out test_a
$ ln -s a.out test_b
$ ./test_a
test_a: called
$ ./test_b
test_b: called
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment