/* ** Return a pointer to the username of the current user in a statically ** allocated string. */ const char *GetUserName(void) { #ifdef VMS return cuserid(NULL); #else /* cuserid has apparently been dropped from the ansi C standard, and if strict ansi compliance is turned on (on Sun anyhow, maybe others), calls to cuserid fail to compile. Older versions of nedit try to use the getlogin call first, then if that fails, use getpwuid and getuid. This results in the user-name of the original terminal being used, which is not correct when the user uses the su command. Now, getpwuid only: */ const struct passwd *passwdEntry; static char *userName=NULL; if (userName) return userName; passwdEntry = getpwuid(getuid()); if (!passwdEntry) { /* This is really serious, but sometimes username service is misconfigured through no fault of the user. Be nice and let the user start nc anyway. */ perror("nedit: getpwuid() failed - reverting to $USER"); return getenv("USER"); } else { userName=malloc(strlen(passwdEntry->pw_name)+1); strcpy(userName, passwdEntry->pw_name); return userName; } #endif /* VMS */ }
void getCurrentUsername(char *buf, int buflen) { #ifdef __WIN32__ DWORD len = buflen; if (!GetUserName(buf, &len)) { fail("GetUserName"); strncpy(buf, "(unknown)", buflen); } #else // unix (SunOS only? we'll see..) #if 0 // old.. linux man page insists 'cuserid' is a bad fn to call char temp[L_cuserid]; cuserid(temp); // fail return? #endif // 0 char const *temp; struct passwd *pw = getpwuid(geteuid()); if (!pw) { fail("getpwuid(geteuid())"); temp = "(unknown)"; } else { temp = pw->pw_name; } strncpy(buf, temp, buflen); #endif }
void _ruserpass(const char *host, char **aname, char **apass) { if (*aname == 0 || *apass == 0) rnetrc(host, aname, apass); if (*aname == 0) { char myname[L_cuserid]; *aname = malloc(MAXANAME + 1); (void) cuserid(myname); (void) printf(dgettext(TEXT_DOMAIN, "Name (%s:%s): "), host, myname); (void) fflush(stdout); if (read(2, *aname, MAXANAME) <= 0) exit(1); aname[0][MAXANAME] = '\0'; if ((*aname)[0] == '\n') (void) strcpy(*aname, myname); else if (index(*aname, '\n')) *index(*aname, '\n') = 0; } if (*aname && *apass == 0) { (void) printf(dgettext(TEXT_DOMAIN, "Password (%s:%s): "), host, *aname); (void) fflush(stdout); *apass = getpass(""); } }
char* cuserid_wrapper(){ char* val = cuserid(NULL); if (val == NULL) { perror("cuserid"); exit(errno); } else { return val; } }
pwr_tStatus syi_UserName( char *user, int len) { char *p; p = cuserid(0); if ( !p) return 0; strncpy( user, p, len); return 1; }
int main (int argc, char **argv) { time_t now; FILE *ofp; char *dateval; char *username; char *userstr; char *datestr; int i; char propname[32]; char *propvalue; char timestr[64]; char *cp; if (argc < 4) { printf ("usage: mkversion ostype version outputfile\n"); exit (1); } ofp = fopen (argv[3], "w"); if (ofp == NULL) { printf ("Couldn't create %s\n", argv[3]); exit (1); } now = time (0); fprintf (ofp, "/*\n"); fprintf (ofp, " * G2 Version Stamp, %s", ctime (&now)); fprintf (ofp, " * Automatically generated, hand edits are pointless.\n"); fprintf (ofp, " */\n\n"); fprintf (ofp, "const char *version_string = \"G2 (%s) major version %s\";\n", argv[1], argv[2]); username = (char *) cuserid (0); strcpy(timestr, ctime(&now)); cp = timestr; while (*cp) { cp++; } if (*--cp == '\n') *cp = 0; fprintf (ofp, "const char *minor_v_string = \"Built by %s at %s\";\n", username, timestr); exit (0); }
int getlogin_r(char *buf, size_t bufsize) { if (bufsize < L_cuserid) { errno = ERANGE; return -1; }; cuserid(buf); return 0; };
char *myusername () { struct stat sbuf; char tmp[MAILTMPLEN]; if (!myUserName) { /* get user name if don't have it yet */ myUserName = cpystr (cuserid (NIL)); myHomeDir = cpystr ("SYS$LOGIN"); } return myUserName; }
/* * wrapper for cuserid() function */ char * getuserid (char *string, int size) { if (cuserid (string) == NULL) { return NULL; } else { string[size - 1] = '\0'; return string; } }
/** 获取用户名 */ inline const char * getUsername() { #if KBE_PLATFORM == PLATFORM_WIN32 DWORD dwSize = MAX_NAME; static char username[MAX_NAME]; memset(username, 0, MAX_NAME); ::GetUserNameA(username, &dwSize); return username; #else char * pUsername = cuserid( NULL ); return pUsername ? pUsername : ""; #endif }
/********************** * * if successful, returns NULL * if file locked, returns username of person locking the file * if other error, returns "LOCK ERROR: explanation" * *********************/ char *dolock(char *fname) { int fd, n; static char lname[MAXLOCK], locker[MAXNAME + 1]; int mask; struct stat sbuf; strcat(xstrcpy(lname, fname), ".lock~"); /* check that we are not being cheated, qname must point to */ /* a regular file - even this code leaves a small window of */ /* vulnerability but it is rather hard to exploit it */ #if defined(S_IFLNK) if (lstat(lname, &sbuf) == 0) #else if (stat(lname, &sbuf) == 0) #endif #if defined(S_ISREG) if (!S_ISREG(sbuf.st_mode)) #else if (!(((sbuf.st_mode) & 070000) == 0)) /* SysV R2 */ #endif return "LOCK ERROR: not a regular file"; mask = umask(0); fd = open(lname, O_RDWR | O_CREAT, 0666); umask(mask); if (fd < 0) { if (errno == EACCES) return NULL; #ifdef EROFS if (errno == EROFS) return NULL; #endif return "LOCK ERROR: cannot access lock file"; } if ((n = read(fd, locker, MAXNAME)) < 1) { lseek(fd, 0, SEEK_SET); /* xstrcpy(locker, getlogin()); */ cuserid(locker); strcat(locker + strlen(locker), "@"); gethostname(locker + strlen(locker), 64); write(fd, locker, strlen(locker)); close(fd); return NULL; } locker[n > MAXNAME ? MAXNAME : n] = 0; return locker; }
/* Set the program parameters from the command-line arguments */ void parameters(int argc, char **argv) { int submit = 0; /* = 1 if submission parameters should be used */ int seed = 0; /* Random seed */ char uid[L_cuserid + 2]; /*User name */ /* Read command-line arguments */ // if (argc != 3) { if ( argc == 1 && !strcmp(argv[1], "submit") ) { /* Use submission parameters */ submit = 1; N = 4; procs = 2; printf("\nSubmission run for \"%s\".\n", cuserid(uid)); /*uid = ID;*/ strcpy(uid,ID); srand(randm()); } else { if (argc == 3) { seed = atoi(argv[3]); srand(seed); printf("Random seed = %i\n", seed); } else { printf("Usage: %s <matrix_dimension> <num_procs> [random seed]\n", argv[0]); printf(" %s submit\n", argv[0]); exit(0); } } // } /* Interpret command-line args */ if (!submit) { N = atoi(argv[1]); if (N < 1 || N > MAXN) { printf("N = %i is out of range.\n", N); exit(0); } procs = atoi(argv[2]); if (procs < 1) { printf("Warning: Invalid number of processors = %i. Using 1.\n", procs); procs = 1; } } /* Print parameters */ printf("\nMatrix dimension N = %i.\n", N); printf("Number of processors = %i.\n", procs); }
/* ================ Sys_GetCurrentUser returns username for current profile ================ */ char *Sys_GetCurrentUser( void ) { #if defined(_WIN32) static string s_userName; dword size = sizeof( s_userName ); if( GetUserName( s_userName, &size )) return s_userName; return "Player"; #elif !defined(__ANDROID__) return cuserid( NULL ); #else return "Player"; #endif }
void logout_user() { pid_t pid; pid=fork(); if(pid<0) { printf("Error\n"); return 0; } else if(pid>0) { wait(NULL); } else { char *username; char root[5]="root"; username=(char *)malloc(20*sizeof(char)); cuserid(username); printf("%s\n", username); if(strcmp(username,root)==0) { char line[1024]="pkill -KILL -u "; char *argv[64]; char kill_user[20]; int i=0, j=0; printf("Enter username:\n"); scanf("%s", kill_user); i=15; for(j=0;kill_user[j]!='\0';j++) { line[i]=kill_user[j]; i++; } parse(line, argv); if (strcmp(argv[0], "exit") == 0) { exit(0); } execute(argv); } else { execlp("gnome-session-quit", "gnome-session-quit", (char *)NULL); } } }
void dbSetQueryDefaults(void) { char *ConFigFileName; char *defsid = getenv( "TWO_TASK" ); if( defsid ) strcpy(sid,defsid); /* First set some defaults to use if no config file is found */ MaxRowsInQuery = 100; ShowSQL = 0; cuserid(logname); strcpy(passwd,logname); strcpy(Phaselist,""); strcpy(Authlist,""); strcpy(ConnectString,logname); strcat(ConnectString,"/"); strcat(ConnectString,passwd); strcpy(CropFromPath, ""); strcpy(PrependToPath, ""); strcpy(CaseSensitivity, "ON"); strcpy( wfdiscTableName , "WFDISC" ) ; strcpy( wftagTableName , "WFTAG" ) ; strcpy( originTableName , "ORIGIN" ) ; strcpy( eventTableName , "EVENT" ) ; strcpy( arrivalTableName , "ARRIVAL" ) ; strcpy( assocTableName , "ASSOC" ) ; strcpy( siteTableName , "SITE" ) ; strcpy( sitechanTableName , "SITECHAN" ) ; strcpy( searchlinkTableName , "SEARCH_LINK" ) ; strcpy( instrumentTableName , "INSTRUMENT" ) ; strcpy( sensorTableName , "SENSOR" ) ; /* Now see if there is a configuration file specified in environment */ if( ( ConFigFileName = getenv("DBSELECT_CONFIG_FILE") ) ){ dbUpdateDefaultsFromFile(ConFigFileName); } /* If user did not specify schema name, try to make the schema name same as the user's name. */ if( !strlen( Schema ) && strlen( logname ) ) strcpy( Schema, logname ); }
int MRIwriteControlPoints(const MPoint *pointArray, int count, int useRealRAS, const char *fname) { FILE *fp; int i; int res; time_t time; if (Gdiag & DIAG_SHOW) fprintf(stderr, "Writing control points to %s...\n", fname) ; if (useRealRAS > 1 || useRealRAS < 0) ErrorReturn(ERROR_BADPARM, (ERROR_BADPARM, "MRIwriteControlPoints useRealRAS must" " be 0 (surfaceRAS) or 1 (scannerRAS) but %d\n", useRealRAS)) fp = fopen(fname, "w") ; if (!fp) ErrorReturn(ERROR_BADPARM, (ERROR_BADPARM, "MRIwriteControlPoints(%s): could not" " open file", fname)) ; for (i=0 ; i < count; ++i) { if ((res = fprintf(fp, "%f %f %f\n", pointArray[i].x, pointArray[i].y, pointArray[i].z))< 0) ErrorReturn(ERROR_BADPARM, (ERROR_BADPARM, "MRIwriteControlPoints(%s): could not" " write file", fname)) ; } // if res < 0, then error res=fprintf(fp, "info\n"); res=fprintf(fp, "numpoints %d\n", count); res=fprintf(fp, "useRealRAS %d\n", useRealRAS); res=fprintf(fp, "written by %s on %s\n", cuserid(0), asctime(localtime(&time))); res=fclose(fp); return (NO_ERROR); }
void v2_collect_history_info(void) { int status; int maxlen; char *value, *newline; time_t tim; #if CUSERID_AVAIL_OS #else struct passwd *pw; #endif if (strlen(history[TASK].value) != 0) /* Already collected */ return; maxlen = sizeof(history[TASK].value); /* max len of dest string */ maxlen = MIN(maxlen, MAX_HIST_NAME_SIZE+1); /* limit size for back compat */ status = v2_get_one_string_parm("_PROC", 0, &value); /* Get proc name */ if (status == SUCCESS) { strncpy(history[TASK].value, value, maxlen); history[TASK].value[maxlen-1] = '\0'; } else /* Not there, use default */ strcpy(history[TASK].value, TASK_KEY); v2_make_upper_case(history[TASK].value, history[TASK].value); maxlen = sizeof(history[USER].value); /* max len of dest string */ #if CUSERID_AVAIL_OS value = cuserid(NULL); /* Get user ID */ #else pw = getpwuid(getuid()); value = pw->pw_name; #endif strncpy(history[USER].value, value, maxlen); history[USER].value[maxlen-1] = '\0'; maxlen = sizeof(history[DAT_TIM].value); /* max len of dest string */ tim = time(0); /* Get date and time */ value = ctime(&tim); strncpy(history[DAT_TIM].value, value, maxlen); history[DAT_TIM].value[maxlen-1] = '\0'; newline = strchr(history[DAT_TIM].value, '\n'); /* Truncate @ trailing \n */ if (newline != NULL) *newline = '\0'; }
void SetMail() { static char buf[1024]; if (MailFile == NULL) { MailFile = buf; (void) strcpy(MailFile, MAILBOX_DIRECTORY); #ifndef NO_CUSERID (void) strcat(MailFile, cuserid(NULL)); #else (void) strcat(MailFile, getenv("USER")); #endif } MailCount = 0; MailArrive = False; SetToyState(&Post, TOY_NO); }
void CreateSessionData(char * user_at_host) { char myhost[MAX_STR_LEN]={0}; char username[MAX_STR_LEN]={0}; gethostname(myhost, MAX_STR_LEN); cuserid(username); generateUniqueID(); sprintf(g_SessionData.uri, "sip:%s", user_at_host); sprintf(g_SessionData.CallID, "%s@%s", g_UniqueID, myhost); sprintf(g_SessionData.MyHost, "%s", myhost); sprintf(g_SessionData.From, "%s", username, myhost); sprintf(g_SessionData.FromAdrs, "%s@%s", username, myhost); sprintf(g_SessionData.ToAdrs, "%s", user_at_host); sprintf(g_SessionData.To, "%s", strsep(&user_at_host, "@")); sprintf(g_SessionData.RemoteHost, "%s", user_at_host); }
line_list make_file_preface (void) { char *the_string, *temp; long idiotic_interface; struct tm *tm; long prefix_length; long user_length; long date_length; line_list result; prefix_length = strlen (FROM_PREFIX); time (&idiotic_interface); /* Convert to a string, checking for out-of-range time stamps. Don't use 'ctime', as that might dump core if the hardware clock is set to a bizarre value. */ tm = localtime (&idiotic_interface); if (! (tm && TM_YEAR_IN_ASCTIME_RANGE (tm->tm_year) && (the_date = asctime (tm)))) fatal ("current time is out of range"); /* the_date has an unwanted newline at the end */ date_length = strlen (the_date) - 1; the_date[date_length] = '\0'; temp = cuserid ((char *) NULL); user_length = strlen (temp); the_user = alloc_string (user_length + 1); strcpy (the_user, temp); the_string = alloc_string (3 + prefix_length + user_length + date_length); temp = the_string; strcpy (temp, FROM_PREFIX); temp = &temp[prefix_length]; *temp++ = ' '; strcpy (temp, the_user); temp = &temp[user_length]; *temp++ = ' '; strcpy (temp, the_date); result = new_list (); result->string = the_string; result->continuation = ((line_list) NULL); return result; }
void _PXFGETLOGIN( #endif _fcd S, _f_int *ILEN, _f_int *IERROR ) { char *loginptr, *sptr; int loginlen, slen, movelen, ierr; slen = _fcdlen(S); sptr = _fcdtocp(S); ierr = 0; /* get user's login name */ if ((loginptr = cuserid(NULL)) != NULL) { loginlen = strlen(loginptr); *ILEN = loginlen; /* check for string truncation */ if (loginlen > slen) { ierr = ETRUNC; movelen = slen; } else { movelen = loginlen; } /* copy login name to S user variable */ if (movelen > 0) { (void) memcpy(sptr, loginptr, movelen); } /* blank-fill user variable S */ if (slen > movelen) { (void) memset(sptr + sizeof(char)*movelen, BLANK, slen - movelen); } } *IERROR = ierr; }
void recorder_mpi_initialize(int *argc, char ***argv) { int nprocs; int rank; ::begin = recorder_wtime(); RECORDER_MPI_CALL(PMPI_Comm_size)(MPI_COMM_WORLD, &nprocs); RECORDER_MPI_CALL(PMPI_Comm_rank)(MPI_COMM_WORLD, &rank); char logfile_name[PATH_MAX] = {0}; char logdir_name[PATH_MAX] = {0}; char aux_file_name[PATH_MAX] = {0}; char cuser[L_cuserid] = {0}; cuserid(cuser); /** shouldn't be freed **/ char* OutDir = getenv("RECORDER_OUTDIR"); if(OutDir) snprintf(logdir_name, sizeof(logdir_name), "%s", OutDir); else snprintf(logdir_name, sizeof(logdir_name), "%s_%s_%lu", cuser, __progname, time(0)); int status; status = mkdir(logdir_name, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); sprintf(logfile_name,"%s/log.%d",logdir_name,rank); __recorderfh=fopen(logfile_name,"w"); sprintf(aux_file_name, "%s/aux.%d", logdir_name, rank); __auxiliaryfh = fopen(aux_file_name, "w"); printf(" logfile_name %s ,recorderfh %d\n",logfile_name,__recorderfh); // initialize sc create_sc_by_filename(&sc, logfile_name); tm4 = recorder_wtime(); return; }
struct view *text_new(void) { char dir[128], path[128]; const char *me, *home; time_t now = time(NULL); struct tm *gmt = gmtime(&now); fd_t fd = -1; struct view *view; if ((home = getenv("HOME"))) { sprintf(dir, "%s/.aoeui", home); fd = try_dir(path, dir, gmt); } if (fd < 0 && (me = getenv("LOGNAME"))) { sprintf(dir, "/tmp/aoeui-%s", me); fd = try_dir(path, dir, gmt); } #if !defined __APPLE__ && !defined BSD if (fd < 0 && (me = cuserid(NULL))) { sprintf(dir, "/tmp/aoeui-%s", me); fd = try_dir(path, dir, gmt); } #endif if (fd < 0) fd = try_dir(path, "/tmp/aoeui", gmt); if (fd < 0) fd = try_dir(path, "./aoeui", gmt); if (fd < 0) view = text_create("* New *", TEXT_EDITOR); else { view = text_create(path, TEXT_CREATED | TEXT_SCRATCH); view->text->fd = fd; } return view; }
void ls_stamp( void ) { char rcsid[] = "$Id$"; char revid[] = "$Revision$"; char dateid[] = "$Date$"; struct tm *nowtime; time_t nowtime_t; long date; /* report version of LaRCsim*/ printf("\nLaRCsim %s, %s\n\n", revid, dateid); nowtime_t = time( 0 ); nowtime = localtime( &nowtime_t ); /* set fields to correct time values */ date = (nowtime->tm_year % 100)*10000 + (nowtime->tm_mon + 1)*100 + (nowtime->tm_mday); sprintf(sim_control_.date_string, "%06ld\0", date); sprintf(sim_control_.time_stamp, "%02d:%02d:%02d\0", nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec); #ifdef COMPILE_THIS_CODE_THIS_USELESS_CODE cuserid( sim_control_.userid ); /* set up user id */ #endif /* COMPILE_THIS_CODE_THIS_USELESS_CODE */ return; }
void UnistdCuserid(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { ReturnValue->Val->Pointer = cuserid(Param[0]->Val->Pointer); }
/// Return effective username inline std::string username() { char buf[L_cuserid]; return unlikely(cuserid(buf) == nullptr) ? "" : buf; }
int main(int argc, char **argv) { int goerr = 0; /* non-zero indicates cmd error */ int i; int optsw; /* switch for while of getopt() */ (void) setlocale(LC_ALL, ""); #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ #endif (void) textdomain(TEXT_DOMAIN); validtype[USER_PROCESS] = 1; validtype[EMPTY] = 0; stbufp = &stbuf; /* * Strip off path name of this command */ for (i = strlen(argv[0]); i >= 0 && argv[0][i] != '/'; --i); if (i >= 0) argv[0] += i+1; program = argv[0]; /* * Buffer stdout for speed */ setbuf(stdout, outbuf); /* * Retrieve options specified on command line * XCU4 - add -m option */ while ((optsw = getopt(argc, argv, "abdHlmn:pqrstTu")) != EOF) { optcnt++; switch (optsw) { case 'a': optcnt += 7; validtype[BOOT_TIME] = 1; validtype[DEAD_PROCESS] = 1; validtype[LOGIN_PROCESS] = 1; validtype[INIT_PROCESS] = 1; validtype[RUN_LVL] = 1; validtype[OLD_TIME] = 1; validtype[NEW_TIME] = 1; validtype[USER_PROCESS] = 1; #ifdef XPG4 aopt = 1; #endif /* XPG4 */ uopt = 1; Topt = 1; if (!sopt) terse = 0; break; case 'b': validtype[BOOT_TIME] = 1; if (!uopt) validtype[USER_PROCESS] = 0; break; case 'd': validtype[DEAD_PROCESS] = 1; if (!uopt) validtype[USER_PROCESS] = 0; #ifdef XPG4 dopt = 1; #endif /* XPG4 */ break; case 'H': optcnt--; /* Don't count Header */ Hopt = 1; break; case 'l': validtype[LOGIN_PROCESS] = 1; if (!uopt) validtype[USER_PROCESS] = 0; terse = 0; break; case 'm': /* New XCU4 option */ justme = 1; break; case 'n': errno = 0; number = strtol(optarg, &end, 10); if (errno != 0 || *end != '\0') { (void) fprintf(stderr, gettext( "%s: Invalid numeric argument\n"), program); exit(1); } if (number < 1) { (void) fprintf(stderr, gettext( "%s: Number of users per line must " "be at least 1\n"), program); exit(1); } break; case 'p': validtype[INIT_PROCESS] = 1; if (!uopt) validtype[USER_PROCESS] = 0; break; case 'q': qopt = 1; break; case 'r': validtype[RUN_LVL] = 1; terse = 0; if (!uopt) validtype[USER_PROCESS] = 0; break; case 's': sopt = 1; terse = 1; break; case 't': validtype[OLD_TIME] = 1; validtype[NEW_TIME] = 1; if (!uopt) validtype[USER_PROCESS] = 0; break; case 'T': Topt = 1; #ifdef XPG4 terse = 1; /* XPG4 requires -T */ #else /* XPG4 */ terse = 0; #endif /* XPG4 */ break; case 'u': uopt = 1; validtype[USER_PROCESS] = 1; if (!sopt) terse = 0; break; case '?': goerr++; break; default: break; } } #ifdef XPG4 /* * XCU4 changes - check for illegal sopt, Topt & aopt combination */ if (sopt == 1) { terse = 1; if (Topt == 1 || aopt == 1) goerr++; } #endif /* XPG4 */ if (goerr > 0) { #ifdef XPG4 /* * XCU4 - slightly different usage with -s -a & -T */ (void) fprintf(stderr, gettext("\nUsage:\t%s"), program); (void) fprintf(stderr, gettext(" -s [-bdHlmpqrtu] [utmpx_like_file]\n")); (void) fprintf(stderr, gettext( "\t%s [-abdHlmpqrtTu] [utmpx_like_file]\n"), program); #else /* XPG4 */ (void) fprintf(stderr, gettext( "\nUsage:\t%s [-abdHlmpqrstTu] [utmpx_like_file]\n"), program); #endif /* XPG4 */ (void) fprintf(stderr, gettext("\t%s -q [-n x] [utmpx_like_file]\n"), program); (void) fprintf(stderr, gettext("\t%s [am i]\n"), program); /* * XCU4 changes - be explicit with "am i" options */ (void) fprintf(stderr, gettext("\t%s [am I]\n"), program); (void) fprintf(stderr, gettext( "a\tall (bdlprtu options)\n")); (void) fprintf(stderr, gettext("b\tboot time\n")); (void) fprintf(stderr, gettext("d\tdead processes\n")); (void) fprintf(stderr, gettext("H\tprint header\n")); (void) fprintf(stderr, gettext("l\tlogin processes\n")); (void) fprintf(stderr, gettext( "n #\tspecify number of users per line for -q\n")); (void) fprintf(stderr, gettext("p\tprocesses other than getty or users\n")); (void) fprintf(stderr, gettext("q\tquick %s\n"), program); (void) fprintf(stderr, gettext("r\trun level\n")); (void) fprintf(stderr, gettext( "s\tshort form of %s (no time since last output or pid)\n"), program); (void) fprintf(stderr, gettext("t\ttime changes\n")); (void) fprintf(stderr, gettext( "T\tstatus of tty (+ writable, - not writable, " "? hung)\n")); (void) fprintf(stderr, gettext("u\tuseful information\n")); (void) fprintf(stderr, gettext("m\tinformation only about current terminal\n")); (void) fprintf(stderr, gettext( "am i\tinformation about current terminal " "(same as -m)\n")); (void) fprintf(stderr, gettext( "am I\tinformation about current terminal " "(same as -m)\n")); exit(1); } /* * XCU4: If -q option ignore all other options */ if (qopt == 1) { Hopt = 0; sopt = 0; Topt = 0; uopt = 0; justme = 0; validtype[ACCOUNTING] = 0; validtype[BOOT_TIME] = 0; validtype[DEAD_PROCESS] = 0; validtype[LOGIN_PROCESS] = 0; validtype[INIT_PROCESS] = 0; validtype[RUN_LVL] = 0; validtype[OLD_TIME] = 0; validtype[NEW_TIME] = 0; validtype[USER_PROCESS] = 1; } if (argc == optind + 1) { optcnt++; ck_file(argv[optind]); (void) utmpxname(argv[optind]); } /* * Test for 'who am i' or 'who am I' * XCU4 - check if justme was already set by -m option */ if (justme == 1 || (argc == 3 && strcmp(argv[1], "am") == 0 && ((argv[2][0] == 'i' || argv[2][0] == 'I') && argv[2][1] == '\0'))) { justme = 1; myname = nameval; (void) cuserid(myname); if ((mytty = ttyname(fileno(stdin))) == NULL && (mytty = ttyname(fileno(stdout))) == NULL && (mytty = ttyname(fileno(stderr))) == NULL) { (void) fprintf(stderr, gettext( "Must be attached to terminal for 'am I' option\n")); (void) fflush(stderr); exit(1); } else mytty += 5; /* bump past "/dev/" */ } if (!terse) { if (Hopt) (void) printf(gettext( "NAME LINE TIME IDLE PID COMMENTS\n")); timnow = time(0); if ((fildes = open("/etc/inittab", O_NONBLOCK|O_RDONLY)) == -1) { (void) snprintf(errmsg, sizeof (errmsg), gettext("%s: Cannot open /etc/inittab"), program); perror(errmsg); exit(errno); } if (fstat(fildes, stbufp) == -1) { (void) snprintf(errmsg, sizeof (errmsg), gettext("%s: Cannot stat /etc/inittab"), program); perror(errmsg); exit(errno); } if ((inittab = malloc(stbufp->st_size + 1)) == NULL) { (void) snprintf(errmsg, sizeof (errmsg), gettext("%s: Cannot allocate %ld bytes"), program, stbufp->st_size); perror(errmsg); exit(errno); } if (read(fildes, inittab, stbufp->st_size) != stbufp->st_size) { (void) snprintf(errmsg, sizeof (errmsg), gettext("%s: Error reading /etc/inittab"), program); perror(errmsg); exit(errno); } inittab[stbufp->st_size] = '\0'; iinit = inittab; } else { if (Hopt) { #ifdef XPG4 if (dopt) { (void) printf(gettext( "NAME LINE TIME COMMENTS\n")); } else { (void) printf( gettext("NAME LINE TIME\n")); } #else /* XPG4 */ (void) printf( gettext("NAME LINE TIME\n")); #endif /* XPG4 */ } } process(); /* * 'who -q' requires EOL upon exit, * followed by total line */ if (qopt) (void) printf(gettext("\n# users=%d\n"), totlusrs); return (0); }
void parameters(int argc, char **argv) { /* <submit> is set to 1 if submission parameters are used. */ int submit = 0; /* Random seed. */ int seed = 0; /* <L_cuserid> is a macro defined in stdio.h */ /* <uid> contains the user name. */ char uid[L_cuserid + 2]; /* Randomise. */ srand(time_seed()); /* Read command-line arguments. */ if (argc != 3) { if (argc == 2 && !strcmp(argv[1], "submit")) { /* Use submission parameters. */ submit = 1; N = 4; NumThreads = 2; printf("Submission run for \"%s\".\n", cuserid(uid)); srand(uid[0]); } else { if (argc == 4) { seed = atoi(argv[3]); srand(seed); printf("Random seed = %d.\n", seed); } else { printf("Usage: %s <matrix_dimension> <num_threads> \ [random seed]\n", argv[0]); printf(" %s submit\n", argv[0]); exit(0); } } } /* Interpret command-line arguments. */ if (!submit) { N = atoi(argv[1]); if (N < 1 || N > MAXN) { printf("N = %d is out of range.\n", N); exit(0); } NumThreads = atoi(argv[2]); if (NumThreads < 1) { printf("Warning: Invalid number of threads = %d. Using 1.\n", NumThreads); NumThreads = 1; } if (NumThreads > _POSIX_THREAD_THREADS_MAX) { printf("Warning: %d threads requested; only %d available.\n", NumThreads, _POSIX_THREAD_THREADS_MAX); NumThreads = _POSIX_THREAD_THREADS_MAX; } } /* Print parameters. */ printf("Matrix dimension N = %d.\n", N); printf("Number of threads = %d.\n", NumThreads); }
int main(int argc, char *argv[]) { struct dirent **namelist; int n; char *fullpath; struct stat buf; char time[25]; int i = 0; if(argc != 2) { write(1, "Usage: ./ls-l path\n", 19); exit(EXIT_FAILURE); } n = scandir(argv[1], &namelist, 0, alphasort); if (n < 0) perror("scandir"); else { while (n > i) { if(strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) { fullpath = malloc(strlen(argv[1]) + strlen(namelist[i]->d_name) + 2); sprintf(fullpath, "%s/%s", argv[1], namelist[i]->d_name); if(stat(fullpath, &buf) == -1) { perror("Stat"); exit(EXIT_FAILURE); } switch(buf.st_mode & S_IFMT) { //File type case S_IFSOCK: write(1, "s", 1); break; case S_IFLNK: write(1, "l", 1); break; case S_IFREG: write(1, "-", 1); break; case S_IFBLK: write(1, "b", 1); break; case S_IFDIR: write(1, "d", 1); break; case S_IFCHR: write(1, "c", 1); break; case S_IFIFO: write(1, "p", 1); break; default: write(1, "?", 1); break; } readable((unsigned long)(buf.st_mode & S_IRWXU)/64); //Owner permissions readable((unsigned long)(buf.st_mode & S_IRWXG)/8); //Group permissions readable((unsigned long)buf.st_mode & S_IRWXO); // Others permissions printf(" %d", buf.st_nlink); //Print hard links printf(" %s", (char *)cuserid(&buf.st_uid)); printf(" %s", (char *)cuserid(&buf.st_gid)); printf(" %lld", (long long) buf.st_size); //printf(" %s", ctime(&buf.st_mtime)); strftime(time, 25, "%Y-%m-%d %H:%M", localtime(&buf.st_mtime)); printf("\t%s", time); printf(" %s\n", namelist[i]->d_name); free(namelist[i]); } i++; } free(namelist); } }
char *getlogin() { static char login[256]; return cuserid(login); }