⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arg.c

📁 监控linux指定目录
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <signal.h>
#include <dirent.h>
#include <getopt.h>
#include "arg.h"
#include "func.h"

const struct option long_options[] = 
{
         { "daemon",	0, NULL, 'd' },
         { "archive",	1, NULL, 'a' },
		 { "time",		1, NULL, 't' },
         { "suffix",	1, NULL, 's' },
		 { "file",		0, NULL, 'f' },
		 { "help",		0, NULL, 'h' },
         { NULL,		0, NULL, 0}
};

void print_help()
{
    static const char *help =
        "Usage: ICLES [OPTIONS]\n"
        "Options are:\n"
        " -d				--daemon			run the applicaton in background\n"
		" -a directories			--archive directories		specify directories(separate directories with '#')\n"
	" -t interval(second)		--time interval(second)		specify the interval to scan the directory\n"
        " -s suffix(such as pp, png)	--suffix (such as cpp, png)	specify the file suffix such as png, default is *\n"
	" -f				--file				read the configure data from /home/iclse/iclse.conf\n"
	" -h				--help				show help\n";
    printf("%s", help);
}

int parse_cmd_line(int argc, char *const argv[], runconf *conf)
{
    int option;
	int valid = 0; 
	const char *optstring = "da:t:s:fh";

	while ((option = getopt_long(argc, argv, optstring, long_options, NULL)) != -1)
	{
		switch (option) 
		{
	    case 'd':
			valid = 1;
			setenv("daemon", "yes", 1);
            break;
        case 'a':
            valid = 1;
			strcpy(conf->path, optarg);
            break;
        case 't':
			valid = 1;
            conf->interval = atoi(optarg);
            break;
        case 's':
			valid = 1;
            strcpy(conf->suffix, optarg);
            break;
		case 'f':
			if (access (CONFIG_FILE, R_OK) != 0)
			{
				printf("Cannot find the configure file\n");
				return -1;
			}
			else
			{
				valid = 1;
				strcpy(conf->file, CONFIG_FILE);
				get_param_from_file(conf);
			}
			break;
		case 'h':
			print_help();
            return -1;
		case ':':
			print_help();
            return -1;
		case '?': 
			print_help();
            return -1;
        default:
			print_help();
            return -1;
        }
	}
	
	if (valid)
	{
		return 0;
	}
	else
	{
		return -1;
	}
}

void get_param_from_file(runconf *conf)
{
	char *p = NULL;
	char buf[SIZE] = {0};
	FILE *f = fopen(CONFIG_FILE, "r");
	if (f == NULL)
	{
		//printf("open conf error\n");
		return ;
	}
	while (fgets(buf, SIZE, f))
	{
		trim_end(buf);
		conf->interval;
		
		if (p = strstr(buf, "path"))
		{
			strcpy(conf->path, p + strlen("path="));
			continue;
		}
		else if (p = strstr(buf, "interval"))
		{
			conf->interval = atoi(p + strlen("interval="));
		}
		else if (p = strstr(buf, "suffix"))
		{
			strcpy(conf->suffix, p + strlen("suffix="));
		}
		p = NULL;
	}
	fclose(f);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -