Changeset bde7714 for zephyr.c


Ignore:
Timestamp:
Sep 22, 2002, 3:20:00 PM (22 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
5a6e6b9
Parents:
f9c43ae
Message:
Started updating the intro document
Small changes to help / about
The 'subscribe' and 'unsubscribe' commands (and their aliases)
  update .zephyr.subs by default.  If either is given the '-t'
  (for "temporary") option the .zephyr.subs will not be updated
File:
1 edited

Legend:

Unmodified
Added
Removed
  • zephyr.c

    r56330ff rbde7714  
    408408  }
    409409}
     410
     411void owl_zephyr_addsub(char *filename, char *class, char *inst, char *recip) {
     412  char *line, subsfile[LINE], buff[LINE];
     413  FILE *file;
     414
     415  line=owl_zephyr_makesubline(class, inst, recip);
     416
     417  if (filename==NULL) {
     418    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
     419  } else {
     420    strcpy(subsfile, filename);
     421  }
     422
     423  /* first check if it exists already */
     424  file=fopen(subsfile, "r");
     425  if (!file) {
     426    owl_function_makemsg("Error opening file %s", subsfile);
     427    owl_free(line);
     428    return;
     429  }
     430  while (fgets(buff, LINE, file)!=NULL) {
     431    if (!strcasecmp(buff, line)) {
     432      owl_function_makemsg("Subscription already present in %s", subsfile);
     433      owl_free(line);
     434      return;
     435    }
     436  }
     437
     438  /* if we get here then we didn't find it */
     439  fclose(file);
     440  file=fopen(subsfile, "a");
     441  if (!file) {
     442    owl_function_makemsg("Error opening file %s for writing", subsfile);
     443    owl_free(line);
     444    return;
     445  }
     446  fputs(line, file);
     447  fclose(file);
     448  owl_function_makemsg("Subscription added");
     449 
     450  owl_free(line);
     451}
     452
     453void owl_zephyr_delsub(char *filename, char *class, char *inst, char *recip) {
     454  char *line, subsfile[LINE], buff[LINE], *text;
     455  char backupfilename[LINE];
     456  FILE *file, *backupfile;
     457  int size;
     458 
     459  line=owl_zephyr_makesubline(class, inst, recip);
     460
     461  /* open the subsfile for reading */
     462  if (filename==NULL) {
     463    sprintf(subsfile, "%s/%s", owl_global_get_homedir(&g), ".zephyr.subs");
     464  } else {
     465    strcpy(subsfile, filename);
     466  }
     467  file=fopen(subsfile, "r");
     468  if (!file) {
     469    owl_function_makemsg("Error opening file %s", subsfile);
     470    owl_free(line);
     471    return;
     472  }
     473
     474  /* open the backup file for writing */
     475  sprintf(backupfilename, "%s.backup", subsfile);
     476  backupfile=fopen(backupfilename, "w");
     477  if (!backupfile) {
     478    owl_function_makemsg("Error opening file %s for writing", backupfilename);
     479    owl_free(line);
     480    return;
     481  }
     482
     483  /* we'll read the entire file into memory, minus the line we don't want and
     484   * and at the same time create a backup file */
     485  text=malloc(LINE);
     486  size=LINE;
     487  while (fgets(buff, LINE, file)!=NULL) {
     488    /* if we don't match the line, add to text */
     489    if (strcasecmp(buff, line)) {
     490      size+=LINE;
     491      text=realloc(text, size);
     492      strcat(text, buff);
     493    }
     494
     495    /* write to backupfile */
     496    fputs(buff, backupfile);
     497  }
     498  fclose(backupfile);
     499  fclose(file);
     500
     501  /* now open the original subs file for writing and write out the
     502   * subs */
     503  file=fopen(subsfile, "w");
     504  if (!file) {
     505    owl_function_makemsg("WARNING: Error opening %s to rewrite subscriptions.  Use %s to restore", subsfile, backupfilename);
     506    owl_function_beep();
     507    owl_free(line);
     508    return;
     509  }
     510
     511  fputs(text, file);
     512  fclose(file);
     513  owl_free(line);
     514
     515  owl_function_makemsg("Subscription removed");
     516}
     517
     518char *owl_zephyr_makesubline(char *class, char *inst, char *recip) {
     519  /* caller must free the return */
     520  char *out;
     521
     522  out=owl_malloc(strlen(class)+strlen(inst)+strlen(recip)+30);
     523  sprintf(out, "%s,%s,%s\n", class, inst, !strcmp(recip, "") ? "*" : recip);
     524  return(out);
     525}
Note: See TracChangeset for help on using the changeset viewer.