Changeset 38cf544c for util.c


Ignore:
Timestamp:
Jun 4, 2003, 12:04:54 AM (18 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
96f8e5b
Parents:
fd93b41
Message:
Added the 'startup' and 'unstartup' commands
The $HOME/.owl directory is created on startup if it does not exist
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    r7c8060d0 r38cf544c  
    648648}
    649649
     650/* Delete the line matching "line" from the named file.  If no such
     651 * line is found the file is left intact.  If backup==1 then create a
     652 * backupfile containing the original contents.  This is an
     653 * inefficient impelementation which reads the entire file into
     654 * memory.
     655 */
     656void owl_util_file_deleteline(char *filename, char *line, int backup)
     657{
     658  char buff[LINE], *text;
     659  char *backupfilename;
     660  FILE *file, *backupfile;
     661  int size, newline;
     662
     663  /* open the file for reading */
     664  file=fopen(filename, "r");
     665  if (!file) {
     666    owl_function_makemsg("Error opening file %s", filename);
     667    return;
     668  }
     669 
     670  /* open the backup file for writing */
     671  if (backup) {
     672    backupfilename=owl_sprintf("%s.backup", filename);
     673    backupfile=fopen(backupfilename, "w");
     674    owl_free(backupfilename);
     675    if (!backupfile) {
     676      owl_function_makemsg("Error opening file %s for writing", backupfilename);
     677      return;
     678    }
     679  }
     680
     681  /* we'll read the entire file into memory, minus the line we don't want and
     682   * and at the same time create the backup file if necessary
     683   */
     684  text=owl_malloc(LINE);
     685  strcpy(text, "");
     686  size=LINE;
     687  while (fgets(buff, LINE, file)!=NULL) {
     688    /* strip the newline */
     689    newline=0;
     690    if (buff[strlen(buff)-1]=='\n') {
     691      buff[strlen(buff)-1]='\0';
     692      newline=1;
     693    }
     694   
     695    /* if we don't match the line, add to saved text in memory */
     696    if (strcasecmp(buff, line)) {
     697      size+=LINE;
     698      text=owl_realloc(text, size);
     699      strcat(text, buff);
     700      if (newline) strcat(text, "\n");
     701    }
     702
     703    /* write to backupfile if necessary */
     704    if (backup) fputs(buff, backupfile);
     705  }
     706  fclose(backupfile);
     707  fclose(file);
     708
     709  /* now rewrite the original file from memory */
     710  file=fopen(filename, "w");
     711  if (!file) {
     712    owl_function_makemsg("WARNING: Error opening %s for writing.  Use %s to restore.", filename, backupfilename);
     713    owl_function_beep();
     714    owl_free(line);
     715    return;
     716  }
     717
     718  fputs(text, file);
     719  fclose(file);
     720}
     721
    650722/**************************************************************************/
    651723/************************* REGRESSION TESTS *******************************/
Note: See TracChangeset for help on using the changeset viewer.