示例#1
0
文件: ft_cd.c 项目: Snosky/minishell
static void	ft_cd_do(t_list *envlst, char *path, int show)
{
	char	*buf;
	int		len;
	int		t;

	buf = NULL;
	t = test_pwd(path);
	if (t == 1)
	{
		chdir(path);
		len = ft_strlen(path);
		buf = getcwd(buf, len);
		if (!buf)
			buf = clear_path(path);
		if (find_env(envlst, "PWD"))
			update_env(envlst, "OLDPWD", find_env(envlst, "PWD")->value);
		update_env(envlst, "PWD", buf);
		if (show)
			ft_putendl(remove_home(envlst, buf));
	}
	else if (t == -1)
		err_cd_perm(remove_home(envlst, path));
	else
		err_cd_no_file(remove_home(envlst, path));
}
示例#2
0
文件: _builtin_cd.c 项目: kokaz/42sh
int		_builtin_cd(char **cmd, t_shenv *shenv)
{
  int		ret;
  char		*cwd;
  static char	*oldpwd = NULL;

  cwd = my_getcwd();
  if (cmd[1] == NULL)
    ret = chdir_home();
  else if (!strcmp(cmd[1], "-"))
    {
      if ((ret = my_chdir(oldpwd)) == EXIT_SUCCESS)
	{
	  printf("%s\n", (oldpwd == NULL) ? my_getcwd() : oldpwd);
	  xfree(1, &oldpwd);
	  oldpwd = cwd;
	  update_env(oldpwd, shenv);
	  return (EXIT_SUCCESS);
	}
      return (EXIT_FAILURE);
    }
  else
    ret = my_chdir(cmd[1]);
  update_env(cwd, shenv);
  if (ret == EXIT_SUCCESS && xfree(1, &oldpwd))
    oldpwd = cwd;
  return ((ret) ? (EXIT_FAILURE) : (EXIT_SUCCESS));
}
示例#3
0
int			ft_setenv(t_list *envlst, char **args)
{
	if (args[1] == NULL || args[2] == NULL)
		return (err_setenv());
	update_env(envlst, args[1], args[2]);
	return (1);
}
示例#4
0
void 		my_set_env(t_mysh *mysh)
{
  char 		*param_in_env;
  int 		i;

  i = 0;
  while (mysh->tab_com[i])
    i++;
  if (comp_str(mysh->tab_com[0], "setenv") == 1 && mysh->tab_com[1] && i < 4)
    {
      if ((param_in_env = src_env(mysh->env, mysh->tab_com[1])))
	update_env(mysh);
      else
	add_env(mysh);
      if (param_in_env)
	free (param_in_env);
    }
  else if (comp_str(mysh->tab_com[0], "setenv") == 1 && i == 1)
    {
      i = -1;
      while (mysh->env && mysh->env[++i])
	my_printf("%s\n", mysh->env[i]);
    }
  else if (comp_str(mysh->tab_com[0], "setenv") == 1 && i >= 4)
    my_error("setenv: Too many arguments.\n");
}
示例#5
0
void			apply_cd(char *command)
{
	char	**av;
	int		size;
	t_env	*env;

	av = ft_strsplit(command, ' ');
	size = av_size(av);
	env = init_env(NULL, 0);
	if (!parse_cd(av))
	{
		if (size == 1 || (size == 2 && !ft_strcmp(av[1], "-P"))
			|| (size == 2 && !ft_strcmp(av[1], "-L"))
			|| (size == 2 && !ft_strcmp(av[1], "~"))
			|| (size == 3 && !ft_strcmp(av[2], "~")))
		{
			if (!chdir(env->home))
				update_env(&env, "/nfs");
			set_flag(&env, 0);
		}
		else
			go_newdir(env, av[size - 1]);
		if (g_pid.built == -1)
			g_pid.built = 1;
	}
	del_av(av);
}
示例#6
0
文件: built_cd.c 项目: kasou/42sh
int				cd_fct(char *path)
{
	int			opt_p;
	char		*oldpwd;
	char		**tmp;
	t_env		*env;

	tmp = ft_strsplit(path, ' ');
	if ((opt_p = check_cd_path(&path, tmp)) < 0)
	{
		ft_putstr("cd: arguments invalid");
		del_tab(tmp);
		path = NULL;
		return (0);
	}
	oldpwd = NULL;
	oldpwd = getcwd(oldpwd, 0);
	env = init_env(NULL);
	if (change_directory(&path, env) != -1)
		update_env(path, oldpwd, env);
	else
		return (0);
	del_tab(tmp);
	return (1);
}
示例#7
0
文件: ft_cd.c 项目: mzane42/ft_sh1
void			ft_cd(char *buf, t_params *p)
{
	char	**av;
	char	*new_dir;
	int		ret;
	int		size;

	ret = 0;
	av = ft_split(buf);
	new_dir = NULL;
	size = av_size(av);
	if (size > 2)
		ft_putendl("cd: syntax error");
	else if (size == 1 || (size == 2 && !ft_strcmp(av[1], "~")))
		new_dir = ft_strdup(p->home);
	else
		new_dir = get_newdir(p, av[1]);
	if (new_dir && !(ret = check_path(new_dir)) && !chdir(new_dir))
		update_env(p, new_dir);
	else if (new_dir && !(ret = check_path(new_dir)) && chdir(new_dir) == -1)
		ft_print_error("cd: not a directory: ", av[1]);
	if (new_dir && ret && av[1])
		improper_path(ret, av[1]);
	ft_strdel(&new_dir);
	del_av(av);
}
示例#8
0
static void		cd_proc(t_env *env, char *new_dir, char *av)
{
	int		ret;

	if (new_dir && !(ret = check_path(new_dir)) && !chdir(new_dir))
	{
		if (av[0] == '/')
			update_env(&env, new_dir);
		else
			update_env(&env, "/nfs");
	}
	if (ret)
	{
		improper_path(ret, av);
		g_pid.built = 0;
	}
	set_flag(&env, 0);
}
示例#9
0
static int
cr_callback(void *unused)
{
	int rc;
	char *step_image_dir = NULL;

	rc = CR_CHECKPOINT_READY;
	if (step_launched) {
		step_image_dir = get_step_image_dir(1);
		if (step_image_dir == NULL) {
			error ("failed to get step image directory");
			rc = CR_CHECKPOINT_PERM_FAILURE;
		} else if (slurm_checkpoint_tasks(jobid,
						  stepid,
						  time(NULL), /* timestamp */
						  step_image_dir,
						  60, /* wait */
						  nodelist) != SLURM_SUCCESS) {
			error ("failed to checkpoint step tasks");
			rc = CR_CHECKPOINT_PERM_FAILURE;
		}
		xfree(step_image_dir);
	}
	rc = cr_checkpoint(rc);	/* dump */

	if (rc < 0) {
		fatal("checkpoint failed: %s", cr_strerror(errno));
	} else if (rc == 0) {
		/* continue, nothing to do */
	} else {
		/* restarted */
		if (srun_pid) { /* srun forked */
			if (step_launched) {
				step_image_dir = get_step_image_dir(0);
				if (step_image_dir == NULL) {
					fatal("failed to get step image directory");
				}
				update_env("SLURM_RESTART_DIR", step_image_dir);
				xfree(step_image_dir);
			}

			if (fork_exec_srun()) {
				fatal("failed fork/exec srun");
			}
		}

		/* XXX: step_launched => listen_fd valid */
		step_launched = 0;

		debug2("step not launched.");

		pthread_cond_broadcast(&step_launch_cond);
	}

	return 0;
}
示例#10
0
int _tmain(int argc, _TCHAR* argv[]) {
	// arguments
	if (argc > 1) {
		CString s(argv[1]);
		if (!s.Compare(L"-v") || !s.Compare(L"--version")) {
			wprintf(L"%s %s\n", TEXT(VER_DATE), TEXT(VER));			
		} else {
			wprintf(L"Invalid argument.\n\n");
			usage();
		}
		return 0;
	}

	update_env();

	return 0;
}
示例#11
0
void	ft_env(char **args, t_list *env)
{
	int		i;
	t_list	*mirror;

	i = 0;
	while (args[i] && ft_strcmp(args[i], "-i"))
		i++;
	if (!args[i])
		mirror = cpy_list(env, &env_node);
	else
		mirror = NULL;
	update_env(args, &mirror, &i);
	if (!args[i])
		printenv(mirror);
	else
	{
		get_home(args + i, mirror);
		route_me(args + i, mirror);
	}
	ft_dellist(&mirror);
}
示例#12
0
/*
 * fork_exec_srun - fork and exec srun
 * GLOBALS cr_argv: arguments for running srun
 * RETURN: 0 on success, otherwise on error
 */
