home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3904 < prev    next >
Encoding:
Text File  |  1992-07-25  |  3.2 KB  |  146 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!cs.utexas.edu!torn!watserv1!watmath!undergrad.math.waterloo.edu!bbobak
  3. From: bbobak@undergrad.math.waterloo.edu (Brad Bobak)
  4. Subject: Re: I am stumped on this one!!!
  5. Message-ID: <Brx394.4qL@undergrad.math.waterloo.edu>
  6. Organization: University of Waterloo
  7. References: <1992Jul24.204944.18516@cbfsb.cb.att.com>
  8. Date: Fri, 24 Jul 1992 23:43:51 GMT
  9. Lines: 135
  10.  
  11. In article <1992Jul24.204944.18516@cbfsb.cb.att.com> rajeev@cbnewsf.cb.att.com (rajeev.dolas) writes:
  12. >
  13. >Hello all,
  14. >
  15. >    I have included a function from my code which doesn't work on a Sun
  16. >    (690 w/ 4.1.2) but works on an Amdahl and I can't figure out why. 
  17. >    I am seeking help from the net after spending considerable amount of
  18. >    time trying to debug this. I appreciate your help.
  19. >
  20. >    Thanks in advance.
  21. >
  22. >    Raj Dolas
  23. >    raj@qsun.att.com
  24. >
  25. >
  26. >
  27. >#include <stdio.h>
  28. >
  29. >/* I have deleted lots of stuff to keep it simple for netters */
  30. >parse_conf(f2)
  31. >int f2;
  32. >{
  33. >
  34. >    char fname[80], ch, cmp[80];
  35. >    char pars_wbuf[128];
  36. >    char *pbuf;
  37. >    char *cp_line;
  38. >    char *tmp_line;
  39. >    char *var, *getenv();
  40. >    int comp, he;
  41. >    int parse_ret;
  42. >    int fd;
  43. >    FILE *fp1, *fp2;
  44. >
  45. >    strcpy(fname, "/qsun/h5/raj/progs/acs/config/Star3/072392");
  46. >
  47. >    umask(0);
  48. >    umask(066);
  49. >    if((fp1 = fopen(fname, "w")) == NULL) 
  50. >    {
  51. >        if((fp1 = fopen(fname, "w+")) == NULL)
  52. >        printf("Cannot open fp1 in parse_conf()\n");
  53. >        return(-78);
  54. >    }
  55. >
  56. >    /** f2 was opened in another function with "w" and data was written */
  57. >    if((fp2 = fdopen(f2, "r")) == NULL)
  58. >    {
  59. >        printf("Cannot open fp2 in parse_conf()\n");
  60. >        return(-78);
  61. >    }
  62. >
  63. >    /* As data was written to this file before, I must do a fseek */
  64. >    if((he = fseek(fp2, 0L, 0)) < 0)
  65. >    {
  66. >        printf("fseek error in parse_conf()\n");
  67. >        return(-78);
  68. >    }
  69. >
  70. >    /* str_comp is my function which returns -1 if match is not found */
  71. >    while((fgets(pars_wbuf, 80, fp2) != NULL) &&
  72. >        ((parse_ret = str_comp(cmp, pars_wbuf)) != 0))
  73. >    {
  74. >        pars_wbuf[strlen(pars_wbuf)] = '\0';
  75. >        pbuf = pars_wbuf;
  76. >        cp_line = (char *)malloc(sizeof(strlen(pars_wbuf)));
  77.  
  78.  
  79.  You're allocing the sizeof an int, which is likely four bytes.
  80.  You also forgot to alloc space for the '\0'. Try it again with:
  81.  
  82.     cp_line = (char*)malloc(sizeof(strlen(pars_wbuf) + 1));
  83.  
  84. >        strcpy(cp_line, "\0");
  85. >        tmp_line = cp_line;
  86. >
  87. >        if((comp = line_comp(pars_wbuf)) == 0)
  88. >            ;
  89. >        else 
  90. >        {
  91. >            /* Traverse thru pbuf, skip "
  92. >            while(*pbuf != '\0')
  93. >            {
  94. >                if(*pbuf != 0x0D)
  95. >                {
  96. >                    /* I was writing a char at a time to
  97. >                       the file before. This part works
  98. >                       on Amdahl but not on a sun.    */
  99. >
  100. >                    /**
  101. >                    ch = *pbuf;
  102. >                    fprintf(stdout, "%c", ch); 
  103. >                    if((he = putc(ch, fp1)) < 0)
  104. >                    {
  105. >                         printf("pars_conf: Write error\n");
  106. >                        return(-13);
  107. >                    }
  108. >                    **/
  109. >
  110. >                    /* Now I save it to a buffer */
  111. >                    *cp_line++ = *pbuf;
  112. >                }
  113. >                *pbuf++;
  114. >            }
  115. >            /* If I do a fputs to stdout, it works fine */
  116. >            /**
  117. >            fputs(tmp_line, stdout);
  118. >            fflush(stdout);
  119. >            **/
  120. >
  121. >            /* dbxtool shows the following error on this instr -
  122. >             signal SEGV in malloc at 0xf773634
  123. >             malloc+0x150:    st    %o0, [%15]
  124. >             */
  125. >            fputs(tmp_line, fp1);
  126. >        }
  127. >        free(cp_line);
  128. >    }
  129. >    
  130. >    fclose(fp1);
  131. >
  132. >    
  133. >    if(fname)
  134. >        free(*fname);
  135. >    
  136. >    if(parse_ret != 0)
  137. >        return(-89);
  138. >
  139. >}
  140. >
  141. >
  142. >
  143.  
  144.  
  145.  
  146.