How to check which terminals are open using c -


i trying check terminals open using c. when type "w" terminal shows 4 terminals open (which how many terminals have open). however, when run code tells me there 20 open. how fix this?

#include <stdlib.h> #include <stdio.h>  #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/wait.h> #include <signal.h> #include <time.h>   const char pts[] = "/dev/pts/"; int s1=0;  file *fp = null; char *terminal[4]; char* check;  int main(int argc, char *argv[]){  int i; char strdev[100];   for(i=0; i<100; i++){   sprintf(strdev, "%s%d", pts, i);   printf("opening %s...\n", strdev); fflush(stdout);    if((fp = fopen(strdev, "w")) == null) ;   else{    printf("\topened %s\n", strdev); fflush(stdout);      terminal[s1] = strdev;     s1++;   } } return 0; } 

running w reasonably portable. count needed, not headers. first line of headers usually status line giving actual number of users (but awkward parse in c), , remaining line of headers shows column names, beginning in first column. there no standard w or format. posix describe who, avoids describing format uses. so, w:

#include <stdio.h> int main(void) {     int result = 0;     file *fp;     if ((fp = popen("w", "r")) != 0) {         int lineno = 0;         char *buffer = 0;         size_t size = 0;         int head = 0;         while (getline(&buffer, &size, fp) > 0) {             if (lineno++ == 0) {                 head = (*buffer != ' ') ? 2 : 1;             } else if (head++ > 1) {                 ++result;             }         }         pclose(fp);     }     printf("%d terminals open\n", result);     return 0; } 

if want know terminals in use, way use column information header , select text following lines. no applicable standard, width (and order) of columns can vary 1 system another, solution using specific offsets , string-lengths flawed.

however, displaying list of available (unused) terminals, on own, because on systems corresponding special devices protected ordinary (non-privileged) programs not open them, test existence.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -