Esempio n. 1
0
void run_with_choice_callb(GtkWidget *wdg, gpointer render_struct_ptr) {
//	Render a povray image after choosing a scene file
	gchar *path_n_file, *dir;
	if (wdg)
		if (GTK_IS_TOGGLE_BUTTON(wdg))
			if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(wdg)))
				return;

	
	dir = (gchar *) get_current_dir_name();
	chdir(POV_DIR);

	path_n_file =
		GetFilename(	scene_ext_list,
				NB_SCENE_EXT,
				_("Open"),
				POV_DIR,
				EXISTING_FILE,
				scene_ext_list[0].lbl);
	if (path_n_file)
		SCENE = path_n_file;
	else
		return;
	POV_DIR = get_dir_name(path_n_file,FILESEP);
	// It could be dangerous to free SCENE before reinitializing it
	// It could contain a default #define value (check this... small memory leakage here).
	// However path_n_file is always "malloced" in GetFilename
	run_callb(wdg, render_struct_ptr);

	chdir(dir);
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
  DIR *dip;
  struct dirent *dit;
  
  int i = 0;

  if (argc < 2)
    {
      //prevents argv seg faults
      argv[1] = (char*)malloc(sizeof(char*));
      argv[1] = "";
    }

  char *dirname;
  dirname = get_dir_name(argv, argc);

  //open dir
  if ((dip = opendir(dirname)) == NULL)
    {
      perror("opendir");
      return 0;
    }
 
  char **output;
  output = (char**)malloc(sizeof(char**));

  //read dir
  while ((dit = readdir(dip)) != NULL)
    {
      output[i] = (char*)malloc(sizeof(dit->d_name));
      output[i] = strdup(dit->d_name);
      i++;
    }
 
  print_output(output, i, argv, argc);
 
  //close dirpwd
  //produces seg faults!!!!
  /*
    if (closedir(dip) == -1)
    {
      perror("closedir");
      return 0;
    }
  */
 
  return 1;
}
Esempio n. 3
0
// Recreate the rooms structure by reading the files
struct room* deserialize_rooms() {
        struct room *rooms = malloc(NUM_ROOMS * sizeof(struct room));
        unsigned int room_count = 0;
        char *dir_name = get_dir_name();
        // FIXME: Make sure that directory exists with stat
        chdir(dir_name);
        DIR *dp;
        struct dirent *dir;
        dp = opendir (".");
        assert(dp != NULL);

        while ((dir = readdir (dp))) {
                rooms[room_count] = deserialize_single_room(dir->d_name);
        }

        closedir (dp);

        free(dir_name);
        chdir("..");
        return rooms;
}
Esempio n. 4
0
// Serialize the rooms to disk.
void serialize_rooms(struct room rooms[NUM_ROOMS]) {
        // get the directory name
        char *dir_name = get_dir_name();
        // make the directory with all of the permissions
        mkdir(dir_name, 0777);
        // cd/enter the directory
        chdir(dir_name);
        // For each room make a file
        for (int i = 0; i < NUM_ROOMS; i++) {
                // open the file
                FILE *fp = fopen(rooms[i].name, "w");

                // Write the room name
                fprintf(fp, "ROOM NAME: %s\n", rooms[i].name);

                // Write each of the connections
                for (int j = 0; j < rooms[i].num_conns; j++) {
                        fprintf(fp, "CONNECTION %d: %s\n", j + 1, rooms[j].name);
                }

                // Write the right room type
                switch (rooms[i].type) {
                        case END_ROOM:
                                fprintf(fp, "ROOM TYPE: END_ROOM");
                                break;
                        case MID_ROOM:
                                fprintf(fp, "ROOM TYPE: MID_ROOM");
                                break;
                        case START_ROOM:
                                fprintf(fp, "ROOM TYPE: START_ROOM");
                                break;
                }
                // Close the file
                fclose(fp);
        }
        // Return to the original directory.
        chdir("..");
        // Freedom! Free the directory name
        free(dir_name);
}
Esempio n. 5
0
int open_logfile_obj(obj_t *logfile)
{
/*  (Re)opens the specified 'logfile' obj.
 *  Since this logfile can be re-opened after the daemon has chdir()'d,
 *    it must be specified with an absolute pathname.
 *  Returns 0 if the logfile is successfully opened; o/w, returns -1.
 */
    char  dirname[PATH_MAX];
    int   flags;
    char *now;
    char *msg;

    assert(logfile != NULL);
    assert(is_logfile_obj(logfile));
    assert(logfile->name != NULL);
    assert(logfile->name[0] == '/');
    assert(logfile->aux.logfile.console != NULL);
    assert(logfile->aux.logfile.console->name != NULL);

    if (logfile->fd >= 0) {
        if (close(logfile->fd) < 0)     /* log err and continue */
            log_msg(LOG_WARNING, "Unable to close logfile \"%s\": %s",
                logfile->name, strerror(errno));
        logfile->fd = -1;
    }
    /*  Perform conversion specifier expansion.
     */
    if (logfile->aux.logfile.fmtName) {

        char buf[MAX_LINE];

        if (format_obj_string(buf, sizeof(buf),
          logfile->aux.logfile.console,
          logfile->aux.logfile.fmtName) < 0) {
            log_msg(LOG_WARNING,
                "Unable to open logfile for [%s]: filename exceeded buffer",
                logfile->aux.logfile.console->name);
            logfile->fd = -1;
            return(-1);
        }
        free(logfile->name);
        logfile->name = create_string(buf);
    }
    /*  Create intermediate directories.
     */
    if (get_dir_name(logfile->name, dirname, sizeof(dirname))) {
        (void) create_dirs(dirname);
    }
    /*  Only truncate on the initial open if ZeroLogs was enabled.
     */
    flags = O_WRONLY | O_CREAT | O_APPEND | O_NONBLOCK;
    if (logfile->aux.logfile.gotTruncate) {
        logfile->aux.logfile.gotTruncate = 0;
        flags |= O_TRUNC;
    }
    if ((logfile->fd = open(logfile->name, flags, S_IRUSR | S_IWUSR)) < 0) {
        log_msg(LOG_WARNING, "Unable to open logfile \"%s\": %s",
            logfile->name, strerror(errno));
        return(-1);
    }
    if (logfile->aux.logfile.opts.enableLock
            && (get_write_lock(logfile->fd) < 0)) {
        log_msg(LOG_WARNING, "Unable to lock \"%s\"", logfile->name);
        close(logfile->fd);             /* ignore err on close() */
        logfile->fd = -1;
        return(-1);
    }
    logfile->gotEOF = 0;
    set_fd_nonblocking(logfile->fd);    /* redundant, just playing it safe */
    set_fd_closed_on_exec(logfile->fd);

    now = create_long_time_string(0);
    msg = create_format_string("%sConsole [%s] log opened at %s%s",
        CONMAN_MSG_PREFIX, logfile->aux.logfile.console->name, now,
        CONMAN_MSG_SUFFIX);
    write_obj_data(logfile, msg, strlen(msg), 0);
    free(now);
    free(msg);
    /*
     *  Since the above console log message is not marked "informational",
     *    the test in write_obj_data() to re-init the line state will not
     *    be triggered.  Thusly, we re-initialize the line state here.
     */
    logfile->aux.logfile.lineState = CONMAN_LOG_LINE_INIT;

    DPRINTF((9, "Opened [%s] logfile: fd=%d file=%s.\n",
        logfile->aux.logfile.console->name, logfile->fd, logfile->name));
    return(0);
}
Esempio n. 6
0
static void open_daemon_logfile(server_conf_t *conf)
{
/*  (Re)opens the daemon logfile.
 *  Since this logfile can be re-opened after the daemon has chdir()'d,
 *    it must be specified with an absolute pathname.
 */
    static int  once = 1;
    const char *mode = "a";
    mode_t      mask;
    char        dirname[PATH_MAX];
    FILE       *fp = NULL;
    int         fd;

    assert(conf->logFileName != NULL);
    assert(conf->logFileName[0] == '/');
    assert(!conf->enableForeground);

    /*  Only truncate logfile at startup if needed.
     */
    if (once) {
        if (conf->enableZeroLogs) {
            mode = "w";
        }
        once = 0;
    }
    /*  Perform conversion specifier expansion.
     */
    if (conf->logFmtName) {

        char buf[MAX_LINE];

        if (format_obj_string(buf, sizeof(buf), NULL, conf->logFmtName) < 0) {
            log_msg(LOG_WARNING,
                "Unable to open daemon logfile: filename too long");
            goto err;
        }
        free(conf->logFileName);
        conf->logFileName = create_string(buf);
    }
    /*  Protect logfile against unauthorized writes by removing
     *    group+other write-access from current mask.
     */
    mask = umask(0);
    umask(mask | 022);
    /*
     *  Create intermediate directories.
     */
    if (get_dir_name(conf->logFileName, dirname, sizeof(dirname))) {
        (void) create_dirs(dirname);
    }
    /*  Open the logfile.
     */
    fp = fopen(conf->logFileName, mode);
    umask(mask);

    if (!fp) {
        log_msg(LOG_WARNING, "Unable to open daemon logfile \"%s\": %s",
            conf->logFileName, strerror(errno));
        goto err;
    }
    if ((fd = fileno(fp)) < 0) {
        log_msg(LOG_WARNING,
            "Unable to obtain descriptor for daemon logfile \"%s\": %s",
            conf->logFileName, strerror(errno));
        goto err;
    }
    if (get_write_lock(fd) < 0) {
        log_msg(LOG_WARNING, "Unable to lock daemon logfile \"%s\"",
            conf->logFileName);
        goto err;
    }
    set_fd_closed_on_exec(fd);
    /*
     *  Transition to new log file.
     */
    log_set_file(fp, conf->logFileLevel, 1);
    if (conf->logFilePtr) {
        if (fclose(conf->logFilePtr) == EOF) {
            log_msg(LOG_WARNING, "Unable to close daemon logfile \"%s\"",
                conf->logFileName);
        }
    }
    conf->logFilePtr = fp;
    DPRINTF((9, "Opened logfile \"%s\": fd=%d.\n", conf->logFileName, fd));
    return;

err:
    if (fp) {
        (void) fclose(fp);
    }
    /*  Abandon old log file and go logless.
     */
    log_set_file(NULL, 0, 0);
    if (conf->logFilePtr) {
        if (fclose(conf->logFilePtr) == EOF) {
            log_msg(LOG_WARNING, "Unable to close daemon logfile \"%s\"",
                conf->logFileName);
        }
    }
    conf->logFilePtr = NULL;
    return;
}