📄 rdesktop.c
字号:
}/* malloc; exit if out of memory */void *xmalloc(int size){ void *mem = malloc(size); if (mem == NULL) { error("xmalloc %d\n", size); exit(1); } return mem;}/* strdup */char *xstrdup(const char *s){ char *mem = strdup(s); if (mem == NULL) { perror("strdup"); exit(1); } return mem;}/* realloc; exit if out of memory */void *xrealloc(void *oldmem, int size){ void *mem; if (size < 1) size = 1; mem = realloc(oldmem, size); if (mem == NULL) { error("xrealloc %d\n", size); exit(1); } return mem;}/* free */voidxfree(void *mem){ free(mem);}/* report an error */voiderror(char *format, ...){ va_list ap; fprintf(stderr, "ERROR: "); va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);}/* report a warning */voidwarning(char *format, ...){ va_list ap; fprintf(stderr, "WARNING: "); va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);}/* report an unimplemented protocol feature */voidunimpl(char *format, ...){ va_list ap; fprintf(stderr, "NOT IMPLEMENTED: "); va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);}/* produce a hex dump */voidhexdump(unsigned char *p, unsigned int len){ unsigned char *line = p; int i, thisline, offset = 0; while (offset < len) { printf("%04x ", offset); thisline = len - offset; if (thisline > 16) thisline = 16; for (i = 0; i < thisline; i++) printf("%02x ", line[i]); for (; i < 16; i++) printf(" "); for (i = 0; i < thisline; i++) printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.'); printf("\n"); offset += thisline; line += thisline; }}/* input: src is the string we look in for needle. Needle may be escaped by a backslash, in that case we ignore that particular needle. return value: returns next src pointer, for succesive executions, like in a while loop if retval is 0, then there are no more args. pitfalls: src is modified. 0x00 chars are inserted to terminate strings. return val, points on the next val chr after ins 0x00 example usage: while( (pos = next_arg( optarg, ',')) ){ printf("%s\n",optarg); optarg=pos; }*/char *next_arg(char *src, char needle){ char *nextval; char *p; char *mvp = 0; /* EOS */ if (*src == (char) 0x00) return 0; p = src; /* skip escaped needles */ while ((nextval = strchr(p, needle))) { mvp = nextval - 1; /* found backslashed needle */ if (*mvp == '\\' && (mvp > src)) { /* move string one to the left */ while (*(mvp + 1) != (char) 0x00) { *mvp = *(mvp + 1); mvp++; } *mvp = (char) 0x00; p = nextval; } else { p = nextval + 1; break; } } /* more args available */ if (nextval) { *nextval = (char) 0x00; return ++nextval; } /* no more args after this, jump to EOS */ nextval = src + strlen(src); return nextval;}voidtoupper_str(char *p){ while (*p) { if ((*p >= 'a') && (*p <= 'z')) *p = toupper((int) *p); p++; }}BOOLstr_startswith(const char *s, const char *prefix){ return (strncmp(s, prefix, strlen(prefix)) == 0);}/* Split input into lines, and call linehandler for each line. Incomplete lines are saved in the rest variable, which should initially point to NULL. When linehandler returns False, stop and return False. Otherwise, return True. */BOOLstr_handle_lines(const char *input, char **rest, str_handle_lines_t linehandler, void *data){ char *buf, *p; char *oldrest; size_t inputlen; size_t buflen; size_t restlen = 0; BOOL ret = True; /* Copy data to buffer */ inputlen = strlen(input); if (*rest) restlen = strlen(*rest); buflen = restlen + inputlen + 1; buf = (char *) xmalloc(buflen); buf[0] = '\0'; if (*rest) STRNCPY(buf, *rest, buflen); strncat(buf, input, inputlen); p = buf; while (1) { char *newline = strchr(p, '\n'); if (newline) { *newline = '\0'; if (!linehandler(p, data)) { p = newline + 1; ret = False; break; } p = newline + 1; } else { break; } } /* Save in rest */ oldrest = *rest; restlen = buf + buflen - p; *rest = (char *) xmalloc(restlen); STRNCPY((*rest), p, restlen); xfree(oldrest); xfree(buf); return ret;}/* Execute the program specified by argv. For each line in stdout/stderr output, call linehandler. Returns false on failure. */BOOLsubprocess(char *const argv[], str_handle_lines_t linehandler, void *data){ pid_t child; int fd[2]; int n = 1; char output[256]; char *rest = NULL; if (pipe(fd) < 0) { perror("pipe"); return False; } if ((child = fork()) < 0) { perror("fork"); return False; } /* Child */ if (child == 0) { /* Close read end */ close(fd[0]); /* Redirect stdout and stderr to pipe */ dup2(fd[1], 1); dup2(fd[1], 2); /* Execute */ execvp(argv[0], argv); perror("Error executing child"); _exit(128); } /* Parent. Close write end. */ close(fd[1]); while (n > 0) { n = read(fd[0], output, 255); output[n] = '\0'; str_handle_lines(output, &rest, linehandler, data); } xfree(rest); return True;}/* not all clibs got ltoa */#define LTOA_BUFSIZE (sizeof(long) * 8 + 1)char *l_to_a(long N, int base){ static char ret[LTOA_BUFSIZE]; char *head = ret, buf[LTOA_BUFSIZE], *tail = buf + sizeof(buf); register int divrem; if (base < 36 || 2 > base) base = 10; if (N < 0) { *head++ = '-'; N = -N; } tail = buf + sizeof(buf); *--tail = 0; do { divrem = N % base; *--tail = (divrem <= 9) ? divrem + '0' : divrem + 'a' - 10; N /= base; } while (N); strcpy(head, tail); return ret;}intload_licence(unsigned char **data){ char *home, *path; struct stat st; int fd, length; home = getenv("HOME"); if (home == NULL) return -1; path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence.")); sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname); fd = open(path, O_RDONLY); if (fd == -1) return -1; if (fstat(fd, &st)) return -1; *data = (uint8 *) xmalloc(st.st_size); length = read(fd, *data, st.st_size); close(fd); xfree(path); return length;}voidsave_licence(unsigned char *data, int length){ char *home, *path, *tmppath; int fd; home = getenv("HOME"); if (home == NULL) return; path = (char *) xmalloc(strlen(home) + strlen(g_hostname) + sizeof("/.rdesktop/licence.")); sprintf(path, "%s/.rdesktop", home); if ((mkdir(path, 0700) == -1) && errno != EEXIST) { perror(path); return; } /* write licence to licence.hostname.new, then atomically rename to licence.hostname */ sprintf(path, "%s/.rdesktop/licence.%s", home, g_hostname); tmppath = (char *) xmalloc(strlen(path) + sizeof(".new")); strcpy(tmppath, path); strcat(tmppath, ".new"); fd = open(tmppath, O_WRONLY | O_CREAT | O_TRUNC, 0600); if (fd == -1) { perror(tmppath); return; } if (write(fd, data, length) != length) { perror(tmppath); unlink(tmppath); } else if (rename(tmppath, path) == -1) { perror(path); unlink(tmppath); } close(fd); xfree(tmppath); xfree(path);}/* Create the bitmap cache directory */BOOLrd_pstcache_mkdir(void){ char *home; char bmpcache_dir[256]; home = getenv("HOME"); if (home == NULL) return False; sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop"); if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST) { perror(bmpcache_dir); return False; } sprintf(bmpcache_dir, "%s/%s", home, ".rdesktop/cache"); if ((mkdir(bmpcache_dir, S_IRWXU) == -1) && errno != EEXIST) { perror(bmpcache_dir); return False; } return True;}/* open a file in the .rdesktop directory */intrd_open_file(char *filename){ char *home; char fn[256]; int fd; home = getenv("HOME"); if (home == NULL) return -1; sprintf(fn, "%s/.rdesktop/%s", home, filename); fd = open(fn, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (fd == -1) perror(fn); return fd;}/* close file */voidrd_close_file(int fd){ close(fd);}/* read from file*/intrd_read_file(int fd, void *ptr, int len){ return read(fd, ptr, len);}/* write to file */intrd_write_file(int fd, void *ptr, int len){ return write(fd, ptr, len);}/* move file pointer */intrd_lseek_file(int fd, int offset){ return lseek(fd, offset, SEEK_SET);}/* do a write lock on a file */BOOLrd_lock_file(int fd, int start, int len){ struct flock lock; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = start; lock.l_len = len; if (fcntl(fd, F_SETLK, &lock) == -1) return False; return True;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -