Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Last active April 5, 2018 03:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktnyt/96d69d3467686bdeede1 to your computer and use it in GitHub Desktop.
Save ktnyt/96d69d3467686bdeede1 to your computer and use it in GitHub Desktop.
Iteractive qstat viewer $ gcc -o iqstat -O3 -lncurses iqstat.c
/******************************************************************************
** iqstat - Interactive qstat viewer
**
** Created by kotone on 2015/01/16.
** Copyright (c) 2015 Kotone Itaya. All rights reserved.
**
** iqstat is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** iqstat is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with iqstat. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
char* exec(char* cmd) {
FILE* pipe;
char* out;
int i, c;
if((out = (char*)malloc(sizeof(char))) == NULL) {
return NULL;
}
if((pipe = popen(cmd, "r")) == NULL) {
return NULL;
}
for(i = 0; (c = fgetc(pipe)) != EOF; ++i) {
if((out = (char*)realloc(out, sizeof(char) * (i + 1))) == NULL) {
return NULL;
}
out[i] = c;
}
pclose(pipe);
return out;
}
char* join(char* s1, char* s2, char d) {
char* p;
int i, c, l = 0;
if((p = (char*)malloc(sizeof(char))) == NULL) {
return NULL;
}
for(i = 0; (c = s1[i]) != '\0'; ++i) {
if((p = (char*)realloc(p, sizeof(char) * (l + 1))) == NULL) {
return NULL;
}
p[l] = s1[i];
++l;
}
if((p = (char*)realloc(p, sizeof(char) * (l + 1))) == NULL) {
return NULL;
}
p[l] = d;
++l;
for(i = 0; (c = s2[i]) != '\0'; ++i) {
if((p = (char*)realloc(p, sizeof(char) * (l + 1))) == NULL) {
return NULL;
}
p[l] = s2[i];
++l;
}
return p;
}
char* join_vec(int argc, char* argv[], char d, int offset) {
char* p;
int i;
if((p = (char*)malloc(sizeof(char))) == NULL) {
return NULL;
}
for(i = offset; i < argc; ++i) {
p = join(p, argv[i], d);
}
return p;
}
int main(int argc, char* argv[]) {
initscr();
noecho();
cbreak();
curs_set(FALSE);
char* cmd;
char* out;
time_t timer;
cmd = join("qstat", join_vec(argc, argv, ' ', 1), ' ');
while(1) {
time(&timer);
erase();
mvprintw(0, 0, "%s", ctime(&timer));
if((out = exec(cmd)) != NULL) {
mvprintw(1, 0, "%s", out);
}
refresh();
usleep(66);
}
endwin();
return 0;
}
@JonnoFTW
Copy link

Thanks for this, super useful, although on my machine I had to compile with -ltinfo

@ktnyt
Copy link
Author

ktnyt commented Oct 19, 2016

Thanks for your comment! Must be useful info for people with compiling issues. I must admit I wasn't really considering cross-platform compatibilty back then.

@JonnoFTW
Copy link

@ktnyt
There's a bug where quote marks are consumed, so running

iqstat -u "*"

Actually tries to run

iqstat -u *

It works if you do

iqstat -u \"*\"

@JonnoFTW
Copy link

JonnoFTW commented Apr 5, 2018

Actually I've made my own version that doesn't have a memory leak: https://gist.github.com/JonnoFTW/11389ecfec0ceda67a554f7650cf9cef

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment