fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #define BUF_SIZE 1024
  6.  
  7.  
  8. int options(char* buf, char* optBuf,int linecount, int* flags )
  9. {
  10. if(flags[0]) {
  11. if (strcmp(buf,"\n")!=0) {
  12. sprintf(optBuf,"%s %d", buf,linecount);
  13. return linecount++;
  14. }
  15. sprintf(optBuf,"%s", buf);
  16. } else if(flags[1]) {
  17. sprintf(optBuf,"%s %d", buf,linecount);
  18. return linecount++;
  19. }
  20. sprintf(optBuf,"%s", buf);
  21. return linecount;
  22. }
  23.  
  24.  
  25. int copypaste(char* fileName, int linecount, int* flags)
  26. {
  27. static char buf[BUF_SIZE];
  28. static char optBuf[2*BUF_SIZE];
  29. FILE* f;
  30. if (strcmp(fileName,"-") == 0) {
  31. f = stdin;
  32. } else {
  33. if((f=fopen(fileName,'r'))== NULL) {
  34. perror("Can't open the file\n");
  35. return linecount;
  36. }
  37. }
  38. while (fgets(f, buf, BUF_SIZE)!=NULL) {
  39. linecount = options(buf, optBuf,linecount, flags);
  40. if (fputs(stdout,optBuf) == EOF) {
  41. perror("Error while writing\n");
  42. break;
  43. }
  44. }
  45. if (ferror(f)) {
  46. }
  47. perror("Error while reading\n");
  48. if (f!=0) {
  49. if (close(f) == -1)
  50. {
  51. perror("Can't close the file\n");
  52. }
  53. }
  54. return linecount;
  55. }
  56.  
  57. int main(int argc, char** argv)
  58. {
  59. int i;
  60. int linecount = 1;
  61. int flags[2];
  62. char opt;
  63. flags[0] = 0;
  64. flags[1] = 0;
  65. while ((opt = getopt(argc,argv,"bn"))!=-1) {
  66. switch (opt) {
  67. case 'b': flags[0] = 1; break;
  68. case 'n': flags[1] = 1; break;
  69. default: perror("Wrong option\n"); return -1; break;
  70. }
  71. }
  72. if(optind > argc) {
  73. linecount = copypaste("-", linecount, flags);
  74. } else {
  75. for(i = optind; i<argc;i++) {
  76. linecount = copypaste(argv[i], linecount, flags);
  77. }
  78. }
  79. return 0;
  80. }
  81.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘copypaste’:
prog.c:33:24: warning: passing argument 2 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
   if((f=fopen(fileName,'r'))== NULL) {
                        ^~~
In file included from prog.c:1:0:
/usr/include/stdio.h:274:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
 extern FILE *fopen (const char *__restrict __filename,
              ^~~~~
prog.c:38:15: warning: passing argument 1 of ‘fgets’ from incompatible pointer type [-Wincompatible-pointer-types]
  while (fgets(f, buf, BUF_SIZE)!=NULL) {
               ^
In file included from prog.c:1:0:
/usr/include/stdio.h:624:14: note: expected ‘char * restrict’ but argument is of type ‘FILE * {aka struct _IO_FILE *}’
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
              ^~~~~
prog.c:38:18: warning: passing argument 2 of ‘fgets’ makes integer from pointer without a cast [-Wint-conversion]
  while (fgets(f, buf, BUF_SIZE)!=NULL) {
                  ^~~
In file included from prog.c:1:0:
/usr/include/stdio.h:624:14: note: expected ‘int’ but argument is of type ‘char *’
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
              ^~~~~
prog.c:5:18: warning: passing argument 3 of ‘fgets’ makes pointer from integer without a cast [-Wint-conversion]
 #define BUF_SIZE 1024
                  ^
prog.c:38:23: note: in expansion of macro ‘BUF_SIZE’
  while (fgets(f, buf, BUF_SIZE)!=NULL) {
                       ^~~~~~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:624:14: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘int’
 extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
              ^~~~~
prog.c:40:13: warning: passing argument 1 of ‘fputs’ from incompatible pointer type [-Wincompatible-pointer-types]
   if (fputs(stdout,optBuf) == EOF) {
             ^~~~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:691:12: note: expected ‘const char * restrict’ but argument is of type ‘struct _IO_FILE *’
 extern int fputs (const char *__restrict __s, FILE *__restrict __stream);
            ^~~~~
prog.c:40:20: warning: passing argument 2 of ‘fputs’ from incompatible pointer type [-Wincompatible-pointer-types]
   if (fputs(stdout,optBuf) == EOF) {
                    ^~~~~~
In file included from prog.c:1:0:
/usr/include/stdio.h:691:12: note: expected ‘FILE * restrict {aka struct _IO_FILE * restrict}’ but argument is of type ‘char *’
 extern int fputs (const char *__restrict __s, FILE *__restrict __stream);
            ^~~~~
prog.c:49:13: warning: passing argument 1 of ‘close’ makes integer from pointer without a cast [-Wint-conversion]
   if (close(f) == -1)
             ^
In file included from prog.c:4:0:
/usr/include/unistd.h:356:12: note: expected ‘int’ but argument is of type ‘FILE * {aka struct _IO_FILE *}’
 extern int close (int __fd);
            ^~~~~
prog.c: In function ‘main’:
prog.c:65:17: warning: implicit declaration of function ‘getopt’ [-Wimplicit-function-declaration]
   while ((opt = getopt(argc,argv,"bn"))!=-1) {
                 ^~~~~~
prog.c:72:5: error: ‘optind’ undeclared (first use in this function)
  if(optind > argc) {
     ^~~~~~
prog.c:72:5: note: each undeclared identifier is reported only once for each function it appears in
stdout
Standard output is empty