Skip to content
Snippets Groups Projects
Select Git revision
  • 52cf25d0ab7f78eeecc59ac652ed5090f69b619e
  • master default protected
  • android-msm-bullhead-3.10-nougat_kgdb_less_changes
  • android-msm-bullhead-3.10-nougat_kgdb
  • android-msm-bullhead-3.10-nougat_klist
  • android-4.4
  • android-msm-vega-4.4-oreo-daydream
  • android-msm-wahoo-4.4-p-preview-5
  • android-msm-wahoo-4.4-pie
  • android-msm-marlin-3.18-p-preview-5
  • android-msm-marlin-3.18-pie
  • android-msm-wahoo-2018.07-oreo-m2
  • android-msm-wahoo-2018.07-oreo-m4
  • android-msm-wahoo-4.4-p-preview-4
  • android-msm-bullhead-3.10-oreo-m6
  • android-msm-angler-3.10-oreo-m6
  • android-msm-marlin-3.18-p-preview-4
  • android-msm-stargazer-3.18-oreo-wear-dr
  • android-msm-catshark-3.18-oreo-wear-dr
  • android-msm-wahoo-4.4-oreo-m2
  • android-msm-wahoo-4.4-oreo-m4
  • android-daydreamos-8.0.0_r0.5
  • android-8.1.0_r0.92
  • android-8.1.0_r0.91
  • android-daydreamos-8.0.0_r0.4
  • android-p-preview-5_r0.2
  • android-p-preview-5_r0.1
  • android-9.0.0_r0.5
  • android-9.0.0_r0.4
  • android-9.0.0_r0.2
  • android-9.0.0_r0.1
  • android-8.1.0_r0.81
  • android-8.1.0_r0.80
  • android-8.1.0_r0.78
  • android-8.1.0_r0.76
  • android-8.1.0_r0.75
  • android-8.1.0_r0.72
  • android-8.1.0_r0.70
  • android-p-preview-4_r0.2
  • android-p-preview-4_r0.1
  • android-wear-8.0.0_r0.30
41 results

class.c

Blame
  • argv_split.c 1.78 KiB
    /*
     * Helper function for splitting a string into an argv-like array.
     */
    
    #include <linux/kernel.h>
    #include <linux/ctype.h>
    #include <linux/string.h>
    #include <linux/slab.h>
    #include <linux/export.h>
    
    static const char *skip_arg(const char *cp)
    {
    	while (*cp && !isspace(*cp))
    		cp++;
    
    	return cp;
    }
    
    static int count_argc(const char *str)
    {
    	int count = 0;
    
    	while (*str) {
    		str = skip_spaces(str);
    		if (*str) {
    			count++;
    			str = skip_arg(str);
    		}
    	}
    
    	return count;
    }
    
    /**
     * argv_free - free an argv
     * @argv - the argument vector to be freed
     *
     * Frees an argv and the strings it points to.
     */
    void argv_free(char **argv)
    {
    	char **p;
    	for (p = argv; *p; p++)
    		kfree(*p);
    
    	kfree(argv);
    }
    EXPORT_SYMBOL(argv_free);
    
    /**
     * argv_split - split a string at whitespace, returning an argv
     * @gfp: the GFP mask used to allocate memory
     * @str: the string to be split
     * @argcp: returned argument count
     *
     * Returns an array of pointers to strings which are split out from
     * @str.  This is performed by strictly splitting on white-space; no
     * quote processing is performed.  Multiple whitespace characters are
     * considered to be a single argument separator.  The returned array
     * is always NULL-terminated.  Returns NULL on memory allocation
     * failure.
     */
    char **argv_split(gfp_t gfp, const char *str, int *argcp)
    {
    	int argc = count_argc(str);
    	char **argv = kzalloc(sizeof(*argv) * (argc+1), gfp);
    	char **argvp;
    
    	if (argv == NULL)
    		goto out;
    
    	if (argcp)
    		*argcp = argc;
    
    	argvp = argv;
    
    	while (*str) {
    		str = skip_spaces(str);
    
    		if (*str) {
    			const char *p = str;
    			char *t;
    
    			str = skip_arg(str);
    
    			t = kstrndup(p, str-p, gfp);
    			if (t == NULL)
    				goto fail;
    			*argvp++ = t;
    		}
    	}
    	*argvp = NULL;
    
      out:
    	return argv;
    
      fail:
    	argv_free(argv);
    	return NULL;
    }
    EXPORT_SYMBOL(argv_split);