getexpiration.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include "authcmdlib.h"
  5. /*
  6. * get the date in the format yyyymmdd
  7. */
  8. Tm
  9. getdate(char *d)
  10. {
  11. Tm date;
  12. int i;
  13. date.year = date.mon = date.mday = 0;
  14. date.hour = date.min = date.sec = 0;
  15. for(i = 0; i < 8; i++)
  16. if(!isdigit(d[i]))
  17. return date;
  18. date.year = (d[0]-'0')*1000 + (d[1]-'0')*100 + (d[2]-'0')*10 + d[3]-'0';
  19. date.year -= 1900;
  20. d += 4;
  21. date.mon = (d[0]-'0')*10 + d[1]-'0' - 1;
  22. d += 2;
  23. date.mday = (d[0]-'0')*10 + d[1]-'0';
  24. date.yday = 0;
  25. return date;
  26. }
  27. long
  28. getexpiration(char *db, char *u)
  29. {
  30. char buf[Maxpath];
  31. char prompt[128];
  32. char cdate[32];
  33. Tm date;
  34. ulong secs, now, fd;
  35. int n;
  36. /* read current expiration (if any) */
  37. snprint(buf, sizeof buf, "%s/%s/expire", db, u);
  38. fd = open(buf, OREAD);
  39. buf[0] = 0;
  40. if(fd >= 0){
  41. n = read(fd, buf, sizeof(buf)-1);
  42. if(n > 0)
  43. buf[n-1] = 0;
  44. close(fd);
  45. }
  46. if(buf[0]){
  47. if(strncmp(buf, "never", 5)){
  48. secs = atoi(buf);
  49. memmove(&date, localtime(secs), sizeof(date));
  50. sprint(buf, "%4.4d%2.2d%2.2d", date.year+1900, date.mon+1, date.mday);
  51. } else
  52. buf[5] = 0;
  53. } else
  54. strcpy(buf, "never");
  55. sprint(prompt, "Expiration date (YYYYMMDD or never)[return = %s]: ", buf);
  56. now = time(0);
  57. for(;;){
  58. readln(prompt, cdate, sizeof cdate, 0);
  59. if(*cdate == 0)
  60. return -1;
  61. if(strcmp(cdate, "never") == 0)
  62. return 0;
  63. date = getdate(cdate);
  64. secs = tm2sec(&date);
  65. if(secs > now && secs < now + 2*365*24*60*60)
  66. break;
  67. print("expiration time must fall between now and 2 years from now\n");
  68. }
  69. return secs;
  70. }