📄 lockscreen.c
字号:
/* 1025886 vmh: added this string utility */char * findnextstart(string, substring) char *string, *substring;{ while ((*string != '\0') && (*string != *substring)) string++; return string;}/*----------------------------------------------------------------*//* substring -- determine whether string contains substring * return 1 if found, 0 if not found. *//* 1025886 vmh: added this string utility */int substring(string, substring) char *string, *substring;{ int found = 1, notfound = 0; char *sub = substring; char *str = string; while (1) { string = findnextstart(string, substring); str = string; sub = substring; while ((*str != '\0') && (*sub != '\0') && (*str == *sub)) { str++; sub++; } if (*sub == '\0') return found; if (*str == '\0') return notfound; string++; } }/*----------------------------------------------------------------*//* get_other_procs -- collect list of processes * I own and not called 'lockscreen' and attached to something and * not a remote login shell or attached to a remote login shell. * return the number of processes attached to the console, * or return -1 if popen fails *//* 1025886 vmh: added this proc */intget_other_procs(pids, maxpids) int *pids, maxpids;{ char *linearray[SHORTBUFFER], **lp, *strdup(); char line[SHORTBUFFER]; char command[LONGBUFFER]; char fgrepcommand[LONGBUFFER]; int index = 0; FILE *fp, *popen(); char newline[MAXLLEN+1]; char *lineptr; int npids = 0; int i; /* Do a "finger" to get all the login processes. * Put the ones that have not an empty 'Where' field * into a stoplist. They are the ones we do not want killed. */ if ((fp = popen("finger -f", "r")) == NULL) return(-1) ; while (fgets(line, 100, fp) != NULL) { if (line[50] != '\0') /* where field first character empty */ { linearray[index++] = strdup(line); } } linearray[index] = (char *)NULL; /* terminate */ pclose(fp); sprintf(command, "ps -x |grep -v \"^......a\" |grep -v \"^......b\" "); for (lp = linearray; *lp; lp++) { sprintf(fgrepcommand, "| grep -v \"^......%c%c \" ", (*lp)[31], (*lp)[32]); strcat(command, fgrepcommand); } if ((fp = popen(command, "r")) == NULL) return(-1); lineptr = &newline[0]; i = strlen(lineptr) - 3; /* point at the the ' ' of "...lockscreen \n\0"*/ while (fgets(newline, MAXLLEN, fp) && npids < maxpids) { if (substring(lineptr+20, "lockscreen -e")); if (newline[6] == '?'); /* do not kill processes without parents */ else { pids[npids++] = atoi(newline); } } pclose(fp); return(npids);}/*----------------------------------------------------------------*//* get_lockscreen_procs -- collect a list of processes * called 'lockscreen' * return the number of processes attached to the console, * or return -1 if popen fails *//* 1025886 vmh: added this proc */intget_lockscreen_procs(pids, maxpids) int *pids, maxpids;{ FILE *fp, *popen(); char line[MAXLLEN+1]; int npids = 0; if ((fp = popen("ps -ax|egrep lockscreen", "r")) == NULL) return(-1); while (fgets(line, MAXLLEN, fp) && npids < maxpids) { if (line[20] != 'e') /* catch the 'egrep' */ pids[npids++] = atoi(line); } pclose(fp); return(npids);}/* -------------------- end exiting --------------------------------------*/staticblank_cursor(windowfd) int windowfd;{ struct pixrect *mpr = mem_create(16, 16, 1); struct cursor zero; /* Build cursor framework */ zero.cur_shape = mpr; /* Get current cursor */ win_getcursor(windowfd, &zero); /* Set "don't show cursor" flag */ zero.flags = 1; /* Reset cursor */ win_setcursor(windowfd, &zero); /* Free allocated memory */ pr_destroy(mpr);}staticinitpanel(tsw) struct toolsw *tsw;{ Panel panel = tsw->ts_data; void abort_proc(), exit_proc(); int showdtop_proc(); char hostname[1024]; int line; line = 7; if (show_hostname) { gethostname(hostname, 1023); hostname_item = panel_create_item(panel, PANEL_MESSAGE, PANEL_LABEL_STRING, hostname, PANEL_LABEL_X, PANEL_CU(33), PANEL_LABEL_Y, PANEL_CU(line++), PANEL_VALUE_DISPLAY_LENGTH, 45, 0); } name_item = panel_create_item(panel, PANEL_TEXT, PANEL_LABEL_STRING,"Name:", PANEL_LABEL_X, PANEL_CU(33), PANEL_LABEL_Y, PANEL_CU(line++), PANEL_VALUE_DISPLAY_LENGTH, 45, 0); passwd_item = panel_create_item(panel, PANEL_TEXT, PANEL_LABEL_STRING, "Password:", PANEL_LABEL_X, PANEL_CU(33), PANEL_LABEL_Y, PANEL_CU(line++), PANEL_MASK_CHAR, ' ', PANEL_NOTIFY_PROC, notify, PANEL_NOTIFY_LEVEL, PANEL_ALL, 0); abort_item = panel_create_item(panel, PANEL_BUTTON, PANEL_LABEL_IMAGE, jump_pr[0], PANEL_LABEL_X, PANEL_CU(25), PANEL_LABEL_Y, PANEL_CU(7), PANEL_MENU_TITLE_STRING, "Screen Control", PANEL_MENU_CHOICE_STRINGS, "Re-lock screen", 0, PANEL_NOTIFY_PROC, abort_proc, 0); if ((small_font = pf_open(SMALL_FONT)) == NULL) { (void)fprintf(stderr, "lockscreen: Can't find font: %s\n", SMALL_FONT); } instructions_item = panel_create_item(panel, PANEL_MESSAGE, PANEL_LABEL_X, PANEL_CU(25), PANEL_LABEL_Y, PANEL_CU(10), PANEL_LABEL_STRING, "Enter password to unlock; select square to lock.", PANEL_LABEL_BOLD, FALSE, PANEL_LABEL_FONT, small_font, 0); msg_item = panel_create_item(panel, PANEL_MESSAGE, PANEL_LABEL_STRING, "", PANEL_LABEL_BOLD, FALSE, PANEL_LABEL_X, PANEL_CU(25), PANEL_LABEL_Y, PANEL_CU(11), PANEL_LABEL_FONT, small_font, 0); if (exitenable) { exit_item = panel_create_item(panel, PANEL_BUTTON, PANEL_LABEL_X, PANEL_CU(10), PANEL_LABEL_Y, PANEL_CU(7), PANEL_LABEL_IMAGE, panel_button_image(panel, "Exit Desktop", 0, (Pixfont *)0), PANEL_NOTIFY_PROC, exit_proc, PANEL_MENU_CHOICE_STRINGS, "Exit suntools environment and logout", 0, 0); }}staticinittool(){ struct inputmask im; register struct inputmask *mask = &im; int toolselected(), toolhandlesigwinch(); int i; /* * Notice every bit of key input */ (void)input_imnull(mask); mask->im_flags |= IM_ASCII | IM_NEGEVENT; for (i=1; i<17; i++) { win_setinputcodebit(mask, KEY_LEFT(i)); win_setinputcodebit(mask, KEY_TOP(i)); win_setinputcodebit(mask, KEY_RIGHT(i)); } win_setinputcodebit(mask, SHIFT_LEFT); /* Pick up the shift */ win_setinputcodebit(mask, SHIFT_RIGHT); /* keys for VT-100 */ win_setinputcodebit(mask, SHIFT_LOCK); /* compatibility */ win_setinputcodebit(mask, MS_LEFT); win_setinputcodebit(mask, MS_MIDDLE); win_setinputcodebit(mask, MS_RIGHT); (void)win_setinputmask(tool->tl_windowfd, mask, (struct inputmask *)0, WIN_NULLLINK); /* * Notification overrides */ tool->tl_io.tio_selected = toolselected; tool->tl_io.tio_handlesigwinch = toolhandlesigwinch; tool->tl_io.tio_timer = &tooltimer;}staticgetrectcreate(){ char parentname[WIN_NAMESIZE]; /* * Determine parent */ if (we_getparentwindow(parentname)) if (strcmp(ttyname(0), "/dev/console")) { (void)printf("%s not passed parent window in environment\n",progname); exit_and_kill(1); } else { (void)strcpy(parentname, "/dev/win0"); } /* * Open parent window */ if (!parentfd && (parentfd = open(parentname, O_RDONLY, 0)) < 0) { (void)printf("%s (parent) would not open\n", parentname); perror(progname); exit_and_kill(1); } /* * Determine size of parents window. */ (void)win_getsize(parentfd, &rectcreate);}static voidsigwinchcatcher(){ (void)tool_sigwinch(tool);}static char *getloginname(){ struct passwd *passwdent; extern struct passwd *getpwuid(); char *loginname; static char name_buf[1024]; extern char *getlogin(); loginname = getlogin(); if (loginname == 0) { if ((passwdent = getpwuid(getuid())) == 0) { paintmsg("Couldn't find user name."); return(0); } loginname = passwdent->pw_name; } (void)strcpy(name_buf, loginname); return(name_buf);}staticmake_twinkle()/* * make_twinkle advances the abort_item to the next choice. */{ static int counter = 0; static int cur_choice = 0; if (!(counter++ % 3)) { (void)panel_set(abort_item, PANEL_LABEL_IMAGE, jump_pr[cur_choice], PANEL_PAINT, PANEL_NO_CLEAR, 0); cur_choice = (cur_choice + 1) % 6; }}/* * Convert hue/saturation/value to red/green/blue. */statichsv_to_rgb(h, s, v, rp, gp, bp) int h, s, v; u_char *rp, *gp, *bp;{ int i, f; u_char p, q, t; if (s == 0) *rp = *gp = *bp = v; else { if (h == 360) h = 0; h = h * 256 / 60; i = h / 256 * 256; f = h - i; p = v * (256 - s) / 256; q = v * (256 - (s*f)/256) / 256; t = v * (256 - (s * (256 - f))/256) / 256; switch (i/256) { case 0: *rp = v; *gp = t; *bp = p; break; case 1: *rp = q; *gp = v; *bp = p; break; case 2: *rp = p; *gp = v; *bp = t; break; case 3: *rp = p; *gp = q; *bp = v; break; case 4: *rp = t; *gp = p; *bp = v; break; case 5: *rp = v; *gp = p; *bp = q; break; } }}/* * Return 1 if user has no password. */static intno_password(){ char *loginname; struct passwd *passwdent; extern struct passwd *getpwnam(); char *getloginname(); if ((loginname = getloginname()) == 0) return 1; if ((passwdent = getpwnam(loginname)) == 0) return 1; return 0;}static char *getpath(){ char *getenv(); char *cp = getenv("PATH"); if(cp == 0) cp=":/usr/ucb:/bin:/usr/bin"; return cp;}#define oktox(n) \ (!stat((n),&stbuf) && (stbuf.st_mode & S_IFMT) == S_IFREG && !access((n),1))static char *expandname(name, spath, buf) char *name, *spath, *buf;{ register char *cp; struct stat stbuf; if(*spath == ':' || *name == '/') { spath++; if(oktox(name)) { (void)strcpy(buf, name); return(spath); } if (*name == '/') goto error_exit; } for(;*spath;) { /* copy over current directory and then append name */ for(cp = buf; *spath != 0 && *spath != ':'; ) *cp++ = *spath++; *cp++ = '/'; (void)strcpy(cp, name); if (*spath) spath++; if (oktox(buf)) return(spath); }error_exit: *buf = 0; return spath;}staticprint_help_and_exit(){ (void)fprintf(stderr, "Usage: %s [flags] [displayprogram]\n", progname); (void)fprintf(stderr, "FLAG DESCRIPTION ARGS\n"); (void)fprintf(stderr, "-r enable root login\n"); (void)fprintf(stderr, "-e enable exit option\n"); (void)fprintf(stderr, "-n enable no-lock option\n"); (void)fprintf(stderr, "-b run a background program program\n"); (void)fprintf(stderr, "-t number of seconds for timeout integer\n"); (void)tool_usage(progname); exit_and_kill(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -