0
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int kp_test_open(const char *name);
int kp_test_close(int fd);

int kp_test_open(const char *name){
   int dskr;
   dskr = open( name, O_RDONLY );
   if( dskr == -1 ){
      perror( name );
      exit(1);
   }
   printf( "dskr = %d\n", dskr );
   return dskr;
}

int kp_test_close(int fd){
   int rv;
   rv = close( fd );
   if( rv != 0 ) perror ( "close() failed" );
   else puts( "closed" );
   return rv;
}

int main( int argc, char *argv[] ){
   int d;
   if( argc != 2 ){
      printf( "Naudojimas:\n %s failas_ar_katalogas\n", argv[0] );
      exit( 255 );
   }
   d = kp_test_open( argv[1] );
   kp_test_close( d );
   kp_test_close( d ); /* turi mesti close klaida */
   return 0;
}

so i have program written in C called test and i want to run it in bash so that

 d = kp_test_open( argv[1] );
   kp_test_close( d );
   kp_test_close( d ); 

these 3 lines would be activated, i have to somehow change argc value to 2, can i somehow do it in bash?

When i run my compiled script i use

./test

and now i get these results

Naudojimas:
./test failas_ar_katalogas
  • 1
    Pass one argument other than the program's name on the command line. That's all there is to it. The program's name becomes `argv[0]` (by default), the other argument becomes `argv[1]`, and because there are two of them, argc is 2. – Charles Duffy May 09 '18 at 21:08
  • Could you [edit] the question to show the invocation you're using, which you *expect* to have this effect but does not have it in practice? – Charles Duffy May 09 '18 at 21:09
  • About the code comment `/* turi mesti close klaida */` (must throw a close mistake). C does not throw/catch mistakes. They either break the code immediately, or later, or you detect them yourself from the return value of functions you call, before executing more code. – Weather Vane May 09 '18 at 21:10
  • @WeatherVane I think he's referring to the `perror("close() failed");` in the function. – Barmar May 09 '18 at 21:12
  • 1
    You run the program as `./programname filename`. That will set `argv[1]` to `"filename"` – Barmar May 09 '18 at 21:13
  • @Barmar, ...was I not clear enough in trying to communicate the same thing in the very first comment on the question? – Charles Duffy May 09 '18 at 21:14
  • BTW, "test" is not a particularly good name to use for a program, since it's also the name of a shell-builtin command. – Charles Duffy May 09 '18 at 21:16
  • @CharlesDuffy I'm just helping you understand what he meant by "must throw a close mistake". The comment is just saying that he expects that second call to trigger the perror. – Barmar May 09 '18 at 21:19

1 Answers1

0

argc is set to the number of arguments your program is invoked with, including argv[0] (which is by default set to the name of the program itself).

Thus, if you run:

./yourprogram "some argument"

argc will be 2, because yourprogram is one argument (argv[0]) and some argument is another (argv[1]).

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441