예제 #1
0
파일: shell.c 프로젝트: zhangsl16/Project_1
int do_redirection(char *input)	//redircetion
{
	char *command_path,*real_command;
	char *out_filename,*in_filename;
	char **parsed_command;
	int len,status,i,j,k,back=0,fd_out=0,fd_in=0,flag_out=0,flag_in=0;
	pid_t pid;

	back=is_back(input);
	len=strlen(input);
	
	out_filename=(char *)malloc((len+1)*(sizeof(char)));
	in_filename=(char *)malloc((len+1)*(sizeof(char)));
	real_command=(char *)malloc((len+1)*(sizeof(char)));

	for(i=0; i<len; i++)
	{
		if (input[i]!='>' && input[i]!='<')
			real_command[i]=input[i];
		else
		{
			if (input[i]=='>')
				flag_out=1;
			if (input[i]=='<')
				flag_in=1;
			break;
		}
	}
	
	real_command[i]='\0';
	i++;
	
	if(flag_out==1 && input[i]=='>')
	{
		flag_out=2;
		i++;
	}
	else if (flag_in==1 && input[i]=='<')
	{
		flag_in=2;
		i++;
	}

	while ((input[i]==' ' || input[i]=='	') && i<len)
		i++;
	j=0;
	out_filename[0]='\0';
	in_filename[0]='\0';

	if(flag_out>0)
	{	
		while (i<=len)
		{
			if(input[i]=='<')
			{
				out_filename[j]='\0';
				break;
			}
			out_filename[j]=input[i];
			i++;
			j++;
		}
	}
	
	if(flag_in>0)
	{
		while (i<=len)
		{
			if (input[i]=='>')
			{
				in_filename[j]='\0';
				break;
			}
			in_filename[j]=input[i];
			i++;
			j++;
		}
	}
	if (i<len)
	{
		j=0;
		if (flag_out>0 && input[i]=='<')
		{
			i++;
			flag_in=1;
			if(input[i]=='>')
			{
				flag_in=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len)
			{
				in_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else if (flag_in>0 && input[i]=='>')
		{
			i++;
			flag_out=1;
			if(input[i]=='>')
			{
				flag_out=2;
				i++;
			}

			while ((input[i]==' ' || input[i]=='	') && i<len)
				i++;
			while (i<=len)
			{
				out_filename[j]=input[i];
				i++;
				j++;
			}
		}
		else
		{
			fprintf(stderr,"ERROR!can't find the file!\n");
			return -1;
		}
	}
	
	//for debug
	/*printf("real_command: %s\n",real_command);
	printf("out_filename: %s\n",out_filename);
	printf("in_filename: %s\n",in_filename);*/

	k=number(real_command);
	parsed_command=parse(real_command);
	
	if(strcmp(parsed_command[0],"cd")==0 || strcmp(parsed_command[0], "echo") == 0 || strcmp(parsed_command[0], "viewproc") == 0 || strcmp(parsed_command[0], "time")==0) //build-in functions
	{
			if(strcmp(parsed_command[0], "cd") == 0) //cd
			{
				do_cd(parsed_command);
			}
			else if(strcmp(parsed_command[0], "echo") == 0) //echo
			{
				if (pid=fork() == 0)
				{
					if(flag_out==1)
						fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
					if(flag_out==2)
						fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
					if(fd_out==-1) 
					{
						printf("Open out %s error \n", out_filename);
						return -1;
					}
					if(flag_out>0)
					{
						if(dup2(fd_out, STDOUT_FILENO) == -1)
						{
							fprintf(stderr,"Redirect Standard Out Error !\n");
							exit(1);
						}
					}
					do_echo(real_command);
					exit(1);
				}
				else
				{                     //parent
					if(back==0)
						pid=waitpid(pid, &status, 0);
					else
						pid=waitpid(pid, &status, WNOHANG);
				}
			}
			else if(strcmp(parsed_command[0], "viewproc") == 0) //viewproc
			{
				if (pid=fork() == 0)
				{
					if(flag_out==1)
						fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
					if(flag_out==2)
						fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
					if(fd_out==-1) 
					{
						printf("Open out %s error \n", out_filename);
						return -1;
					}
					if(flag_out>0)
					{
						if(dup2(fd_out, STDOUT_FILENO) == -1)
						{
							fprintf(stderr,"Redirect Standard Out Error !\n");
							exit(1);
						}
					}
					do_viewproc(parsed_command[1]);
					close(flag_out);
					exit(1);
				}
				else
				{                     //parent
					if(back==0)
						pid=waitpid(pid, &status, 0);
					else
						pid=waitpid(pid, &status, WNOHANG);
				}
			}
			else if(strcmp(parsed_command[0], "time") == 0) //time
			{
				gettimeofday(&tpstart,0);
				do_time(real_command);
			}
			for(i=0;i<k;i++)
				free(parsed_command[i]);
			free(parsed_command);
			free(real_command);
			return 1;
	}
	
	command_path=is_file_exist(parsed_command[0]);
	if(command_path==NULL)	//can't find the order
	{
		fprintf(stderr,"This is command is not founded ?!\n");
		// free space
		for(i=0;i<k;i++)
			free(parsed_command[i]);
		free(parsed_command);
		free(real_command);
		return -1;
	}
	
	if((pid = fork()) == 0) 
	{
		if(flag_out==1)
			fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
		if(flag_out==2)
			fd_out = open(out_filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR );
		if(flag_in==1)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
		if(flag_in==2)
			fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
       		if(fd_out==-1) 
		{
			printf("Open out %s error \n", out_filename);
			return -1;
		}
		if(fd_in==-1) 
		{
			fprintf(stderr,"Open in %s error \n", in_filename);
			return -1;
		}

		if(flag_out>0)
		{
			if(dup2(fd_out, STDOUT_FILENO) == -1)
			{
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
		if(flag_in>0)
		{
			if (dup2(fd_in,STDIN_FILENO)==-1)
			{
				fprintf(stderr,"Redirect Standard Out Error !\n");
				exit(1);
			}
		}
			execv(command_path,parsed_command);		
			exit(1);
	}		
	else
	{                     //parent
		if(back==0)
			pid=waitpid(pid, &status, 0);
		else
			pid=waitpid(pid, &status, WNOHANG);
	}
	//free space
	free(out_filename);
	free(in_filename);
	free(command_path);
	for(i=0;i<k;i++)   
		free(parsed_command[i]);
	free(parsed_command);
	return 1;
}
예제 #2
0
파일: prefs.c 프로젝트: Mortal/claws
gint prefs_file_close(PrefFile *pfile)
{
	FILE *fp, *orig_fp;
	gchar *path;
	gchar *tmppath;
	gchar *bakpath = NULL;
	gchar buf[BUFFSIZE];

	cm_return_val_if_fail(pfile != NULL, -1);

	fp = pfile->fp;
	orig_fp = pfile->orig_fp;
	path = pfile->path;

	if (!pfile->writing) {
		fclose(fp);
		g_free(pfile);
		g_free(path);
		return 0;
	}

	if (orig_fp) {
    		while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
			/* next block */
			if (buf[0] == '[') {
				if (fputs(buf, fp)  == EOF) {
					g_warning("failed to write configuration to file");
					prefs_file_close_revert(pfile);
				
					return -1;
				}
				break;
			}
		}
		
		while (fgets(buf, sizeof(buf), orig_fp) != NULL)
			if (fputs(buf, fp) == EOF) {
				g_warning("failed to write configuration to file");
				prefs_file_close_revert(pfile);			
				
				return -1;
			}
		fclose(orig_fp);
	}

	tmppath = g_strconcat(path, ".tmp", NULL);

	
	if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) < 0) {
		FILE_OP_ERROR(tmppath, "fsync");
		fclose(fp);
		claws_unlink(tmppath);
		g_free(path);
		g_free(tmppath);
		return -1;
	}

	if (fclose(fp) == EOF) {
		FILE_OP_ERROR(tmppath, "fclose");
		claws_unlink(tmppath);
		g_free(path);
		g_free(tmppath);
		return -1;
	}

	if (is_file_exist(path)) {
		bakpath = g_strconcat(path, ".bak", NULL);
#ifdef G_OS_WIN32
                claws_unlink(bakpath);
#endif
		if (g_rename(path, bakpath) < 0) {
			FILE_OP_ERROR(path, "rename");
			claws_unlink(tmppath);
			g_free(path);
			g_free(tmppath);
			g_free(bakpath);
			return -1;
		}
	}

#ifdef G_OS_WIN32
        claws_unlink(path);
#endif
	if (g_rename(tmppath, path) < 0) {
		FILE_OP_ERROR(tmppath, "rename");
		claws_unlink(tmppath);
		g_free(path);
		g_free(tmppath);
		g_free(bakpath);
		return -1;
	}

	g_free(pfile);
	g_free(path);
	g_free(tmppath);
	g_free(bakpath);
	return 0;
}
예제 #3
0
/*!
 *\brief	
 */
gint stock_pixbuf_gdk(GtkWidget *window, StockPixmap icon, GdkPixbuf **pixbuf)
{
	StockPixmapData *pix_d;
	static const char *extension[]={".png", ".xpm", NULL};
	int i = 0;
	gboolean theme_changed = FALSE;

	if (pixbuf)
		*pixbuf = NULL;
		
	cm_return_val_if_fail(icon >= 0 && icon < N_STOCK_PIXMAPS, -1);

	pix_d = &pixmaps[icon];

	theme_changed = (strcmp2(pix_d->icon_path, prefs_common.pixmap_theme_path) != 0);
	if (!pix_d->pixbuf || theme_changed) {
		GdkPixbuf *pix = NULL;
		
		if (theme_changed && pix_d->pixmap) {
			g_object_unref(pix_d->pixmap);
			pix_d->pixmap = NULL;
		}

		if (strcmp(prefs_common.pixmap_theme_path, DEFAULT_PIXMAP_THEME) != 0) {
			if (is_dir_exist(prefs_common.pixmap_theme_path)) {
				char *icon_file_name; 
try_next_extension:				
				icon_file_name = g_strconcat(prefs_common.pixmap_theme_path,
							     G_DIR_SEPARATOR_S,
							     pix_d->file,
							     extension[i],
							     NULL);
				if (is_file_exist(icon_file_name)) {
					GError *err = NULL;
					pix = gdk_pixbuf_new_from_file(icon_file_name, &err);	
					if (err) g_error_free(err);
				}					
				if (pix) {
					g_free(pix_d->icon_path);
					pix_d->icon_path = g_strdup(prefs_common.pixmap_theme_path);
				}
				g_free(icon_file_name);
				if (!pix) {
					i++;
					if (extension[i])
						goto try_next_extension;
				}
			} else {
				/* even the path does not exist (deleted between two sessions), so
				set the preferences to the internal theme */
				prefs_common.pixmap_theme_path = g_strdup(DEFAULT_PIXMAP_THEME);
			}
		}
		pix_d->pixbuf = pix;
	}

	if (!pix_d->pixbuf) {
		pix_d->pixbuf = gdk_pixbuf_new_from_xpm_data((const gchar **) pix_d->data);
		if (pix_d->pixbuf) {
			g_free(pix_d->icon_path);
			pix_d->icon_path = g_strdup(DEFAULT_PIXMAP_THEME);	
		}
	}

	cm_return_val_if_fail(pix_d->pixbuf != NULL, -1);

	if (pixbuf)
		*pixbuf = pix_d->pixbuf;

	/* pixbuf should have one ref outstanding */		

	return 0;
}
예제 #4
0
main(int argc ,char **argv)
{
	if(argc!=2)
	{
		printf("please tell me config file!\n");
		exit(0);
	}
	if(!is_file_exist(argv[1]))
	{
		printf("file %s doesn't exist!\n",argv[1]);
		exit(0);
	}

	read_config(argv[1]);
	proclog("starting...");

	int n=0;
	int i=0;
	fd_set fds1;
	struct timeval tv;

	readfd=open("/dev/null",0);
	writefd=open("/dev/null",0);
	dup2(0,readfd);
	dup2(1,writefd);
	close(0);
	close(1);

	if(atexit(&procquit))
	{
		printf("quit code can't be load!\n");
		exit(0);
	}

	mysql_create_connect(&mysql, ip, user,pass,db);

	while(9)
	{
		FD_ZERO(&fds1);
		FD_SET(readfd,&fds1);
		tv.tv_sec = 20;
		tv.tv_usec = 0;
		//proclog("MESSAGE:waiting for belle.....");
		if((n=select(readfd+1,&fds1,NULL,NULL,NULL))>0)
		{
			sgip_read();
		}
		else if(n<0)
		{
			if(errno==EINTR)
				continue;
			proclog( "ALERT:f**k select error\n");
				continue;
		}
		else//return 0
		{
			proclog("MESSAGE:long time no mo!\n");
			exit(0);
		}
	}
}
예제 #5
0
static void prefs_custom_header_val_from_file_cb(void)
{
	gchar *filename = NULL;
	gchar *contents = NULL;
	const gchar *hdr = gtk_entry_get_text(GTK_ENTRY(customhdr.hdr_entry));
	
	if (!strcmp(hdr, "Face"))
		filename = filesel_select_file_open(_("Choose a PNG file"), NULL);
	else if (!strcmp(hdr, "X-Face"))
		filename = filesel_select_file_open(_("Choose an XBM file"), NULL);
	else
		filename = filesel_select_file_open(_("Choose a text file"), NULL);

	if (!strcmp(hdr, "Face") || !strcmp(hdr, "X-Face")) {
		if (filename && is_file_exist(filename)) {
			FILE *fp = NULL;
			gint len;
			gchar inbuf[B64_LINE_SIZE], *outbuf;
			gchar *tmp = NULL;
			gint w, h;
			GdkPixbufFormat *format = gdk_pixbuf_get_file_info(
							filename, &w, &h);
			
			if (format == NULL) {
				alertpanel_error(_("This file isn't an image."));
				g_free(filename);
				return;
			}
			if (w != 48 || h != 48) {
				alertpanel_error(_("The chosen image isn't the correct size (48x48)."));
				g_free(filename);
				return;	
			}
			if (!strcmp(hdr, "Face")) {
				if (get_file_size(filename) > 725) {
					alertpanel_error(_("The image is too big; it must be maximum 725 bytes."));
					g_free(filename);
					return;
				}
				if (g_ascii_strcasecmp("png", gdk_pixbuf_format_get_name(format))) {
					alertpanel_error(_("The image isn't in the correct format (PNG)."));
					g_print("%s\n", gdk_pixbuf_format_get_name(format));
					g_free(filename);
					return;
				}
			} else if (!strcmp(hdr, "X-Face")) {
				gchar *tmp = NULL, *cmd = NULL;
				int i = 0;
				if (g_ascii_strcasecmp("xbm", gdk_pixbuf_format_get_name(format))) {
					alertpanel_error(_("The image isn't in the correct format (XBM)."));
					g_print("%s\n", gdk_pixbuf_format_get_name(format));
					g_free(filename);
					return;
				}
				cmd = g_strdup_printf("compface %s", filename);
				tmp = get_command_output(cmd);
				g_free(cmd);
				if (tmp == NULL || *tmp == '\0') {
					alertpanel_error(_("Couldn't call `compface`. Make sure it's in your $PATH."));
					g_free(filename);
					g_free(tmp);
					return;
				}
				if (strstr(tmp, "compface:")) {
					alertpanel_error(_("Compface error: %s"), tmp);
					g_free(filename);
					g_free(tmp);
					return;
				}
				while (tmp[i]) {
					gchar *tmp2 = NULL;
					if (tmp[i] == ' ') {
						i++; continue;
					} 
					if (tmp[i] == '\r' || tmp[i] == '\n') {
						i++; continue;
					}
					tmp2 = contents;
					contents = g_strdup_printf("%s%c",tmp2?tmp2:"", tmp[i]);
					g_free(tmp2);
					i++;
				}
				g_free(tmp);
				goto settext;
			}

			fp = g_fopen(filename, "rb");
			if (!fp) {
				g_free(filename);
				return;	
			}

			while ((len = fread(inbuf, sizeof(gchar),
					    B64_LINE_SIZE, fp))
			       == B64_LINE_SIZE) {
				outbuf = g_base64_encode(inbuf, B64_LINE_SIZE);

				tmp = contents;
				contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
				g_free(outbuf);
				g_free(tmp);
			}
			if (len > 0 && feof(fp)) {
				tmp = contents;
				outbuf = g_base64_encode(inbuf, len);
				contents = g_strconcat(tmp?tmp:"",outbuf, NULL);
				g_free(outbuf);
				g_free(tmp);
			}
			fclose(fp);
		}
	} else {
		if (!filename)
			return;

		contents = file_read_to_str(filename);
		if (strchr(contents, '\n') || strchr(contents,'\r')) {
			alertpanel_error(_("This file contains newlines."));
			g_free(contents);
			g_free(filename);
			return;
		}
	}
settext:
	if (contents && strlen(contents))
		gtk_entry_set_text(GTK_ENTRY(customhdr.val_entry), contents);
	
	g_free(contents);
	g_free(filename);
}
예제 #6
0
    pattern<T> const make_pattern( Cond cond_, std::string const& dir_path_, std::complex<T> const& thickness_, std::size_t column_index_ = 0 )
    {
        typedef std::size_t size_type;
        typedef T value_type;
        typedef matrix<value_type> matrix_type;
        typedef matrix<size_type> size_matrix_type;
        typedef std::complex<value_type> complex_type;
        pattern<T> pt;
        pt.column_index = column_index_;

        if ( std::abs( std::real(thickness_) ) > T{1.0e-10} ) return pt;
        if ( std::imag(thickness_)  < T{1.0e-10} ) return pt;

        pt.thickness = thickness_;

        //load ug
        std::string const& ug_file_path = dir_path_ + std::string{"/_UgMasterList.txt"};
        std::ifstream ifs_ug{ ug_file_path };
        //if ( !ifs_ug.good() ) return pt;

        if ( ifs_ug.good() ) 
        {
            std::stringstream iss;
            std::copy( std::istreambuf_iterator<char>{ifs_ug}, std::istreambuf_iterator<char>{}, std::ostreambuf_iterator<char>{iss} );
            std::string orig_ug_str = iss.str();
            std::for_each( orig_ug_str.begin(), orig_ug_str.end(), [](char& ch){ if (':' == ch) ch = ' ';} );
            iss.str(orig_ug_str);

            std::vector<T> buff;
            std::copy( std::istream_iterator<T>(iss), std::istream_iterator<T>(), std::back_inserter(buff) );
            matrix<T> ug_tmp{ buff.size(), 1};
            ug_tmp.reshape( buff.size()/3, 3 );
            std::copy( buff.begin(), buff.end(), ug_tmp.begin() );

            pt.ug.resize( ug_tmp.row(), 1 );
            for ( size_type r = 0; r != ug_tmp.row(); ++r )
                pt.ug[r][0] = complex_type{ ug_tmp[r][1], ug_tmp[r][2] };
        }

        size_matrix_type ar_x;
        matrix_type diag_x;
        matrix_type intensity_x;
        for ( size_type index = 0; true; ++index )
        {
            std::string const& id = std::to_string( index );
            std::string const& ar_file_path = dir_path_ + std::string{"/Ar_"} + id + std::string{".txt"};
            std::string const& diag_file_path = dir_path_ + std::string{"/Diag_"} + id + std::string{".txt"};
            std::string const& intensity_file_path = dir_path_ + std::string{"/Intensities_"} + id + std::string{".txt"};

            if ( ! ( is_file_exist( ar_file_path ) && is_file_exist( diag_file_path ) && is_file_exist( intensity_file_path ) ) )
            {
                break; 
            }
            if ( !cond_(index) ) continue;

            ar_x.load( ar_file_path );
            diag_x.load( diag_file_path );
            intensity_x.load( intensity_file_path );
            for ( auto& x : intensity_x )
            {
                if ( x < 0.0 )
                    x = 0.0;
            }

            std::for_each( intensity_x.begin(), intensity_x.end(), [](value_type& x){ x = std::max(x, value_type{0}); } );
            value_type sum = std::accumulate( intensity_x.begin(), intensity_x.end(), value_type{0} );
            std::for_each( intensity_x.begin(), intensity_x.end(), [sum](value_type& x){ x /= sum; } );

            pt.ar.push_back( ar_x );
            pt.diag.push_back( diag_x );
            pt.intensity.push_back( intensity_x );
        }

        pt.ug_size = 0;
        for ( auto&& ar_ : pt.ar )
            pt.ug_size = std::max( pt.ug_size, *std::max_element(ar_.begin(), ar_.end()) );

        pt.ug_size++;

        pt.tilt_size = pt.diag.size();

        return pt;
    }
예제 #7
0
파일: headerview.c 프로젝트: Mortal/claws
static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
{
#ifndef USE_ALT_ADDRBOOK
	GtkWidget *hbox = headerview->hbox;
	GtkWidget *image;
	gchar *filename = NULL;
	GError *error = NULL;
	GdkPixbuf *picture = NULL;
	gint w, h;

	if (!gtk_widget_get_visible(headerview->hbox)) return -1;

	if (headerview->image) {
		gtk_widget_destroy(headerview->image);
		headerview->image = NULL;
	}
	
	filename = addrindex_get_picture_file(msginfo->from);
	
	if (!filename)
		return -1;
	if (!is_file_exist(filename)) {
		g_free(filename);
		return -1;
	}
	gdk_pixbuf_get_file_info(filename, &w, &h);
	
	if (w > 48 || h > 48)
		picture = gdk_pixbuf_new_from_file_at_scale(filename, 
						48, 48, TRUE, &error);
	else
		picture = gdk_pixbuf_new_from_file(filename, &error);

	g_free(filename);
	if (error) {
		debug_print("Failed to import image: \n%s",
				error->message);
		g_error_free(error);
		return -1;
	}
	if (picture)
		image = gtk_image_new_from_pixbuf(picture);
	else 
		return -1;

	g_object_unref(picture);
	if (image) {
		gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
		gtk_widget_show(image);
	}

	headerview->image = image;
	if (image == NULL)
		return -1;
	else 
		return 0;
#else
	/* new address book */
	return -1;
#endif
}