Example #1
0
int
main(void)
{
    unsigned short i;
    char *tmtz, *net, *avgs, *root, *vol, *bat;
    tmtz = net = avgs = root = vol = bat = NULL;
    char *line;
    static unsigned long long rec = 0, sent = 0;

    if (!(dpy = XOpenDisplay(NULL))) {
        fprintf(stderr, "dwmstatus: cannot open display.\n");
        return 1;
    }

    parse_netdev(&rec, &sent);

    for (i = 0;; sleep(1), i++) {
        if (i % 10 == 0) {
            free(bat);
            bat = batstat();
        }
        if (i % 5 == 0) {
            free(avgs);
            free(root);
            free(vol);
            avgs = loadavg();
            root = getfree("/");
            vol = runcmd("amixer get PCM | grep -om1 '[0-9]*%'");
        }
        net = netusage(&rec, &sent);
        tmtz = mktimes("%a %b %d %T");

        line = smprintf("♪ %s ⚡ %s │ %s │ / %s │ %s │ %s",
                        vol, bat, net, root, avgs, tmtz);

        XStoreName(dpy, DefaultRootWindow(dpy), line);
        XSync(dpy, False);

        free(net);
        free(tmtz);
        free(line);

        if (i == 60)
            i = 0;
    }

    XCloseDisplay(dpy);

    return 0;
}
Example #2
0
int main(int argc, char **argv)
{
	static char *sockname=NULL;
	register ssize_t nx;
	struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700};

	uname(&me);
	//get the login name
	callerpwd=getpwuid(getuid());

	if (argv[0][0] == '-')
		netusage(); //implies exit
	/* option parsing */
	{
		int c;
		while (1) {
			int option_index = 0;

			static struct option long_options[] = {
				{"sock", 1, 0, 's'},
				{"vdesock", 1, 0, 's'},
				{"unix", 1, 0, 's'},
				{"port", 1, 0, 'p'},
				{"help",0,0,'h'},
				{"mod",1,0,'m'},
				{"group",1,0,'g'},
				{0, 0, 0, 0}
			};
			c = GETOPT_LONG (argc, argv, "hc:p:s:m:g:l",
					long_options, &option_index);
			if (c == -1)
				break;

			switch (c) {
				case 'c':
					if (strcmp(optarg,"vde_plug")==0) {
#ifdef DO_SYSLOG
						write_syslog_entry("START");
						atexit(write_syslog_close);
#ifdef VDE_IP_LOG
						vde_ip_log=1;
#endif
#endif

					}
					else
						netusage(); //implies exit
					break;

				case 'p':
					open_args.port=atoi(optarg);
					if (open_args.port <= 0)
						usage(argv[0]); //implies exit
					break;

				case 'h':
					usage(argv[0]); //implies exit
					break;

				case 's':
					sockname=strdup(optarg);
					break;

				case 'm': 
					sscanf(optarg,"%o",(unsigned int *)&(open_args.mode));
					break;

				case 'g':
					open_args.group=strdup(optarg);
					break;

				case 'l':
#ifdef VDE_IP_LOG
					write_syslog_entry("START");
					atexit(write_syslog_close);
					vde_ip_log=1;
					break;
#endif

				default:
					usage(argv[0]); //implies exit
			}
		}

		if (optind < argc && sockname==NULL)
			sockname=argv[optind];
	}
	atexit(cleanup);
	setsighandlers();
	conn=vde_open(sockname,"vde_plug:",&open_args);
	if (conn == NULL) {
		fprintf(stderr,"vde_open %s: %s\n",sockname?sockname:"DEF_SWITCH",strerror(errno));
		exit(1);
	}

	vdestream=vdestream_open(conn,STDOUT_FILENO,vdeplug_recv,vdeplug_err);

	pollv[1].fd=vde_datafd(conn);
	pollv[2].fd=vde_ctlfd(conn);

	for(;;) {
		poll(pollv,3,-1);
		if ((pollv[0].revents | pollv[1].revents | pollv[2].revents) & POLLHUP ||
				pollv[2].revents & POLLIN)
			break;
		if (pollv[0].revents & POLLIN) {
			nx=read(STDIN_FILENO,bufin,sizeof(bufin));
			/* if POLLIN but not data it means that the stream has been
			 * closed at the other end */
			/*fprintf(stderr,"%s: RECV %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/
			if (nx==0)
				break;
			vdestream_recv(vdestream, bufin, nx);
		}
		if (pollv[1].revents & POLLIN) {
			nx=vde_recv(conn,bufin,BUFSIZE-2,0);
			if (nx<0)
				perror("vde_plug: recvfrom ");
			else
			{
				vdestream_send(vdestream, bufin, nx);
				/*fprintf(stderr,"%s: SENT %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/
			}
		}
	}

	return(0);
}