Browse code

sshchat: get user id from parameter instead of from file

Dario Rodriguez authored on 18/12/2015 18:30:06
Showing 1 changed files
... ...
@@ -33,7 +33,6 @@ int
33 33
 main(int argc, char *argv[])
34 34
 {
35 35
 	char id[MAXIDSIZE];
36
-	int fd,n;
37 36
 	char *ptr;
38 37
 	char *host;
39 38
 	int port;
... ...
@@ -48,17 +47,14 @@ main(int argc, char *argv[])
48 47
 	int nwritefds;
49 48
 	int i;
50 49
 	int queued;
51
-	/* get the id */
52
-	if((fd=open(".sshchat",O_RDONLY))==-1)
53
-		return(-1); /* error */
54
-	if((n=read(fd,id,MAXIDSIZE))<1 || id[0]=='\n') {
55
-		close(fd),fd=-1;
56
-		return(-2); /* no id */
50
+	if(argc!=2 || strcmp(argv[argc-1],"--help")==0) {
51
+		fprintf(stderr,"Syntax: %s username",argv[0]);
52
+		return(1); /* no id */
57 53
 	}
58
-	close(fd),fd=-1;
59
-	id[(n>=MAXIDSIZE)?MAXIDSIZE-1:n]='\0';
60
-	if((ptr=strchr(id,'\n'))!=NULL)
61
-		*ptr='\0';
54
+	strncpy(id,argv[1],sizeof(MAXIDSIZE));
55
+	id[MAXIDSIZE-1]='\0';
56
+	if((ptr=strchr(id,' '))!=NULL)
57
+		*id='\0'; /* spaces not allowed */
62 58
 	/* open socket to server */
63 59
 	if((host=ipv4_genip("127.0.0.1",&hostsize))==NULL)
64 60
 		return(-3); /* couldn't resolv localhost */