Ejemplo n.º 1
0
int main(int argc, char **argv) {
  parse_opts(argc,argv);
  if (optind == argc) argc++;
  int i = optind;

  size_t buffer_size = 4096;
  char **d = malloc(buffer_size*sizeof(char*));
  while (i < argc) {
    FILE *values = open_arg(argv[i++]);
    char *line, *buffer = NULL;
    size_t length;
    while (line = get_line(values,&buffer,&length)) {
      unsigned long j, k;
      d[0] = line-1;
      for (j = 1;; j++) {
        if (j >= buffer_size) {
          buffer_size *= 2;
          d = (char**) realloc(d,buffer_size*sizeof(char*));
        }
        char *p = d[j-1]+1;
        d[j] = p + strcspn(p,delimiters);
        if (*d[j] == '\n' || *d[j] == '\0') break;
      }
      for (k = 1; k <= j; k++) {
        unsigned long l = (unsigned long) floor(j*drand48());
        fwrite(d[l]+1, d[l+1]-d[l]-1, 1, stdout);
        if (*d[k]) putchar(*d[k]);
      }
    }
    fclose(values);
    wait(NULL);
  }

  return 0;
}
Ejemplo n.º 2
0
/**
 * Parses the arguments given to the program. It ignores invalid arguments
 * instead of failing. FILE pointers and PID arrays are set in the functions
 * called, so are passed as pointers to pointers.
 */
void 
handle_args(int argc, char *argv[], options *opt)
{
	char *optstr = "-s::d::D::";
	int o;
	pid_t pid;
	while ((o = getopt(argc, argv, optstr)) != -1) {
		switch(o) {
		case 1:
			pid = try_to_read_PID(argv[optind - 1]);
			if (pid != 0)
				add_pid_to_array(pid, &(opt->pids),
				                 &(opt->pidcount));
			else
				fprintf(stderr, "Warning: Received unexpected "
				        "argument: %s. Ignoring.\n",
				        argv[optind - 1]);
			break;
		case 's':
			opt->summary = 1;
			if(optarg)
				opt->summaryfile = open_arg(optarg);
			else
				opt->summaryfile = stdout;
			break;
		case 'd':
			opt->compactdetail = 1;
			if(optarg)
				opt->compactdetailfile = open_arg(optarg);
			else
				opt->compactdetailfile = stdout;
			break;
		case 'D':
			opt->detail = 1;
			if(optarg)
				opt->detailfile = open_arg(optarg);
			else
				opt->detailfile = stdout;
			break;
		default:
			/* getopt will print that it receives an invalid option
			 * so no reporting needs to be added
			 */
		}
	}
}
Ejemplo n.º 3
0
int main(int argc, char **argv) {
  parse_opts(argc,argv);
  if (optind == argc-1) argc++;
  int i = optind;

  char *packets_file = argv[i++];
  FILE *file = fopen(packets_file,"r+");
  if (!file)
    die("fopen(\"%s\",\"r\"): %s\n",packets_file,errstr);
  struct stat fs;
  fstat(fileno(file),&fs);
  u_int32_t n = fs.st_size / sizeof(packet_record);

  packet_record *packets = mmap(
    0,
    fs.st_size,
    PROT_READ | PROT_WRITE,
    MAP_SHARED,
    fileno(file),
    0
  );
  long long p = 0;

  while (i < argc) {
    FILE *values = open_arg(argv[i++]);
    char *line, *buffer = NULL;
    size_t length;
    if (sizes) {
      while (line = get_line(values,&buffer,&length)) {
        for (;;) {
          line += strcspn(line,"+-0123456789\n");
          if (*line == '\n' || *line == '\0') break;
          long z = strtol(line,&line,10);
          if (!z || z & 0xffff0000)
            die("Invalid packet size: %f\n",z);

          if (p >= n) goto too_many_values;
          packets[p++].size = htons((u_int16_t) z);
        }
      }
    }
    if (intervals) {
      long long flow = -1;
      long long time_usec;
      while (line = get_line(values,&buffer,&length)) {
        for (;;) {
          if (flow != packets[p].flow) {
            u_int32_t sec  = ntohl(packets[p].sec);
            u_int32_t usec = ntohl(packets[p].usec);
            time_usec = sec*1000000L + usec;
            flow = packets[p].flow;
            p++;
          }

          line += strcspn(line,"+-0123456789.\n");
          if (*line == '\n' || *line == '\0') break;
          double v = strtod(line,&line);
          time_usec += llround(v*1e6);
          u_int32_t sec  = (u_int32_t) (time_usec / 1000000L);
          u_int32_t usec = (u_int32_t) (time_usec % 1000000L);

          if (p >= n) goto too_many_values;
          packets[p].sec  = htonl(sec);
          packets[p].usec = htonl(usec);
          p++;
        }
      }
    }
    fclose(values);
    wait(NULL);
  }
  if (p < n)
    die("Too few splice values.\n");

  munmap(packets,fs.st_size);
  fclose(file);
  return 0;

too_many_values:
  die("Too many splice values.\n");
}