0

I'm trying to use fgets in a simple while loop. fgets just simply isn't being called.

Full code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_BUFF 20
#define OSPID //Here is where I would put the ospid I was interested in

int readContentsOfProcessMapsFile(int target);


int main(int argc, char** argv) {

char lineprompt[LINE_BUFF];
char command1[LINE_BUFF], command2[LINE_BUFF];
int flag;


if( readContentsOfProcessMapsFile(OSPID) != 0)
{
    printf("Error reading file");
    exit(EXIT_FAILURE);
}

while(1) {

    fflush(stdout);
    printf("enter name:>");
    if ( fgets(lineprompt, sizeof(lineprompt), stdin) == NULL)
    {
        printf("Error using fgets\n");
        break;
    }

    sscanf(lineprompt, "%s %s", &command1, &command2);

    if (strcmp(command1, "show") == 0)
    {
        if( (strcmp(command2, "active") == 0) )
        {
            flag = 1;
        }
        else if( (strcmp(command2, "inactive") == 0) )
        {
            flag = 2;
        }
        else
        {
           flag =0;
        }
        printf("run show function on ospid file");
    }
    else if (strcmp(command1, "exit") == 0) 
    {
        printf("run exit/cleanup");
        break;
    } 
    else if (strcmp(command1, "help") == 0) 
    {
        printf("run help function");
        continue;
    } 
    else {
    printf("Nothing entered, run help function");
    continue;
  }
}
}

int readContentsOfProcessMapsFile(int target) {
  FILE *fd; // /proc/<target>/maps
  char name[128], *line = NULL, *match = NULL;
  size_t len = 0;
  char* filename = NULL;

  snprintf(name, sizeof (name), "/proc/%u/maps", target);

  if ((fd = fopen(name, "r")) == NULL) {
    printf("error");
    return (EXIT_FAILURE);
  }

  while (getline(&line, &len, fd) != -1) 
  {
    unsigned long start, end;
    char read, write, exec, cow;
    int offset, dev_major, dev_minor, inode;

    filename = realloca(filename, len);

    sscanf(line, "%p-%p %c%c%c%c %x %x:%x %u %[^\n]", &start, &end, &read,
            &write, &exec, &cow, &offset, &dev_major, &dev_minor, &inode, filename);
    match = strstr(filename, "/ora_");
    if (match != NULL) 
    {
        printf("##lowAddr:%p, highAddr:%p and filename:%s\n", start, end, name);
    }
  }
  free(filename);
  free(line);
  if (fclose(fd) == -1)) 
  {
    printf("Failed to close %s", fd);
    return (EXIT_FAILURE);
  }
  return (EXIT_SUCCESS);
}

I've tried adding fseek(stdin,0,SEEK_END); and fflush(stdin); just after the while loop, but fgets() is still being skipped over. I understand it's not good pratice to use fflush(stdin) anyway's.

When this happens getchar() is printing either a square or a Y with two dots above it. So am I right in thinking the reason for fgets() being skipped over is that it's picking up these chars? Could anyone offer a way to stop/prevent this?

Thanks for all the comments. I've narrowed it down to the above readContentsOfProcessMapsFile function. If I comment this out then fgets works as expected.

Now that I fixed my typo regarding the close(fd) it doesn't seem to actually be closing the file! Just found my own error there! Changed to fclose but see getting the same problem

29
  • 2
    There's nothing wrong with your fgets call. Get rid of the char b = getchar() and the printf("%c", b); those are just confusing the issue. Don't muck aroiund with fseek or fflush(stdin); those will just confuse the issue more. Jan 24, 2018 at 10:59
  • 2
    Your problem is probably in something we can't see. The snippet you posted is fine. Jan 24, 2018 at 11:00
  • 1
    Something that we can't see, because it is not visible in the snippet you posted, is causing your problem. Beyond that it's hard to say. But I doubt it's being "skipped over"; I suspect some other aspect of your larger program's behavior is confusing you. Jan 24, 2018 at 11:10
  • 1
    But, seriously, enough speculating. You need to show us code that actually demonstrates your problem -- that is, what the pedants call an "MVCE". Not your whole program, but a stripped-down, but complete program that both compiles and demonstrates your problem. We can't debug what we can't see. Jan 24, 2018 at 12:08
  • 1
    What is this: if (close(fd == -1)) ? The close(FILE *) function just requires the file descriptor as an argument, not a Boolean expression. (which will probably not be the same as the file descriptor.
    – ryyker
    Jan 24, 2018 at 13:57

1 Answer 1

0

I'm trying to use fgets in a simple while loop. fgets just simply isn't being called....

When the problem you are trying to solve is is surrounded with distractions, it is likely you are spending all your efforts on the distractions and not the problem. As shown, your code contains not only the problem(s) you describe, but a bunch of distractions.

The comments offer several suggestions to clean up your distractions. The following is my take on no distractions:

A very simple example of a working fgets in a loop reading stdin example:

#include <stdio.h>

int main(void)
{
        char temp[BUFSIZ];  //BUFSIZ defined as 512 in stdio on my system.

        while((fgets(temp,BUFSIZ,stdin)) != NULL)
                printf("%s\n",temp);

        return 0;
}

EOF will cause the loop to exit. (ctrl-C to stop on Windows)

When you get this to work, slowly start to include other desired functionality around it, verifying each time that the added functionality does not break the code. As you progress, you may run into an occasional bug, When that happens use a step-wise, pragmatic approach to determining the problem, then continue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.