Exemplo n.º 1
0
char *
search_program(const char *progname)
{
  char *launch = NULL;
  char *cwd;

  if (progname[0] == '/')
  {
    launch = strdup(progname);
    if (!launch)
      die(1, "allocating program name buffer");
  }
  else if (strchr(progname, '/') != NULL)
  {
    cwd = get_current_dir_name();
    launch = merge_paths(cwd, progname);
    free(cwd);
  }
  else
  {
    char *path = getenv("PATH");
    char *saveptr = NULL;
    char *token;

    if (path == NULL)
      die(1, "could not get PATH environment variable");
    path = strdup(path);

    for (token = strtok_r(path, ":", &saveptr); token != NULL;
         token = strtok_r(NULL, ":", &saveptr))
    {
      launch = merge_paths(token, progname);

      if (access(launch, X_OK) == 0)
	break;

      free(launch);
      launch = NULL;
    }

    free(path);

    if (launch == NULL)
      die(1, "could not locate program to launch");

    if (launch[0] != '/')
    {
      char *relative = launch;

      cwd = get_current_dir_name();
      launch = merge_paths(cwd, relative);

      free(cwd);
      free(relative);
    }
  }

  return launch;
}
Exemplo n.º 2
0
void merge_path_operation::apply (svg_path *path_dest, svg_path_geom_iterator dest, svg_path *path_src, svg_path_geom_iterator src)
{
  if (path_dest->get_geom () == path_src->get_geom ())
    merge_inside (path_dest, dest, src);
  else
    merge_paths (path_dest, dest, path_src, src);
}
Exemplo n.º 3
0
/* This function directly implements the "Transform References"
 * algorithm described in RFC 3986 section 5.2.2. */