static int
fork_exec_srun(void)
{
	int rc = 0;
	sigset_t sigset;

	listen_fd = create_listen_socket();
	if (listen_fd < 0) {
		return -1;
	}

	srun_pid = fork();
	if (srun_pid < 0) {
		error("failed to fork child process: %m");
		return -1;
	} else if (srun_pid == 0) {	/* child */
		/*
		 * remove srun from the foreground process group,
		 * or Ctrl-C will cause SIGINT duplicated
		 */
		setpgrp();

		update_env("SLURM_SRUN_CR_SOCKET", cr_sock_addr);

		/*
		 * BLCR blocks all signals in thread-context callback functions
		 */
		sigemptyset(&sigset);
		pthread_sigmask(SIG_SETMASK, &sigset, NULL);

		execv(srun_argv[0], srun_argv);
		perror("failed execv srun");
		exit(-1);
	}

	return rc;
}
示例#13
0
static void do_mm1_push(Octstr *rcpt_to, int isphonenum, MmsEnvelope *e, MmsMsg *msg)
{
     List *pheaders;
     static unsigned char ct; /* Transaction counter -- do we need it? */
     Octstr *to = NULL;     
     Octstr *pduhdr = octstr_create("");     
     Octstr *s = NULL;     
     
     if (!rcpt_to) {
	  mms_error(0, "MM1", NULL, "mobilesender: Queue entry %s has no recipient address!", e->xqfname);
	  goto done;
     } else
	  to = octstr_duplicate(rcpt_to);
     

     ct++;    
     octstr_append_char(pduhdr, ct);  /* Pushd id */
     octstr_append_char(pduhdr, 0x06); /* PUSH */

     
#if 1
     octstr_append_char(pduhdr, 1 + 1 + 1);
     octstr_append_char(pduhdr, 0xbe); /* content type. */     
#else
     octstr_append_char(pduhdr, 
			1 + 1 + strlen("application/vnd.wap.mms-message") + 1); /*header length. */     
     octstr_append_cstr(pduhdr, "application/vnd.wap.mms-message");
     octstr_append_char(pduhdr, 0x0); /* string terminator. */
#endif
     octstr_append_char(pduhdr, 0xaf); /* push application ID header and value follows. */
     octstr_append_char(pduhdr, 0x84); /* ... */

     s = mms_tobinary(msg);
     if (isphonenum) {
	  Octstr *url = octstr_format("%S&text=%E%E&to=%E&udh=%%06%%05%%04%%0B%%84%%23%%F0",	
				      settings->sendsms_url, pduhdr, s, to);     
	  int status;
	  List *rph  = NULL;
	  Octstr *rbody = NULL;
	  MmsEnvelopeTo *xto = gwlist_get(e->to, 0);
	  
	  pheaders = http_create_empty_headers();
	  http_header_add(pheaders, "Connection", "close");
	  http_header_add(pheaders, "User-Agent", MM_NAME "/" MMSC_VERSION);	       
	  
	  if ((status = mms_url_fetch_content(HTTP_METHOD_GET, url, pheaders, NULL, &rph, &rbody)) < 0 ||
	      http_status_class(status) != HTTP_STATUS_SUCCESSFUL) {
	       
	       mms_error(0,  "MM1", NULL, " Push[%s] from %s, to %s, failed, HTTP code => %d", e->xqfname, 
			 octstr_get_cstr(e->from), octstr_get_cstr(to), status);	       

	       e = update_env(e,xto,0);
	  } else {	 /* Successful push. */      

	       mms_log2("Notify", octstr_imm("system"), to, 
			-1, e ? e->msgId : NULL, NULL, NULL, "MM1", NULL,NULL);
	       e = update_env(e, xto, 1);	       
	  }     
	  
	  http_destroy_headers(pheaders);
	  http_destroy_headers(rph);
	  octstr_destroy(rbody);
	  octstr_destroy(url);
     } else { /* An IP Address: Send packet, forget. */
	  Octstr *addr = udp_create_address(to, WAPPUSH_PORT);
	  int sock = udp_client_socket();
	  MmsEnvelopeTo *xto = gwlist_get(e->to,0);
	  
	  if (sock > 0) {
	       octstr_append(pduhdr, s);
#if 0
	       octstr_dump(pduhdr, 0);
#endif
	       udp_sendto(sock, pduhdr, addr);
	       close(sock); /* ?? */      
	       mms_log2("Notify", octstr_imm("system"), to, 
			-1, e ? e->msgId : NULL, 
			NULL, NULL, "MM1", NULL,NULL);
	       e = update_env(e, xto, 1);
	  } else {
	       e = update_env(e, xto, 0);
	       mms_error(0,  "MM1", NULL, "push to %s:%d failed: %s", 
			 octstr_get_cstr(to), WAPPUSH_PORT, strerror(errno));
	  }
	  octstr_destroy(addr);
     }
 done:
     octstr_destroy(to);
     octstr_destroy(pduhdr);
     octstr_destroy(s);
     
     if (e)
	  settings->qfs->mms_queue_free_env(e);     
}
示例#14
0
文件: postalias.c 项目: ii0/postfix
int     main(int argc, char **argv)
{
    char   *path_name;
    int     ch;
    int     fd;
    char   *slash;
    struct stat st;
    int     postalias_flags = POSTALIAS_FLAG_AS_OWNER | POSTALIAS_FLAG_SAVE_PERM;
    int     open_flags = O_RDWR | O_CREAT | O_TRUNC;
    int     dict_flags = (DICT_FLAG_DUP_WARN | DICT_FLAG_FOLD_FIX
			  | DICT_FLAG_UTF8_REQUEST);
    char   *query = 0;
    char   *delkey = 0;
    int     sequence = 0;
    int     found;
    ARGV   *import_env;

    /*
     * Fingerprint executables and core dumps.
     */
    MAIL_VERSION_STAMP_ALLOCATE;

    /*
     * Be consistent with file permissions.
     */
    umask(022);

    /*
     * To minimize confusion, make sure that the standard file descriptors
     * are open before opening anything else. XXX Work around for 44BSD where
     * fstat can return EBADF on an open file descriptor.
     */
    for (fd = 0; fd < 3; fd++)
	if (fstat(fd, &st) == -1
	    && (close(fd), open("/dev/null", O_RDWR, 0)) != fd)
	    msg_fatal("open /dev/null: %m");

    /*
     * Process environment options as early as we can. We are not set-uid,
     * and we are supposed to be running in a controlled environment.
     */
    if (getenv(CONF_ENV_VERB))
	msg_verbose = 1;

    /*
     * Initialize. Set up logging, read the global configuration file and
     * extract configuration information.
     */
    if ((slash = strrchr(argv[0], '/')) != 0 && slash[1])
	argv[0] = slash + 1;
    msg_vstream_init(argv[0], VSTREAM_ERR);
    msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY);

    /*
     * Check the Postfix library version as soon as we enable logging.
     */
    MAIL_VERSION_CHECK;

    /*
     * Parse JCL.
     */
    while ((ch = GETOPT(argc, argv, "Nc:d:finopq:rsuvw")) > 0) {
	switch (ch) {
	default:
	    usage(argv[0]);
	    break;
	case 'N':
	    dict_flags |= DICT_FLAG_TRY1NULL;
	    dict_flags &= ~DICT_FLAG_TRY0NULL;
	    break;
	case 'c':
	    if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
		msg_fatal("out of memory");
	    break;
	case 'd':
	    if (sequence || query || delkey)
		msg_fatal("specify only one of -s -q or -d");
	    delkey = optarg;
	    break;
	case 'f':
	    dict_flags &= ~DICT_FLAG_FOLD_FIX;
	    break;
	case 'i':
	    open_flags &= ~O_TRUNC;
	    break;
	case 'n':
	    dict_flags |= DICT_FLAG_TRY0NULL;
	    dict_flags &= ~DICT_FLAG_TRY1NULL;
	    break;
	case 'o':
	    postalias_flags &= ~POSTALIAS_FLAG_AS_OWNER;
	    break;
	case 'p':
	    postalias_flags &= ~POSTALIAS_FLAG_SAVE_PERM;
	    break;
	case 'q':
	    if (sequence || query || delkey)
		msg_fatal("specify only one of -s -q or -d");
	    query = optarg;
	    break;
	case 'r':
	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_IGNORE);
	    dict_flags |= DICT_FLAG_DUP_REPLACE;
	    break;
	case 's':
	    if (query || delkey)
		msg_fatal("specify only one of -s or -q or -d");
	    sequence = 1;
	    break;
	case 'u':
	    dict_flags &= ~DICT_FLAG_UTF8_REQUEST;
	    break;
	case 'v':
	    msg_verbose++;
	    break;
	case 'w':
	    dict_flags &= ~(DICT_FLAG_DUP_WARN | DICT_FLAG_DUP_REPLACE);
	    dict_flags |= DICT_FLAG_DUP_IGNORE;
	    break;
	}
    }
    mail_conf_read();
    /* Enforce consistent operation of different Postfix parts. */
    import_env = mail_parm_split(VAR_IMPORT_ENVIRON, var_import_environ);
    update_env(import_env->argv);
    argv_free(import_env);
    /* Re-evaluate mail_task() after reading main.cf. */
    msg_syslog_init(mail_task(argv[0]), LOG_PID, LOG_FACILITY);
    mail_dict_init();

    /*
     * Use the map type specified by the user, or fall back to a default
     * database type.
     */
    if (delkey) {				/* remove entry */
	if (optind + 1 > argc)
	    usage(argv[0]);
	if (strcmp(delkey, "-") == 0)
	    exit(postalias_deletes(VSTREAM_IN, argv + optind, argc - optind,
				   dict_flags | DICT_FLAG_LOCK) == 0);
	found = 0;
	while (optind < argc) {
	    if ((path_name = split_at(argv[optind], ':')) != 0) {
		found |= postalias_delete(argv[optind], path_name, delkey,
					  dict_flags | DICT_FLAG_LOCK);
	    } else {
		found |= postalias_delete(var_db_type, argv[optind], delkey,
					  dict_flags | DICT_FLAG_LOCK);
	    }
	    optind++;
	}
	exit(found ? 0 : 1);
    } else if (query) {				/* query map(s) */
	if (optind + 1 > argc)
	    usage(argv[0]);
	if (strcmp(query, "-") == 0)
	    exit(postalias_queries(VSTREAM_IN, argv + optind, argc - optind,
				   dict_flags | DICT_FLAG_LOCK) == 0);
	while (optind < argc) {
	    if ((path_name = split_at(argv[optind], ':')) != 0) {
		found = postalias_query(argv[optind], path_name, query,
					dict_flags | DICT_FLAG_LOCK);
	    } else {
		found = postalias_query(var_db_type, argv[optind], query,
					dict_flags | DICT_FLAG_LOCK);
	    }
	    if (found)
		exit(0);
	    optind++;
	}
	exit(1);
    } else if (sequence) {
	while (optind < argc) {
	    if ((path_name = split_at(argv[optind], ':')) != 0) {
		postalias_seq(argv[optind], path_name,
			      dict_flags | DICT_FLAG_LOCK);
	    } else {
		postalias_seq(var_db_type, argv[optind],
			      dict_flags | DICT_FLAG_LOCK);
	    }
	    exit(0);
	}
	exit(1);
    } else {					/* create/update map(s) */
	if (optind + 1 > argc)
	    usage(argv[0]);
	while (optind < argc) {
	    if ((path_name = split_at(argv[optind], ':')) != 0) {
		postalias(argv[optind], path_name, postalias_flags,
			  open_flags, dict_flags);
	    } else {
		postalias(var_db_type, argv[optind], postalias_flags,
			  open_flags, dict_flags);
	    }
	    optind++;
	}
	exit(0);
    }
}