home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / edit / tcsed / sed.c next >
Encoding:
C/C++ Source or Header  |  1987-06-07  |  2.7 KB  |  133 lines

  1. /*
  2. ** sed.c -- stream editor.
  3. ** Date: Wednesday, 21 January 1987.  Time: 08:18:54.
  4. ** Roberto T. Edwards
  5. */
  6.   
  7. #define LINT_ARGS
  8. #define EOS '\0';
  9.   
  10. #include "stdio.h"
  11. #include "string.h"
  12. #include "dir.h"
  13. #include "dos.h"
  14.   
  15. #define MAXSTR 256
  16.   
  17. FILE *open_file();
  18. char *getfile();
  19. char buf1[MAXSTR],buf2[MAXSTR];
  20. int buf,oldlen,newlen;
  21.   
  22. main(argc,argv)
  23. int argc;
  24. char **argv;
  25. {
  26.    FILE *fp;
  27.    char *name;
  28.   
  29.    if (argc != 4) {
  30.       printf("usage: sed <file> <old_string> <new_string>\n");
  31.       exit(1);
  32.    }
  33.    oldlen = strlen(argv[2]);
  34.    newlen = strlen(argv[3]);
  35.    while (name = getfile(argv[1])) {
  36.       fp = open_file(name,"r");
  37.       printf("\n/* %s */\n\n",name);
  38.       while (fgets(buf1,MAXSTR-1,fp)) {
  39.          buf = 1;
  40.          replace(buf1,argv[2],argv[3]);
  41.          printf("%s",buf == 1 ? buf1:buf2);
  42.       }
  43.       fclose (fp);
  44.    }
  45. }
  46.   
  47. /*
  48. ** open_file -- open a stream and check for errors.
  49. */
  50.   
  51. FILE *open_file(name,mode)
  52. char *name,*mode;
  53. {
  54.    FILE *fp;
  55.   
  56.    fp = fopen(name,mode);
  57.    if (fp == NULL) {
  58.       printf("error: can't open %s",name);
  59.       exit(1);
  60.    }
  61.    return(fp);
  62. }
  63.   
  64. /*
  65. ** replace -- replace all occurrences of oldstr with newstr in buffer
  66. */
  67.   
  68. replace(buffer,oldstr,newstr)
  69. char *buffer,*oldstr,*newstr;
  70. {
  71.    char *osp,*b1p,*b2p;
  72.   
  73.    if ((osp=strstr(buffer,oldstr)) == NULL)
  74.       return;
  75.    b1p = buf1;
  76.    b2p = buf2;
  77.    if (buf == 1) {
  78.       while (b1p != osp)
  79.          *b2p++ = *b1p++;
  80.       while (*newstr)
  81.          *b2p++ = *newstr++;
  82.       b1p += oldlen;
  83.       while (*b1p)
  84.          *b2p++ = *b1p++;
  85.       *b2p = EOS;
  86.       buf = 2;
  87.       replace(buf2,oldstr,newstr-newlen);
  88.    }
  89.    else {
  90.       while (b2p != osp)
  91.          *b1p++ = *b2p++;
  92.       while (*newstr)
  93.          *b1p++ = *newstr++;
  94.       b2p += oldlen;
  95.       while (*b2p)
  96.          *b1p++ = *b2p++;
  97.       *b1p = EOS;
  98.       buf = 1;
  99.       replace(buf1,oldstr,newstr-newlen);
  100.    }
  101. }
  102.   
  103. char *getfile(arg)
  104. char *arg;
  105. {
  106.    static struct ffblk filblk;
  107.    static flag = 0;
  108.    static struct LLIST {
  109.       char *fname;
  110.       struct LLIST *next;
  111.    } *head,*current;
  112.    char *retval;
  113.   
  114.    if (flag == 0) {
  115.       if (findfirst(arg,&filblk,0))
  116.          return(NULL);
  117.       head = current = (struct LLIST *) malloc(sizeof(struct LLIST));
  118.       current->fname = strdup(filblk.ff_name);
  119.       current->next = NULL;
  120.       while (findnext(&filblk) == 0) {
  121.          current->next = (struct LLIST *) malloc(sizeof(struct LLIST));
  122.          current = current->next;
  123.          current->fname = strdup(filblk.ff_name);
  124.          current->next = NULL;
  125.       }
  126.       current = head;
  127.       flag = 1;
  128.    }
  129.    retval = current->fname;
  130.    current = current->next;
  131.    return(retval);
  132. }
  133.