/*
Prints a listing of files in the 'at' daemon's job directory,
ignoring anything with a leading dot.
Takes no arguments or options.
Each line shows the file's uid and name.  Example listing:

1000 a0028f0194a510
1000 a0028e0194a506
*/

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>

#define ATJOB_DIR "/var/spool/cron/atjobs"
//------------------------------------------------------------------------------

void bail (const char *template, ...) {
   va_list ap;
   va_start(ap, template);
   vfprintf(stderr, template, ap);
   exit(EXIT_FAILURE);
}
//------------------------------------------------------------------------------

int main (void) {

   if (chdir(ATJOB_DIR) != 0) bail("Cannot chdir to '%s'.\n", ATJOB_DIR);

   DIR *spool;
   if (NULL == (spool = opendir("."))) bail("Cannot opendir '%s'.\n", ATJOB_DIR);

   struct dirent *dirent;
   struct stat filestat;
   while ((dirent = readdir(spool)) != NULL) {
      // Skip '.', '..' and '.anything'.
      if ('.' == dirent->d_name[0]) continue;
      if (stat(dirent->d_name, &filestat) != 0) {
         bail("Cannot stat job file '%s'.", dirent->d_name);
      }
      printf("%d %s\n", filestat.st_uid, dirent->d_name);
   }
   return EXIT_SUCCESS;
}
//------------------------------------------------------------------------------
