Esempio n. 1
0
json_t* extract(json_t* json, char* key)
{
    int i, s;
    json_t* temp;
    switch (json_typeof(json))
    {
        case JSON_OBJECT:
            temp = json_object_get(json, key);
            if (temp == NULL)
                {break;}
            return temp;
        case JSON_ARRAY:
            s = json_array_size(json);
            if (s == 0)
                {json_err("index out of bounds", json); break;}
            i = estrtol(key);
            if ((i < -s) || (i >= s))
                {json_err("index out of bounds", json);}
            // stupid fix for a stupid modulus operation
            while (i<0)
                {i+=s;}
            return json_array_get(json, i % s);
        case JSON_STRING:
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
        default:
            break;
    }
    json_err("has no elements to extract", json);
    return json_null();
}
Esempio n. 2
0
int
main(int argc, char *argv[])
{
	char c;
	long n = 10;
	FILE *fp;
	void (*tail)(FILE *, const char *, long) = taketail;

	while((c = getopt(argc, argv, "n:")) != -1)
		switch(c) {
		case 'n':
			n = abs(estrtol(optarg, 0));
			if(optarg[0] == '+')
				tail = dropinit;
			break;
		default:
			exit(EXIT_FAILURE);
		}
	if(optind == argc)
		tail(stdin, "<stdin>", n);
	else if(optind == argc-1) {
		if(strcmp(argv[optind], "-") == 0) argv[optind] = "/dev/stdin";
		if(!(fp = fopen(argv[optind], "r")))
			eprintf("fopen %s:", argv[optind]);
		tail(fp, argv[optind], n);
		fclose(fp);
	}
	else
		eprintf("usage: %s [-n lines] [file]\n", argv[0]);

	return EXIT_SUCCESS;
}
Esempio n. 3
0
int
main(int argc, char *argv[])
{
	char c;
	long width = 80;
	FILE *fp;

	while((c = getopt(argc, argv, "bsw:")) != -1)
		switch(c) {
		case 'b':
			bflag = true;
			break;
		case 's':
			sflag = true;
			break;
		case 'w':
			width = estrtol(optarg, 0);
			break;
		default:
			exit(EXIT_FAILURE);
		}
	if(optind == argc)
		fold(stdin, width);
	else for(; optind < argc; optind++) {
		if(strcmp(argv[optind], "-") == 0) argv[optind] = "/dev/stdin";
		if(!(fp = fopen(argv[optind], "r")))
			eprintf("fopen %s:", argv[optind]);
		fold(fp, width);
		fclose(fp);
	}
	return EXIT_SUCCESS;
}
Esempio n. 4
0
int
main(int argc, char *argv[])
{
	char c;
	FILE *fp;

	while((c = getopt(argc, argv, "b:i:s:")) != -1)
		switch(c) {
		case 'b':
			mode = optarg[0];
			if(optarg[0] == 'p')
				regcomp(&preg, &optarg[1], REG_NOSUB);
			else if(!strchr("ant", optarg[0]) || optarg[1] != '\0')
				eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]);
			break;
		case 'i':
			incr = estrtol(optarg, 0);
			break;
		case 's':
			sep = optarg;
			break;
		default:
			exit(2);
		}
	if(optind == argc)
		nl(stdin);
	else for(; optind < argc; optind++) {
		if(strcmp(argv[optind], "-") == 0) argv[optind] = "/dev/stdin";
		if(!(fp = fopen(argv[optind], "r")))
			eprintf("fopen %s:", argv[optind]);
		nl(fp);
		fclose(fp);
	}
	return EXIT_SUCCESS;
}
Esempio n. 5
0
json_t* extract(json_t* json, char* key)
{
    int i, s;
    json_t* temp;
    switch (json_typeof(json))
    {
        case JSON_OBJECT:
            temp = json_object_get(json, key);
            if (temp == NULL)
                {break;}
            return temp;
        case JSON_ARRAY:
            s = json_array_size(json);
            if (s == 0)
                {break;}
            i = estrtol(key);
            return json_array_get(json, i % s);
        case JSON_STRING:
        case JSON_INTEGER:
        case JSON_REAL:
        case JSON_TRUE:
        case JSON_FALSE:
        case JSON_NULL:
        default:
            break;
    }
    json_err("has no elements to extract", json);
    return json_null();
}
Esempio n. 6
0
int
main(int argc, char *argv[])
{
	char buf[BUFSIZ], c;
	char *fmt = "%c";
	struct tm *now = NULL;
	struct tm *(*tztime)(const time_t *) = localtime;
	const char *tz = "local";
	time_t t;

	t = time(NULL);
	while((c = getopt(argc, argv, "d:u")) != -1)
		switch(c) {
		case 'd':
			t = estrtol(optarg, 0);
			break;
		case 'u':
			tztime = gmtime;
			tz = "gm";
			break;
		default:
			exit(EXIT_FAILURE);
		}
	if(optind < argc && argv[optind][0] == '+')
		fmt = &argv[optind][1];
	if(!(now = tztime(&t)))
		eprintf("%stime failed\n", tz);

	strftime(buf, sizeof buf, fmt, now);
	puts(buf);
	return EXIT_SUCCESS;
}
Esempio n. 7
0
struct group *xgetgrnamid(char *group)
{
  struct group *gr = getgrnam(group);
  gid_t gid;

