Пример #1
0
gchar *unique_filename_simple(const gchar *path)
{
	gchar *unique;
	const gchar *name;
	const gchar *ext;

	if (!path) return NULL;

	name = filename_from_path(path);
	if (!name) return NULL;

	ext = extension_from_path(name);

	if (!ext)
		{
		unique = unique_filename(path, NULL, "_", TRUE);
		}
	else
		{
		gchar *base;

		base = remove_extension_from_path(path);
		unique = unique_filename(base, ext, "_", TRUE);
		g_free(base);
		}

	return unique;
}
Пример #2
0
void flint_remove(char * name)
{
	#if defined(WINCE) || defined(macintosh)
  char * tmp_dir = NULL;
#else
  char * tmp_dir = getenv("TMPDIR");
#endif
  if (tmp_dir == NULL) tmp_dir = "./";
  char * unique = unique_filename(name);
  char * full_name = get_filename(tmp_dir, unique);
  remove(full_name);
  free(unique);
  free(full_name);
}
Пример #3
0
long mergesort_lp_file(char *REL_str, char *NEW_str, char *TMP_str, FILE *COMB)
{
  FILE *NEW = flint_fopen(NEW_str, "r");
  
#if defined(WINCE) || defined(macintosh)
  char * tmp_dir = NULL;
#else
  char * tmp_dir = getenv("TMPDIR");
#endif
  if (tmp_dir == NULL) tmp_dir = "./";
  char * unique = unique_filename(TMP_str);
  char * TMP_name = get_filename(tmp_dir, unique);
  free(unique);
  unique = unique_filename(REL_str);
  char * REL_name = get_filename(tmp_dir, unique);
  free(unique);
  FILE * TMP = fopen(TMP_name,"w");
  FILE * REL = fopen(REL_name,"r");
  if ((!TMP) || (!REL))
  {
     printf("Unable to open temporary file\n");
     abort();
  }
  
  long tp = mergesort_lp_file_internal(REL, NEW, COMB, TMP);
  fclose(REL);
  fclose(NEW);
  
  if (rename(TMP_name,REL_name))
  {
     printf("Cannot rename file %s to %s", TMP_str, REL_str);
     abort();
  } 
  free(TMP_name);
  free(REL_name);
  return tp;
}
Пример #4
0
FILE * flint_fopen(char * name, char * mode)
{
#if defined(WINCE) || defined(macintosh)
  char * tmp_dir = NULL;
#else
  char * tmp_dir = getenv("TMPDIR");
#endif
  if (tmp_dir == NULL) tmp_dir = "./";
  char * unique = unique_filename(name);
  char * full_name = get_filename(tmp_dir, unique);
  FILE * temp_file = fopen(full_name, mode);
  if (!temp_file)
  {
     printf("Unable to open temporary file\n");
     abort();
  }
  free(unique);
  free(full_name);
  return temp_file;
}
Пример #5
0
static gint collection_save_private(CollectionData *cd, const gchar *path)
{
	FILE *f;
	GList *work;
	gchar *tmp_path;
	gchar *pathl;
	mode_t save_mask;

	if (!path && !cd->path) return FALSE;

	if (!path)
		{
		path = cd->path;
		}

	tmp_path = unique_filename(path, ".tmp", "_", 3);
	if (!tmp_path) return FALSE;

	pathl = path_from_utf8(tmp_path);
	save_mask = umask(0077);
	f = fopen(pathl, "w");
	umask(save_mask);
	g_free(pathl);

	if (!f)
		{
		/* file open failed */
		printf("failed to open collection (write) \"%s\"\n", tmp_path);
		g_free(tmp_path);
		return FALSE;
		}

	fprintf(f, "%s collection\n", GQVIEW_COLLECTION_MARKER);
	fprintf(f, "#created with GQview version %s\n", VERSION);

	collection_update_geometry(cd);
	if (cd->window_read)
		{
		fprintf(f, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h);
		}

	work = cd->list;
	while (work)
		{
		CollectInfo *ci = work->data;
		if (fprintf(f, "\"%s\"\n", ci->path) < 0)
			{
			fclose(f);
			printf("Error writing to %s\n", tmp_path);
			unlink_file(tmp_path);
			g_free(tmp_path);
			return FALSE;
			}
		work = work->next;
		}

	fprintf(f, "#end\n");

	fclose(f);

	copy_file_attributes(path, tmp_path, TRUE, FALSE);
	if (!rename_file(tmp_path, path))
		{
		printf("collection save unable to rename %s to %s\n", tmp_path, path);
		unlink_file(tmp_path);
		g_free(tmp_path);
		return FALSE;
		}

	g_free(tmp_path);

	if (!cd->path || strcmp(path, cd->path) != 0)
		{
		gchar *buf = cd->path;
		cd->path = g_strdup(path);
		path = cd->path;
		g_free(buf);

		g_free(cd->name);
		cd->name = g_strdup(filename_from_path(cd->path));

		collection_path_changed(cd);
		}

	cd->changed = FALSE;

	return TRUE;
}
Пример #6
0
static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
{
	gchar *base_path;
	gchar *tmp_path;
	gint fail;

	if (!tl->cache_enable || tl->cache_hit) return;
	if (tl->thumb_path) return;

	if (!pixbuf)
		{
		/* local failures are not stored */
		if (tl->cache_local) return;

		pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
		fail = TRUE;
		}
	else
		{
		g_object_ref(G_OBJECT(pixbuf));
		fail = FALSE;
		}

	tl->thumb_path = thumb_loader_std_cache_path(tl, tl->cache_local, pixbuf, fail);
	if (!tl->thumb_path)
		{
		g_object_unref(G_OBJECT(pixbuf));
		return;
		}
	tl->thumb_path_local = tl->cache_local;

	/* create thumbnail dir if needed */
	base_path = remove_level_from_path(tl->thumb_path);
	if (tl->cache_local)
		{
		if (!isdir(base_path))
			{
			struct stat st;
			gchar *source_base;

			source_base = remove_level_from_path(tl->source_path);
			if (stat_utf8(source_base, &st))
				{
				cache_ensure_dir_exists(base_path, st.st_mode);
				}
			g_free(source_base);
			}
		}
	else
		{
		cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER);
		}
	g_free(base_path);

	if (debug)
		{
		printf("thumb saving: %s\n", tl->source_path);
		printf("       saved: %s\n", tl->thumb_path);
		}

	/* save thumb, using a temp file then renaming into place */
	tmp_path = unique_filename(tl->thumb_path, ".tmp", "_", 2);
	if (tmp_path)
		{
		const gchar *mark_uri;
		gchar *mark_app;
		gchar *mark_mtime;
		gchar *pathl;
		gint success;

		mark_uri = (tl->cache_local) ? tl->local_uri :tl->thumb_uri;

		mark_app = g_strdup_printf("GQview %s", VERSION);
		mark_mtime = g_strdup_printf("%lu", tl->source_mtime);

		pathl = path_from_utf8(tmp_path);
		success = gdk_pixbuf_save(pixbuf, pathl, "png", NULL,
					  THUMB_MARKER_URI, mark_uri,
					  THUMB_MARKER_MTIME, mark_mtime,
					  THUMB_MARKER_APP, mark_app,
					  NULL);
		if (success)
			{
			chmod(pathl, (tl->cache_local) ? tl->source_mode : THUMB_PERMS_THUMB);
			success = rename_file(tmp_path, tl->thumb_path);
			}

		g_free(pathl);

		g_free(mark_mtime);
		g_free(mark_app);

		g_free(tmp_path);
		if (!success && debug)
			{
			printf("thumb save failed: %s\n", tl->source_path);
			printf("            thumb: %s\n", tl->thumb_path);
			}

		}

	g_object_unref(G_OBJECT(pixbuf));
}
Status
SetAuthentication(int count, IceListenObj *listenObjs, 
		  IceAuthDataEntry **authDataEntries)
{
    FILE	*addfp = NULL;
    FILE	*removefp = NULL;
    const char	*path;
    mode_t	original_umask;
    char	command[256];
    int		i;
#ifdef HAVE_MKSTEMP
    int         fd;
#endif

    original_umask = umask (0077);	/* disallow non-owner access */

    path = getenv ("SM_SAVE_DIR");
    if (!path)
    {
	path = getenv ("HOME");
	if (!path)
	    path = ".";
    }
#ifndef HAVE_MKSTEMP
    if ((addAuthFile = unique_filename (path, ".xsm")) == NULL)
	goto bad;

    if (!(addfp = fopen (addAuthFile, "w")))
	goto bad;
    fcntl(fileno(addfp), F_SETFD, FD_CLOEXEC);

    if ((remAuthFile = unique_filename (path, ".xsm")) == NULL)
	goto bad;

    if (!(removefp = fopen (remAuthFile, "w")))
	goto bad;
    fcntl(fileno(removefp), F_SETFD, FD_CLOEXEC);
#else
    if ((addAuthFile = unique_filename (path, ".xsm", &fd)) == NULL)
	goto bad;
    
    if (!(addfp = fdopen(fd, "wb"))) 
	goto bad;
    fcntl(fileno(addfp), F_SETFD, FD_CLOEXEC);

    if ((remAuthFile = unique_filename (path, ".xsm", &fd)) == NULL)
	goto bad;
    
    if (!(removefp = fdopen(fd, "wb"))) 
	goto bad;
    fcntl(fileno(removefp), F_SETFD, FD_CLOEXEC);
#endif

    if ((*authDataEntries = (IceAuthDataEntry *) XtMalloc (
	count * 2 * sizeof (IceAuthDataEntry))) == NULL)
	goto bad;

    for (i = 0; i < count * 2; i += 2)
    {
	(*authDataEntries)[i].network_id =
	    IceGetListenConnectionString (listenObjs[i/2]);
	(*authDataEntries)[i].protocol_name = "ICE";
	(*authDataEntries)[i].auth_name = "MIT-MAGIC-COOKIE-1";

	(*authDataEntries)[i].auth_data =
	    IceGenerateMagicCookie (MAGIC_COOKIE_LEN);
	(*authDataEntries)[i].auth_data_length = MAGIC_COOKIE_LEN;

	(*authDataEntries)[i+1].network_id =
	    IceGetListenConnectionString (listenObjs[i/2]);
	(*authDataEntries)[i+1].protocol_name = "XSMP";
	(*authDataEntries)[i+1].auth_name = "MIT-MAGIC-COOKIE-1";

	(*authDataEntries)[i+1].auth_data = 
	    IceGenerateMagicCookie (MAGIC_COOKIE_LEN);
	(*authDataEntries)[i+1].auth_data_length = MAGIC_COOKIE_LEN;

	write_iceauth (addfp, removefp, &(*authDataEntries)[i]);
	write_iceauth (addfp, removefp, &(*authDataEntries)[i+1]);

	IceSetPaAuthData (2, &(*authDataEntries)[i]);

	IceSetHostBasedAuthProc (listenObjs[i/2], HostBasedAuthProc);
    }

    fclose (addfp);
    fclose (removefp);

    umask (original_umask);

    snprintf (command, sizeof(command), "iceauth source %s", addAuthFile);
    execute_system_command (command);

    remove (addAuthFile);

    return (1);

 bad:

    if (addfp)
	fclose (addfp);

    if (removefp)
	fclose (removefp);

    if (addAuthFile)
    {
	remove (addAuthFile);
	free (addAuthFile);
    }
    if (remAuthFile)
    {
	remove (remAuthFile);
	free (remAuthFile);
    }

    return (0);
}
Пример #8
0
void PreviewLoader::Impl::startLoading(bool wait)
{
	if (pending_.empty() || !pconverter_)
		return;

	// Only start the process off after the buffer is loaded from file.
	if (!buffer_.isFullyLoaded())
		return;

	LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()");

	// As used by the LaTeX file and by the resulting image files
	string const directory = buffer_.temppath();

	string const filename_base = unique_filename(directory);

	// Create an InProgress instance to place in the map of all
	// such processes if it starts correctly.
	InProgress inprogress(filename_base, pending_, pconverter_->to);

	// clear pending_, so we're ready to start afresh.
	pending_.clear();

	// Output the LaTeX file.
	FileName const latexfile(filename_base + ".tex");

	// we use the encoding of the buffer
	Encoding const & enc = buffer_.params().encoding();
	ofdocstream of;
	try { of.reset(enc.iconvName()); }
	catch (iconv_codecvt_facet_exception const & e) {
		LYXERR0("Caught iconv exception: " << e.what()
			<< "\nUnable to create LaTeX file: " << latexfile);
		return;
	}

	TexRow texrow;
	otexstream os(of, texrow);
	OutputParams runparams(&enc);
	LaTeXFeatures features(buffer_, buffer_.params(), runparams);

	if (!openFileWrite(of, latexfile))
		return;

	if (!of) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					<< "Unable to create LaTeX file\n" << latexfile);
		return;
	}
	of << "\\batchmode\n";
	dumpPreamble(os);
	// handle inputenc etc.
	buffer_.params().writeEncodingPreamble(os, features);
	of << "\n\\begin{document}\n";
	dumpData(of, inprogress.snippets);
	of << "\n\\end{document}\n";
	of.close();
	if (of.fail()) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					 << "File was not closed properly.");
		return;
	}

	double const font_scaling_factor = 
		buffer_.isExporting() ? 75.0 * buffer_.params().html_math_img_scale 
			: 0.01 * lyxrc.dpi * lyxrc.zoom * lyxrc.preview_scale_factor;

	// FIXME XHTML 
	// The colors should be customizable.
	ColorCode const bg = buffer_.isExporting() 
	               ? Color_white : PreviewLoader::backgroundColor();
	ColorCode const fg = buffer_.isExporting() 
	               ? Color_black : PreviewLoader::foregroundColor();
	// The conversion command.
	ostringstream cs;
	cs << pconverter_->command
	   << " " << quoteName(latexfile.toFilesystemEncoding())
	   << " --dpi " << int(font_scaling_factor)
	   << " --fg " << theApp()->hexName(fg)
	   << " --bg " << theApp()->hexName(bg);
	// FIXME what about LuaTeX?
	if (buffer_.params().useNonTeXFonts)
		cs << " --latex=xelatex";
	if (buffer_.params().encoding().package() == Encoding::japanese)
		cs << " --latex=platex";
	if (buffer_.params().bibtex_command != "default")
		cs << " --bibtex=" << quoteName(buffer_.params().bibtex_command);
	else if (buffer_.params().encoding().package() == Encoding::japanese)
		cs << " --bibtex=" << quoteName(lyxrc.jbibtex_command);
	else
		cs << " --bibtex=" << quoteName(lyxrc.bibtex_command);
	if (buffer_.params().bufferFormat() == "lilypond-book")
		cs << " --lilypond";

	string const command = libScriptSearch(cs.str());

	if (wait) {
		ForkedCall call(buffer_.filePath());
		int ret = call.startScript(ForkedProcess::Wait, command);
		static int fake = (2^20) + 1;
		int pid = fake++;
		inprogress.pid = pid;
		inprogress.command = command;
		in_progress_[pid] = inprogress;
		finishedGenerating(pid, ret);
		return;
	}

	// Initiate the conversion from LaTeX to bitmap images files.
	ForkedCall::SignalTypePtr
		convert_ptr(new ForkedCall::SignalType);
	convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));

	ForkedCall call(buffer_.filePath());
	int ret = call.startScript(command, convert_ptr);

	if (ret != 0) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					<< "Unable to start process\n" << command);
		return;
	}

	// Store the generation process in a list of all such processes
	inprogress.pid = call.pid();
	inprogress.command = command;
	in_progress_[inprogress.pid] = inprogress;
}
Пример #9
0
static
Status SetAuthentication(
	int count,FIceListenObj *listenObjs,FIceAuthDataEntry **authDataEntries)
{
	FILE *addfp = NULL;
	FILE *removefp = NULL;
	char *path;
	int original_umask;
	char command[256];
	int i;
	int fd;

	if (!SessionSupport)
	{
		return 0;
	}

	original_umask = umask (0077);	/* disallow non-owner access */

	path = (char *)getenv("SM_SAVE_DIR");
	if (!path)
	{
		path = getenv("HOME");
	}

#ifdef HAVE_GETPWUID
	if (!path)
	{
		struct passwd *pwd;

		pwd = getpwuid(getuid());
		if (pwd)
		{
			path = pwd->pw_dir;
		}
	}
#endif
	if (!path)
	{
		path = ".";
	}
	if ((addAuthFile = unique_filename (path, ".fsm-", &fd)) == NULL)
	{
		goto bad;
	}
	if (!(addfp = fdopen(fd, "wb")))
	{
		goto bad;
	}
	if ((remAuthFile = unique_filename (path, ".fsm-", &fd)) == NULL)
	{
		goto bad;
	}
	if (!(removefp = fdopen(fd, "wb")))
	{
		goto bad;
	}

	*authDataEntries = (FIceAuthDataEntry *) safemalloc(
		count * 2 * sizeof (FIceAuthDataEntry));

	for (i = 0; i < count * 2; i += 2)
	{
		(*authDataEntries)[i].network_id =
			FIceGetListenConnectionString(listenObjs[i/2]);
		(*authDataEntries)[i].protocol_name = "ICE";
		(*authDataEntries)[i].auth_name = "MIT-MAGIC-COOKIE-1";

		(*authDataEntries)[i].auth_data =
			FIceGenerateMagicCookie (MAGIC_COOKIE_LEN);
		(*authDataEntries)[i].auth_data_length = MAGIC_COOKIE_LEN;

		(*authDataEntries)[i+1].network_id =
			FIceGetListenConnectionString(listenObjs[i/2]);
		(*authDataEntries)[i+1].protocol_name = "XSMP";
		(*authDataEntries)[i+1].auth_name = "MIT-MAGIC-COOKIE-1";

		(*authDataEntries)[i+1].auth_data =
			FIceGenerateMagicCookie (MAGIC_COOKIE_LEN);
		(*authDataEntries)[i+1].auth_data_length = MAGIC_COOKIE_LEN;

		write_iceauth(addfp, removefp, &(*authDataEntries)[i]);
		write_iceauth(addfp, removefp, &(*authDataEntries)[i+1]);

		FIceSetPaAuthData(2, &(*authDataEntries)[i]);
		FIceSetHostBasedAuthProc(listenObjs[i/2], HostBasedAuthProc);
	}

	fclose(addfp);
	fclose(removefp);

	umask (original_umask);

	sprintf (command, "iceauth source %s", addAuthFile);
	{
		int n;

		n = system(command);
		(void)n;
	}

	unlink (addAuthFile);

	return 1;

 bad:

	if (addfp)
	{
		fclose (addfp);
	}

	if (removefp)
	{
		fclose (removefp);
	}

	if (addAuthFile)
	{
		unlink (addAuthFile);
		free (addAuthFile);
	}
	if (remAuthFile)
	{
		unlink (remAuthFile);
		free (remAuthFile);
	}

	return 0;
}