home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1984 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  4.6 KB

  1. From: davidsen@sixhub.UUCP (Wm E. Davidsen Jr)
  2. Newsgroups: alt.sources
  3. Subject: yargs - alternative to xargs
  4. Message-ID: <2147@sixhub.UUCP>
  5. Date: 23 Oct 90 22:10:41 GMT
  6.  
  7.  
  8.   There has been some discussion of bugs and security problems with
  9. xargs. Here is a stripped version, without options, features, or
  10. hopefully bugs.
  11.  
  12. #!/bin/sh
  13. # shar:    Shell Archiver  (v1.27)
  14. #
  15. #    Run the following text with /bin/sh to create:
  16. #      readme
  17. #      makefile
  18. #      yargs.c
  19. #
  20. sed 's/^X//' << 'SHAR_EOF' > readme &&
  21. X                                 YARGS
  22. X
  23. X
  24. X  Many vendors versions of xargs have problems caused by not treating
  25. Xevery input line as a separate argument. Thus shell escapes (as ; and #)
  26. Xand enbedded blanks can cause problems.
  27. X
  28. X  In addition, some vendors limit the length of a data line to 470
  29. Xcharacters, rather than the maximum (often 5120 or more), which causes
  30. Xmultiple forking of the program.
  31. X
  32. X  This is my attempt to solve those problems in a brute force way. The
  33. Xinitial public version has no options. It won't trace, debug, string
  34. Xsubstitute, limit the string lengths or the number of arguments. It does
  35. Xnot sing, not does it dance, it just executes a command using the stdin
  36. Xas arguments, and it does that one thing very well.
  37. X
  38. X  I suppose at some time I will add some features, but for now I would
  39. Xjust like feedback on how well it performs its one major function.
  40. SHAR_EOF
  41. chmod 0644 readme || echo "restore of readme fails"
  42. sed 's/^X//' << 'SHAR_EOF' > makefile &&
  43. X# makefile for yargs
  44. X
  45. XSHAR        = shar
  46. XSHARLIST    = readme makefile yargs.c
  47. X
  48. X# magic makerules for SCCS - prevent making a writable .c file
  49. X.c~.c:
  50. X    get $?
  51. X.c~.o:
  52. X    get $?
  53. X    $(CC) -c $(CFLAGS) $*
  54. X
  55. Xyargs: yargs.o
  56. X    $(CC) -o yargs $(LDFLAGS) yargs.o
  57. X
  58. Xshar:    yargs.shar
  59. Xyargs.shar: $(SHARLIST)
  60. X    $(SHAR) $(SHARLIST) > yargs.shar
  61. SHAR_EOF
  62. chmod 0644 makefile || echo "restore of makefile fails"
  63. sed 's/^X//' << 'SHAR_EOF' > yargs.c &&
  64. X/*
  65. X *  yargs - another implementation of yargs
  66. X *
  67. X *  written to attempt to avoid security problems with several
  68. X *  vendor's versions of yargs.
  69. X *
  70. X**/
  71. X
  72. X/* note - NCARGS is the max number of chars in an exec arglist */
  73. X#include <stdio.h>
  74. X#ifndef NCARGS
  75. X#include <sys/param.h>
  76. X#ifndef NCARGS
  77. X#define NCARGS            470            /* some version like it short */
  78. X#endif
  79. X#endif
  80. X
  81. Xstatic char *Info[] = {
  82. X    "@(#)yargs - v1.4 10/23/90",
  83. X    "Copyright (c) 1990 by Bill Davidsen, all rights reserved.",
  84. X    "This program may be freely used and distributed by anyone",
  85. X    "in the original unmodified form."
  86. X};
  87. X
  88. X#define MAX_IN_LINE        256            /* longest input line */
  89. X#define MAX_ARGS        256            /* args in the vector */
  90. X#define EOS                '\0'        /* end of string */
  91. X
  92. X/* global variables */
  93. Xchar inputline[MAX_IN_LINE+1];        /* input into this buffer */
  94. Xchar *argvect[MAX_ARGS+1];            /* save arg pointers here */
  95. Xint arg_ptr = 0;                    /* highest arg # */
  96. X
  97. Xmain(argc,argv)
  98. Xint argc;
  99. Xchar *argv[];
  100. X{
  101. X    char *saveptr = Info[0];
  102. X    int testlen;
  103. X    int roomleft;
  104. X
  105. X    /* no fancy options in this version */
  106. X    if (argc < 2) {
  107. X        fprintf(stderr, "yargs: no arguments!\n");
  108. X        exit(1);
  109. X    }
  110. X    inputline[MAX_IN_LINE] = EOS;
  111. X
  112. X    /* initialize the command line */
  113. X    init_cmd(argc, argv, argvect, &arg_ptr, &roomleft);
  114. X
  115. X    while (fgets(inputline, MAX_IN_LINE, stdin)) {
  116. X        if ((testlen = strlen(inputline)) == MAX_IN_LINE) {
  117. X            /* input line too long */
  118. X            fprintf("yargs: input line too long!\n");
  119. X            exit(1);
  120. X        }
  121. X
  122. X        /* see if room in line */
  123. X        if (strlen(inputline) > roomleft) {
  124. X            if (argvect[arg_ptr+1]) free(argvect[arg_ptr]);
  125. X            argvect[arg_ptr] = NULL;
  126. X            if (execvp(argv[1], argvect)) exit(1);
  127. X
  128. X            /* now setup for another command line */
  129. X            init_cmd(argc, argv, argvect, &arg_ptr, &roomleft);
  130. X        }
  131. X
  132. X        if (argvect[arg_ptr] == NULL) {
  133. X            argvect[arg_ptr] = (char *)malloc(MAX_IN_LINE+1);
  134. X            if (argvect[arg_ptr] == NULL) {
  135. X                /* ran out of memory */
  136. X                fprintf(stderr, "Can't alloc memory for arg list!\n");
  137. X                exit(1);
  138. X            }
  139. X        }
  140. X
  141. X        /* drop the newline and add the argument */
  142. X        inputline[testlen-1] = EOS;
  143. X        strcpy(argvect[arg_ptr++], inputline);
  144. X        roomleft -= (testlen+2);
  145. X    }
  146. X
  147. X    argvect[arg_ptr] = NULL;
  148. X    execvp(argv[1], argvect);
  149. X}
  150. X
  151. Xinit_cmd(argc, argv, argvect, arg_ptr, roomleft)
  152. Xint argc;
  153. Xchar *argv[], *argvect[];
  154. Xint *arg_ptr, *roomleft;
  155. X{
  156. X    register int m;
  157. X
  158. X    *roomleft = NCARGS-10;
  159. X    for (m=1, *arg_ptr=0; m<argc; ) {
  160. X        *roomleft -= (strlen(argv[m])+3);
  161. X        argvect[(*arg_ptr)++] = argv[m++];
  162. X    }
  163. X}
  164. SHAR_EOF
  165. chmod 0444 yargs.c || echo "restore of yargs.c fails"
  166. exit 0
  167.  
  168. -- 
  169. bill davidsen - davidsen@sixhub.uucp (uunet!crdgw1!sixhub!davidsen)
  170.     sysop *IX BBS and Public Access UNIX
  171.     moderator of comp.binaries.ibm.pc and 80386 mailing list
  172. "Stupidity, like virtue, is its own reward" -me
  173.