home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / unix / volume04 / rpt < prev    next >
Encoding:
Internet Message Format  |  1988-09-11  |  3.9 KB

  1. From: <talcott!bradley!brad>
  2. Subject: A program called 'rpt'
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 4, Issue 63
  7. Submitted by: Bradley Smith <talcott!bradley!brad>
  8.  
  9.  
  10. Here is the submission of a program called 'rpt'. We find it more useful
  11. than repeat on 'csh' and since 'sh' doesn't have it I wrote this.
  12.  
  13.  
  14. Bradley Smith            UUCP: {cepu,ihnp4,noao,uiucdcs}!bradley!brad
  15. Text Processing            ARPA: cepu!bradley!brad@UCLA-LOCUS
  16. Bradley University        PH: (309) 676-7611 Ext. 446
  17. Peoria, IL 61625
  18.  
  19. : This is a shar archive.  Extract with sh, not csh.
  20. : This archive ends with exit, so do not worry about trailing junk.
  21. echo 'Extracting rpt.1'
  22. sed 's/^X//' > rpt.1 << '+ END-OF-FILE rpt.1'
  23. X.TH RPT 1
  24. X.SH NAME
  25. Xrpt  \-   general purpose repeat command
  26. X.SH SYNOPSIS
  27. X.B rpt
  28. X[ -inputfilename ]
  29. X.I count command
  30. X.SH DESCRIPTION
  31. XExecutes \fIcommand\fR the number of times specified by \fIcount\fR.
  32. X\fIcommand\fR may be a simple command, a pipeline, a command
  33. Xlist, a parenthesized command list or a quoted command list.
  34. XAll metacharacters in a parenthesized command list must be
  35. Xpreceded by a backslash.
  36. X.PP
  37. XInput for \fIcommand\fR may be taken from a file by adding
  38. Xa `-inputfilename' flag.
  39. XInput will be read in as many times as specified in \fIcount\fR.
  40. X.SH EXAMPLES
  41. X.nf
  42. Xrpt 100 nroff -T12 letter
  43. X\0\0\0formats letter 100 times
  44. Xrpt -names 50 nroff -T10 label
  45. X\0\0\0formats label 50 times using names as input file each time
  46. Xrpt 15 tbl costs | nroff -Tcrt12 | col | lpr
  47. X\0\0\0formats costs 15 times; `tbl costs' is input to rest of
  48. X\0\0\0pipeline during each execution
  49. Xrpt 15 "tbl cost | nroff -Tcrt12 | col | lpr"
  50. X\0\0\0similar to above, except all of quoted list before `| lpr'
  51. X\0\0\0is input for lpr during each execution
  52. Xrpt 50 \\( nroff -T12 file1 \\; nroff -T10 file2 \\)
  53. X\0\0\0parenthesized command list formats file1 and file2 50 times
  54. X\0\0\0in 12 and 10 pitch, respectively
  55. X.fi
  56. + END-OF-FILE rpt.1
  57. chmod 'u=r,g=r,o=' 'rpt.1'
  58. echo '    -r--r-----  1 brad         1192 Feb 11 22:23 rpt.1        (as sent)'
  59. echo -n '    '
  60. /bin/ls -l rpt.1
  61. echo 'Extracting rpt.c'
  62. sed 's/^X//' > rpt.c << '+ END-OF-FILE rpt.c'
  63. X/*
  64. X * rpt - repeat command much like csh but works on both sh and csh
  65. X *
  66. X * By Bradley Smith , Sep 1984
  67. X * {ihnp4,uiucdcs,cepu}!bradley!brad
  68. X * Bradley University
  69. X * Text Processing
  70. X * Peoria, IL  61625
  71. X */
  72. X#include <stdio.h>
  73. X#include <signal.h>
  74. X
  75. Xint    pid;
  76. Xmain(argc, argv)
  77. Xchar *argv[];
  78. Xint argc;
  79. X{
  80. X    register int i, j;
  81. X    char *c;
  82. X    int status[2], inpfd, swi, ret;
  83. X    int onintr();
  84. X
  85. X
  86. X    swi = 0;
  87. X    if(argv[1][0] == '-') {    /* input file */
  88. X        c = argv[1];
  89. X        c++;
  90. X        swi = 1;
  91. X        argv++;
  92. X        argc--;
  93. X    }
  94. X        
  95. X    if((swi== 0) && (argc < 3)) {
  96. X        fprintf(stderr,"Usage: %s [-inputfile] count command\n",
  97. X            argv[0]);
  98. X        exit(4);
  99. X    }
  100. X    signal(SIGINT, onintr);
  101. X    signal(SIGQUIT, onintr);
  102. X    signal(SIGKILL, onintr);
  103. X    ++argv;
  104. X    --argc;
  105. X    i = atoi(*argv);
  106. X    argv++;
  107. X    --argc;
  108. X    for(j = 0; j < i; j++) {
  109. X        pid = fork();
  110. X        if(pid == 0) {    /* child process */
  111. X            signal(SIGINT,SIG_DFL);
  112. X            signal(SIGQUIT,SIG_DFL);
  113. X            signal(SIGKILL,SIG_DFL);
  114. X            if(swi==1)  {    /* check on input */
  115. X                if((inpfd=open(c,0))== (-1))
  116. X                    fprintf(stderr,"Can't open '%s'\n", c);
  117. X                else {
  118. X                    close(0);
  119. X                    if((ret=dup(inpfd))) {
  120. X                       fprintf(stderr,"Error %d on dup\n",
  121. X                        ret);
  122. X                    }
  123. X                }
  124. X            }
  125. X            run( argv, argc);
  126. X            _exit(2);
  127. X        }
  128. X        while((wait(status)) != pid)
  129. X            ;
  130. X    }
  131. X    exit(0);
  132. X}
  133. Xrun(argv, argc)
  134. Xchar **argv;
  135. Xint argc;
  136. X{
  137. X    char **cp;
  138. X    char cmd[5120],*cx;
  139. X
  140. X    cp = argv;
  141. X    cx = cmd;
  142. X
  143. X    while(argc > 0) {
  144. X        sprintf(cx,"%s ", *cp);
  145. X        cx = strlen(cmd) + cmd;
  146. X        cp++;
  147. X        --argc;
  148. X    }
  149. X    execl("/bin/sh","sh","-c",cmd,0);
  150. X        
  151. X}
  152. Xonintr()
  153. X{
  154. X    sleep(2);
  155. X    fprintf(stderr,"\nInterrupt...\n");
  156. X    exit(1);
  157. X}
  158. + END-OF-FILE rpt.c
  159. chmod 'u=rw,g=rw,o=' 'rpt.c'
  160. echo '    -rw-rw----  1 brad         1535 Feb 11 22:34 rpt.c        (as sent)'
  161. echo -n '    '
  162. /bin/ls -l rpt.c
  163. exit 0
  164.  
  165.