/* Parse arguments. */ static void parseargs(BuildCtx *ctx, char **argv) { const char *a; int i; ctx->mode = (BuildMode)-1; ctx->outname = "-"; for (i = 1; (a = argv[i]) != NULL; i++) { if (a[0] != '-') break; switch (a[1]) { case '-': if (a[2]) goto err; i++; goto ok; case '\0': goto ok; case 'm': i++; if (a[2] || argv[i] == NULL) goto err; ctx->mode = parsemode(argv[i]); break; case 'o': i++; if (a[2] || argv[i] == NULL) goto err; ctx->outname = argv[i]; break; default: err: usage(); break; } } ok: ctx->args = argv+i; if (ctx->mode == (BuildMode)-1) goto err; }
int main(int argc,char *argv[]) { int i; int p=0,v=0; int mode=0777-umask(0); mode_t and,or; if (argc<2) usage(); for (i=1; i<argc; i++) { if (!argv[i]) continue; if (argv[i][0]=='-') { int j,len=strlen(argv[i]); for (j=1; j<len; j++) { switch (argv[i][j]) { case '-': if (!strcmp(argv[i],"--parent")) p=1; else if (!strcmp(argv[i],"--verbose")) v=1; else if (argv[i][2]) usage(); j=len; break; case 'p': p=1; break; case 'v': v=1; break; case 'm': parsemode(argv[i+1],&and,&or); mode=(mode&and)|or; argv[i+1]=0; break; default: usage(); } } } else { if (p) { minusp(argv[i],mode,v); } else domkdir(argv[i],mode,v,0,argv[i]); } } return res; }
int main(int argc, char *argv[]) { mode_t mode = 0, mask; int mflag = 0, ret = 0; ARGBEGIN { case 'm': mflag = 1; mask = getumask(); mode = parsemode(EARGF(usage()), mode, mask); break; default: usage(); } ARGEND; if (!argc) usage(); for (; *argv; argc--, argv++) { if (mkfifo(*argv, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) { weprintf("mkfifo %s:", *argv); ret = 1; } else if (mflag) { if (chmod(*argv, mode) < 0) { weprintf("chmod %s:", *argv); ret = 1; } } } return ret; }
FILE* fdopen(int fd, const char *mode) { FILE *f; for (f=tab; f<tab+MAX_FILE && f->fd>=0; f++); if (f==tab+MAX_FILE) return 0; f->mod=parsemode(mode); if (f->mod<0) return 0; f->fd=fd; f->eof=0; f->err=0; return f; }
FILE* fopen(const char *name, const char *mode) { int fd,oflag; FILE *f; oflag=parsemode(mode); if (oflag<0) return 0; fd=open(name,oflag); if (fd<0) return 0; if (f=fdopen(fd,mode)) return f; close(fd); return 0; }
FILE *fopen(const char *filename,const char *mode) { uint flags = parsemode(mode); if((flags & O_ACCMODE) == 0) return NULL; /* open */ int fd = open(filename,flags,FILE_DEF_MODE); if(fd < 0) return NULL; /* create file */ FILE *f = NULL; if(!(f = bcreate(fd,flags,NULL,IN_BUFFER_SIZE,OUT_BUFFER_SIZE,false))) { close(fd); free(f); return NULL; } benqueue(f); return f; }
int main(int argc, char *argv[]) { int dflag = 0; char *gflag = 0; char *oflag = 0; char *mflag = 0; char *tflag = 0; struct group *gr; struct passwd *pw; struct stat st; char *p; ARGBEGIN { case 'd': dflag = 1; break; case 'D': Dflag = 1; break; case 's': sflag = 1; break; case 'g': gflag = EARGF(usage()); break; case 'o': oflag = EARGF(usage()); break; case 'm': mflag = EARGF(usage()); break; case 't': tflag = EARGF(usage()); break; default: usage(); } ARGEND if (argc < 1 + (!tflag & !dflag) || dflag & (Dflag | sflag | !!tflag)) usage(); if (gflag) { errno = 0; gr = getgrnam(gflag); if (gr) { group = gr->gr_gid; } else { if (errno) eprintf("getgrnam %s:", gflag); group = estrtonum(gflag, 0, UINT_MAX); } } else { group = getgid(); } if (oflag) { errno = 0; pw = getpwnam(oflag); if (pw) { owner = pw->pw_uid; } else { if (errno) eprintf("getpwnam %s:", oflag); owner = estrtonum(oflag, 0, UINT_MAX); } } else { owner = getuid(); } if (mflag) { mode = parsemode(mflag, mode, 0); if (mode < 0) return 1; } if (tflag) { memmove(argv - 1, argv, argc); argv[argc++] = tflag; } if (tflag || argc > 2) { if (stat(argv[argc - 1], &st) < 0) { if ((errno == ENOENT) && Dflag) { make_dirs(argv[argc - 1], 1); } else { eprintf("stat %s:", argv[argc - 1]); } } else if (!S_ISDIR(st.st_mode)) { eprintf("%s: not a directory\n", argv[argc - 1]); } } if (dflag) { for (; *argv; argc--, argv++) make_dirs(*argv, 0); } else { if (stat(argv[argc - 1], &st) < 0) { if (errno != ENOENT) eprintf("stat %s:", argv[argc - 1]); if (tflag || Dflag || argc > 2) { if ((p = strrchr(argv[argc - 1], '/')) != NULL) { *p = '\0'; make_dirs(argv[argc - 1], 1); *p = '/'; } } } enmasse(argc, argv, install); } return 0; }
int main (int argc, char **argv) { char *name=0, *dir=0, *pfx=0, *sfx=0, *filename=0; mode_t mode = 0600; int fd, optc; struct option long_options[] = { {"prefix", required_argument, 0, 'p'}, {"suffix", required_argument, 0, 's'}, {"directory", required_argument, 0, 'd'}, {"mode", required_argument, 0, 'm'}, {"name", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; progname = argv[0]; while ((optc = getopt_long (argc, argv, "p:s:d:m:n:", long_options, 0)) != EOF) { switch (optc) { case 0: break; case 'p': pfx = optarg; break; case 's': sfx = optarg; break; case 'd': dir = optarg; break; case 'm': if (parsemode(optarg, &mode)) { fprintf(stderr, "Invalid mode `%s'. Mode must be octal.\n", optarg); usage(1); } break; case 'n': /* strdup because it is freed later on */ if((name = strdup(optarg)) == NULL) syserror("strdup"); break; case 'h': usage(0); case 'v': puts("tempfile " PACKAGE_VERSION); exit(0); default: usage(1); } } if (name) { if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) syserror("open"); filename = name; } else { for (;;) { if (!(name = tempnam(dir, pfx))) syserror("tempnam"); if(sfx) { char *namesfx; if (!(namesfx = (char *)malloc(strlen(name) + strlen(sfx) + 1))) syserror("malloc"); strcpy(namesfx, name); strcat(namesfx, sfx); filename = namesfx; } else filename = name; if ((fd = open(filename, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) { if (errno == EEXIST) { if(name != filename) free(name); free(filename); continue; } syserror("open"); } break; } } if (close(fd)) syserror("close"); puts(filename); if(name != filename) free(name); free(filename); exit(0); }
int main (int argc, char **argv) { char *name=0, *dir=0, *pfx="file", *sfx=0, *filename=0; mode_t mode = 0600; int fd, optc; struct option long_options[] = { {"prefix", required_argument, 0, 'p'}, {"suffix", required_argument, 0, 's'}, {"directory", required_argument, 0, 'd'}, {"mode", required_argument, 0, 'm'}, {"name", required_argument, 0, 'n'}, {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'}, {0, 0, 0, 0} }; progname = argv[0]; while ((optc = getopt_long (argc, argv, "p:s:d:m:n:", long_options, 0)) != EOF) { switch (optc) { case 0: break; case 'p': pfx = optarg; break; case 's': sfx = optarg; break; case 'd': dir = optarg; break; case 'm': if (parsemode(optarg, &mode)) { fprintf(stderr, "Invalid mode `%s'. Mode must be octal.\n", optarg); usage(1); } break; case 'n': /* strdup because it is freed later on */ if((name = strdup(optarg)) == NULL) syserror("strdup"); break; case 'h': usage(0); case 'v': puts("tempfile " PACKAGE_VERSION); exit(0); default: usage(1); } } if (name) { if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) syserror("open"); filename = name; } else { /* a) In case the environment variable TMPDIR exists and contains the name of an appropriate directory, that is used. b) Otherwise, if the --directory argument is specified and appropriate, it is used. c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropriate. d) Finally an implementation-defined directory (/tmp) may be used. */ char *tmpdirs[] = { getenv("TMPDIR"), dir, P_tmpdir, "/tmp" }; int i; for (i = 0; i < sizeof(tmpdirs) / sizeof(char *); i++) { char *tmpdir = tmpdirs[i]; if (!tmpdir) continue; size_t len = strlen(tmpdir) + 1 + /* / */ (pfx ? strlen(pfx) : 0) + 6 + /* XXXXXX */ (sfx ? strlen(sfx) : 0) + 1; /* NUL */ if (!(filename = malloc(len))) syserror("malloc"); snprintf(filename, len, "%s/%sXXXXXX%s", tmpdir, pfx ? pfx : "", sfx ? sfx : ""); if ((fd = mkstemps(filename, sfx ? strlen(sfx) : 0)) < 0) { if (errno == EEXIST) { free(filename); continue; } syserror("mkstemps"); } if (fchmod(fd, mode) < 0) syserror("fchmod"); break; } } if (close(fd)) syserror("close"); puts(filename); if(name != filename) free(name); free(filename); exit(0); }