Example #1
0
/*
 * This function 'normalizes' a single argument: it puts single quotes around
 * it and escapes other special characters. If quote is false, it just
 * returns its argument.
 * Bash only needs special treatment for single quotes; tcsh also recognizes
 * exclamation marks within single quotes, and nukes whitespace.
 * This function returns a pointer to a buffer that is overwritten by 
 * each call.
 */
const char *normalize(const char *arg)
{
	static char *BUFFER=NULL;
	const char *argptr=arg;
	char *bufptr;

	if (BUFFER != NULL)
		free(BUFFER);

	if (!quote) { /* Just copy arg */
		BUFFER=our_malloc(strlen(arg)+1);
			
		strcpy(BUFFER,arg);
		return BUFFER;
	}

	/* Each character in arg may take upto four characters in the result:
	   For a quote we need a closing quote, a backslash, a quote and an
	   opening quote! We need also the global opening and closing quote,
	   and one extra character for '\0'. */
	BUFFER=our_malloc(strlen(arg)*4+3);

	bufptr=BUFFER;
	*bufptr++='\'';

	while (*argptr) {
		if (*argptr == '\'') {
			/* Quote: replace it with: '\'' */
			*bufptr++='\'';
			*bufptr++='\\';
			*bufptr++='\'';
			*bufptr++='\'';
		} else if (shell==TCSH && *argptr=='!') {
			/* Exclamation mark: replace it with: \! */
			*bufptr++='\'';
			*bufptr++='\\';
			*bufptr++='!';
			*bufptr++='\'';
		} else if (shell==TCSH && *argptr=='\n') {
			/* Newline: replace it with: \n */
			*bufptr++='\\';
			*bufptr++='n';
		} else if (shell==TCSH && isspace(*argptr)) {
			/* Non-newline whitespace: replace it with \<ws> */
			*bufptr++='\'';
			*bufptr++='\\';
			*bufptr++=*argptr;
			*bufptr++='\'';
		} else
			/* Just copy */
			*bufptr++=*argptr;
		argptr++;
	}
	*bufptr++='\'';
	*bufptr++='\0';
	return BUFFER;
}
Example #2
0
/* Register a long option. The contents of name is copied. */
void add_longopt(const char *name,int has_arg)
{
	char *tmp;
	if (!name) { /* init */
		free(long_options);
		long_options=NULL;
		long_options_length=0;
		long_options_nr=0;
	}

	if (long_options_nr == long_options_length) {
		long_options_length += LONG_OPTIONS_INCR;
		long_options=our_realloc(long_options,
		                         sizeof(struct option) * 
		                           long_options_length);
	}

	long_options[long_options_nr].name=NULL;
	long_options[long_options_nr].has_arg=0;
	long_options[long_options_nr].flag=NULL;
	long_options[long_options_nr].val=0;

	if (long_options_nr) { /* Not for init! */
		long_options[long_options_nr-1].has_arg=has_arg;
		long_options[long_options_nr-1].flag=NULL;
		long_options[long_options_nr-1].val=LONG_OPT;
		tmp = our_malloc(strlen(name)+1);
		strcpy(tmp,name);
		long_options[long_options_nr-1].name=tmp;
	}
	long_options_nr++;
}
Example #3
0
int get_cmdline_options (int argc, char **argv, struct prog_options *args)
{
    int i, j, c;
    int other = 0;
    for (i = 1; i < argc; i++) {
	if (*argv[i] != '-') {	/* something that is not an option */
	    for (j = 0; args[j].type; j++)
		if (args[j].char_opt == ' ') {
		    args[j].strs[other] = our_malloc (strlen (argv[i]) + 1);
		    strcpy (args[j].strs[other], argv[i]);
		    other++;
		    goto cont;
		}
	    return i;
	}
	c = 0;
	while (++c > 0) {	/* try each letter in a combined option eg 'tar -xvzf' */
	    for (j = 0; args[j].type; j++) {
		if (!strcmp (args[j].long_opt, argv[i]) || !strcmp (args[j].short_opt, argv[i])) {
		    c = -1;	/* not a combined option */
		    goto valid_opt;
		}
		if (argv[i][0] == '-' && argv[i][c] == args[j].char_opt) {
		    if (!argv[i][c + 1])	/* this must be the last letter in the combined option */
			c = -1;
		    goto valid_opt;
		}
		continue;

	      valid_opt:;
		switch (args[j].type) {
		case ARG_SET:{
			int *t;
			t = (int *) args[j].option;
			*t = 1;
			goto next;
		    }
		case ARG_CLEAR:{
			int *t;
			t = (int *) args[j].option;
			*t = 0;
			goto next;
		    }
		case ARG_IGNORE:
		    /* do nothing with this option */
		    goto next;
		}

		if (i + 1 != argc && argv[i + 1]
		    && c < 0	/* must be the last option if a combined option */
		    ) {
		    ++i;
		    switch (args[j].type) {
			int *t;
			double *f;
		    case ARG_ON_OFF:
			if (strcmp (argv[i], "on") == 0) {
			    t = (int *) args[j].option;
			    *t = 1;
			} else if (strcmp (argv[++i], "off") == 0) {
			    t = (int *) args[j].option;
			    *t = 0;
			} else
			    return i;
			goto next;
		    case ARG_YES_NO:
			if (strcmp (argv[i], "yes") == 0) {
			    t = (int *) args[j].option;
			    *t = 1;
			} else if (strcmp (argv[++i], "no") == 0) {
			    t = (int *) args[j].option;
			    *t = 0;
			} else
			    return i;
			goto next;
		    case ARG_STRING:
			*(args[j].str) = our_malloc (strlen (argv[i]) + 1);
			strcpy (*(args[j].str), argv[i]);
			goto next;
		    case ARG_STRINGS:{
			    /* get all argv's after this option until we reach another option */
			    int k = 0;
			    while (i < argc && *argv[i] != '-') {
				args[j].strs[k] = our_malloc (strlen (argv[i]) + 1);
				strcpy (args[j].strs[k], argv[i]);
				k++;
				i++;
			    }
			    i--;	/* will be incremented at end of loop */
			    goto next;
			}
		    case ARG_INT:
			t = (int *) args[j].option;
			*t = atoi (argv[i]);
			goto next;
		    case ARG_DOUBLE:
			f = (double *) args[j].option;
			*f = atof (argv[i]);
			goto next;
		    }
		    i--;
		}
		return i;	/* option parameter not found */
	    }			/* j */
	    return i;		/* option not found */
	  next:;
	}			/* c */
      cont:;
    }
    return 0;
}
Example #4
0
int main(int argc, char *argv[])
{
	char *optstr=NULL;
	char *name=NULL;
	int opt;
	int compatible=0;

#if WITHOUT_GETTEXT
#else
	setlocale(LC_ALL,"");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
#endif

	init_longopt();

	if (getenv("GETOPT_COMPATIBLE")) 
		compatible=1;

	if (argc == 1) 
	{
		if (compatible) {
			/* For some reason, the original getopt gave no error
                           when there were no arguments. */
			printf(" --\n");
			exit(0);
		}
		else
			parse_error(_("missing optstring argument"));
	}
	
	if (argv[1][0] != '-' || compatible) {
		quote=0;
		optstr=our_malloc(strlen(argv[1])+1);
		strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
		argv[1]=argv[0];
		exit(generate_output(argv+1,argc-1,optstr,long_options));
	}
	
	while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) 
		switch (opt) {
		case 'a':
			alternative=1;
			break;
		case 'h':
			print_help();
			exit(0);
		case 'o':
			if (optstr)
				free(optstr);
			optstr=our_malloc(strlen(optarg)+1);
			strcpy(optstr,optarg);
			break;
		case 'l':
			add_long_options(optarg);
			break;
		case 'n':
			if (name)
				free(name);
			name=our_malloc(strlen(optarg)+1);
			strcpy(name,optarg);
			break;
		case 'q':
			quiet_errors=1;
			break;
		case 'Q':
			quiet_output=1;
			break;
		case 's':
			set_shell(optarg);
			break;
		case 'T':
			exit(4);
		case 'u':
			quote=0;
			break;
		case 'V':
			printf(_("getopt (enhanced) 1.1.3\n"));
			exit(0);
		case '?':
		case ':':
			parse_error(NULL);
		default:
			parse_error(_("internal error, contact the author."));
		}
	
	if (!optstr) 
	{
		if (optind >= argc)
			parse_error(_("missing optstring argument"));
		else {
			optstr=our_malloc(strlen(argv[optind])+1);
			strcpy(optstr,argv[optind]);
			optind++;
		}
	}
	if (name)
		argv[optind-1]=name;
	else
		argv[optind-1]=argv[0];
	exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
}
Example #5
0
void SlaveStart()
{
  long i;
  long MyNum;
  double *upriv;
  
  //int a = 2*(rootN-1)*sizeof(double);

  //double upriv[a];

  long initdone; 
  long finish; 
  long l_transtime=0;
  long MyFirst; 
  long MyLast;


    
  BARRIER(Global->start, P);

  LOCK(Global->idlock);
    MyNum = Global->id;
    Global->id++;
  UNLOCK(Global->idlock);
    
  

  BARINCLUDE(Global->start);
  
 
  BARRIER(Global->start, P);  

  //upriv = (double *) malloc(2*(rootN-1)*sizeof(double));  
  upriv = (double *) our_malloc(2*(rootN-1)*sizeof(double));
  
  if (upriv == NULL) {
    fprintf(stderr,"Proc %ld could not malloc memory for upriv\n",MyNum);
    exit(-1);
  }
  for (i=0;i<2*(rootN-1);i++) {
    upriv[i] = umain[i];
  }   



  MyFirst = rootN*MyNum/P;
  MyLast = rootN*(MyNum+1)/P;

  TouchArray(x, trans, umain2, upriv, MyFirst, MyLast);


  BARRIER(Global->start, P);

  if ((MyNum == 0) || (dostats)) {
    CLOCK(initdone);
  }

  //printf("\nentrando em forward FFT\n");
  /* perform forward FFT */
  FFT1D(1, M, N, x, trans,  upriv, umain2, MyNum, &l_transtime, MyFirst, 
	MyLast, pad_length, test_result, dostats);



  /* perform backward FFT */
  if (test_result) {
    FFT1D(-1, M, N, x, trans,  upriv, umain2, MyNum, &l_transtime, MyFirst, 
	  MyLast, pad_length, test_result, dostats);
  }  



  if ((MyNum == 0) || (dostats)) {
    CLOCK(finish);
    Global->transtimes[MyNum] = l_transtime;
    Global->totaltimes[MyNum] = finish-initdone;
  }
  if (MyNum == 0) {
    Global->finishtime = finish;
    Global->initdonetime = initdone;
  }

  join_point(&myJoinPoint);
  
}
Example #6
0
void *X(kernel_malloc)(size_t n)
{
     void *p;

#if defined(MIN_ALIGNMENT)

#  if defined(WITH_OUR_MALLOC)
     p = our_malloc(n);
#    undef real_free
#    define real_free our_free

#  elif defined(__FreeBSD__) && (MIN_ALIGNMENT <= 16)
     /* FreeBSD does not have memalign, but its malloc is 16-byte aligned. */
     p = malloc(n);

#  elif (defined(__MACOSX__) || defined(__APPLE__)) && (MIN_ALIGNMENT <= 16)
     /* MacOS X malloc is already 16-byte aligned */
     p = malloc(n);

#  elif defined(HAVE_MEMALIGN)
     p = memalign(MIN_ALIGNMENT, n);

#  elif defined(HAVE_POSIX_MEMALIGN)
     /* note: posix_memalign is broken in glibc 2.2.5: it constrains
	the size, not the alignment, to be (power of two) * sizeof(void*).
        The bug seems to have been fixed as of glibc 2.3.1. */
     if (posix_memalign(&p, MIN_ALIGNMENT, n))
	  p = (void*) 0;

#  elif defined(__ICC) || defined(__INTEL_COMPILER) || defined(HAVE__MM_MALLOC)
     /* Intel's C compiler defines _mm_malloc and _mm_free intrinsics */
     p = (void *) _mm_malloc(n, MIN_ALIGNMENT);
#    undef real_free
#    define real_free _mm_free

#  elif defined(_MSC_VER)
     /* MS Visual C++ 6.0 with a "Processor Pack" supports SIMD
	and _aligned_malloc/free (uses malloc.h) */
     p = (void *) _aligned_malloc(n, MIN_ALIGNMENT);
#    undef real_free
#    define real_free _aligned_free

#  elif defined(macintosh) /* MacOS 9 */
     p = (void *) MPAllocateAligned(n,
#    if MIN_ALIGNMENT == 8
				    kMPAllocate8ByteAligned,
#    elif MIN_ALIGNMENT == 16
				    kMPAllocate16ByteAligned,
#    elif MIN_ALIGNMENT == 32
				    kMPAllocate32ByteAligned,
#    else
#      error "Unknown alignment for MPAllocateAligned"
#    endif
				    0);
#    undef real_free
#    define real_free MPFree

#  else
     /* Add your machine here and send a patch to [email protected] 
        or (e.g. for Windows) configure --with-our-malloc */
#    error "Don't know how to malloc() aligned memory ... try configuring --with-our-malloc"
#  endif

#else /* !defined(MIN_ALIGNMENT) */
     p = malloc(n);
#endif

     return p;
}