home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libcnews / mkinperm.c < prev    next >
C/C++ Source or Header  |  1994-11-27  |  2KB  |  76 lines

  1. /*
  2.  - mkinperm - move in.coming temporary to a unique permanent name
  3.  *
  4.  * Names are based on the current time in hopes of keeping input in order.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11. #include <errno.h>
  12. #include "fixerrno.h"
  13. #ifndef EEXIST
  14. #define    EEXIST    0
  15. #endif
  16. #include "libc.h"
  17. #include "news.h"
  18. #include "config.h"
  19. /* #include "mkinperm.h" */
  20.  
  21. #ifndef MAXTRIES
  22. #define    MAXTRIES    100    /* limit on attempts to make links */
  23. #endif
  24.  
  25. int mkinpdebug = 0;
  26. char *progname;
  27.  
  28. int                        /* true iff succeeded */
  29. mkinperm(tmpname, grade, class)
  30. char *tmpname;
  31. char *grade;        /* 3 chars = digit, period, NUL */
  32. char *class;        /* suffix for filename, default is plain text */
  33. {
  34.     register char *p;
  35.     register char *name;
  36.     register int ntries;
  37.     register time_t now;
  38.     register int uniq = '0';
  39.  
  40.     p = fullartfile("in.coming/");
  41.     name = nemalloc(strlen(p) + 20);    /* plenty for a number */
  42.     (void) strcpy(name, p);
  43.     p = name + strlen(name);
  44.  
  45.     now = time((time_t *)NULL);
  46.     for (ntries = 0; ; ntries++) {
  47.         (void) sprintf(p, "%s%ld%c%s", grade, now, uniq, class);
  48.         if (mkinpdebug)
  49.             (void) fprintf(stderr, "trying renaming to %s\n", name);
  50.         if (link(tmpname, name) >= 0)
  51.             break;        /* NOTE BREAK OUT */
  52.         if (errno != EEXIST)    /* something strange is wrong */
  53.             return NO;
  54.         errno = 0;
  55.         if (ntries > MAXTRIES)    /* sanity check */
  56.             return NO;
  57.  
  58.         if (++uniq == '9'+1)
  59.             uniq = 'a';
  60.         else if (uniq == 'z'+1)
  61.             uniq = 'A';
  62.         else if (uniq == 'Z'+1) {
  63.             (void) sleep(2);
  64.             now = time((time_t *)NULL);
  65.             uniq = '0';
  66.         }
  67.         if (mkinpdebug)
  68.             (void) fprintf(stderr, "link to %s failed\n", name);
  69.     }
  70.  
  71.     if (mkinpdebug)
  72.         (void) fprintf(stderr, "succeeded\n");
  73.     (void) unlink(tmpname);
  74.     return YES;
  75. }
  76.