Changeset b7ee89b


Ignore:
Timestamp:
Oct 17, 2009, 6:08:15 PM (15 years ago)
Author:
Karl Ramm <kcr@1ts.org>
Branches:
master, release-1.10, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
6ea3890
Parents:
435001d
git-author:
Karl Ramm <kcr@1ts.org> (09/24/09 11:05:55)
git-committer:
Karl Ramm <kcr@1ts.org> (10/17/09 18:08:15)
Message:
Cleanup and use owl_getline{,_chomp} and owl_slurp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • functions.c

    r08e414a rb7ee89b  
    13631363  owl_fmtext fm;
    13641364  FILE *file;
    1365   char buff[1024];
     1365  char *s = NULL;
    13661366
    13671367  file=fopen(filename, "r");
     
    13721372
    13731373  owl_fmtext_init_null(&fm);
    1374   while (fgets(buff, 1024, file)) {
    1375     owl_fmtext_append_normal(&fm, buff);
    1376     /*    owl_fmtext_append_normal(&fm, "\n"); */
    1377   }
     1374  while (owl_getline(&s, file))
     1375    owl_fmtext_append_normal(&fm, s);
     1376  owl_free(s);
    13781377
    13791378  owl_function_popless_fmtext(&fm);
     
    20222021  const char *redirect = " 2>&1 < /dev/null";
    20232022  char *newbuff;
    2024   char *out, buff2[1024];
    2025   int size;
     2023  char *out;
    20262024  FILE *p;
    20272025
     
    20412039    owl_popexec_new(newbuff);
    20422040  } else {
    2043     p=popen(newbuff, "r");
    2044     out=owl_malloc(1024);
    2045     size=1024;
    2046     strcpy(out, "");
    2047     while (fgets(buff2, 1024, p)!=NULL) {
    2048       size+=1024;
    2049       out=owl_realloc(out, size);
    2050       strcat(out, buff2);
    2051     }
     2041    p = popen(newbuff, "r");
     2042    out = owl_slurp(p);
    20522043    pclose(p);
    20532044   
     
    33383329  char *path;
    33393330  FILE *file;
    3340   char buff[LINE];
     3331  char *s = NULL;
    33413332  int fail_silent = 0;
    33423333
     
    33473338    path = owl_util_makepath(filename);
    33483339  }
    3349   file=fopen(path, "r");
     3340  file = fopen(path, "r");
    33503341  owl_free(path);
    33513342  if (!file) {
     
    33553346    return;
    33563347  }
    3357   while (fgets(buff, LINE, file)!=NULL) {
    3358     if (buff[0] == '\0' || buff[0] == '#') continue;
    3359     if (buff[strlen(buff) - 1] == '\n')
    3360       buff[strlen(buff) - 1] = '\0';
    3361     owl_function_command(buff);
    3362   }
     3348  while (owl_getline_chomp(&s, file)) {
     3349    if (s[0] == '\0' || s[0] == '#')
     3350      continue;
     3351    owl_function_command(s);
     3352  }
     3353
     3354  owl_free(s);
    33633355  fclose(file);
    33643356}
  • zephyr.c

    rdcc3f80 rb7ee89b  
    246246  FILE *file;
    247247  char *tmp, *start;
    248   char buffer[1024], subsfile[1024];
     248  char *buffer = NULL;
     249  char *subsfile;
    249250  ZSubscription_t *subs;
    250251  int subSize = 1024;
    251   int count, ret;
     252  int count;
    252253  struct stat statbuff;
    253254
    254255  subs = owl_malloc(sizeof(ZSubscription_t) * subSize);
    255   if (filename==NULL) {
    256     sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
    257   } else {
    258     strcpy(subsfile, filename);
    259   }
    260 
    261   ret=stat(subsfile, &statbuff);
    262   if (ret) {
    263     if (error_on_nofile==1) return(-1);
    264     return(0);
     256  if (filename == NULL)
     257    subsfile = owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
     258  else
     259    subsfile = owl_strdup(filename);
     260
     261  if (stat(subsfile, &statbuff) != 0) {
     262    if (error_on_nofile == 1)
     263      return -1;
     264    return 0;
    265265  }
    266266
    267267  ZResetAuthentication();
    268   count=0;
    269   file=fopen(subsfile, "r");
    270   if (!file) return(-1);
    271   while ( fgets(buffer, 1024, file)!=NULL ) {
    272     if (buffer[0]=='#' || buffer[0]=='\n' || buffer[0]=='\n') continue;
    273    
    274     if (buffer[0]=='-') {
    275       start=buffer+1;
    276     } else {
    277       start=buffer;
    278     }
    279    
     268  count = 0;
     269  file = fopen(subsfile, "r");
     270  owl_free(subsfile);
     271  if (!file)
     272    return -1;
     273  while (owl_getline(&buffer, file)) {
     274    if (buffer[0] == '#' || buffer[0] == '\n')
     275        continue;
     276
     277    if (buffer[0] == '-')
     278      start = buffer + 1;
     279    else
     280      start = buffer;
     281
    280282    if (count >= subSize) {
    281283      subSize *= 2;
     
    284286   
    285287    /* add it to the list of subs */
    286     if ((tmp=strtok(start, ",\n\r"))==NULL) continue;
    287     subs[count].zsub_class=owl_strdup(tmp);
    288     if ((tmp=strtok(NULL, ",\n\r"))==NULL) continue;
    289     subs[count].zsub_classinst=owl_strdup(tmp);
    290     if ((tmp=strtok(NULL, " \t\n\r"))==NULL) continue;
    291     subs[count].zsub_recipient=owl_strdup(tmp);
    292    
     288    if ((tmp = strtok(start, ",\n\r")) == NULL)
     289      continue;
     290    subs[count].zsub_class = owl_strdup(tmp);
     291    if ((tmp=strtok(NULL, ",\n\r")) == NULL)
     292      continue;
     293    subs[count].zsub_classinst = owl_strdup(tmp);
     294    if ((tmp = strtok(NULL, " \t\n\r")) == NULL)
     295      continue;
     296    subs[count].zsub_recipient = owl_strdup(tmp);
     297
    293298    /* if it started with '-' then add it to the global punt list, and
    294299     * remove it from the list of subs. */
    295     if (buffer[0]=='-') {
     300    if (buffer[0] == '-') {
    296301      owl_function_zpunt(subs[count].zsub_class, subs[count].zsub_classinst, subs[count].zsub_recipient, 0);
    297302      owl_free(subs[count].zsub_class);
    298303      owl_free(subs[count].zsub_classinst);
    299304      owl_free(subs[count].zsub_recipient);
    300     }
    301     else {
     305    } else {
    302306      count++;
    303307    }
    304308  }
    305309  fclose(file);
    306 
    307   ret = owl_zephyr_loadsubs_helper(subs, count);
    308   return(ret);
    309 #else
    310   return(0);
     310  if (buffer)
     311    owl_free(buffer);
     312
     313  return owl_zephyr_loadsubs_helper(subs, count);
     314#else
     315  return 0;
    311316#endif
    312317}
     
    362367  ZSubscription_t *subs;
    363368  int numSubs = 100;
    364   char subsfile[1024], buffer[1024];
    365   int count, ret;
     369  char *subsfile;
     370  char *buffer = NULL;
     371  int count;
    366372  struct stat statbuff;
    367373
    368374  subs = owl_malloc(numSubs * sizeof(ZSubscription_t));
    369375
    370   if (filename==NULL) {
    371     sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".anyone");
    372   } else {
    373     strcpy(subsfile, filename);
    374   }
    375  
    376   ret=stat(subsfile, &statbuff);
    377   if (ret) return(0);
    378 
    379   ret=0;
     376  if (filename==NULL)
     377    subsfile = owl_sprintf("%s/%s", owl_global_get_homedir(&g), ".anyone");
     378  else
     379    subsfile = owl_strdup(filename);
     380
     381  if (stat(subsfile, &statbuff) == -1)
     382    return 0;
    380383
    381384  ZResetAuthentication();
    382   count=0;
    383   file=fopen(subsfile, "r");
     385  count = 0;
     386  file = fopen(subsfile, "r");
     387  owl_free(subsfile);
    384388  if (file) {
    385     while ( fgets(buffer, 1024, file)!=NULL ) {
    386       if (buffer[0] == '\0' || buffer[0] == '#' || buffer[0] == '\n') continue;
    387      
     389    while (owl_getline_chomp(&buffer, file)) {
     390      if (buffer[0] == '\0' || buffer[0] == '#')
     391        continue;
     392
    388393      if (count == numSubs) {
    389394        numSubs *= 2;
     
    391396      }
    392397
    393       if (buffer[strlen(buffer) - 1] == '\n')
    394         buffer[strlen(buffer) - 1] = '\0';
    395       subs[count].zsub_class=owl_strdup("login");
    396       subs[count].zsub_recipient=owl_strdup("*");
    397       if (strchr(buffer, '@')) {
    398         subs[count].zsub_classinst=owl_strdup(buffer);
    399       } else {
    400         subs[count].zsub_classinst=owl_sprintf("%s@%s", buffer, ZGetRealm());
    401       }
     398      subs[count].zsub_class = owl_strdup("login");
     399      subs[count].zsub_recipient = owl_strdup("*");
     400      subs[count].zsub_classinst = long_zuser(buffer);
    402401
    403402      count++;
     
    405404    fclose(file);
    406405  } else {
    407     count=0;
    408     ret=-1;
    409   }
    410 
    411   ret = owl_zephyr_loadsubs_helper(subs, count);
    412   return(ret);
    413 #else
    414   return(0);
     406    return 0;
     407  }
     408  if (buffer)
     409    owl_free(buffer);
     410
     411  return owl_zephyr_loadsubs_helper(subs, count);
     412#else
     413  return 0;
    415414#endif
    416415}
     
    953952{
    954953#ifdef HAVE_LIBZEPHYR
    955   char *line, subsfile[LINE], buff[LINE];
     954  char *line, *subsfile, *s = NULL;
    956955  FILE *file;
    957 
    958   line=owl_zephyr_makesubline(class, inst, recip);
    959 
    960   if (filename==NULL) {
    961     sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
    962   } else {
    963     strcpy(subsfile, filename);
    964   }
     956  int duplicate = 0;
     957
     958  line = owl_zephyr_makesubline(class, inst, recip);
     959
     960  if (filename == NULL)
     961    subsfile = owl_sprintf("%s/.zephyr.subs", owl_global_get_homedir(&g));
     962  else
     963    subsfile = owl_strdup(filename);
    965964
    966965  /* if the file already exists, check to see if the sub is already there */
    967   file=fopen(subsfile, "r");
     966  file = fopen(subsfile, "r");
    968967  if (file) {
    969     while (fgets(buff, LINE, file)!=NULL) {
    970       if (!strcasecmp(buff, line)) {
     968    while (owl_getline(&s, file)) {
     969      if (strcasecmp(s, line) == 0) {
    971970        owl_function_error("Subscription already present in %s", subsfile);
    972         owl_free(line);
    973         fclose(file);
    974         return;
     971        duplicate++;
    975972      }
    976973    }
    977974    fclose(file);
    978   }
    979 
    980   /* if we get here then we didn't find it */
    981   file=fopen(subsfile, "a");
    982   if (!file) {
    983     owl_function_error("Error opening file %s for writing", subsfile);
    984     owl_free(line);
    985     return;
    986   }
    987   fputs(line, file);
    988   fclose(file);
    989   owl_function_makemsg("Subscription added");
    990  
     975    owl_free(s);
     976  }
     977
     978  if (!duplicate) {
     979    file = fopen(subsfile, "a");
     980    if (file) {
     981      fputs(line, file);
     982      fclose(file);
     983      owl_function_makemsg("Subscription added");
     984    } else {
     985      owl_function_error("Error opening file %s for writing", subsfile);
     986    }
     987  }
     988
    991989  owl_free(line);
    992990#endif
     
    12771275{
    12781276#ifdef HAVE_LIBZEPHYR
    1279   char *ourfile, *tmp, buff[LINE];
     1277  char *ourfile, *tmp, *s = NULL;
    12801278  FILE *f;
    12811279
    1282   if (filename==NULL) {
    1283     ourfile=owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
    1284   } else {
    1285     ourfile=owl_strdup(filename);
    1286   }
    1287  
    1288   f=fopen(ourfile, "r");
     1280  if (filename == NULL)
     1281    ourfile = owl_sprintf("%s/.anyone", owl_global_get_homedir(&g));
     1282  else
     1283    ourfile = owl_strdup(filename);
     1284
     1285  f = fopen(ourfile, "r");
    12891286  if (!f) {
    12901287    owl_function_error("Error opening file %s: %s", ourfile, strerror(errno) ? strerror(errno) : "");
    12911288    owl_free(ourfile);
    1292     return(-1);
    1293   }
    1294 
    1295   while (fgets(buff, LINE, f)!=NULL) {
     1289    return -1;
     1290  }
     1291  owl_free(ourfile);
     1292
     1293  while (owl_getline_chomp(&s, f)) {
    12961294    /* ignore comments, blank lines etc. */
    1297     if (buff[0]=='#') continue;
    1298     if (buff[0]=='\n') continue;
    1299     if (buff[0]=='\0') continue;
    1300    
    1301     /* strip the \n */
    1302     buff[strlen(buff)-1]='\0';
    1303    
    1304     /* ingore from # on */
    1305     tmp=strchr(buff, '#');
    1306     if (tmp) tmp[0]='\0';
    1307    
    1308     /* ingore from SPC */
    1309     tmp=strchr(buff, ' ');
    1310     if (tmp) tmp[0]='\0';
    1311    
    1312     /* stick on the local realm. */
    1313     if (!strchr(buff, '@')) {
    1314       strcat(buff, "@");
    1315       strcat(buff, ZGetRealm());
    1316     }
    1317     owl_list_append_element(in, owl_strdup(buff));
     1295    if (s[0] == '#' || s[0] == '\0')
     1296      continue;
     1297
     1298    /* ignore from # on */
     1299    tmp = strchr(s, '#');
     1300    if (tmp)
     1301      tmp[0] = '\0';
     1302
     1303    /* ignore from SPC */
     1304    tmp = strchr(s, ' ');
     1305    if (tmp)
     1306      tmp[0] = '\0';
     1307
     1308    owl_list_append_element(in, long_zuser(s));
    13181309  }
    13191310  fclose(f);
    1320   owl_free(ourfile);
    1321   return(0);
    1322 #else
    1323   return(-1);
     1311  return 0;
     1312#else
     1313  return -1;
    13241314#endif
    13251315}
Note: See TracChangeset for help on using the changeset viewer.