コード例 #1
0
ファイル: jdate.c プロジェクト: AhmadHamzeei/jcal
int main(int argc, char** argv)
{
    int opt;
    int i;
    int err;
    int option_index;

    char buf[MAX_BUF_SIZE];
    char date_format[MAX_BUF_SIZE];
    char date_string[MAX_BUF_SIZE];

    struct jtm j;
    struct tm g;

    struct jdate_action action = {0};

    /* Long options, see 'jdate.h' for complete list. */
    struct option long_options[] = {
        {DATE_OPT, 1, 0, 'd'},
        {REF_OPT, 1, 0, 'r'},
        {ACC_OPT, 1, 0, 'a'},
        {RFC2822_OPT, 0, 0, 'R'},
        {UTC_OPT, 0, 0, 'u'},
        {JALALI_OPT, 1, 0, 'j'},
        {GREGORIAN_OPT, 1, 0, 'g'},
        {UNIVERSAL_OPT, 0, 0, 'u'},
        {HELP_OPT, 0, 0, 'h'},
        {VERSION_OPT, 0, 0, 'V'},
        {0, 0, 0, 0}
    };


    action.normal = 1;

    time_t t;
    time(&t);
    jlocaltime_r(&t, &j);

    while ((opt = getopt_long(argc, argv, JDATE_VALID_ARGS,
                              long_options, &option_index)) != -1) {
        switch (opt) {
            /* last access time. */
        case 'a':
            action.access = 1;
            action.access_ptr = optarg;
            break;

            /* last modification time. */
        case 'r':
            action.reference = 1;
            action.reference_ptr = optarg;
            break;

            /* display time described by FORMAT and DATE_STRING, not `now'. */
        case 'd':
            action.date = 1;
            action.date_ptr = optarg;
            break;

            /* convert a jalali date to gregorian. */
        case 'g':
            action.gregorian = 1;
            action.gregorian_ptr = optarg;
            break;

            /* convert a gregorian date to jalali. */
        case 'j':
            action.jalali = 1;
            action.jalali_ptr = optarg;
            break;

            /*
             * output date and time in RFC 2822 format.
             * %h, %m %b %Y %H:%M:%S %z
             */
        case 'R':
            action.normal = 0;
            action.format = 0;
            action.rfc2822 = 1;
            break;

            /* print Coordinated Universal Time */
        case 'u':
            action.utc = 1;
            break;

            /* help */
        case 'h':
            action.help = 1;
            action.normal = 0;
            action.format = 0;
            action.rfc2822 = 0;
            break;

            /* version */
        case 'V':
            action.version = 1;
            action.help = 0;
            action.normal = 0;
            action.format = 0;
            action.rfc2822 = 0;
            break;

        default:
            fprintf(stderr, "jdate: usage [OPTION]... [+FORMAT]\n");
            exit(EXIT_FAILURE);
        }
    }

    /*
     * Format string handler. INPUT_FORMAT and DATE_STRING
     * are separated using a semicolon. ';'
     * e.g. "%Y/%m/%d %H:%M:%S;1390/03/06 18:35:41"
     */
    for (i=1; i<argc; i++) {
        if (argv[i][0] == '+') {
            action.format = 1;
            action.format_ptr = &argv[i][1];
        }
    }

    /*
     *@action_handlers
     */
    if (action.jalali) {
        if (!strptime(action.jalali_ptr, "%Y/%m/%d", &g)) {
            fprintf(stderr, "Specify gregorian date in the following format\n");
            fprintf(stderr, "%%Y/%%m/%%d e.g. 2011/06/15\n");
            exit(EXIT_FAILURE);
        }

        g.tm_hour = 0;
        g.tm_min = 0;
        g.tm_sec = 0;

        t = mktime(&g);
    } else if (action.gregorian) {
        if (!jstrptime(action.gregorian_ptr, "%Y/%m/%d", &j)) {
            fprintf(stderr, "Specify jalali date in the following format\n");
            fprintf(stderr, "%%Y/%%m/%%d e.g. 1390/03/25\n");
            exit(EXIT_FAILURE);
        }

        jalali_update(&j);
        j.tm_hour = 0;
        j.tm_min = 0;
        j.tm_sec = 0;

        t = jmktime(&j);
    }

    if (action.date) {

        char* ptr;

        ptr = strchr(action.date_ptr, ';');

        if (!ptr) {
            fprintf(stderr, "Malformed date string.");
            fprintf(stderr, " Use ';' to specify format and date string\n");
            exit(EXIT_FAILURE);
        }

        sscanf(action.date_ptr, "%[^;];%s", date_format, date_string);

        jstrptime(date_string, date_format, &j);
        jalali_update(&j);
        t = jmktime(&j);
    }

    if (action.access) {
        err = mod_time(action.access_ptr, &t, 1);

        if (err != 0) {
            fprintf(stderr, "jdate: %s: No such file or directory\n",
                    action.access_ptr);
            exit(EXIT_FAILURE);
        }
    }

    if (action.reference) {
        err = mod_time(action.reference_ptr, &t, 0);

        if (err != 0) {
            fprintf(stderr, "jdate: %s: No such file or directory\n",
                    action.reference_ptr);
            exit(EXIT_FAILURE);
        }
    }

    if (action.rfc2822) {
        if (!action.gregorian) {
            action.utc ? jgmtime_r(&t, &j) : jlocaltime_r(&t, &j);
            jstrftime(buf, MAX_BUF_SIZE, "%h, %d %b %Y %H:%M:%S %z", &j);
        } else {
            action.utc ? gmtime_r(&t, &g) : localtime_r(&t, &g);
            strftime(buf, MAX_BUF_SIZE, "%a, %d %b %Y %H:%M:%S %z", &g);
        }

        printf("%s\n", buf);
        exit(EXIT_SUCCESS);
    }

    if (action.format) {
        if (!action.gregorian) {
            action.utc ? jgmtime_r(&t, &j) : jlocaltime_r(&t, &j);
            jstrftime(buf, MAX_BUF_SIZE, action.format_ptr, &j);
        } else {
            action.utc ? gmtime_r(&t, &g) : localtime_r(&t, &g);
            strftime(buf, MAX_BUF_SIZE, action.format_ptr, &g);
        }

        printf("%s\n", buf);
        exit(EXIT_SUCCESS);
    }

    if (action.normal) {
        if (!action.gregorian) {
            action.utc ? jgmtime_r(&t, &j) : jlocaltime_r(&t, &j);
            jstrftime(buf, MAX_BUF_SIZE, "%h %b %d %H:%M:%S %Z %Y", &j);
        } else {
            action.utc ? gmtime_r(&t, &g) : localtime_r(&t, &g);
            strftime(buf, MAX_BUF_SIZE, "%a %b %d %H:%M:%S %Z %Y", &g);
        }

        printf("%s\n", buf);
        exit(EXIT_SUCCESS);
    }

    if (action.help) {
        printf("%s\n", HELP_STR);
        exit(EXIT_SUCCESS);
    }

    if (action.version) {
        printf("jdate %s (libjalali-%s)\n", JDATE_VERSION, LIBJALALI_VERSION);
        printf("Written by Ashkan Ghassemi.\n");
        exit(EXIT_SUCCESS);
    }

    return 0;
}
コード例 #2
0
ファイル: ff.c プロジェクト: AlainODea/illumos-gate
int
main(int argc, char *argv[])
{
	long n;
	int	opt;
	char	*suboptions,	*value;
	char *p;
	int first = 0;

	Today = time((time_t *)0);
	while ((opt = getopt(argc, argv, "Ia:c:i:lm:n:o:p:su")) != EOF) {
		switch (opt) {

		case 'a':
			Aflg++;
			Adelay = atoi(optarg);
			Asign = optarg[0];
			break;

		case 'I':
			Iflg++;
			break;

		case 'c':
			Cflg++;
			Cdelay = atoi(optarg);
			Csign = optarg[0];
			break;

		case 'l':
			Lflg++;
			Lname = tmpnam((char *)0);
			if ((Lfile = fopen(Lname, "w+")) == NULL) {
				perror("open");
				(void) fprintf(stderr,
				"ff: unable to open temp file, -l ignored\n");
				Lflg = 0;
			}
			break;

		case 'm':
			Mflg++;
			Mdelay = atoi(optarg);
			Msign = optarg[0];
			break;

		case 'n':
			Nflg++;
			Nage = mod_time(optarg);
			break;

		case 'o':
			/*
			 * ufs specific options.
			 */
			suboptions = optarg;

			if (*suboptions == '\0')
				usage();
			while (*suboptions != '\0') {
				switch ((getsubopt(&suboptions,
							subopts, &value))) {

				case A_FLAG:
					aflg++;
					break;

				case M_FLAG:
					mflg++;
					break;

				case S_FLAG:
					sflg++;
					break;

				default:
					usage();
				}
			}
			break;

		case 'i':
			while ((p = (char *)strtok(((first++ == 0) ?
			optarg: ((char *)0)), ", ")) != NULL) {
				if ((n = atoi(p)) == 0)
					break;
				ilist[iflg].ino = n;
				nxfile = iflg;
				iflg++;
			}
			break;

		case 'p':
			prefix = optarg;
			pflg++;
			break;

		case 's':
			Sflg++;
			break;

		case 'u':
			uflg++;
			break;

		case '?':
			usage();
		}
	}
	argc -= optind;
	argv = &argv[optind];
	while (argc--) {
		check(*argv);
		argv++;
	}
	if (Lflg) {
		out_multilinks();
	}
	if (nerror)
		return (32);
	return (0);
}