1

I am trying to modify cat command in MINIX 3.2.1, and I am having some problems with it. I want to add -H flag, so that when it is used, file name is printed before its contents. I have added some code in cat.c (referring to original lines' numbers):

Line 64 (Hflag variable):

int Hflag, bflag, eflag, fflag, lflag, nflag, sflag, tflag, vflag;

Line 85 ('H' case):

        case 'H':
            Hflag = 1;
            break;
        case 'b':

Line 130 (Hflag parameter):

if (Hflag || bflag || eflag || nflag || sflag || tflag || vflag)

Line 142 (added fprintf to ensure that cat.c was indeed added to recompiled MINIX):

fprintf(stdout, "new line\n");
FILE *fp;

Line 157 (line that should print file name):

fprintf(stdout, "%s\n", *argv);
filename = *argv++;

The problem is that after recompiling (make build in /usr/src), cat does not seem to recognize new -H flag. If I type

# cat -H .exrc

I get

cat: unknown option -- H

However, if I type

# cat -b .exrc

I get
new line
.exrc
1  set autoindent autowrite report=2 showmatch

So cat has actually changed, as I get "new line" line as well as ".exrc" line (looks like I should have put that inside if condition, but anyway), yet -H is not recognized.

What have I done wrong? What do I actually have to do to add new flag? Thanks in advance.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
BEEET Hvcw
  • 49
  • 4

1 Answers1

6

Since cat in Minix uses getopt, you also need to add a new option into getopt call (which lists all possible options).

The code you have provided doesn't do this. You might also consult man getopt.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • This is minix, not linux, the userland is BSDish, so no `getopt_long`. Just `getopt`. – n. m. could be an AI Mar 18 '19 at 17:46
  • I had a look at it, but, unfortunately, it did not make things any easier. From what I understood, getopt looks for the letter in optstring. To make -H flag work, I have find this optstring and add 'H' letter there. Am I correct? And if so, in which source file can I find toptstring to modify it? – BEEET Hvcw Mar 18 '19 at 18:09
  • 1
    @BEEETHvcw your first part is correct, you need to modify optstring. I do not know where minix version of `cat` defines it, you can probably just grep in the repo. – SergeyA Mar 18 '19 at 18:18
  • 1
    Oh, I am dumb. All this time it was right in front of my eyes, hidden in plain sight on line 83: while ((ch = getopt(argc, argv, "beflnstuv")) != -1). I am recompiling MINIX now and hopefully it will work. – BEEET Hvcw Mar 18 '19 at 18:30