cminix

Adding custom flag to CAT command in MINIX


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.


Solution

  • 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.