OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.1 KiB

  1. /*
  2. * --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. *
  5. * Filename: package/.../sysfiles/btee.c
  6. * Copyright (C) 2004 - 2006 The T2 SDE Project
  7. *
  8. * More information can be found in the files COPYING and README.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License. A copy of the
  13. * GNU General Public License can be found in the file COPYING.
  14. * --- SDE-COPYRIGHT-NOTE-END ---
  15. */
  16. /* btee.c, a buffered tee clone - written for ROCK Linux
  17. Copyright (C) 1998, 1999, 2001, 2003 Clifford Wolf
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22. This program is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. GNU General Public License for more details.
  26. You should have received a copy of the GNU General Public License
  27. along with this program; if not, write to the Free Software
  28. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. */
  30. #define _GNU_SOURCE
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <signal.h>
  36. #include <stdlib.h>
  37. #include <fcntl.h>
  38. #define BUFFER_SIZE (8*1024-1)
  39. static char buffer[BUFFER_SIZE+1];
  40. #define EOT 004
  41. void exit_handler(int sig) {
  42. exit(1);
  43. }
  44. int main(int argc, char ** argv) {
  45. int rc, mode, x, y;
  46. int remove_zeros=0;
  47. int pos=0, killme=0;
  48. if ( argc!=3 || (argv[1][0]!='a' && argv[1][0]!='t') ) {
  49. printf("Usage: %s {a|t} [file]\n",argv[0]);
  50. return 1;
  51. }
  52. if (argv[1][0]=='a')
  53. mode=O_WRONLY|O_CREAT|O_APPEND;
  54. else
  55. mode=O_WRONLY|O_CREAT|O_TRUNC;
  56. signal(SIGALRM, exit_handler);
  57. while (1) {
  58. if (killme == 1) {
  59. killme = -1;
  60. alarm(3);
  61. }
  62. if (pos >= BUFFER_SIZE) {
  63. fprintf(stderr, "%s: Buffer is full -> "
  64. "drop data!\n",argv[0]);
  65. pos=0;
  66. }
  67. rc=read(0,buffer+pos,BUFFER_SIZE-pos);
  68. if (rc <= 0) return 0;
  69. buffer[pos+rc+1]=0;
  70. if (rc>0) {
  71. for (x=0; x<rc; x++) {
  72. if ( buffer[pos+x] != EOT )
  73. write(1,buffer+pos+x,1);
  74. }
  75. for (x=0; x<rc; x++) {
  76. if (buffer[pos+x]==EOT) {
  77. /* We wait a few seconds so we are
  78. * still able to pipe thru 'early
  79. * errors' from daemons. */
  80. buffer[pos+x]=0;
  81. if (!killme) killme = 1;
  82. remove_zeros=1;
  83. }
  84. if (buffer[pos+x]=='\r' &&
  85. buffer[pos+x+1]!='\n') {
  86. for (y=pos+x; y>=0; y--) {
  87. if (buffer[y]=='\n') break;
  88. buffer[y]=0;
  89. }
  90. remove_zeros=1;
  91. }
  92. }
  93. pos+=rc;
  94. if (remove_zeros) {
  95. for (x=y=0; x<pos; x++) {
  96. if (buffer[x])
  97. buffer[y++]=buffer[x];
  98. }
  99. pos=y; remove_zeros=0;
  100. }
  101. rc=open(argv[2],mode,0666);
  102. if (rc>=0) {
  103. write(rc,buffer,pos);
  104. close(rc);
  105. pos=0;
  106. mode=O_WRONLY|O_APPEND;
  107. }
  108. }
  109. }
  110. return 0;
  111. }