示例#1
0
文件: setting.c 项目: alick/mytunet
// NOTE:
// filename = NULL to read/write global config ( ~/.MyTunet )
CHAR *setting_load_file(CHAR *filename)
{
	CHAR *tmpbuf;
	CHAR globalconfigfile[1024];
	CHAR *p;
	FILE *fp;
	INT filesize;
	CHAR *comment = "# WARNING: This file is created by MyTunet. DO NOT edit this file!\r\n";

	if(!filename)
	{
		memset(globalconfigfile, 0, sizeof(globalconfigfile));
		strncpy(globalconfigfile, os_get_home_dir(), sizeof(globalconfigfile));

		strncat(globalconfigfile, "/.MyTunet/MyTunet", sizeof(globalconfigfile));

		filename = globalconfigfile;
	}

	fp = fopen(filename, "rb+");
	if(!fp)
	{
		fp = fopen(filename, "wb+");
		if(!fp)
		{
			os_mkdir(globalconfigfile, TRUE);
			fp = fopen(filename, "wb+");
		}
		
		if(fp) fclose(fp);

		tmpbuf = os_new(CHAR, strlen(comment) + 1);
		strcpy(tmpbuf, comment);
		return tmpbuf;

	}

	fseek(fp, 0, SEEK_END);
	filesize = ftell(fp);
	fseek(fp, 0, SEEK_SET);

	tmpbuf = os_new(CHAR, filesize + 1);
	fread(tmpbuf, filesize, 1, fp);
	fclose(fp);

	tmpbuf[filesize] = 0;
	while( (p=strchr(tmpbuf, '\r')) != NULL) *p ='\n';

	return tmpbuf;
}
示例#2
0
文件: setting.c 项目: alick/mytunet
// NOTE:
// filename = NULL to read/write global config ( ~/.MyTunet )
VOID setting_save_file(CHAR *filename, CHAR *buf)
{
	CHAR *p;
	FILE *fp;
	CHAR globalconfigfile[1024];

	if(!filename)
	{
		memset(globalconfigfile, 0, sizeof(globalconfigfile));
		strncpy(globalconfigfile, os_get_home_dir(), sizeof(globalconfigfile));
		strncat(globalconfigfile, "/.MyTunet/MyTunet", sizeof(globalconfigfile));

		filename = globalconfigfile;
	}

	
    p = buf;

	while( (p=strstr(buf,"\n\n")) != NULL)
	{
		if(*(p-1) != '\r') 
			*p = '\r';
		else
			*(p+1) = '\r';
	}

	p = buf + strlen(buf) - 1;
	if(p)
	{
		while((*p == '\r' || *p =='\n') && (p != buf)) p--;
		p++;
		if(*p) p++;
		if(*p) p++;
		*p = 0;
	}
	
	if( (fp = fopen(filename, "wb+")) == NULL ) 
	{
		os_mkdir(filename, TRUE);
		if( (fp = fopen(filename, "wb+")) == NULL )
			return;
	}
	fwrite(buf, strlen(buf), 1, fp);
	fclose(fp);
}
示例#3
0
文件: os.cpp 项目: JamesLinus/genesis
void os_get_app_dir(ByteBuffer &out) {
    ByteBuffer home_dir;
    os_get_home_dir(home_dir);

    os_path_join(out, home_dir, ".genesis");
}
示例#4
0
文件: xdcmd.c 项目: fachat/XD2031
int main(int argc, const char *argv[]) {

	const char *socket = NULL;

	mem_init();
	atexit(mem_exit);

	terminal_init();

	// --------------------------------------------
	// parse the parameters

        // Check -v (verbose) first to enable log_debug()
        // when processing other options
        for (int i=1; i < argc; i++) if (!strcmp("-v", argv[i])) set_verbose(1);

	int p = 1;
	while (p < argc && argv[p][0]=='-') {

		switch(argv[p][1]) {
		case 0:
			// single '-' option ends parameter processing
			p++;
			goto endpars;
		case 'v':
                	assert_single_char(argv[p]);
			// verbose is already checked above
			set_verbose(1);
			break;
            	case '?':
                	assert_single_char(argv[p]);
                	usage(EXIT_SUCCESS);    /* usage() exits already */
                	break;
		case 'T':
			assert_single_char(argv[p]);
                	if (p < argc-2) {
                  		p++;
                  		socket = argv[p];
                  		log_info("main: tools socket = %s\n", socket);
                	} else {
                  		log_error("-T requires <socket name> parameter\n");
                  		exit(EXIT_RESPAWN_NEVER);
                	}
                	break;
            	default:
                	log_error("Unknown command line option %s\n", argv[p]);
                	usage(EXIT_RESPAWN_NEVER);
                	break;
		}
		p++;
	}
endpars:
	// --------------------------------------------
	// open the socket

        if (socket == NULL) {
                const char *home = os_get_home_dir();
                socket = malloc_path(home, ".xdtools");
        }

	int sockfd = socket_open(socket, 0);
	if (sockfd < 0) {
		log_errno("Could not open socket %s\n", socket);
		mem_free(socket);
		exit(1);
	}
	mem_free(socket);

	// --------------------------------------------
	// find our command either as ending part of the name of the binary ...
	

	const cmdtab_t *cmd = NULL;
	int l = strlen(argv[0]);

	for (int i = 0; i < numcmds; i++) {
		int cl = strlen(cmdtab[i].name);

		if ((cl <= l)
			&& !strcmp(cmdtab[i].name, &argv[0][l-cl])) {
			cmd = &cmdtab[i];
			break;
		}
	}
	// ... or as command line parameter
	if (p < argc) {
		l = strlen(argv[p]);
		if (cmd == NULL) {
			for (int i = 0; i < numcmds; i++) {
				int cl = strlen(cmdtab[i].name);

				if ((cl <= l)
					&& !strcmp(cmdtab[i].name, argv[p])) {
					cmd = &cmdtab[i];
					p++;
					break;
				}
			}
		}
	}

	if (cmd == NULL) {
		log_error("Could not identify any of the commands!\n");
		usage(1);
	}	

	int rv = cmd->func(sockfd, argc-p, argv+p);


	close(sockfd);

	return rv;
}