Jump to content

Command line/ stdin

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
erasm

erasm

    Newbie

  • Members
  • Pip
  • 2 posts
Im having some trouble implementing this cat emulation program. What would be the best way to go about parsing fp as stdin if no non-option(file args) are parsed through the command line.
pseudo
if(file){
fp = fopen(argv[index])        //as i have
else if(no file args)
fp = stdin;
}
while(c = fgetc(fp) != EOF)
read
}
I'm basically having trouble writing/interpreting this logic.

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

int main(int argc, char *argv[]){
  int optchar;
  int index;
  FILE *fp;
  int lineCount = 1;
  int nflag = 0;
  int sflag = 0;
  int vflag = 0;
  int tflag = 0;
  int c;
  int endline = 1;
  int newlineCount = 0;
  int cntrlFlag = 0;

  while((optchar = getopt (argc, argv, "nsvt")) != -1){
    switch(optchar){
      case 'n':
            nflag = 1;
        break;
      case 's':
            sflag = 1;
        break;
      case 'v':
            vflag = 1;
        break;      
      case 't':
            tflag = 1;
        break;
    }
  }


  for(index = optind; index < argc; index++){
    printf("hi");
    fp = fopen(argv[index], "r");
    printf("index = %d \n", index);
    printf("arg = %s \n", argv[index]);


      if(fp == NULL){
        fprintf(stdout,"File %s couldn't be opened!\n", argv[index]);
        fp = stdin;
      }
        while((c = fgetc(fp)) != EOF){
          cntrlFlag = 0;
          if(c != '\n'){
            newlineCount = 0;
          }
          if(nflag && endline){
            if(sflag && newlineCount < 2){
              printf("%5d  ", lineCount);
              lineCount++;
            }
            if(!sflag){
              printf("%5d  ", lineCount);
              lineCount++;
            }
            endline = 0;
          }
          if(iscntrl(c) && tflag && c != '\n'){
            c += 64;
            printf("^%c", c);
            cntrlFlag = 1;
          }
          if(newlineCount < 2 && sflag && !cntrlFlag){
            printf("%c", c);
          }
          if(!sflag && !cntrlFlag){
            printf("%c", c);
          }
          if(c == '\n'){
            endline = 1;
            newlineCount++;
          }
        }
    fclose(fp);
  }
  return 0;
}

Edited by Jaan, 11 August 2009 - 03:32 AM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
stdin will never return EOF. You will need some other sentinel value for it. The ASCII value of Ctrl-Z might be one option.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
G_Morgan

G_Morgan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 537 posts
Ctrl+Z on Windows gives EOF AFAIK. Also Ctrl+D on Unix systems.

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts

Quote

Also Ctrl+D on Unix systems.

Not necessarily; you can disable that with your cshrc/bashrc file. I would also stick in a CTRL-C handler, as that's a more or less universal way of telling the program to stop.
sudo rm -rf /

#5
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
ctrl+D is different from ctrl+c.
check this page : [SOLVED] Difference bw ctrl C & ctrl D & ctrl Z - Linux Forums

It says ctrl+c, ctrl+d and ctrl+z are all different.

God is real... unless declared an integer

my blog :: http://techarraz.com/


#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,715 posts
I know they're different. But you can't override CTRL-C functionality on a command prompt, only in your program. Hence my "more or less universal" comment. They may send different signals, but CTRL-C and CTRL-D accomplish the same thing in the end. CTRL-Z is a special case, because on Windows systems it sends EOF like G_Morgan said, and on Linux (probably UNIX as well) it tells the shell to suspend the current process, but not kill it. AFAIK you can't override that either. Think of it this way: CTRL-C sends SIGINT on Windows and *NIX. It would do you well to stick in a handler for it at the very least. CTRL-D and CTRL-Z could get messy on Windows.
sudo rm -rf /