Permalink
Switch branches/tags
android-wear-o-preview-4 android-wear-o-preview-3 android-wear-n-preview-2 android-wear-n-preview-1 android-wear-8.0.0_r1 android-wear-7.1.1_r1 android-wear-5.1.1_r1 android-wear-5.1.0_r1 android-wear-5.0.0_r1 android-vts-8.0_r7 android-vts-8.0_r6 android-vts-8.0_r2 android-vts-8.0_r1 android-sdk-support_r11 android-sdk-adt_r20 android-sdk-adt_r16.0.1 android-sdk-4.4.2_r1.0.1 android-sdk-4.4.2_r1 android-sdk-4.0.3_r1 android-sdk-4.0.3-tools_r1 android-o-preview-4 android-o-preview-3 android-o-preview-2 android-o-preview-1 android-o-mr1-preview-2 android-o-mr1-preview-1 android-o-mr1-iot-preview-6 android-o-iot-preview-5 android-n-preview-5 android-n-preview-4 android-n-preview-3 android-n-preview-2 android-n-preview-1 android-n-mr2-preview-2 android-n-mr2-preview-1 android-n-mr1-preview-2 android-n-mr1-preview-1 android-n-iot-preview-4 android-n-iot-preview-2 android-m-preview android-m-preview-2 android-m-preview-1 android-l-preview_r2 android-cts-verifier-4.0.3_r1 android-cts-verifier-4.0_r1 android-cts-8.1_r2 android-cts-8.1_r1 android-cts-8.0_r6 android-cts-8.0_r5 android-cts-8.0_r4 android-cts-8.0_r3 android-cts-8.0_r2 android-cts-8.0_r1 android-cts-7.1_r14 android-cts-7.1_r13 android-cts-7.1_r12 android-cts-7.1_r11 android-cts-7.1_r10 android-cts-7.1_r9 android-cts-7.1_r8 android-cts-7.1_r7 android-cts-7.1_r6 android-cts-7.1_r5 android-cts-7.1_r4 android-cts-7.1_r3 android-cts-7.1_r2 android-cts-7.1_r1 android-cts-7.0_r18 android-cts-7.0_r17 android-cts-7.0_r16 android-cts-7.0_r15 android-cts-7.0_r14 android-cts-7.0_r13 android-cts-7.0_r12 android-cts-7.0_r11 android-cts-7.0_r10 android-cts-7.0_r9 android-cts-7.0_r8 android-cts-7.0_r7 android-cts-7.0_r6 android-cts-7.0_r5 android-cts-7.0_r4 android-cts-7.0_r3 android-cts-7.0_r2 android-cts-7.0_r1 android-cts-6.0_r26 android-cts-6.0_r25 android-cts-6.0_r24 android-cts-6.0_r23 android-cts-6.0_r22 android-cts-6.0_r21 android-cts-6.0_r20 android-cts-6.0_r19 android-cts-6.0_r18 android-cts-6.0_r17 android-cts-6.0_r16 android-cts-6.0_r15 android-cts-6.0_r14 android-cts-6.0_r13 android-cts-6.0_r12
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (75 sloc) 2.6 KB
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
static void usage(void)
{
fprintf(stderr, "touch: usage: touch [-alm] [-t time_t] <file>\n");
exit(1);
}
int touch_main(int argc, char *argv[])
{
int i, fd, aflag = 0, mflag = 0, debug = 0, flags = 0;
struct timespec specified_time, times[2];
char *file = 0;
specified_time.tv_nsec = UTIME_NOW;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
/* an option */
const char *arg = argv[i]+1;
while (arg[0]) {
switch (arg[0]) {
case 'a': aflag = 1; break;
case 'm': mflag = 1; break;
case 't':
if ((i+1) >= argc)
usage();
specified_time.tv_sec = atol(argv[++i]);
if (specified_time.tv_sec == 0) {
fprintf(stderr, "touch: invalid time_t\n");
exit(1);
}
specified_time.tv_nsec = 0;
break;
case 'l': flags |= AT_SYMLINK_NOFOLLOW; break;
case 'd': debug = 1; break;
default:
usage();
}
arg++;
}
} else {
/* not an option, and only accept one filename */
if (i+1 != argc)
usage();
file = argv[i];
}
}
if (! file) {
fprintf(stderr, "touch: no file specified\n");
exit(1);
}
if (access(file, F_OK))
if ((fd=creat(file, 0666)) != -1)
close(fd);
if ((mflag == 0) && (aflag == 0))
aflag = mflag = 1;
if (aflag)
times[0] = specified_time;
else
times[0].tv_nsec = UTIME_OMIT;
if (mflag)
times[1] = specified_time;
else
times[1].tv_nsec = UTIME_OMIT;
if (debug) {
fprintf(stderr, "file = %s\n", file);
fprintf(stderr, "times[0].tv_sec = %ld, times[0].tv_nsec = %ld\n", times[0].tv_sec, times[0].tv_nsec);
fprintf(stderr, "times[1].tv_sec = %ld, times[1].tv_nsec = %ld\n", times[1].tv_sec, times[1].tv_nsec);
fprintf(stderr, "flags = 0x%8.8x\n", flags);
}
return utimensat(AT_FDCWD, file, times, flags);
}