Changeset ed88113


Ignore:
Timestamp:
Oct 16, 2009, 10:25:39 AM (14 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:
49d67b1
Parents:
a006a662 (diff), 946058b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:
Merge branch 'rewrite_deleteline'
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rf119757 r946058b  
    552552}
    553553
    554 /* Delete the line matching "line" from the named file.  If no such
    555  * line is found the file is left intact.  If backup==1 then create a
    556  * backupfile containing the original contents.  This is an
    557  * inefficient impelementation which reads the entire file into
    558  * memory.
     554/* Delete all lines matching "line" from the named file.  If no such
     555 * line is found the file is left intact.  If backup==1 then leave a
     556 * backup file containing the original contents.  The match is
     557 * case-insensitive.
    559558 *
    560559 * Returns the number of lines removed
     
    562561int owl_util_file_deleteline(const char *filename, const char *line, int backup)
    563562{
    564   char buff[LINE], *text;
    565   char *backupfilename=NULL;
    566   FILE *file, *backupfile=NULL;
    567   int size, newline;
     563  char *backupfile, *newfile, *buf = NULL;
     564  FILE *old, *new;
     565  struct stat st;
    568566  int numremoved = 0;
    569567
    570   /* open the file for reading */
    571   file=fopen(filename, "r");
    572   if (!file) {
    573     owl_function_error("Error opening file %s", filename);
     568  if ((old = fopen(filename, "r")) == NULL) {
     569    owl_function_error("Cannot open %s (for reading): %s",
     570                       filename, strerror(errno));
    574571    return 0;
    575572  }
    576573
    577   /* open the backup file for writing */
     574  if (fstat(fileno(old), &st) != 0) {
     575    owl_function_error("Cannot stat %s: %s", filename, strerror(errno));
     576    return 0;
     577  }
     578
     579  newfile = owl_sprintf("%s.new", filename);
     580  if ((new = fopen(newfile, "w")) == NULL) {
     581    owl_function_error("Cannot open %s (for writing): %s",
     582                       filename, strerror(errno));
     583    free(newfile);
     584    fclose(old);
     585    return 0;
     586  }
     587
     588  if (fchmod(fileno(new), st.st_mode & 0777) != 0) {
     589    owl_function_error("Cannot set permissions on %s: %s",
     590                       filename, strerror(errno));
     591    unlink(newfile);
     592    fclose(new);
     593    free(newfile);
     594    fclose(old);
     595    return 0;
     596  }
     597
     598  while (owl_getline_chomp(&buf, old))
     599    if (strcasecmp(buf, line) != 0)
     600      fprintf(new, "%s\n", buf);
     601    else
     602      numremoved++;
     603
     604  fclose(new);
     605  fclose(old);
     606
    578607  if (backup) {
    579     backupfilename=owl_sprintf("%s.backup", filename);
    580     backupfile=fopen(backupfilename, "w");
    581     if (!backupfile) {
    582       owl_function_error("Error opening file %s for writing", backupfilename);
    583       owl_free(backupfilename);
    584       fclose(file);
     608    backupfile = owl_sprintf("%s.backup", filename);
     609    unlink(backupfile);
     610    if (link(filename, backupfile) != 0) {
     611      owl_function_error("Cannot link %s: %s", backupfile, strerror(errno));
     612      owl_free(backupfile);
     613      unlink(newfile);
     614      owl_free(newfile);
    585615      return 0;
    586616    }
    587   }
    588 
    589   /* we'll read the entire file into memory, minus the line we don't want and
    590    * and at the same time create the backup file if necessary
    591    */
    592   text=owl_malloc(LINE);
    593   strcpy(text, "");
    594   size=LINE;
    595   while (fgets(buff, LINE, file)!=NULL) {
    596     /* strip the newline */
    597     newline=0;
    598     if (buff[0] != '\0' && buff[strlen(buff) - 1] == '\n') {
    599       buff[strlen(buff)-1]='\0';
    600       newline=1;
    601     }
    602    
    603     /* if we don't match the line, add to saved text in memory */
    604     if (strcasecmp(buff, line)) {
    605       size+=LINE;
    606       text=owl_realloc(text, size);
    607       strcat(text, buff);
    608       if (newline) strcat(text, "\n");
    609     } else {
    610       numremoved++;
    611     }
    612 
    613     /* write to backupfile if necessary */
    614     if (backup) {
    615       fputs(buff, backupfile);
    616       if (newline) fputs("\n", backupfile);
    617     }
    618   }
    619   if (backup) fclose(backupfile);
    620   fclose(file);
    621 
    622   /* now rewrite the original file from memory */
    623   file=fopen(filename, "w");
    624   if (!file) {
    625     owl_function_error("WARNING: Error opening %s for writing.  Use %s to restore.", filename, backupfilename);
    626     owl_function_beep();
    627   } else {
    628     fputs(text, file);
    629     fclose(file);
    630   }
    631 
    632   if (backup)
    633     owl_free(backupfilename);
    634   owl_free(text);
     617    owl_free(backupfile);
     618  }
     619
     620  if (rename(newfile, filename) != 0) {
     621    owl_function_error("Cannot move %s to %s: %s",
     622                       newfile, filename, strerror(errno));
     623    numremoved = 0;
     624  }
     625
     626  unlink(newfile);
     627  owl_free(newfile);
    635628
    636629  return numremoved;
     
    781774  return g_string_free(out, 0);
    782775}
     776
     777/* innards of owl_getline{,_chomp} below */
     778static int owl_getline_internal(char **s, FILE *fp, int newline)
     779{
     780  int size = 0;
     781  int target = 0;
     782  int count = 0;
     783  int c;
     784
     785  while (1) {
     786    c = getc(fp);
     787    if ((target + 1) > size) {
     788      size += BUFSIZ;
     789      *s = owl_realloc(*s, size);
     790    }
     791    if (c == EOF)
     792      break;
     793    count++;
     794    if (c != '\n' || newline)
     795        (*s)[target++] = c;
     796    if (c == '\n')
     797      break;
     798  }
     799  (*s)[target] = 0;
     800
     801  return count;
     802}
     803
     804/* Read a line from fp, allocating memory to hold it, returning the number of
     805 * byte read.  *s should either be NULL or a pointer to memory allocated with
     806 * owl_malloc; it will be owl_realloc'd as appropriate.  The caller must
     807 * eventually free it.  (This is roughly the interface of getline in the gnu
     808 * libc).
     809 *
     810 * The final newline will be included if it's there.
     811 */
     812int owl_getline(char **s, FILE *fp)
     813{
     814  return owl_getline_internal(s, fp, 1);
     815}
     816
     817/* As above, but omitting the final newline */
     818int owl_getline_chomp(char **s, FILE *fp)
     819{
     820  return owl_getline_internal(s, fp, 0);
     821}
     822
     823/* Read the rest of the input available in fp into a string. */
     824char *owl_slurp(FILE *fp)
     825{
     826  char *buf = NULL;
     827  char *p;
     828  int size = 0;
     829  int count;
     830
     831  while (1) {
     832    buf = owl_realloc(buf, size + BUFSIZ);
     833    p = &buf[size];
     834    size += BUFSIZ;
     835
     836    if ((count = fread(p, 1, BUFSIZ, fp)) < BUFSIZ)
     837      break;
     838  }
     839  p[count] = 0;
     840
     841  return buf;
     842}
  • commands.c

    r5ade618 ra006a662  
    8686              "creates a binding in a keymap",
    8787              "bindkey <keymap> <keyseq> command <command>",
     88              "(Note: There is a literal word "command" between <keyseq>\n"
     89              " and <command>.)\n"
    8890              "Binds a key sequence to a command within a keymap.\n"
    8991              "Use 'show keymaps' to see the existing keymaps.\n"
    90               "Key sequences may be things like M-C-t or NPAGE.\n"),
     92              "Key sequences may be things like M-C-t or NPAGE.\n\n"
     93              "Ex.: bindkey recv C-b command zwrite -c barnowl"),
    9194
    9295  OWLCMD_ARGS("zwrite", owl_command_zwrite, OWL_CTX_INTERACTIVE,
Note: See TracChangeset for help on using the changeset viewer.