  if (!gr) {
    char *s = 0;

    gid = estrtol(group, &s, 10);
    if (!errno && s && !*s) gr = getgrgid(gid);
  }
  if (!gr) perror_exit("group '%s'", group);

  return gr;
}
Esempio n. 8
0
struct passwd *xgetpwnamid(char *user)
{
  struct passwd *up = getpwnam(user);
  uid_t uid;

  if (!up) {
    char *s = 0;

    uid = estrtol(user, &s, 10);
    if (!errno && s && !*s) up = getpwuid(uid);
  }
  if (!up) perror_exit("user '%s'", user);

  return up;
}
Esempio n. 9
0
int main(int argc, char** argv) {
    long level = -1;
    bool read  = true;
    bool clear_before = false;
    bool clear_after = false;
    /* Parse command line options */
    OPTBEGIN {
    case 'C':
        clear_before = true;
        read = false;
        break;
    case 'c':
        clear_after = true;
        read = true;
        break;
    case 'n':
        level = estrtol(EOPTARG(usage()), 10);
        break;
    default:
        usage();
    } OPTEND;
    /* clear the before before  doing anything else */
    if (clear_before)
        eklogctl(SYSLOG_ACTION_CLEAR, NULL, 0);
    /* Set the log level */
    if (level >= 0)
        eklogctl(SYSLOG_ACTION_CONSOLE_LEVEL, NULL, level);
    /* Read the raw log data and print it out */
    if (read) {
        int nbytes = eklogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
        char* buf  = (char*)emalloc(nbytes);
        eklogctl(SYSLOG_ACTION_READ_ALL, buf, nbytes);
        nbytes = efwrite(buf, 1, nbytes, stdout);
        if (buf[nbytes - 1] != '\n')
            fputc('\n', stdout);
        free(buf);
    }
    /* Clear the log after we've read it out */
    if (clear_after)
        eklogctl(SYSLOG_ACTION_CLEAR, NULL, 0);
    return 0;
}
Esempio n. 10
0
int
main(int argc, char *argv[])
{
	int cflag = 0, sflag = 0;
	int fd, i, ret = 0;
	long size = 0;

	ARGBEGIN {
	case 's':
		sflag = 1;
		size = estrtol(EARGF(usage()), 10);
		break;
	case 'c':
		cflag = 1;
		break;
	default:
		usage();
	} ARGEND;

	if (argc < 1 || sflag == 0)
		usage();

	for (i = 0; i < argc; i++) {
		fd = open(argv[i], O_WRONLY | (cflag ? 0 : O_CREAT), 0644);
		if (fd < 0) {
			weprintf("open: cannot open `%s' for writing:", argv[i]);
			ret = 1;
			continue;
		}
		if (ftruncate(fd, size) < 0) {
			weprintf("ftruncate: cannot open `%s' for writing:", argv[i]);
			ret = 1;
		}
		close(fd);
	}
	return ret;
}
Esempio n. 11
0
int
main(int argc, char *argv[])
{
	DIR *dp;
	struct dirent *entry;
	pid_t pid;
	struct procstat ps;
	char cmdline[BUFSIZ], *cmd, *cmdbase = NULL, *p, *arg = NULL;
	int i, found = 0;
	int sflag = 0, oflag = 0;
	struct pidentry *pe, *tmp;

	ARGBEGIN {
	case 's':
		sflag = 1;
		break;
	case 'o':
		oflag = 1;
		arg = EARGF(usage());
		break;
	default:
		usage();
	} ARGEND;

	if (!argc)
		return 1;

	TAILQ_INIT(&omitpid_head);

	for (p = strtok(arg, ","); p; p = strtok(NULL, ",")) {
		pe = emalloc(sizeof(*pe));
		if (strcmp(p, "%PPID") == 0)
			pe->pid = getppid();
		else
			pe->pid = estrtol(p, 10);
		TAILQ_INSERT_TAIL(&omitpid_head, pe, entry);
	}

	if (!(dp = opendir("/proc")))
		eprintf("opendir /proc:");

	while ((entry = readdir(dp))) {
		if (!pidfile(entry->d_name))
			continue;
		pid = estrtol(entry->d_name, 10);
		if (oflag) {
			TAILQ_FOREACH(pe, &omitpid_head, entry)
				if (pe->pid == pid)
					break;
			if (pe)
				continue;
		}
		if (parsestat(pid, &ps) < 0)
			continue;
		if (parsecmdline(ps.pid, cmdline,
				 sizeof(cmdline)) < 0) {
			cmd = ps.comm;
			cmdbase = cmd;
		} else {
			if ((p = strchr(cmdline, ' ')))
				*p = '\0';
			cmd = cmdline;
			cmdbase = basename(cmdline);
		}
		/* Workaround for login shells */
		if (cmd[0] == '-')
			cmd++;
		for (i = 0; i < argc; i++) {
			if (strcmp(cmd, argv[i]) == 0 ||
			    strcmp(cmdbase, argv[i]) == 0) {
				putword(entry->d_name);
				found++;
				if (sflag)
					goto out;
			}
		}
	}

out:
	if (found)
		putchar('\n');

	closedir(dp);

	for (pe = TAILQ_FIRST(&omitpid_head); pe; pe = tmp) {
		tmp = TAILQ_NEXT(pe, entry);
		TAILQ_REMOVE(&omitpid_head, pe, entry);
		free(pe);
	}

	return 0;
}
Esempio n. 12
0
int main(int argc, char **argv) {
	int i;
	int linesold, colsold;
	int graphlines = 0;
	double delay = 0.5;
	char key;
	struct iface ifa;
	WINDOW *title, *rxgraph, *txgraph, *stats;

	bool colors = true;
	bool siunits = false;
	bool hidescale = false;
	bool syncgraphmax = false;
	bool fixedlines = false;

	memset(&ifa, 0, sizeof ifa);

	for (i = 1; i < argc; i++) {
		if (!strcmp("-v", argv[i]))
			eprintf("%s-%s\n", argv[0], VERSION);
		else if (!strcmp("-C", argv[i]))
			colors = false;
		else if (!strcmp("-s", argv[i]))
			siunits = true;
		else if (!strcmp("-S", argv[i]))
			hidescale = true;
		else if (!strcmp("-m", argv[i]))
			syncgraphmax = true;
		else if (argv[i+1] == NULL || argv[i+1][0] == '-')
			usage(argv);
		else if (!strcmp("-d", argv[i]))
			delay = estrtod(argv[++i]);
		else if (!strcmp("-i", argv[i]))
			strlcpy(ifa.ifname, argv[++i], IFNAMSIZ);
		else if (!strcmp("-l", argv[i])) {
			graphlines = estrtol(argv[++i]);
			fixedlines = true;
		}
	}
	if (ifa.ifname[0] == '\0')
		detectiface(ifa.ifname);

	initscr();
	curs_set(0);
	noecho();
	keypad(stdscr, TRUE);
	timeout(delay * 1000);
	if (colors && has_colors()) {
		start_color();
		use_default_colors();
		init_pair(1, COLOR_GREEN, -1);
		init_pair(2, COLOR_RED, -1);
	}

	signal(SIGWINCH, sighandler);
	ifa.rxs = ecalloc(COLS, sizeof(long));
	ifa.txs = ecalloc(COLS, sizeof(long));
	mvprintw(0, 0, "collecting data from %s for %.2f seconds\n", ifa.ifname, delay);

	if (!fixedlines)
		graphlines = (LINES-5)/2;
	title = newwin(1, COLS, 0, 0);
	rxgraph = newwin(graphlines, COLS, 1, 0);
	txgraph = newwin(graphlines, COLS, graphlines+1, 0);
	stats = newwin(LINES-(graphlines*2+1), COLS, graphlines*2+1, 0);

	getdata(&ifa, delay, COLS);

	while ((key = getch()) != 'q') {
		if (key != ERR)
			resize = 1;

		getdata(&ifa, delay, COLS);
		if (syncgraphmax)
			ifa.rxmax = ifa.txmax = MAX(ifa.rxmax, ifa.txmax);

		if (resize) {
			linesold = LINES;
			colsold = COLS;
			endwin();
			refresh();

			if (COLS != colsold) {
				arrayresize(&ifa.rxs, COLS, colsold);
				arrayresize(&ifa.txs, COLS, colsold);
			}
			if (LINES != linesold && !fixedlines)
				graphlines = (LINES-5)/2;

			wresize(title, 1, COLS);
			wresize(rxgraph, graphlines, COLS);
			wresize(txgraph, graphlines, COLS);
			wresize(stats, LINES-(graphlines*2+1), COLS);
			mvwin(txgraph, graphlines+1, 0);
			mvwin(stats, graphlines*2+1, 0);
			resize = 0;
		}

		werase(title);
		mvwprintw(title, 0, COLS/2-7, "interface: %s\n", ifa.ifname);
		wnoutrefresh(title);
		printgraphw(rxgraph, ifa.rxs, ifa.rxmax, siunits, graphlines, COLS, hidescale, COLOR_PAIR(1));
		printgraphw(txgraph, ifa.txs, ifa.txmax, siunits, graphlines, COLS, hidescale, COLOR_PAIR(2));
		printstatsw(stats, ifa, siunits, COLS);
		doupdate();
	}

	delwin(title);
	delwin(rxgraph);
	delwin(txgraph);
	delwin(stats);
	endwin();
	return EXIT_SUCCESS;
}