ne_uri *ne_uri_resolve(const ne_uri *base, const ne_uri *relative,
                       ne_uri *target)
{
    memset(target, 0, sizeof *target);

    if (relative->scheme) {
        target->scheme = ne_strdup(relative->scheme);
        copy_authority(target, relative);
        target->path = remove_dot_segments(relative->path);
        if (relative->query) target->query = ne_strdup(relative->query);
    } else {
        if (relative->host) {
            copy_authority(target, relative);
            target->path = remove_dot_segments(relative->path);
            if (relative->query) target->query = ne_strdup(relative->query);
        } else {
            if (relative->path[0] == '\0') {
                target->path = ne_strdup(base->path);
                if (relative->query) {
                    target->query = ne_strdup(relative->query);
                } else if (base->query) {
                    target->query = ne_strdup(base->query);
                }
            } else {
                if (relative->path[0] == '/') {
                    target->path = remove_dot_segments(relative->path);
                } else {
                    char *merged = merge_paths(base, relative->path);
                    target->path = remove_dot_segments(merged);
                    ne_free(merged);
                }
                if (relative->query) target->query = ne_strdup(relative->query);
            }
            copy_authority(target, base);
        }
        if (base->scheme) target->scheme = ne_strdup(base->scheme);
    }
    
    if (relative->fragment) target->fragment = ne_strdup(relative->fragment);

    return target;
}
Exemplo n.º 4
0
variable_t* update_var(variable_t* evar, variable_t* vvar)
{
   linked_list* testlist;

   switch (vvar->type)
   {
      case VAR_LIT_SET:
         evar->literal = vvar->literal;
         evar->type = VAR_LIT_SET;
         break;

      case VAR_PATH_SET:
         evar->pathlist = vvar->pathlist;
         evar->type = VAR_PATH_SET;
         break;

      case VAR_PATH_ADD:
         switch (evar->type)
         {
            case VAR_LIT_SET:
               evar->pathlist = merge_paths(make_pathlist(evar->literal),
                                            vvar->pathlist);
               break;

            case VAR_PATH_SET:
            case VAR_PATH_ADD:
               evar->pathlist = merge_paths(evar->pathlist, vvar->pathlist);
               break;

            default:
               break;
         }
         evar->type = VAR_PATH_ADD;
         break;
         
      case VAR_PATH_TESTSET:
         testlist = test_paths(vvar->pathlist);
         if (head(testlist))
         {
            evar->pathlist = testlist;
         }
         else
         {
            evar->pathlist = NULL;
         }
         evar->type = VAR_PATH_SET;
         break;

      case VAR_PATH_TESTADD:
         switch (evar->type)
         {
            case VAR_LIT_SET:
               evar->pathlist = merge_paths(make_pathlist(evar->literal),
                                            test_paths(vvar->pathlist));
               break;

            case VAR_PATH_SET:
            case VAR_PATH_ADD:
               evar->pathlist = merge_paths(evar->pathlist, test_paths(vvar->pathlist));
               break;
               
            default:
               break;
         }
         evar->type = VAR_PATH_ADD;
         break;

      default:
         break;
   }

   return(evar);
}
Exemplo n.º 5
0
Arquivo: cp.c Projeto: jvesely/helenos
static int64_t do_copy(const char *src, const char *dest,
    size_t blen, int vb, int recursive, int force, int interactive)
{
	int r = -1;
	char dest_path[PATH_MAX];
	char src_path[PATH_MAX];
	DIR *dir = NULL;
	struct dirent *dp;

	dentry_type_t src_type = get_type(src);
	dentry_type_t dest_type = get_type(dest);

	const size_t src_len = str_size(src);

	if (src_type == TYPE_FILE) {
		char *src_fname;

		/* Initialize the src_path with the src argument */
		str_cpy(src_path, src_len + 1, src);
		str_rtrim(src_path, '/');
		
		/* Get the last component name from the src path */
		src_fname = get_last_path_component(src_path);
		
		/* Initialize dest_path with the dest argument */
		str_cpy(dest_path, PATH_MAX, dest);

		if (dest_type == TYPE_DIR) {
			/* e.g. cp file_name /data */
			/* e.g. cp file_name /data/ */
			
			/* dest is a directory,
			 * append the src filename to it.
			 */
			merge_paths(dest_path, PATH_MAX, src_fname);
			dest_type = get_type(dest_path);
		} else if (dest_type == TYPE_NONE) {
			if (dest_path[str_size(dest_path) - 1] == '/') {
				/* e.g. cp /textdemo /data/dirnotexists/ */

				printf("The dest directory %s does not exists",
				    dest_path);
				goto exit;
			}
		}

		if (dest_type == TYPE_DIR) {
			printf("Cannot overwrite existing directory %s\n",
			    dest_path);
			goto exit;
		} else if (dest_type == TYPE_FILE) {
			/* e.g. cp file_name existing_file */

			/* dest already exists, 
			 * if force is set we will try to remove it.
			 * if interactive is set user input is required.
			 */
			if (force && !interactive) {
				if (unlink(dest_path) != 0) {
					printf("Unable to remove %s\n",
					    dest_path);
					goto exit;
				}
			} else if (!force && interactive) {
				bool overwrite = get_user_decision(false,
				    "File already exists: %s. Overwrite? [y/N]: ",
				    dest_path);
				if (overwrite) {
					printf("Overwriting file: %s\n", dest_path);
					if (unlink(dest_path) != 0) {
						printf("Unable to remove %s\n", dest_path);
						goto exit;
					}
				} else {
					printf("Not overwriting file: %s\n", dest_path);
					r = 0;
					goto exit;
				}
			} else {
				printf("File already exists: %s\n", dest_path);
				goto exit;
			}
		}

		/* call copy_file and exit */
		r = (copy_file(src, dest_path, blen, vb) < 0);

	} else if (src_type == TYPE_DIR) {
		/* e.g. cp -r /x/srcdir /y/destdir/ */

		if (!recursive) {
			printf("Cannot copy the %s directory without the "
			    "-r option\n", src);
			goto exit;
		} else if (dest_type == TYPE_FILE) {
			printf("Cannot overwrite a file with a directory\n");
			goto exit;
		}

		char *src_dirname;

		/* Initialize src_path with the content of src */
		str_cpy(src_path, src_len + 1, src);
		str_rtrim(src_path, '/');

		src_dirname = get_last_path_component(src_path);

		str_cpy(dest_path, PATH_MAX, dest);

		switch (dest_type) {
		case TYPE_DIR:
			if (str_cmp(src_dirname, "..") &&
			    str_cmp(src_dirname, ".")) {
				/* The last component of src_path is
				 * not '.' or '..'
				 */
				merge_paths(dest_path, PATH_MAX, src_dirname);

				if (mkdir(dest_path, 0) != 0) {
					printf("Unable to create "
					    "dest directory %s\n", dest_path);
					goto exit;
				}
			}
			break;
		default:
		case TYPE_NONE:
			/* dest does not exists, this means the user wants
			 * to specify the name of the destination directory
			 *
			 * e.g. cp -r /src /data/new_dir_src
			 */
			if (mkdir(dest_path, 0) != 0) {
				printf("Unable to create "
				    "dest directory %s\n", dest_path);
				goto exit;
			}
			break;
		}

		dir = opendir(src);
		if (!dir) {
			/* Something strange is happening... */
			printf("Unable to open src %s directory\n", src);
			goto exit;
		}

		/* Copy every single directory entry of src into the
		 * destination directory.
		 */
		while ((dp = readdir(dir))) {
			struct stat src_s;
			struct stat dest_s;

			char src_dent[PATH_MAX];
			char dest_dent[PATH_MAX];

			str_cpy(src_dent, PATH_MAX, src);
			merge_paths(src_dent, PATH_MAX, dp->d_name);

			str_cpy(dest_dent, PATH_MAX, dest_path);
			merge_paths(dest_dent, PATH_MAX, dp->d_name);

			/* Check if we are copying a directory into itself */
			stat(src_dent, &src_s);
			stat(dest_path, &dest_s);

			if (dest_s.index == src_s.index &&
			    dest_s.fs_handle == src_s.fs_handle) {
				printf("Cannot copy a directory "
				    "into itself\n");
				goto exit;
			}

			if (vb)
				printf("copy %s %s\n", src_dent, dest_dent);

			/* Recursively call do_copy() */
			r = do_copy(src_dent, dest_dent, blen, vb, recursive,
			    force, interactive);
			if (r)
				goto exit;

		}
	} else
		printf("Unable to open source file %s\n", src);

exit:
	if (dir)
		closedir(dir);
	return r;
}