So I'm going to preface this by saying that this is for a homework project in my class. I am supposed to determine that the user opened four terminal windows before running the program, and I have to do this by determining if I can open four terminal number buffers from /dev/pts/ as read-only. I then have to save these first four buffers so I can open them again to write to the terminals. I know how to open the files with fopen but my issue is even the terminals that aren't open anymore still show up and are accessible. I know that its pretty frowned upon to ask for homework help but I've been working at this for hours and I don't want it written for me I just want some direction. How can I check that there are four terminals open using the method that I have to use? Also here's my code so maybe one of y'all can see what I'm doing wrong.
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 100
int main(){
int i, ptsNum[4], ptsCount = 0;
FILE *fp;
char ptsName[20];
for(i = 0; i < 20; i++){
// Append the terminal number to the end of the buffer name
sprintf(ptsName, "/dev/pts/%d", i);
// Try to open the file
if((fp = fopen(ptsName, "r")) != NULL){
// Save the terminal number if the buffer exists
ptsNum[ptsCount] = i;
ptsCount++;
fclose(fp);
}
}
return 0;
}